zoukankan      html  css  js  c++  java
  • CF 305C ——Ivan and Powers of Two——————【数学】

    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
    ",Maxn+1-S.size());
        }
        return 0;
    }
    

      

  • 相关阅读:
    Java代码实现依赖注入
    Linux shell脚本的字符串截取
    Android教程:wifi热点问题
    Android framework层实现实现wifi无缝切换AP
    http mimetype为multipart/x-mixed-replace报文
    Realtek 8192cu 支持 Android Hotspot 软ap
    http协议详解
    Android 在一个程序中启动另一个程序(包名,或者类名)
    linux定时器
    进程与线程的一个简单解释(转)
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4540544.html
Copyright © 2011-2022 走看看