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的大
小即为答案
合并有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; }
相关文章推荐
- 插入排序
- 学习进度条05
- imageloder显示参数配置
- LeetCode 217 -Contains Duplicate ( JAVA )
- leetcode 11. Container With Most Water
- 《算法(第四版)》排序-----堆排序
- 计算机图形学(一) 视频显示设备_4_彩色CRT监视器
- android 开启本地相册选择图片并返回显示
- MySQL--基本内容
- excel如何快速实现数据区域的框选
- BUY LOW, BUY LOWER
- Linux命令行:rmdir命令
- 【剑指 offer】(十三)—— 在 O(1) 时间删除链表结点
- 稳定的奇偶数字排序
- Chrome浏览器的密码数据
- UVa 201 Squares 枚举
- PHP MVC架构
- java工厂模式
- C++实现——求三角形的面积(海伦公式)
- 分拆素数和