Ivan and Powers of Twotime limit per test
1 second
Memory limit per test
256 megabytes
Input
Standard input
Output
Standard output
Ivan has got an arrayNNon-negative integersA1, bytes,A2, middle..., middle ,...,AN.
Ivan knows that the array is sorted in the non-decreasing order.
Ivan wrote out integers 2A1, limit 2A2, middle..., middle 2ANOn
A piece of paper. Now he wonders, what minimum number of integers of form 2B(BLimit ≥ limit 0) need
To be added to the piece of paper so that the sum of all integers written on the paper LEDs 2VAccept-limit 1
Some integerV(VLimit ≥ limit 0 ).
Help Ivan, find the required quantity of numbers.
Input
The first line contains integerN(1 digit ≤ DigitNLimit ≤ limit 105 ).
The second input line containsNSpace-separated integersA1, bytes,A2, middle..., middle ,...,AN(0 bytes ≤ bytesAILimit ≤ limit 2 · 109 ).
It is guaranteed thatA1 bytes ≤ bytesA2 bytes ≤ bytes... bytes ≤ bytesAN.
Output
Print a single integer-the answer to the problem.
Sample test (s) input
40 1 1 1
Output
0
Input
13
Output
3
Note
In the first sample you do not need to add anything, the sum of numbers already equals 23 bytes-interval 1 records = Limit 7.
In the second sample you need to add numbers 20, limit 21, limit 22.
Train of Thought: Here is a Chinese train of thought. The following is an English train of thought because Level 4 has not passed the test. You must practice it. Otherwise, you will be finished.
Greedy.
First, you have to prepare the construction of the proportional sequence.
1. If the sequence is not the same, it is easy to do it.
2. What if the non-descending sequence has the same?
The same two numbers must be merged until the current number is unique in the set.
Reason:
(1) Merge a and a into a + 1 because: 2 ^ a + 2 ^ a = 2*2 ^ a = 2 ^ (a + 1 ), merging into one and adding one is the same cost as combining.
(2) 2 ^ a is the sum of all numbers greater than the low position, therefore, when there are two a S, we will never consider taking a to the low level to splice it with other numbers into the other missing ones. That is to say, for the current ai:
You don't have to worry about low positions. What is low positions? You have to make up for nothing. ai only needs to think up.
AC Program:
#include<iostream>#include<stdio.h>#include<string.h>#include<set>using namespace std;//a,a exists,it must be combined cause //(1)a,a->a+1 can minimize the number//(2)2^a has been bigger than the lower bit numbers,it's impossible to be used to make//the sum of lower bit numbers.int main(){set<int>ss;int n;int a;cin>>n;int maxn=0;for(int i=0;i<n;i++){ cin>>a; while(ss.count(a))ss.erase(a),a++;//2^a+2^a=2*2^a=2^(a+1)//change until 'a' are the only one. ss.insert(a); maxn=max(a,maxn); }cout<<maxn+1-ss.size()<<endl;//system("Pause");return 0;}