#include<bits/stdc++.h>
using namespace std;
int main()
{
iostream::sync_with_stdio(0);
int a,in,p2=0;
cin>>a;
priority_queue<int>kol;
vector<int>ile(301719);
for(int i = 0; i < a; ++i)
{
cin>>in;
++ile[in];
if(ile[in] == 2)
++p2;
kol.push(-in);
}
int top;
while(p2)
{
top = -kol.top();
kol.pop();
--ile[top];
if(-kol.top() == top)
{
--ile[top];
if(ile[top] < 2)
--p2;
kol.pop();
kol.push(-top - 1);
++ile[top + 1];
if(ile[top + 1] == 2)
++p2;
}
}
while(!kol.empty())
{
top = kol.top();
kol.pop();
}
cout<<-top;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include<bits/stdc++.h> using namespace std; int main() { iostream::sync_with_stdio(0); int a,in,p2=0; cin>>a; priority_queue<int>kol; vector<int>ile(301719); for(int i = 0; i < a; ++i) { cin>>in; ++ile[in]; if(ile[in] == 2) ++p2; kol.push(-in); } int top; while(p2) { top = -kol.top(); kol.pop(); --ile[top]; if(-kol.top() == top) { --ile[top]; if(ile[top] < 2) --p2; kol.pop(); kol.push(-top - 1); ++ile[top + 1]; if(ile[top + 1] == 2) ++p2; } } while(!kol.empty()) { top = kol.top(); kol.pop(); } cout<<-top; } |
English