This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
Compilation message (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... |