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. 下载Chrome浏览器离线安装包

    下面提供了window和Mac OS两个版本的Chrome离线版本: Windows版本 Mac OS版本 说明 基本格式是在 chrome 首页的链接 https://www.google.com/ ...

  2. centos7下git的安装和配置

    git的安装: yum 源仓库里的 Git 版本更新不及时,最新版本的 Git 是 1.8.3.1,但是官方最新版本已经到了 2.9.2.想要安装最新版本的的 Git,只能下载源码进行安装. 1. 查 ...

  3. HTML中&nbsp; &ensp; &emsp; &thinsp;等6种空白空格

  4. Java程序员修炼之路

    作者简介:王成委,CSDN知识库特邀编辑,Java高级工程师,熟悉Java编程语言和Oracle数据库.专注于高并发架构设计和大数据存储方向的研究. 我们为什么选择Java 大多数人选择Java可能只 ...

  5. Configure MongoDB Replica Set

    Table of Contents Introduction Requirements Create Replica Set Add Secondary Members Add an Arbiter ...

  6. c语言数据结构学习心得——数据结构基本概念

    1.数据>数据元素>数据项      数据的基本单位是数据元素,数据元素的基本单位是数据项 2.运算的定义->针对逻辑结构 集合:同属于一个集合,无其他关系.(数学上的集合) 线性结 ...

  7. 使用vue-cli脚手架搭建项目,保存编译时出现的代码检查错误(ESLint)

    一.问题 出现这么写错误是什么原因呢?相信很多小白都会像我一样,第一次接触时有点二丈和尚摸不着头脑.其实是在你用vue-cli脚手架构建项目时用了ESLint代码检查工具,如下图 那么什么是ESLin ...

  8. mysql 5.7.22 解压缩安装

    1.下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 直接点击下载项 下载后: 2.可以把解压的内容随便放到一个目录,我的是如 ...

  9. python之IO目录处理

    IO目录处理 在使用io常用函数之前,必须要在py文件头部import os.os是(Operation system)的缩写,意思就是系统操作. 1. 创建删除目录 #!/usr/bin/pytho ...

  10. I Hate It(线段树区间最值,单点更新)-------------蓝桥备战系列

    很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.  这让很多学生很反感.  不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老 ...