結果
問題 |
No.270 next_permutation (1)
|
ユーザー |
![]() |
提出日時 | 2021-05-10 15:32:22 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 974 bytes |
コンパイル時間 | 2,131 ms |
コンパイル使用メモリ | 197,836 KB |
最終ジャッジ日時 | 2025-01-21 09:51:20 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 4 WA * 11 |
ソースコード
//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; }