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

题干:

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 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441110.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CodeForces - 1082B】Vova and Trophies (贪心模拟,暴力)

题干&#xff1a; Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row. The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wan…

java mac 转换 整形_JAVA的整型与字符串相互转换

1如何将字串 String 转换成整数 int?A. 有两个方法:1). int i Integer.parseInt([String]); 或i Integer.parseInt([String],[int radix]);2). int i Integer.valueOf(my_str).intValue();注: 字串转成 Double, Float, Long 的方法大同小异.2 如何将整数 int 转换成字串 St…

【CodeForces - 798A】Mike and palindrome (回文串,水题,字符串问题)

题干&#xff1a; Mike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome. A palindrome is a string that reads the same backward as forward, for e…

java迷宫生成算法_DFS算法迷宫生成器

我正在尝试使用DFS算法在ASCII中创建迷宫(&#xff03;表示墙和自由空间)&#xff0c;其左上角开始&#xff0c;右下角出口 . 问题是迷宫开始创建&#xff0c;然后它被阻止&#xff0c;因为它的所有邻居都已被访问过 .我从左上角开始&#xff0c;将单元格标记为已访问并放置一个…

【牛客 - 272A】Phrase String(构造,水题)

题干&#xff1a; 给出v, k&#xff0c;请你找到最小的正整数n&#xff0c;满足&#xff1a; n的二进制表示下存在一个长度为v的回文串&#xff0c;该回文串首尾都是1且n的二进制表示中至少有k个1。保证v,k均为偶数&#xff01; 由于n可能很大&#xff0c;你只需要输出对取模的…

eclipse 跑maven命令_maven编写命令行执行mvn package没问题,eclipse执行报错

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼报错是这样的“[ERROR] Unknown lifecycle phase "Systemout3". You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sou…

【CodeForces - 798D】Mike and distribution (思维构造,贪心,黑科技)

题干&#xff1a; Mike has always been thinking about the harshness of social inequality. Hes so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A  [a1, a2, ..., an…

erlang odbc mysql参数_Erlang在Redhat 5.3下使用unixODBC连接Oracle数据库的配置

个人在安装配置时遇到一些麻烦&#xff0c;特此记录如下&#xff1a; 环境 数据库服务器操作系统&#xff1a;Windows 2003 数据库&#xff1a;Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 erlang运行的服务器操作系统&#xff1a;Redhat 5.3 erlang:Erlang R1…

【POJ - 2533】Longest Ordered Subsequence(四种方法解决最长上升子序列 含二分优化版本)

题干&#xff1a; Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 41944 Accepted: 18453 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any se…

halcon图片上传到mysql_C# 10个线程并发执行Halcon图像算法 报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”...

如题&#xff0c;这个问题本人已经纠结了快三个工作日了。本人不同WinFrom程序一起动就会开启10个线程&#xff0c;并发对10张图片进行算法处理&#xff0c;问题是只要程序一起动就会报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”异常。本人试过将8个线程停掉…

【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)

题干&#xff1a; Constructing Roads In JGShinings Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29933 Accepted Submission(s): 8496 Problem Description JGShinings kingdom consists of 2n…

java list详解_Java 中 list 用法案例详解

LIST是个容器接口可以理解为动态数组&#xff0c;传统数组必须定义好数组的个数才可以使用&#xff0c;而容器对象无须定义好数组下标总数&#xff0c;用add()方法即可添加新的成员对象&#xff0c;他可以添加的仅仅只能为对象&#xff0c;不能添加基本数据类型&#xff0c;容器…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐的组合数+(取模,数学,思维)

题干&#xff1a; 小乐乐得知一周有7天之后就对7产生了兴趣。 小乐乐得到了两堆数字数字时连续的。 第一堆包含[1,n]n个数字&#xff0c;第二堆包含[1,m]m个数字。 小乐乐想要从两堆中各挑选出一个整数x,y&#xff0c;使得x,y的和为7的倍数。 请问小乐乐有多少种组合的方式…

java 写文件 属性吗_使用JAVA读写Properties属性文件

自己定义一个属性文件&#xff1a;例如prop.propertiesbaseFilePathD\:/kuanter/resourcetesxabcd我们要做的第一步就是要将文件读取到Properties类对象中&#xff0c;由于load有一个参数是InputStream&#xff0c;所以我们可以用 InputStream的子类FileInputStream将属性文件读…

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】 小乐乐算数字(水题,快速幂,lowbit)

题干&#xff1a; 小乐乐最喜欢玩数字了。 小乐乐最近迷上了2这个整数&#xff0c;他觉得2的幂是一种非常可爱的数字。 小乐乐想知道整数x的最大的 2的幂 &#xff08;2^y&#xff09;的因子。 y为整数。 输入描述: 输入整数x。(1<x<1e18) 输出描述: 输出整数x的最…

java date 相差_java 比较时间相差多少分钟

/** * 返回二个时间相差的分分钟数,如果一个为空&#xff0c;返回为0&#xff1b; * param startDate&#xff0c;比如08&#xff1a;09 * param endDate&#xff0c;如18&#xff1a;09 * return */ public static int getMinutesDiff(String startDate,String endDate){ int …

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐吃糖豆 (fIb博弈)

题干&#xff1a; 小乐乐是一个比较喜欢吃糖豆的小孩子&#xff0c;小乐乐的哥哥大乐乐也同样爱吃糖豆。 作为一个小孩子&#xff0c;他们永远觉得谁吃掉了最后一个糖豆&#xff0c;谁吃的糖豆最多。 为了公平起见小乐乐与大乐乐商量吃糖豆的规则如下&#xff1a; 1. …

html5 sse java_html5----sse实现服务端推送数据给前端

案例基于thinkPHP框架&#xff1a;服务端方法&#xff1a;public function ssefun(){ob_implicit_flush();header(Content-Type: text/event-stream);header(Cache-Control: no-cache);$itime();echo retry:1000.PHP_EOL;//每秒执行一次echo "data: The server time is: {…

【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐切割方块(思维,水题)

题干&#xff1a; 小乐乐的作业本是2n*2n的方格本。 某天小乐乐的童鞋&#xff0c;想要考验一下小乐乐。 他将小乐乐的一张方格纸中的某个格子(x,y)涂成黑色&#xff0c; 小乐乐能否在将4*4的方格本沿着方格边缘且切割线与黑色方格不存在公共交点的情况下将方格本切割成两…

*【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐打游戏(bfs,双元bfs,思维)

题干&#xff1a; 小乐乐觉得学习太简单了&#xff0c;剩下那么多的时间好无聊&#xff0c;于是便想打游戏。 最近新出了一个特别火的游戏&#xff0c;叫吃猪&#xff0c;小乐乐准备玩一玩。 吃猪游戏很简单&#xff0c;给定一个地图&#xff0c;大小为n*m&…