#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
struct Group
{
ll val;
pair<int,int> seg;
Group(){}
Group(ll a, int b, int c){ val=a;seg=mp(b,c);}
Group(ll a, pair<int,int> b){ val=a;seg=b;}
bool operator < (Group b) const { return val>b.val || val==b.val && seg<b.seg;}
};
set<Group> best;
set<pair<int,int> > ord;
set<pair<int,int> >::iterator it;
const int N=400050;
int a[N],n;
ll sum[N];
ll get(pair<int,int> p){ return sum[p.second]+(p.first>0?sum[p.first-1]:0);}
void Del(pair<int,int> p)
{
//printf("Del: (%i %i)\n",p.first,p.second);
if(p.first>=1 && p.second<=n) best.erase(Group(get(p),p));
ord.erase(p);
}
void Ins(pair<int,int> p)
{
//printf("Ins: (%i %i)\n",p.first,p.second);
if(p.first>=1 && p.second<=n) best.insert(Group(get(p),p));
if(p.first>=1 || p.second<=n) ord.insert(p);
}
void Debug()
{
set<Group>::iterator it;
for(it=best.begin();it!=best.end();it++)
{
printf("%lld:(%i %i) ",it->val,it->seg.first,it->seg.second);
}
printf("\n");
}
int main()
{
int i;
scanf("%i",&n);
for(i=1;i<=n;i++)
{
scanf("%i",&a[i]);
sum[i]=-sum[i-1]+a[i];
ord.insert(mp(i,i));
best.insert(Group(a[i],i,i));
}
for(i=n+1;i<=n*2;i++) sum[i]=-sum[i-1];
ll sol=0;int cnt=0;
while(best.size())
{
//Debug();
Group tmp=*best.begin();
//best.erase(tmp);
sol+=tmp.val;
it=ord.find(tmp.seg);
pair<int,int> dl=*it;
//printf("%i %i\n",tmp.seg.first,tmp.seg.second);
tmp.seg.first--;
tmp.seg.second++;
pair<int,int> p;
if(it!=ord.begin())
{
it--;p=*it;it++;
//printf("Left: (%i %i)\n",p.first,p.second);
if(p.second==tmp.seg.first)
{
Del(p);
//best.erase(Group(get(p),p));
//ord.erase(p);
tmp.seg.first=p.first;
}
}
it++;
if(it!=ord.end())
{
p=*it;
//printf("Right: (%i %i)\n",p.first,p.second);
if(p.first==tmp.seg.second)
{
Del(p);
//best.erase(Group(get(p),p));
//ord.erase(p);
tmp.seg.second=p.second;
}
}
it--;
Del(dl);
//ord.erase(*it);
Ins(tmp.seg);
//if(tmp.seg.first>=1 || tmp.seg.second<=n)
//{
// if(tmp.seg.first>=1 && tmp.seg.second<=n) best.insert(Group(get(tmp.seg),tmp.seg));
// ord.insert(tmp.seg);
//}
cnt++;//printf("%i ",cnt);
printf("%lld\n",sol);
//if(cnt==(n+1)/2) break;
}
return 0;
}
Compilation message
candies.cpp: In member function 'bool Group::operator<(Group) const':
candies.cpp:13:67: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
bool operator < (Group b) const { return val>b.val || val==b.val && seg<b.seg;}
~~~~~~~~~~~^~~~~~~~~~~~
candies.cpp: In function 'int main()':
candies.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&n);
~~~~~^~~~~~~~~
candies.cpp:49:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&a[i]);
~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
664 KB |
Output is correct |
4 |
Correct |
6 ms |
884 KB |
Output is correct |
5 |
Correct |
5 ms |
884 KB |
Output is correct |
6 |
Correct |
6 ms |
884 KB |
Output is correct |
7 |
Correct |
6 ms |
972 KB |
Output is correct |
8 |
Correct |
5 ms |
972 KB |
Output is correct |
9 |
Correct |
5 ms |
972 KB |
Output is correct |
10 |
Correct |
5 ms |
1100 KB |
Output is correct |
11 |
Correct |
8 ms |
1100 KB |
Output is correct |
12 |
Correct |
5 ms |
1100 KB |
Output is correct |
13 |
Correct |
7 ms |
1100 KB |
Output is correct |
14 |
Correct |
5 ms |
1100 KB |
Output is correct |
15 |
Correct |
6 ms |
1100 KB |
Output is correct |
16 |
Correct |
5 ms |
1100 KB |
Output is correct |
17 |
Correct |
6 ms |
1100 KB |
Output is correct |
18 |
Correct |
7 ms |
1100 KB |
Output is correct |
19 |
Correct |
4 ms |
1100 KB |
Output is correct |
20 |
Correct |
5 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
664 KB |
Output is correct |
4 |
Correct |
6 ms |
884 KB |
Output is correct |
5 |
Correct |
5 ms |
884 KB |
Output is correct |
6 |
Correct |
6 ms |
884 KB |
Output is correct |
7 |
Correct |
6 ms |
972 KB |
Output is correct |
8 |
Correct |
5 ms |
972 KB |
Output is correct |
9 |
Correct |
5 ms |
972 KB |
Output is correct |
10 |
Correct |
5 ms |
1100 KB |
Output is correct |
11 |
Correct |
8 ms |
1100 KB |
Output is correct |
12 |
Correct |
5 ms |
1100 KB |
Output is correct |
13 |
Correct |
7 ms |
1100 KB |
Output is correct |
14 |
Correct |
5 ms |
1100 KB |
Output is correct |
15 |
Correct |
6 ms |
1100 KB |
Output is correct |
16 |
Correct |
5 ms |
1100 KB |
Output is correct |
17 |
Correct |
6 ms |
1100 KB |
Output is correct |
18 |
Correct |
7 ms |
1100 KB |
Output is correct |
19 |
Correct |
4 ms |
1100 KB |
Output is correct |
20 |
Correct |
5 ms |
1100 KB |
Output is correct |
21 |
Correct |
911 ms |
27980 KB |
Output is correct |
22 |
Correct |
930 ms |
28048 KB |
Output is correct |
23 |
Correct |
907 ms |
28048 KB |
Output is correct |
24 |
Correct |
381 ms |
28092 KB |
Output is correct |
25 |
Correct |
408 ms |
28092 KB |
Output is correct |
26 |
Correct |
403 ms |
28092 KB |
Output is correct |
27 |
Correct |
401 ms |
28092 KB |
Output is correct |
28 |
Correct |
399 ms |
28092 KB |
Output is correct |
29 |
Correct |
414 ms |
28092 KB |
Output is correct |
30 |
Correct |
411 ms |
28092 KB |
Output is correct |
31 |
Correct |
413 ms |
28092 KB |
Output is correct |
32 |
Correct |
435 ms |
28092 KB |
Output is correct |
33 |
Correct |
628 ms |
28092 KB |
Output is correct |
34 |
Correct |
639 ms |
28128 KB |
Output is correct |
35 |
Correct |
562 ms |
28128 KB |
Output is correct |
36 |
Correct |
847 ms |
28128 KB |
Output is correct |
37 |
Correct |
821 ms |
28128 KB |
Output is correct |
38 |
Correct |
884 ms |
28140 KB |
Output is correct |
39 |
Correct |
380 ms |
28168 KB |
Output is correct |
40 |
Correct |
385 ms |
28168 KB |
Output is correct |
41 |
Correct |
405 ms |
28168 KB |
Output is correct |
42 |
Correct |
403 ms |
28168 KB |
Output is correct |
43 |
Correct |
399 ms |
28168 KB |
Output is correct |
44 |
Correct |
411 ms |
28168 KB |
Output is correct |
45 |
Correct |
443 ms |
28168 KB |
Output is correct |
46 |
Correct |
461 ms |
28168 KB |
Output is correct |
47 |
Correct |
479 ms |
28168 KB |
Output is correct |
48 |
Correct |
626 ms |
28168 KB |
Output is correct |
49 |
Correct |
648 ms |
28168 KB |
Output is correct |
50 |
Correct |
724 ms |
28168 KB |
Output is correct |