#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, k;
vector<ll> t;
int main(){
cin >> n >> k;
t.resize(n);
for(int i = 0; i < n; i++) cin >> t[i];
if(n <= k){
cout << n;
return 0;
}
vector<pair<ll, pair<ll, ll>>> pos(n - 1);
for(int i = 0; i < n - 1; i++){
int p = t[i + 1] - t[i];
pos[i].first = p;
pos[i].second = {i, i + 1};
}
sort(pos.begin(), pos.end());
vector<pair<pair<ll, ll>, ll>> tmp;
for(int i = 0; i < n - k; i++) tmp.push_back({{pos[i].second.first + 1, pos[i].second.second + 1}, pos[i].first});
int m = tmp.size();
sort(tmp.begin(), tmp.end());
map<ll, ll> parent;
for(int i = 0; i < m; i++){
if(parent[tmp[i].first.first] == 0) parent[tmp[i].first.second] = tmp[i].first.first;
else{
parent[tmp[i].first.second] = parent[tmp[i].first.first];
parent[tmp[i].first.first] = 0;
}
}
ll ans = 0;
ll last = 0;
for(auto x: parent){
if(x.first != 0 && x.second != 0){
ans += t[x.first - 1] - t[x.second - 1] + 1;
last++;
}
}
ans += (k - last);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
600 KB |
Output is correct |
13 |
Correct |
2 ms |
452 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
600 KB |
Output is correct |
13 |
Correct |
2 ms |
452 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
83 ms |
11968 KB |
Output is correct |
17 |
Correct |
95 ms |
12084 KB |
Output is correct |
18 |
Correct |
88 ms |
11964 KB |
Output is correct |
19 |
Correct |
78 ms |
12484 KB |
Output is correct |
20 |
Correct |
53 ms |
9384 KB |
Output is correct |
21 |
Correct |
35 ms |
4940 KB |
Output is correct |
22 |
Correct |
33 ms |
3520 KB |
Output is correct |
23 |
Correct |
33 ms |
3532 KB |
Output is correct |
24 |
Correct |
32 ms |
3412 KB |
Output is correct |