Submission #1016481

#TimeUsernameProblemLanguageResultExecution timeMemory
1016481phongStove (JOI18_stove)C++17
100 / 100
21 ms5072 KiB
#include<bits/stdc++.h>

#define ll long long
const int nmax = 2e5 + 5, N = 4e5;
const ll oo = 1e9;
const int lg = 18, M = 4e3;
const int base = 2e5, mod = 1e9 + 7;
#define pii pair<int, int>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';cout << endl
using namespace std;



int n, k, a[nmax];
struct node{
    int u, v, w;
};
int r[nmax];

int get(int u){
    return r[u] ? r[u] = get(r[u]) : u;
}
ll sz[nmax];
bool vis[nmax];
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

//    freopen("code.inp", "r", stdin);
//    freopen("code.out", "w", stdout);
    cin >> n >>k;
    for(int i = 1; i <= n; ++i) cin >> a[i];
    sort(a + 1, a + 1 + n);
    vector<node> tmp;
    for(int i = 2; i <= n; ++i){
        tmp.push_back({i, i - 1, max(0, a[i] - a[i - 1] - 1)});
    }
    sort(tmp.begin(), tmp.end(), [](node a, node b){
         return a.w < b.w;
         });
    for(int i = 1; i <= n; ++i) sz[i] = 1;
    for(int i = 0; i < n - k; ++i){
        int x = get(tmp[i].u);
        int y = get(tmp[i].v);
        if(x != y){
            r[x] = y;
            sz[y] += sz[x] + tmp[i].w;
        }
    }
    ll ans = 0;
    for(int i = 1; i <= n; ++i){
        int x = get(i);
        if(vis[x]) continue;
        vis[x] = 1;
        ans += sz[x];
    }
    cout << ans;
}
/*
1_t1 + 1

*/

Compilation message (stderr)

stove.cpp:28:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   28 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...