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 form 2b (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(1 ~ 100000)代表有 n 个数,后给出 n 个数,代表 2 ^ ai,加和之后,问要添加多少个 2 ^ i 使变成 2 ^ v -1。
思路:
因为 2 ^ a + 2 ^ a = 2 ^ (a + 1),所以当某个数出现 2 次以上的时候可以通过合并不断得到新的数,并且保证每个数都出现一次。
AC:
#include <cstdio> #include <cstring> #include <algorithm> #include <set> using namespace std; int main() { int n; set<int> s; scanf("%d", &n); int max_num = -1; while (n--) { int ans; scanf("%d", &ans); while (s.count(ans)) { s.erase(ans); ++ans; } s.insert(ans); max_num = max(max_num, ans); } printf("%d\n", max_num - s.size() + 1); return 0; }
相关推荐
Mitigation of Attacks against TLS and SPDY 212 Mitigation of Attacks against HTTP Compression 213 Padding Oracle Attacks 214 What Is a Padding Oracle? 214 Attacks against TLS 215 Impact 216 Mitigation...
He is the author of two books, Apache Security and ModSecurity Handbook, which he publishes via Feisty Duck, his own platform for continuous writing and publishing. Ivan is an active participant in ...
Ivan Sutherland's Sketchpad is one of the most inuential computer programs ever written by an individual, as recognized in his citation for the Turing award in 1988. The Sketchpad program itself had...
美国新数学丛书 英文版 第一册 (NML-01)Numbers: Rational and Irrational by Ivan Niven 有理数和无理数
He is the author of two books, Apache Security and ModSecurity Handbook, which he publishes via Feisty Duck, his own platform for continuous writing and publishing. Ivan is an active participant in ...
《新浪微博Ivan老师版本:Android学习的宝贵资源》 在当今移动互联网时代,社交媒体平台扮演着举足轻重的角色,而新浪微博作为中国领先的社交媒体之一,吸引了无数用户与开发者参与其中。"新浪微博Ivan老师版本"是...
9月23-25日,中国规模最大的信息安全专业会议——2013中国互联网安全大会(ISC)在北京国家会议中心举行。Compass Security AG CEO Ivan介绍了《网络攻防赛,提升你的安全技术水平》。
Inside-Delphi-2006-by-Ivan-Hladni
Waveform diversity: a way forward to the future of the radar A. De Maio, A. Farina, F. Gini, L. Patton and M.Wicks Contents Chapter 1 Classical radar waveform design Nadav Levanon Chapter 2 ...
本书是集理论、协议细节、漏洞分析、部署建议于一体的详尽Web应用安全指南。...最新的攻击形式,如BEAST、CRIME、BREACH、Lucky 13等;详尽的部署建议;如何使用OpenSSL生成密钥和确认信息;如何使用Apache httpd、IIS...
Written by Ivan Ristic, who designed and wrote much of ModSecurity, this book will teach you everything you need to know to monitor the activity on your web sites and protect them from attack. ...
【Python数据分析源代码——Ivan Idris的作品】 在数据科学领域,Python语言因其易读性强、库丰富以及强大的数据分析能力而备受青睐。Ivan Idris是一位知名的Python作者,他的著作《Python数据分析》深入浅出地介绍...
"Ivan - MetaTrader 5 EA.zip"是一个包含名为"Ivan.mq5"的专家顾问程序,它设计用于自动执行交易策略,尤其是基于可用保证金的风险百分比来计算交易手数。 首先,我们需要理解MetaTrader 5平台的基础知识。MT5是一...
Ivan Kirigin的相机标定工作显然是在优化这一过程,尤其是通过改进toolbox-calib,使用户界面更加友好,操作更为简便。 toolbox-calib是一个常见的开源工具包,用于执行相机标定,通常包括多个步骤。首先,它会要求...
Functional Programming in C++ teaches developers the practical side of functional programming and the tools that C++ provides to develop software in the functional style. This in-depth guide is full ...
《Computer Networks: Performance and Quality of Service》是一本深入探讨现代计算机网络及其关键性能指标与服务质量(QoS)的专业书籍。作者Ivan Marsic是罗格斯大学电气与计算机工程系教授,在本书中,他特别...
Python Data Analysis Cookbook by Ivan Idris-P2P Packt Publishing | 22 July 2016 | English | ISBN: 178528228X | 462 pages Data analysis is a rapidly evolving field and Python is a multi-paradigm ...
Then, the authors address the mechanics of the language itself, providing illustrations of how Python conceives of numbers, strings, and other objects as well as the operators you use to work with ...