# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
58237 | alenam0161 | Candies (JOI18_candies) | C++17 | 4 ms | 508 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+7;
int a[N];
int l[N],r[N],p[N];
long long val[N];
int fnd(int v){
return v==p[v] ? v:p[v]=fnd(p[v]);
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d",a+i);
for(int i=1;i<=n;++i){
r[i]=l[i]=p[i]=i;
val[i]=a[i];
}
multiset<pair<long long,int> > st;
for(int i=1;i<=n;++i){
st.insert({a[i],i});
}
int hw=(n+1)/2;
long long ans=0;
while(st.size()>0){
auto it=st.end();it--;
long long vl=it->first;
int ps=it->second;
ps=fnd(ps);
ans+=vl;
hw--;
if(hw<0){
cout<<endl;
return 0;
}
cout<<ans<<" ";
if(l[ps]==1&&r[ps]==n){
cout<<endl;
return 0;
}
st.erase(st.find({vl,ps}));
val[ps]*=-1;
if(l[ps]>1){
int pl=l[ps]-1;
pl=fnd(pl);
val[ps]+=val[pl];
l[ps]=l[pl];
p[pl]=ps;
st.erase(st.find({val[pl],pl}));
}
if(r[ps]<n){
int pr=r[ps]+1;
pr=fnd(pr);
val[ps]+=val[pr];
r[ps]=r[pr];
p[pr]=ps;
st.erase(st.find({val[pr],pr}));
}
st.insert({val[ps],ps});
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |