結果

問題 No.270 next_permutation (1)
ユーザー kimiyukikimiyuki
提出日時 2016-11-15 22:12:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 900 bytes
コンパイル時間 751 ms
コンパイル使用メモリ 73,032 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-25 03:01:41
合計ジャッジ時間 2,425 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
typedef long long ll;
using namespace std;
int main() {
    int n, k; cin >> n >> k;
    vector<int> a(n); repeat (i,n) cin >> a[i];
    vector<int> b(n); repeat (i,n) cin >> b[i];
    ll ans = 0;
    int l = max(0, n - 20); // 20! = 2.4e18
    ll base = 0; repeat (i,l) base += abs(a[i] - b[i]);
    while (k --) {
        ans += base;
        repeat_from (i,l,n) ans += abs(a[i] - b[i]);
        bool is_not_overflow = next_permutation(a.begin() + l, a.end());
        if (not is_not_overflow) {
            prev_permutation(a.begin() + l, a.end());
            next_permutation(a.begin(),     a.end());
            base = 0; repeat (i,l) base += abs(a[i] - b[i]);
        }
    }
    cout << ans << endl;
    return 0;
}
0