# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
172444 | mhy908 | Candies (JOI18_candies) | C++14 | 5 ms | 632 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const LL llinf=9000000000000000000;
const int inf=2000000000;
set<pair<LL, int> > pq;
set<pii> s;
int n;
LL arr[200010], sum[2][200010];
void relax(int num){
auto it=s.lower_bound(mp(num, inf));
set<pii>::iterator ithi, itlo;
bool hifl=false, lofl=false;
if(it==s.end())hifl=true;
else ithi=it;
it--;
if(it==s.begin())lofl=true;
else{
it--;
itlo=it;
it++;
}
int st, fin;
if(lofl)st=it->F;
else{
LL tsum=sum[itlo->S%2][itlo->S]-sum[itlo->S%2][itlo->F-1];
pq.erase(mp(tsum, itlo->F));
st=itlo->F;
}
if(hifl)fin=it->S;
else{
LL tsum=sum[ithi->S%2][ithi->S]-sum[ithi->S%2][ithi->F-1];
pq.erase(mp(tsum, ithi->F));
fin=ithi->S;
}
LL ttsum=sum[it->S%2][it->S]-sum[it->S%2][it->F-1];
pq.erase(mp(ttsum, it->F));
s.erase(it);
if(!lofl)s.erase(itlo);
if(!hifl)s.erase(ithi);
ttsum=sum[fin%2][fin]-sum[fin%2][st-1];
pq.insert(mp(ttsum, st));
s.insert(mp(st, fin));
}
int main()
{
scanf("%d", &n);
for(int i=1; i<=n+1; i++){
s.insert(mp(i, i));
if(i<=n)scanf("%lld", &arr[i]);
pq.insert(mp(arr[i], i));
sum[i%2][i]=sum[i%2][i-1]+arr[i];
sum[1-i%2][i]=sum[1-i%2][i-1]-arr[i];
}
LL ans=0;
for(int i=1; i<=(n+1)/2; i++){
auto it=pq.end();
it--;
pii t=*it;
ans+=t.F;
printf("%lld\n", ans);
relax(t.S);
}
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |