結果

問題 No.1248 Kth Sum
ユーザー msm1993msm1993
提出日時 2020-10-23 15:29:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 352 ms / 2,000 ms
コード長 2,552 bytes
コンパイル時間 906 ms
コンパイル使用メモリ 88,380 KB
実行使用メモリ 15,112 KB
最終ジャッジ日時 2023-09-28 15:24:54
合計ジャッジ時間 7,271 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 74 ms
4,380 KB
testcase_14 AC 181 ms
12,464 KB
testcase_15 AC 74 ms
4,376 KB
testcase_16 AC 237 ms
14,688 KB
testcase_17 AC 231 ms
14,312 KB
testcase_18 AC 221 ms
14,240 KB
testcase_19 AC 217 ms
14,336 KB
testcase_20 AC 216 ms
14,496 KB
testcase_21 AC 216 ms
14,360 KB
testcase_22 AC 175 ms
12,396 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 207 ms
15,064 KB
testcase_25 AC 352 ms
15,112 KB
testcase_26 AC 223 ms
14,708 KB
testcase_27 AC 73 ms
4,380 KB
testcase_28 AC 74 ms
4,376 KB
testcase_29 AC 73 ms
4,380 KB
testcase_30 AC 171 ms
14,480 KB
testcase_31 AC 149 ms
14,312 KB
testcase_32 AC 146 ms
14,468 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 296 ms
14,376 KB
testcase_35 AC 256 ms
14,280 KB
testcase_36 AC 243 ms
14,236 KB
testcase_37 AC 226 ms
13,804 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: ラムダ関数内:
main.cpp:17:19: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   17 |         for (auto [x, y] : S) cerr << "(" << x << ", " << y << "); ";
      |                   ^
main.cpp:20:19: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   20 |         for (auto [x, y] : T) cerr << "(" << x << ", " << y << "); ";
      |                   ^
main.cpp: 関数 ‘long long int solve(int, int, int*)’ 内:
main.cpp:41:16: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   41 |     for (auto &[val, idx] : T) sum += val;
      |                ^

ソースコード

diff #

#include <vector>
#include <cassert>
#include <algorithm>
#include <numeric>
#include <set>
#include <iostream>
using namespace std;

long long solve(int n, int k, int *a) {
    if (k == 1) return a[0];
    if (2 * k > n) return a[k-1];

    set<pair<int, int> > S, T;
    set<int> U;
    auto dump = [&]() -> void {
        cerr << "S = ";
        for (auto [x, y] : S) cerr << "(" << x << ", " << y << "); ";
        cerr << endl;
        cerr << "T = ";
        for (auto [x, y] : T) cerr << "(" << x << ", " << y << "); ";
        cerr << endl;
        cerr << "U = ";
        for (int u : U) cerr << u << ' ';
        cerr << endl;
    };

    int segmin[n];
    for (int m = 1; m * (k - 1) < n; m++)
        segmin[m] = *min_element(a + (m-1) * (k-1) + 1, a + m * (k - 1) + 1);
    int sufmin[n];
    sufmin[n-1] = a[n-1];
    for (int i = n-2; i >= 0; i--) sufmin[i] = min(sufmin[i+1], a[i]);

    for (int i = k; i < n; i++) S.insert({a[i], i});
    for (int i = 0; i < 2; i++) {
        U.insert(S.begin()->second);
        T.insert(*S.begin());
        S.erase(S.begin());
    }
    long long sum = 0;
    for (auto &[val, idx] : T) sum += val;
    long long ans = a[k-1];
    for (int i = 0, m = 2; m * k <= n; m++) {
        int c = 0;
        long long tmp = sum;
        if (*U.begin() > m * (k - 1)) {
            c++;
            tmp += segmin[m];
            // cerr << "left " << tmp << endl;
        }
        if (*U.rbegin() < m*k-1) {
            c++;
            tmp += sufmin[m*k-1];
            // cerr << "right " << tmp << endl;
        }
        if (c > 0) tmp -= T.rbegin()->first;
        if (c > 1) tmp -= next(T.rbegin())->first;
        ans = min(ans, tmp);
        // dump();
        // cerr << "m = " << m << " => " << tmp << "; sum = " << sum << "; c = " << c << endl;

        if ((m + 1) * k > n) break;

        c = 1;
        for (int i = (m-1)*(k-1)+1; i <= m*(k-1); i++) {
            if (U.find(i) != U.end()) {
                c++;
                sum -= a[i];
                T.erase({a[i], i});
                U.erase(i);
            } else
                S.erase({a[i], i});
        }
        while (c--) {
            auto it = S.begin();
            sum += it->first;
            T.insert(*it);
            U.insert(it->second);
            // cerr << "add " << it->first << " " << it->second << endl;
            S.erase(it);
        }
    }
    return ans;
}

int main() {
    int n, k; cin >> n >> k;
    int a[n]; for (int i = 0; i < n; i++) cin >> a[i];
    
    cout << solve(n, k, a) << endl;
}
0