結果

問題 No.270 next_permutation (1)
ユーザー maine_honzukimaine_honzuki
提出日時 2021-05-10 15:39:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 942 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 206,852 KB
実行使用メモリ 4,892 KB
最終ジャッジ日時 2023-10-19 23:57:46
合計ジャッジ時間 3,528 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 10 ms
4,348 KB
testcase_10 AC 67 ms
4,892 KB
testcase_11 AC 68 ms
4,892 KB
testcase_12 AC 69 ms
4,892 KB
testcase_13 AC 65 ms
4,892 KB
testcase_14 AC 64 ms
4,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://ncode.syosetu.com/n4830bu/270/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    int N, K;
    cin >> N >> K;
    vector<int> p(N);
    vector<ll> B(N);
    for (int i = 0; i < N; i++) {
        cin >> p[i];
    }
    for (int i = 0; i < N; i++) {
        cin >> B[i];
    }
    ll maine = 0, ans = 0;
    for (int i = 0; i < N; i++) {
        maine += abs(p[i] - B[i]);
    }
    auto prev = p;
    while (K--) {
        ans += maine;
        if (!next_permutation(p.begin(), p.end()))
            iota(p.begin(), p.end(), 1);
        for (int i = N - 1; i >= 0; i--) {
            if (i < N - 1 && prev[i] == p[i] && prev[i + 1] == p[i + 1])
                break;
            maine -= abs(prev[i] - B[i]);
            maine += abs(p[i] - B[i]);
        }

        if (!next_permutation(prev.begin(), prev.end()))
            iota(prev.begin(), prev.end(), 1);
    }
    cout << ans << endl;
}
0