Submission #577884

# Submission time Handle Problem Language Result Execution time Memory
577884 2022-06-15T11:14:26 Z haxorman Stove (JOI18_stove) C++14
100 / 100
35 ms 6080 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int mxN = 1e5 + 7;

int n, k, arr[mxN], dsu[mxN];

int find(int x) {
	return (dsu[x] < 0 ? x : dsu[x] = find(dsu[x]));
}

bool unite(int x, int y) {
	x = find(x), y = find(y);

	if (x == y) {
		return false;
	}

	if (dsu[x] > dsu[y]) {
		swap(x, y);
	}

	dsu[x] += dsu[y];
	dsu[y] = x;

	return true;
}

int32_t main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> n >> k;
	memset(dsu, -1, sizeof(dsu));

	for (int i = 0; i < n; ++i) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	
	vector<pair<int,pair<int,int>>> difs;
	for (int i = 0; i < n - 1; ++i) {
		difs.push_back({arr[i + 1] - arr[i], {i + 1, i}});
	}
	sort(difs.begin(), difs.end());
	
	int ans = n, cnt = n;
	for (auto p : difs) {
		int dif = p.first, u = p.second.first, v = p.second.second;

		if (unite(u, v) && cnt > k) {
			//cout << dif << ' ' << u << ' ' << v << "\n";
			ans += dif - 1;
			--cnt;
		}
	}

	cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 1092 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 1096 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 1092 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 1096 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 2 ms 1236 KB Output is correct
11 Correct 2 ms 1236 KB Output is correct
12 Correct 2 ms 1236 KB Output is correct
13 Correct 2 ms 1236 KB Output is correct
14 Correct 2 ms 1236 KB Output is correct
15 Correct 2 ms 1240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 980 KB Output is correct
2 Correct 1 ms 1092 KB Output is correct
3 Correct 1 ms 980 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 980 KB Output is correct
8 Correct 1 ms 1096 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 2 ms 1236 KB Output is correct
11 Correct 2 ms 1236 KB Output is correct
12 Correct 2 ms 1236 KB Output is correct
13 Correct 2 ms 1236 KB Output is correct
14 Correct 2 ms 1236 KB Output is correct
15 Correct 2 ms 1240 KB Output is correct
16 Correct 28 ms 6052 KB Output is correct
17 Correct 26 ms 6080 KB Output is correct
18 Correct 25 ms 5960 KB Output is correct
19 Correct 25 ms 5984 KB Output is correct
20 Correct 35 ms 5956 KB Output is correct
21 Correct 33 ms 5940 KB Output is correct
22 Correct 26 ms 5960 KB Output is correct
23 Correct 26 ms 5960 KB Output is correct
24 Correct 26 ms 5960 KB Output is correct