結果
| 問題 | 
                            No.270 next_permutation (1)
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2015-10-01 00:13:46 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,773 bytes | 
| コンパイル時間 | 765 ms | 
| コンパイル使用メモリ | 85,472 KB | 
| 実行使用メモリ | 10,624 KB | 
| 最終ジャッジ日時 | 2024-07-19 18:45:42 | 
| 合計ジャッジ時間 | 5,672 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 9 WA * 1 TLE * 1 -- * 4 | 
ソースコード
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
typedef long long ll;
using namespace std;
int inputValue(){
    int a;
    cin >> a;
    return a;
};
template <typename T>
void output(T a, int precision) {
    if(precision > 0){
        cout << setprecision(precision)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}
void my_next_permutation(vector<int> &p){
    if (p.size() == 1) {
        return;
    }
    int s = (int)p.size();
    int k, l;
    for (k = s - 2; k >= 0; k--) {
        if (p[k + 1] > p[k]) {
            break;
        }
        if (k == 0) {
            reverse(p.begin(), p.end());
            return;
        }
    }
    
    
    for (l = s - 1; l > k; l--) {
        if (p[l] > p[k]) {
            break;
        }
    }
    
    swap(p[k], p[l]);
    
    for (; k + 1 < s - 1; k++, s--) {
        swap(p[k + 1], p[s - 1]);
    }
    
}
int main() {
    
    // source code
    int N = inputValue();
    int K = inputValue();
    vector<int> P(N);
    vector<int> B(N);
    
    rep(i, N){
        P[i] = inputValue();
    }
    
    rep(i, N){
        B[i] = inputValue();
    }
    
    ll ret = 0;
    rep(i, N){
        ret += abs(P[i] - B[i]);
    }
    
    rep(k, K - 1){
        my_next_permutation(P);
//        rep(i, N){
//            cout << P[i] << " ";
//        }
//        output("", 0);
        rep(i, N){
            ret += abs(P[i] - B[i]);
        }
    }
    
    output(ret, 0);
        
    
    return 0;
}