제출 #1367160

#제출 시각아이디문제언어결과실행 시간메모리
1367160edoStove (JOI18_stove)C++20
컴파일 에러
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct DSU {
  int n, cnt;
  //ll cost;
  vector<int> par, sz; 
  //map<int, ll> costG;
  DSU(int n) : n(n), par(n), sz(n, 1) {
    iota(par.begin(), par.end(), 0);
    //cost = 0;
    cnt = 0;
  }

  //pair<int, ll> leader(int x) {
  int leader(int x) {
    while(x != par[x]) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  
  int merge(int a, int b) {
    a = leader(a), b = leader(b);
    if(a == b) return 0;
    if(sz[a] < sz[b]) 
      swap(a, b);
    sz[a] += sz[b];
    costG[a] += 1;
    par[b] = a;
    return 1;
  }

  //ll get_cost() {
    //ll res = 0;
    //for(auto [x, y] : costG) {
      //res += y;
    //}
    //return res;
  //}
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  for(ll &x : a) 
    cin >> x;

  if(n == k) {
    cout << n << "\n";
    return 0;
  }

  ranges::sort(a);
  //const ll inf = (1ll << 60);
  DSU dsu(n);
  vector<ll> gap;
  int m = 1;
  for(int i = 1; i < n; ++i) {
    if(a[i] == a[i - 1] + 1) 
      continue;
    m++;
    gap.push_back(a[i] - a[i - 1] - 1);
  }
  if(m <= k) {
    cout << n << "\n";
    return 0;
  }

  ranges::sort(gap);
  ll ans = n;
  int need = m - k;
  for(int i = 0; i < need; ++i) {
    ans += gap[i];
  }

  cout << ans << "\n";

  return 0;
}
//et koliko sam vremena potrosio

컴파일 시 표준 에러 (stderr) 메시지

stove.cpp: In member function 'int DSU::merge(int, int)':
stove.cpp:30:5: error: 'costG' was not declared in this scope
   30 |     costG[a] += 1;
      |     ^~~~~