#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
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){
| ~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2516 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2516 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
72 ms |
8788 KB |
Output is correct |
17 |
Correct |
72 ms |
8792 KB |
Output is correct |
18 |
Correct |
70 ms |
8832 KB |
Output is correct |
19 |
Correct |
74 ms |
8784 KB |
Output is correct |
20 |
Correct |
53 ms |
8832 KB |
Output is correct |
21 |
Correct |
45 ms |
8856 KB |
Output is correct |
22 |
Correct |
33 ms |
8784 KB |
Output is correct |
23 |
Correct |
41 ms |
8784 KB |
Output is correct |
24 |
Correct |
31 ms |
8784 KB |
Output is correct |