# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
987606 | Otalp | Permutation (APIO22_perm) | C++17 | 49 ms | 856 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "perm.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
ll pref[200][200], suf[200][200];
ll a[200100];
ll n;
void calc(){
for(int x=0; x<=n + 1; x++) pref[0][x] = 1;
for(int i=1; i<=n; i++){
for(int x=0; x<=n + 1; x++){
pref[i][x] = pref[i - 1][x];
if(a[i] <= x){
pref[i][x] += pref[i-1][a[i]];
}
}
}
for(int x=0; x<=n + 1; x++) suf[n + 1][x] = 1;
for(int i=n; i>=1; i--){
for(int x=n + 1; x>=0; x--){
suf[i][x] = suf[i + 1][x];
if(a[i] >= x){
suf[i][x] += suf[i + 1][a[i]];
}
}
}
}
vector<int> construct_permutation(ll k){
n = 1;
a[1] = 1;
calc();
ll g = pref[n][n];
while(g != k){
ll d = 0;
ll pos, x;
for(int i=1; i<=n + 1; i++){
for(int j=1; j<=n+1; j++){
ll h = pref[i - 1][j - 1] * suf[i][j];
//cout<<i<<' '<<j<<'\n';
//cout<<h<<'\n';
if(h > d and h <= k - g){
d = h;
x = j;
pos = i;
}
}
}
//cout<<g<<' '<<pos<<' '<<x<<'\n';
vector<int> q;
for(int i=1; i<pos; i++){
q.pb(a[i] + (a[i] >= x));
}
q.pb(x);
for(int i=pos; i<=n; i++){
q.pb(a[i] + (a[i] >= x));
}
n ++;
for(int i=1; i<=n; i++){
a[i] = q[i - 1];
}
calc();
g = pref[n][n];
}
vector<int> res;
for(int i=1; i<=n; i++) res.pb(a[i]-1);
//for(int d: res) cout<<d<<' ';
//cout<<'\n';
return res;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |