結果
| 問題 | No.270 next_permutation (1) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-11-15 21:44:59 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 896 bytes |
| コンパイル時間 | 606 ms |
| コンパイル使用メモリ | 73,520 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-26 01:53:32 |
| 合計ジャッジ時間 | 1,966 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 7 WA * 8 |
ソースコード
#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) ans += abs(a[i] - b[i]);
while (k --) {
ans += base;
repeat_from (i,l,n) ans += abs(a[i] - b[i]);
bool is_not_carried = next_permutation(a.begin() + l, a.end());
if (not is_not_carried) {
prev_permutation(a.begin() + l, a.end());
next_permutation(a.begin(), a.end());
base = 0; repeat (i,l) ans += abs(a[i] - b[i]);
}
}
cout << ans << endl;
return 0;
}