`
Simone_chou
  • 浏览: 192586 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Ivan and Powers of Two(杂)

    博客分类:
  • CF
 
阅读更多
C. 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 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.

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(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;
}

 

 

分享到:
评论

相关推荐

    Bulletproof SSL and TLS,PDF , Ivan Ristic

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

    Bulletproof SSL and TLS

    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 _ Sketchpad - UCAM-CL-TR-574.pdf

    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 -美国新数学丛书英文版第一册-有理数和无理数

    美国新数学丛书 英文版 第一册 (NML-01)Numbers: Rational and Irrational by Ivan Niven 有理数和无理数

    openssl cookbook

    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老师版本

    《新浪微博Ivan老师版本:Android学习的宝贵资源》 在当今移动互联网时代,社交媒体平台扮演着举足轻重的角色,而新浪微博作为中国领先的社交媒体之一,吸引了无数用户与开发者参与其中。"新浪微博Ivan老师版本"是...

    Ivan:网络攻防赛,提升你的安全技术水平

    9月23-25日,中国规模最大的信息安全专业会议——2013中国互联网安全大会(ISC)在北京国家会议中心举行。Compass Security AG CEO Ivan介绍了《网络攻防赛,提升你的安全技术水平》。

    Inside-Delphi-2006-by-Ivan-Hladni

    Inside-Delphi-2006-by-Ivan-Hladni

    Waveform Design and Diversity for Advanced Radar Systems

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

    HTTPS权威指南英文版_Ivan Ristic_SSL_TLS and PKI to Secure Servers and Web Applications

    本书是集理论、协议细节、漏洞分析、部署建议于一体的详尽Web应用安全指南。...最新的攻击形式,如BEAST、CRIME、BREACH、Lucky 13等;详尽的部署建议;如何使用OpenSSL生成密钥和确认信息;如何使用Apache httpd、IIS...

    modsecurity handbook

    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语言因其易读性强、库丰富以及强大的数据分析能力而备受青睐。Ivan Idris是一位知名的Python作者,他的著作《Python数据分析》深入浅出地介绍...

    Ivan - MetaTrader 5EA.zip

    "Ivan - MetaTrader 5 EA.zip"是一个包含名为"Ivan.mq5"的专家顾问程序,它设计用于自动执行交易策略,尤其是基于可用保证金的风险百分比来计算交易手数。 首先,我们需要理解MetaTrader 5平台的基础知识。MT5是一...

    Ivan Kirigin 的相机标定

    Ivan Kirigin的相机标定工作显然是在优化这一过程,尤其是通过改进toolbox-calib,使用户界面更加友好,操作更为简便。 toolbox-calib是一个常见的开源工具包,用于执行相机标定,通常包括多个步骤。首先,它会要求...

    Functional Programming in C++ (2018.11出版,PDF格式)

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

    《Computer Networks: Performance and Quality of Service》是一本深入探讨现代计算机网络及其关键性能指标与服务质量(QoS)的专业书籍。作者Ivan Marsic是罗格斯大学电气与计算机工程系教授,在本书中,他特别...

    Python Data Analysis Cookbook

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

    Learning Python

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

Global site tag (gtag.js) - Google Analytics