結果

問題 No.270 next_permutation (1)
ユーザー chocoruskchocorusk
提出日時 2019-07-23 13:36:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,571 bytes
コンパイル時間 1,427 ms
コンパイル使用メモリ 101,672 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-07 18:43:32
合計ジャッジ時間 3,029 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 5 ms
4,380 KB
testcase_02 AC 8 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 8 ms
4,376 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 7 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 12 ms
4,376 KB
testcase_10 AC 67 ms
4,376 KB
testcase_11 AC 72 ms
4,380 KB
testcase_12 AC 72 ms
4,376 KB
testcase_13 AC 63 ms
4,376 KB
testcase_14 AC 64 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
    int n, k; cin>>n>>k;
    vector<int> p(n);
    ll b[100010];
    for(int i=0; i<n; i++){
        cin>>p[i]; p[i]--;
    }
    ll s=0;
    for(int i=0; i<n; i++){
        cin>>b[i]; b[i]--;
        s+=abs(p[i]-b[i]);
    }
    ll ans=0;
    for(int i=0; i<k; i++){
        ans+=s;
        vector<int> v;
        v.push_back(p[n-1]);
        s-=abs(p[n-1]-b[n-1]);
        int x=-1, t=n-1;
        for(int j=n-2; j>=0; j--){
            s-=abs(p[j]-b[j]);
            if(p[j+1]<p[j]) v.push_back(p[j]);
            else{
                x=p[j];
                t=j;
                break;
            }
        }
        if(x==-1){
            s=0;
            for(int i=0; i<n; i++){
                s+=abs(i-b[i]);
            }
            continue;
        }
        v.push_back(x);
        sort(v.begin(), v.end());
        int y=*upper_bound(v.begin(), v.end(), x);
        s+=abs(y-b[t]);
        p[t]=y;
        int c=0;
        for(auto a:v){
            if(a==y) continue;
            p[t+1+c]=a;
            s+=abs(a-b[t+1+c]);
            c++;
        }
    }
    cout<<ans<<endl;
    return 0;
}
0