結果

問題 No.615 集合に分けよう
ユーザー sekiya9311
提出日時 2017-12-15 00:29:00
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 728 bytes
コンパイル時間 1,984 ms
コンパイル使用メモリ 177,708 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-14 13:49:39
合計ジャッジ時間 3,166 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int MAX = 5e4 + 10;
int N, K;
long long a[MAX];

int main() {
    scanf("%d%d", &N, &K);
    for (int i = 0; i < N; i++) {
        scanf("%lld", a + i);
    }
    sort(a, a + N);
    priority_queue<pair<long long, int>> pq;
    for (int i = 0; i < N - 1; i++) {
        pq.emplace(a[i + 1] - a[i], i);
    }
    vector<int> splitIdx;
    for (int i = 0; i < K - 1; i++) {
        splitIdx.emplace_back(pq.top().second);
        pq.pop();
    }
    splitIdx.emplace_back(N - 1);
    long long ans = 0;
    int ptr = 0;
    for (int e : splitIdx) {
        //cerr << e << endl;
        ans += a[e] - a[ptr];
        ptr = e + 1;
    }
    cout << ans << endl;
    return 0;
}
0