日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 ; }

?

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的【CodeForces - 305C】Ivan and Powers of Two(思维,STL,set,优先队列)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。