結果
| 問題 |
No.270 next_permutation (1)
|
| コンテスト | |
| ユーザー |
maine_honzuki
|
| 提出日時 | 2021-05-10 15:39:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 77 ms / 2,000 ms |
| コード長 | 942 bytes |
| コンパイル時間 | 2,091 ms |
| コンパイル使用メモリ | 198,640 KB |
| 最終ジャッジ日時 | 2025-01-21 09:51:29 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 |
ソースコード
//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;
}
maine_honzuki