Submission #996497

# Submission time Handle Problem Language Result Execution time Memory
996497 2024-06-10T17:29:19 Z overwatch9 Stove (JOI18_stove) C++17
100 / 100
46 ms 5328 KB
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
using ll = long long;
vector <int> t;
struct DSU {
    vector <int> link, sz;
    vector <ll> mn, mx;
    DSU (int s) {
        link = sz = vector <int> (s + 1);
        mx = mn = vector <ll> (s+1);
        for (int i = 1; i <= s; i++) {
            link[i] = i;
            sz[i] = 1;
            mx[i] = mn[i] = t[i];
        }
    }
    int head(int x) {
        while (x != link[x])
            x = link[x];
        return x;
    }
    bool same(int a, int b) {
        return head(a) == head(b);
    }
    void unite(int a, int b) {
        a = head(a);
        b = head(b);
        if (a == b)
            return;
        if (sz[a] < sz[b])
            swap(a, b);
        sz[a] += sz[b];
        link[b] = a;
        mx[a] = max(mx[a], mx[b]);
        mn[a] = min(mn[a], mn[b]);
    }
};
int main() {
    int n, k;
    cin >> n >> k;
    t = vector <int> (n+1);
    for (int i = 1; i <= n; i++)
        cin >> t[i];
    DSU dsu(n+1);
    priority_queue <pair <ll, pair <int, int>>> pq;
    for (int i = 1; i+1 <= n; i++) {
        pq.push({-t[i+1] + t[i], {i, i+1}});
    }
    int cnt = n;
    while (cnt > k) {
        int a = pq.top().second.first, b = pq.top().second.second;
        pq.pop();
        if (dsu.same(a, b)) 
            continue;
        dsu.unite(a, b);
        cnt--;
    }
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        if (i == dsu.head(i)) {
            ans += dsu.mx[i] - dsu.mn[i] + 1;
        }
    }
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 46 ms 5276 KB Output is correct
17 Correct 42 ms 5320 KB Output is correct
18 Correct 42 ms 5320 KB Output is correct
19 Correct 41 ms 5324 KB Output is correct
20 Correct 38 ms 5320 KB Output is correct
21 Correct 30 ms 5316 KB Output is correct
22 Correct 29 ms 5328 KB Output is correct
23 Correct 38 ms 5296 KB Output is correct
24 Correct 27 ms 5328 KB Output is correct