結果

問題 No.270 next_permutation (1)
ユーザー ctyl_0ctyl_0
提出日時 2015-10-01 00:13:46
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,773 bytes
コンパイル時間 861 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 8,268 KB
最終ジャッジ日時 2023-09-27 01:06:50
合計ジャッジ時間 5,858 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 20 ms
4,380 KB
testcase_09 AC 499 ms
4,380 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0