【CodeForces - 305C】Ivan and Powers of Two(思维,STL,set,优先队列)

246 篇文章 1 订阅
34 篇文章 0 订阅

题干:

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

Examples

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个数a1,a2....an,分表代表2^a1,2^a2....2^an。现在让你添加一些数(假设num个),使2^a1 + 2^a2 + ... + 2^an + ... + 2^anum 的和为二进制的全1数(题目中叙述是存在一个b,s.t. 数字 = 2^b-1)。求num的最小值

解题报告:

  推出来,需要的数恰好就是最终集合合并后的剩下缺少的元素之后(不难证明吧貌似2333),接下来就是怎么维护这个集合的元素了。

  这题可以贪心,用pq暴力合并,别用multiset+set、、、会炸的(不是炸在迭代器上而是炸在多次的insert上)、

  当然还有更好的做法那就是直接set,然后读入的同时就维护set中的元素就可以了,最后直接输出一个值就是我们需要的那个值了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
set<ll> ss;
int main()
{
	int n;
	ll tmp,maxx = -1;
	cin>>n;
	for(int i = 1; i<=n; i++) {
		scanf("%lld",&tmp);
		while(ss.find(tmp) != ss.end()) {
			ss.erase(tmp);
			tmp++;
		}
		ss.insert(tmp);
		maxx = max(maxx,tmp);
	} 
	printf("%lld\n",maxx + 1 - ss.size());

	return 0 ;
 }

错误代码:(这样写思路是没错的就是insert的时候需要insert   tmp/2 这么多个,所以时间效率还是不高,改成map可能会好一点)(刚开始不是+1,,是+tmp/2,这样是一定错的,因为这是幂次关系啊不是线性的、、)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int n;
ll tmp;
multiset<ll> ms;
set<ll> s;
ll a[MAX];
int main() {
	cin>>n;
	for(int i = 1; i<=n; i++) scanf("%lld",a+i),ms.insert(a[i]),s.insert(a[i]);

	for(set<ll> :: iterator it = s.begin(); it != s.end();) {
//		while(ms.count(*it) >= 2) {
//			ms.erase(ms.find((*it)));
//			ms.erase(ms.find((*it)));
//			ms.insert((*it)+1);
//			s.insert((*it)+1);
//			if(ms.find((*it)) == ms.end()) s.erase((*it));
//		}
//		++it;
		tmp = ms.count((*it));
		if(tmp % 2 == 1) {
			s.insert((*it) + 1);
			ms.erase(*it);
			ms.insert(*it);
			ms.insert((*it) + 1);
		} else {
			ms.erase((*it));
			s.insert((*it) + 1);
			ms.insert((*it) + 1);
			s.erase(*it);

		}
		++it;
	}
	multiset<ll> :: iterator mit = ms.end();
	--mit;
//	cout << *mit<< endl;
	ll ans = (*mit) - s.size() + 1;
	cout << ans << endl;
	return 0 ;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值