#include <bits/stdc++.h>
using namespace std;
#define int long long
int find(int x, vector<int>&par) { return x == par[x] ? x : find(par[x], par); }
bool same(int x, int y, vector<int>&par) { return find(x, par) == find(y, par); }
void unite(int x, int y, vector<int>&sz, vector<int>&par) {
x = find(x, par), y = find(y, par);
if (sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y], par[y] = x;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> arr(n + 5);
for (int i = 1; i <= n; i++) cin >> arr[i];
vector<int> sz(n + 5), par(n + 5), mn(n + 5, 1e18), mx(n + 5, -1e18);
for (int i = 1; i <= n; i++) par[i] = i, sz[i] = 1;
vector<pair<int, int>> edge;
for (int i = 2; i <= n; i++) edge.push_back({arr[i] - arr[i - 1], i});
sort(edge.begin(), edge.end());
int cc = n;
for (int i = 0; i < edge.size() && cc > k; i++) {
int index = edge[i].second;
if (!same(index, index - 1, par)) unite(index, index - 1, sz, par), cc--;
}
for (int i = 1; i <= n; i++) {
mn[find(i, par)] = min(mn[find(i, par)], arr[i]);
mx[find(i, par)] = max(mx[find(i, par)], arr[i]);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (find(i, par) == i) {
ans += mx[i] - mn[i] + 1;
}
}
cout << ans << '\n';
return 0;
}
Compilation message
stove.cpp: In function 'int main()':
stove.cpp:31:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int i = 0; i < edge.size() && cc > k; i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
480 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
480 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
25 ms |
7124 KB |
Output is correct |
17 |
Correct |
25 ms |
7156 KB |
Output is correct |
18 |
Correct |
27 ms |
7392 KB |
Output is correct |
19 |
Correct |
26 ms |
7376 KB |
Output is correct |
20 |
Correct |
21 ms |
7372 KB |
Output is correct |
21 |
Correct |
25 ms |
7376 KB |
Output is correct |
22 |
Correct |
22 ms |
7320 KB |
Output is correct |
23 |
Correct |
20 ms |
7376 KB |
Output is correct |
24 |
Correct |
19 ms |
7368 KB |
Output is correct |