CF 305C ——Ivan and Powers of Two——————【数学】
1 second
256 megabytes
standard input
standard output
Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order.
Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0).
Help Ivan, find the required quantity of numbers.
The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an(0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an.
Print a single integer — the answer to the problem.
4
0 1 1 1
0
1
3
3
In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7.
In the second sample you need to add numbers 20, 21, 22.
题目大意:给你一个n表示有n个ai,ai表示2ai。问你需要再加几个形如2b的数,让他们的总和为2v-1。
解题思路:首先看数据范围,如果想通过暴力是完全行不通的。所以观察所求的2v-1,这个数必然是由2进制所有都为1组成的。然而他给你ai是2ai,是2的次方。所以我们合并相同的ai,2^a+2^a=2*(2^a)=2^(a+1),所以如果有相同的,让ai自加,然后放入set容器中判断,如果还有相同的,从容器中删除后再将该数自加,重复,直到没有相同的该数,放入set中。最后用最大的a减去set的大小即为答案。可以自己模拟一下过程,就能理解了。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
int main(){
int Maxn,a;
int n,i,j,k;
while(scanf("%d",&n)!=EOF){
set<int>S;
S.clear();
Maxn=0;
for(i=0;i<n;i++){
scanf("%d",&a);
while(S.count(a)){
S.erase(a);
a++;
}
S.insert(a);
Maxn=Maxn>a?Maxn:a;
}
printf("%d\n",Maxn+1-S.size());
}
return 0;
}
CF 305C ——Ivan and Powers of Two——————【数学】的更多相关文章
- CodeForces 404C Ivan and Powers of Two
Ivan and Powers of Two Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & % ...
- CF988D Points and Powers of Two 数学结论题 规律 第十题
Points and Powers of Two time limit per test 4 seconds memory limit per test 256 megabytes input sta ...
- CF 317D Game with Powers
题解: 将一个数的指数看着一个堆,问题变成这些堆的异或值 分析一个堆的情况,打SG表. #include<stdio.h> #include<string.h> ]; char ...
- CF A and B and Team Training (数学)
A and B and Team Training time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces Round #529 (Div. 3) C. Powers Of Two(数学????)
传送门 题意: 给出一个整数 n ,问能否将 n 分解成 k 个数之和,且这 k 个数必须是2的幂. 如果可以,输出"YES",并打印出任意一组解,反之输出"NO&quo ...
- 快速切题CF 158B taxi 构造 && 82A double cola 数学观察 难度:0
实在太冷了今天 taxi :错误原因1 忽略了 1 1 1 1 和 1 2 1 这种情况,直接认为最多两组一车了 2 语句顺序错 double cola: 忘了减去n的序号1,即n-- B. Taxi ...
- CF C.Ivan the Fool and the Probability Theory【思维·构造】
题目传送门 题目大意: 一个$n*m$的网格图,每个格子可以染黑色.白色,问每个格子最多有一个相邻格子颜色相同的涂色方案数$n,m<=1e5$ 分析: 首先,考虑到如果有两个相邻的格子颜色相同, ...
- CF R639 div 2 E Quantifier Question 数学 dfs 图论
LINK:Quantifier Question 题面过长 引起不适 读题花了好长时间 对于 和 存在符合不是很熟练 导致很懵逼的做完了. 好在还算很好想.不过wa到了一个坑点上面 自闭一大晌 还以为 ...
- cf 12C Fruits(贪心【简单数学】)
题意: m个水果,n个价格.每种水果只有一个价格. 问如果给每种水果分配价格,使得买的m个水果总价格最小.最大. 输出最小值和最大值. 思路: 贪心. 代码: bool cmp(int a,int b ...
随机推荐
- Selenium API(二)
1.定位一组元素 WebDriver提供了8种定位一组元素的方法. driver.find_elements_by_css_selector() driver.find_elements_by_tag ...
- 【python】Python任务调度模块 – APScheduler
APScheduler是一个Python定时任务框架,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务.并以daemon方式运行应用.目前最新版本为3.0 ...
- HDU6300-2018ACM暑假多校联合训练1003-Triangle Partition
题意是给3n个点,其中不可能存在任意三点共线的情况,让你在其中建n个三角形,点不能重复使用,三角形不能相互覆盖 做法是给每个点排序,按照先y轴排,再x轴排的顺序,三个三个一组从下往上输出,有人说是凸包 ...
- spark执行例子eclipse maven打包jar
首先在eclipse Java EE中新建一个Maven project具体选项如下 点击Finish创建成功,接下来把默认的jdk1.5改成jdk1.8 然后编辑pom.xml加入spark-cor ...
- JDBC的概念&使用步骤
概念: JDBC(Java DataBase Connectivity)是一种数据库连接技术,能实现Java程序对各种数据库的访问.由一组使用Java语言编写的类和接口(JDBC API)组成,他们位 ...
- DIV做的Table
<style> div.table{ border:1px solid #d7d7d7; margin-left:0px; border-bottom-width:; width:1200 ...
- jenkins安装maven工具
下载maven工具,然后解压到/usr/local下面 然后在jenkins上面添加maven工具.指定他的设置配置文件(全局) 指定maven的目录位置 应用保存即可 !!! 虽然maven安装简单 ...
- XSS攻击的防御
XSS攻击的防御 XSS 攻击是什么 XSS 又称 CSS,全称 Cross SiteScript,跨站脚本攻击,是 Web 程序中常见的漏洞,XSS 属于被动式且用于客户端的攻击方式,所以容易被忽略 ...
- bootdo开源项目修改代码后页面无效
修改了JS文件,重启服务后,发现页面没有刷新出效果. 清空缓存一般就可以解决此问题.
- Odd number problem
描述 你一定玩过八数码游戏,它实际上是在一个3*3的网格中进行的,1个空格和1~8这8个数字恰好不重不漏地分布在这3*3的网格中.例如:5 2 81 3 _4 6 7在游戏过程中,可以把空格与其上.下 ...