結果

問題 No.270 next_permutation (1)
ユーザー maine_honzukimaine_honzuki
提出日時 2021-05-10 15:32:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 974 bytes
コンパイル時間 2,876 ms
コンパイル使用メモリ 206,660 KB
実行使用メモリ 4,892 KB
最終ジャッジ日時 2023-10-19 23:52:24
合計ジャッジ時間 3,986 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
        bool flag = true;
        if (!next_permutation(p.begin(), p.end())) {
            iota(p.begin(), p.end(), 1);
            flag = false;
        }
        for (int i = N - 1; i >= 0; i--) {
            if (prev[i] == p[i] && flag)
                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