QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#55545 | #2103. Termity | CharlieVinnie | WA | 4ms | 27188kb | C++20 | 1.8kb | 2022-10-14 15:05:29 | 2022-10-14 15:05:31 |
Judging History
answer
#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(int i=a;i>=b;i--)
#define Fin(file) freopen(file,"r",stdin)
#define Fout(file) freopen(file,"w",stdout)
using namespace std;
const int N=1e6+5; typedef long long ll;
struct Node { int id,type; ll val; bool operator< (const Node& nd) const { return val<nd.val; } };
int n,a[N],lcnt,pre[N],nxt[N],tp; ll st[N],lis[N];
vector<ll> vec[N]; int vcnt,pl[N],pr[N];
priority_queue<Node> q;
void solve(){
if(lcnt==0) return;
tp=0; For(i,1,lcnt) { st[++tp]=lis[i]; while( tp>=3 && st[tp-1]>st[tp-2] && st[tp-1]>st[tp] ) { ll x=st[tp-2]+st[tp]-st[tp-1]; tp-=3; st[++tp]=x; } }
vec[++vcnt].resize(tp+1); pl[vcnt]=1; pr[vcnt]=tp; For(i,1,tp) vec[vcnt][i]=st[i];
}
signed main(){
cin>>n; ll tt=0; For(i,1,n) { cin>>a[i]; tt+=a[i]; }
int s=0; For(i,1,n) if(a[i]==0) { s=i; break; }
For(i,s+1,n){
if(a[i]) lis[++lcnt]=a[i];
else { solve(); lcnt=0; }
}
lis[++lcnt]=-1e18;
For(i,1,s-1) lis[++lcnt]=a[i];
solve();
// For(i,1,vcnt){
// For(j,pl[i],pr[i]) cout<<vec[i][j]<<' ';
// cout<<endl;
// }
For(i,1,vcnt) { q.push(Node{i,0,vec[i][pl[i]]}); q.push(Node{i,1,vec[i][pr[i]]}); }
ll ans=0; int sgn=1; while(q.size()){
Node nd=q.top(); q.pop(); int o=nd.id;
if(pl[o]>pr[o]) continue;
if(nd.val<=-1e18) break;
if(nd.type==0) { ans+=sgn*vec[o][pl[o]++]; q.push(Node{o,0,vec[o][pl[o]]}); }
else { ans+=sgn*vec[o][pr[o]--]; q.push(Node{o,1,vec[o][pr[o]]}); }
sgn=-sgn;
}
// cout<<"ans="<<ans<<endl;
cout<<(tt+ans)/2<<' '<<(tt-ans)/2<<'\n';
cerr<<"Time = "<<clock()<<" ms"<<endl;
return 0;
}
// START TYPING IF YOU DON'T KNOW WHAT TO DO
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 27124kb
input:
1 0
output:
0 0
result:
ok single line: '0 0'
Test #2:
score: 0
Accepted
time: 4ms
memory: 27148kb
input:
2 0 17
output:
17 0
result:
ok single line: '17 0'
Test #3:
score: 0
Accepted
time: 2ms
memory: 27188kb
input:
3 0 13 0
output:
13 0
result:
ok single line: '13 0'
Test #4:
score: 0
Accepted
time: 2ms
memory: 27036kb
input:
8 1 2 0 3 7 4 0 9
output:
17 9
result:
ok single line: '17 9'
Test #5:
score: 0
Accepted
time: 3ms
memory: 27044kb
input:
10 0 0 0 1 0 0 0 5 0 0
output:
5 1
result:
ok single line: '5 1'
Test #6:
score: -100
Wrong Answer
time: 4ms
memory: 27148kb
input:
24 55 33 93 169 237 0 219 125 39 13 96 166 200 278 0 99 2 18 110 0 58 57 0 42
output:
1136 973
result:
wrong answer 1st lines differ - expected: '1125 984', found: '1136 973'