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. 浅谈 kubernetes service 那些事(上篇)

    一.问题 首先,我们思考这样一个问题: 访问k8s集群中的pod, 客户端需要知道pod地址,需要感知pod的状态.那如何获取各个pod的地址?若某一node上的pod故障,客户端如何感知? 二.k8 ...

  2. NSRange 范围

    前言 结构体,这个结构体用来表示事物的一个范围,通常是字符串里的字符范围或者集合里的元素范围. typedef struct _NSRange { NSUInteger location; // 表示 ...

  3. docker kubernetes swarm spring cloud结合学习资源

    http://www.docin.com/p-2062732301.html https://blog.csdn.net/michael_hm/article/details/79213839 htt ...

  4. Python里面的负号的各种神奇用法?来填坑啦

    1.x.reshape(-1,2) x = np.linspace(1,10,10) x.reshape(-1,2) reshape(-1,2)里-1的应该是不管多少行,按两列算,行数自动算出.同理, ...

  5. Python——requests的安装及入门-贴吧爬虫

    一.windows平台下requests的安装 1.win+R,输入cmd,打开命令行窗口,输入命令:pip install requests ,即可自动安装库成功 2.输入命令:pip list,即 ...

  6. P2561 [AHOI2002]黑白瓷砖

    $ \color{#0066ff}{ 题目描述 }$ \(\color{#0066ff}{输入格式}\) 文件中以一行的形式存放一个正整数 n , n ≤ 20 . \(\color{#0066ff} ...

  7. Zookeeper客户端对比选择_4

    Zookeeper客户端对比选择 本文思维导图 使用框架的好处是自带一套实用的API,但是Zookeeper虽然非常强大,但是社区却安静的可怕,版本更新较慢,下面会先从zookeeper原生API的不 ...

  8. python wraps装饰器

    这是一个很有用的装饰器.看过前一篇反射的朋友应该知道,函数是有几个特殊属性比如函数名,在被装饰后,上例中的函数名foo会变成包装函数的名字 wrapper,如果你希望使用反射,可能会导致意外的结果.这 ...

  9. Android 日历视图(Calendarview)

    1.介绍 2.常用属性 3.xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...

  10. Codeforces - 185A 简单矩阵快速幂

    题意:求第n个三角形内部的上三角形个数 对每个三角形分别维护上下三角形个数,记为\(dp[1][i],dp[2][i]\) 规律很明显是 \(dp[1][i+1]=3*dp[1][i]+dp[2][i ...