이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int mxn=1e5+5;
#define fr first
#define sc second
int n,k,a[mxn],nxt[mxn],pre[mxn],par[mxn],sz[mxn],L[mxn],R[mxn],ans;
set<pair<int,int>>s;
int fndpar(int x){
return (par[x]==x)?x:par[x]=fndpar(par[x]);
}
void dsu(int x,int y){
ans+=L[y]-R[x];
int z=pre[x],zz=nxt[y],w=L[x],ww=R[y];
if(y!=n){
int d=L[fndpar(nxt[y])]-R[y];
s.erase({d,y});
}
if(sz[x]<sz[y])swap(x,y);
par[y]=x;
sz[x]+=sz[y];
L[x]=w;
R[x]=ww;
pre[x]=z;
nxt[x]=zz;
if(y!=n){
int d=L[fndpar(nxt[x])]-R[x];
s.erase({d,x});
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
nxt[i]=i+1;
pre[i]=i-1;
par[i]=i;
sz[i]=1;
L[i]=a[i];
R[i]=a[i]+1;
ans++;
}
for(int i=1;i<n;i++){
s.insert({a[i+1]-a[i],i});
}
while(s.size()>=k){
auto xxx=*s.begin();
int x=fndpar(xxx.sc),y=fndpar(nxt[x]);
s.erase(xxx);
dsu(x,y);
}
cout<<ans;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
stove.cpp: In function 'int main()':
stove.cpp:49:16: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
49 | while(s.size()>=k){
| ~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |