#include<bits/stdc++.h>
#define int long long
//#define ll long long
#define down cout<<'\n';
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
const int inf=1e9;
void phongbeo();
const int mod2=1e9+7;
const int mod1=998244353;
struct icd
{
int a,b;
};
struct ib
{
int a;
int b;
};
struct ic
{
int a,b,c;
};
struct id
{
int a,b,c,d;
};
struct ie
{
int a,b,c, d,e,f;
};
int n,m,s1,s2,s4,s3,sf,k,r,mid,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,dem=0,l;
int i,s10,s12;
int el=29;
main()
{
#ifndef ONLINE_JUDGE
//fin(task),fou(task);
#endif
NHP
/// cin>>s1;
// modwwe
phongbeo(),down
checktime
}
bool b[200002];
int a[200002];
int cc[200002];
ib d[200002];
ib dsu[200002];
void reset(int x)
{
dsu[x]= {1,x};
d[x]= {x,x};
}
int get(int x)
{
if(dsu[x].b!=x)dsu[x].b=get(dsu[x].b);
return dsu[x].b;
}
void noi(int x,int y)
{
x=get(x);
y=get(y);
if(x==y) return;
if(dsu[x].a<dsu[y].a) swap(x,y);
dsu[x].a+=dsu[y].a;
dsu[y].b=x;
d[x].a=min(d[x].a,d[y].a);
d[x].b=max(d[y].b,d[x].b);
}
struct cmp
{
bool operator()(ic a,ic b)
{
return a.a<b.a;
}
};
bool check(ic x)
{
auto [c, l, r] = x;
return !b[l] && !b[r] && !b[l-1] && !b[r + 1];
}
void phongbeo()
{
cin>>n;
priority_queue<ic,vector<ic>,cmp>p;
for(int i=1; i<=n; i++)
cin>>a[i],p.push({a[i],i,i}),reset(i);
for(int i=1; i<=n; i++)
{
if(i%2==0)a[i]=-a[i];
cc[i]=cc[i-1]+a[i];
}
for(int i=1; i<=(n+1)/2; i++)
{
ic x=p.top();
while(!check(x))
{
p.pop();
x=p.top();
}
s2+=x.a;
cout<<s2,down
b[x.b]=1;
b[x.c]=1;
if(x.b-1>0) noi(x.b,x.b-1);
if(x.c+1<=n) noi(x.c,x.c+1);
s3=get(x.b);
s4=d[s3].a;
s5=d[s3].b;
if(s5%2==0)s6=-1;
else s6=1;
p.push({(cc[s5]-cc[s4-1])*s6,s4,s5});
}
}
Compilation message
candies.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
44 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
2 ms |
4704 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4724 KB |
Output is correct |
17 |
Correct |
1 ms |
4576 KB |
Output is correct |
18 |
Correct |
2 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4572 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
2 ms |
4704 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4724 KB |
Output is correct |
17 |
Correct |
1 ms |
4576 KB |
Output is correct |
18 |
Correct |
2 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
4572 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
21 |
Correct |
81 ms |
18132 KB |
Output is correct |
22 |
Correct |
121 ms |
19636 KB |
Output is correct |
23 |
Correct |
80 ms |
18632 KB |
Output is correct |
24 |
Correct |
62 ms |
19436 KB |
Output is correct |
25 |
Correct |
58 ms |
18376 KB |
Output is correct |
26 |
Correct |
55 ms |
19384 KB |
Output is correct |
27 |
Correct |
56 ms |
18804 KB |
Output is correct |
28 |
Correct |
69 ms |
18632 KB |
Output is correct |
29 |
Correct |
52 ms |
18376 KB |
Output is correct |
30 |
Correct |
57 ms |
18884 KB |
Output is correct |
31 |
Correct |
60 ms |
18376 KB |
Output is correct |
32 |
Correct |
54 ms |
18888 KB |
Output is correct |
33 |
Correct |
62 ms |
17864 KB |
Output is correct |
34 |
Correct |
60 ms |
18636 KB |
Output is correct |
35 |
Correct |
62 ms |
18836 KB |
Output is correct |
36 |
Correct |
85 ms |
19236 KB |
Output is correct |
37 |
Correct |
83 ms |
18636 KB |
Output is correct |
38 |
Correct |
87 ms |
18380 KB |
Output is correct |
39 |
Correct |
57 ms |
18608 KB |
Output is correct |
40 |
Correct |
66 ms |
19120 KB |
Output is correct |
41 |
Correct |
59 ms |
18448 KB |
Output is correct |
42 |
Correct |
53 ms |
18872 KB |
Output is correct |
43 |
Correct |
56 ms |
19640 KB |
Output is correct |
44 |
Correct |
67 ms |
18972 KB |
Output is correct |
45 |
Correct |
53 ms |
18868 KB |
Output is correct |
46 |
Correct |
63 ms |
18636 KB |
Output is correct |
47 |
Correct |
56 ms |
18288 KB |
Output is correct |
48 |
Correct |
75 ms |
18636 KB |
Output is correct |
49 |
Correct |
63 ms |
18880 KB |
Output is correct |
50 |
Correct |
71 ms |
19124 KB |
Output is correct |