結果

問題 No.1311 Reverse Permutation Index
ユーザー zuminzumin
提出日時 2021-02-24 23:51:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 969 bytes
コンパイル時間 1,062 ms
コンパイル使用メモリ 91,696 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-25 05:01:06
合計ジャッジ時間 1,697 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>

using namespace std;
using ll=long long;

int main(){
    ll n,s,ans=0;
    cin>>n>>s;
    vector<ll> f(s,1);
    priority_queue<ll,vector<ll>,greater<ll>> q;
    vector<pair<ll,ll>> revperm;

    for(ll i=1;i<s;i++) f[i]=i*f[i-1];
    for(ll i=1;i<=s;i++) q.push(i);
    for(ll i=s-1;i>=0;i--){
        ll p=n/f[i];
        n-=p*f[i];
        vector<ll> v;
        while(v.size()<=p){
            v.push_back(q.top());
            q.pop();
        }
        revperm.push_back(make_pair(v.back(),s-1-i+1));
        for(ll i=0;i<v.size()-1;i++) q.push(v[i]);
    }
    sort(revperm.begin(),revperm.end());
    set<ll> used;
    for(ll i=0;i<s;i++){
        ll revi=revperm[i].second;
        ll cnt=0;
        for(ll j=1;j<revi;j++){
            if(!used.count(j)) cnt++;
        }
        ans+=f[s-i-1]*cnt;
        used.insert(revi);
    }
    cout<<ans<<endl;
    return 0;
}
0