Ivan and Powers of Two
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print a single integer — the answer to the problem.

Sample test(s)
input
4
0 1 1 1
output
0
input
1
3
output
3
Note

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——————【数学】的更多相关文章

  1. CodeForces 404C Ivan and Powers of Two

    Ivan and Powers of Two Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  2. 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 ...

  3. CF 317D Game with Powers

    题解: 将一个数的指数看着一个堆,问题变成这些堆的异或值 分析一个堆的情况,打SG表. #include<stdio.h> #include<string.h> ]; char ...

  4. 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 ...

  5. Codeforces Round #529 (Div. 3) C. Powers Of Two(数学????)

    传送门 题意: 给出一个整数 n ,问能否将 n 分解成 k 个数之和,且这 k 个数必须是2的幂. 如果可以,输出"YES",并打印出任意一组解,反之输出"NO&quo ...

  6. 快速切题CF 158B taxi 构造 && 82A double cola 数学观察 难度:0

    实在太冷了今天 taxi :错误原因1 忽略了 1 1 1 1 和 1 2 1 这种情况,直接认为最多两组一车了 2 语句顺序错 double cola: 忘了减去n的序号1,即n-- B. Taxi ...

  7. CF C.Ivan the Fool and the Probability Theory【思维·构造】

    题目传送门 题目大意: 一个$n*m$的网格图,每个格子可以染黑色.白色,问每个格子最多有一个相邻格子颜色相同的涂色方案数$n,m<=1e5$ 分析: 首先,考虑到如果有两个相邻的格子颜色相同, ...

  8. CF R639 div 2 E Quantifier Question 数学 dfs 图论

    LINK:Quantifier Question 题面过长 引起不适 读题花了好长时间 对于 和 存在符合不是很熟练 导致很懵逼的做完了. 好在还算很好想.不过wa到了一个坑点上面 自闭一大晌 还以为 ...

  9. cf 12C Fruits(贪心【简单数学】)

    题意: m个水果,n个价格.每种水果只有一个价格. 问如果给每种水果分配价格,使得买的m个水果总价格最小.最大. 输出最小值和最大值. 思路: 贪心. 代码: bool cmp(int a,int b ...

随机推荐

  1. c#设计模式之:组合模式(Composite)

    一:引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理,因为目录客园包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象和复合对象在功能上 ...

  2. angular 中间人模式

    import { Component, OnInit, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app ...

  3. kali linux之DNS,NTP放大攻击

    DNS放大: 产生大流量的攻击方法-----单机的带宽优势,巨大的单机数量形成的流量汇聚,利用协议特性实现放大效果的流量 DNS协议放大效果----查询请求流量小,但响应流量可能非常巨大(dig AN ...

  4. kali linux之防火墙识别

    通过检查回包,可能识别端口是否经过防火墙过滤,设备多种多样,结果存在一定的误差 Send Response   Type SYN   NO    Filtered(先发送syn 如果不给回复 防火墙可 ...

  5. IIS发布的网页上传文件被拒绝

    在IIS所在的服务器共享的权限(如下图示,但注意不是加everyone)和共享文件夹的权限里都加上IIS_USER完全控制,如果不行再加上NETWORK SERVICE权限

  6. BZOJ 1061 [Noi2008]志愿者招募(费用流)

    题目描述 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能完成,其中第i ...

  7. I/O(输入/输出)---序列化与反序列化

    概念: 序列化就是将对象的状态存储到特定的介质中的过程,也就是将对象状态转换为可保持或传输格式的过程. 反序列化则是从特定存储介质中将数据重新构建对象的过程.可以将存储在文件上的对象信息读取,然后重新 ...

  8. checkbox的常见问题

    1.在使用checkbox时,最好不要阻止他原有的事件,要利用它原有的事件进行控制 2.尽量使用label for属性,不要对input元素的父元素或者input本身绑定事件,这样不能有效的避免冒泡事 ...

  9. [CQOI2006]凸多边形(半平面交)

    很明显是一道半平面交的题. 先说一下半平面交的步骤: 1.用点向法(点+向量)表示直线 2.极角排序,若极角相同,按相对位置排序. 3.去重,极角相同的保留更优的 4.枚举边维护双端队列 5.求答案 ...

  10. 如何查看Centos版本

    使用命令 cat /etc/centos-release 查看效果如下图 当然,你也可以查看红帽的版本 cat /etc/redhat-release 郴州软件开发培训 郴州软件培训 郴州java培训 ...