結果
問題 | No.270 next_permutation (1) |
ユーザー |
|
提出日時 | 2016-11-15 22:12:52 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 61 ms / 2,000 ms |
コード長 | 900 bytes |
コンパイル時間 | 860 ms |
コンパイル使用メモリ | 73,472 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-23 15:02:22 |
合計ジャッジ時間 | 2,136 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 15 |
ソースコード
#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; }