您的位置:首页 > 其它

Ivan and Powers of Two

2016-04-09 12:01 375 查看
对于所求的2^v-1,必然是由2进制所有位都是1组成的。我们将相同的项

合并有2^a+2^a=2^(a+1)用STL中的set模拟一下,每次读取x,判断x是否

存在,如果存在那么删去x,插入x+1需要注意的是,上面所说的过程需要

一直循环进行,如果x+1也存在,那么删去x+1,再看x+2,如果x+2也存在,

那么删去x+2.。。。直到x+i不存在,插入x+i.最后用最大的a减去set的大

小即为答案

#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
int main()
{
int n;
scanf("%d", &n);
set<int> s;
int maxnum = 0;
for (int i = 1; i <= n; i++)
{
int tot;
scanf("%d", &tot);
while (s.count(tot))
{
//tot = tot + 1;
s.erase(tot);
tot = tot + 1;
}
s.insert(tot);
maxnum = max(maxnum, tot);
}
int ans = maxnum - s.size() + 1;
printf("%d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: