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;
typedef long long ll;
ll dp[150],t[150];
ll K;
int n;
void upd(int pos,ll val){
while(pos <= n){
t[pos] += val;
pos += pos & -pos;
}
}
ll sum(int p){
ll ret = 0;
while(p > 0){
ret += t[p];
p -= (p & -p);
}
return ret;
}
ll sum(int l,int r){
return sum(r) - sum(l - 1);
}
ll calc(vector<int> x){
ll S = 1;
n = (int)x.size();
for(int i = 0;i < n;i++){
t[i+1] =0 ;
}
for(int i = 0;i < (int)x.size();i++){
dp[i] = 1 + sum(1,x[i]);
S += dp[i];
if(S > K) return K + 1;
upd(x[i]+1,dp[i]);
}
return S;
}
vector<int> ins(vector<int> &x,int a){
vector<int> ret = x;
for(int i = 0;i < (int)ret.size();i++){
if(ret[i] >= a){
ret[i]++;
}
}
ret.push_back(a);
return ret;
}
vector<int> solve(ll k,ll G,vector<int> a){
while(G != k){
ll mx = -1;
int pos;
int l = -1,r = (int)a.size() + 1;
while(r - l > 1){
int mid = (l + r) >> 1;
ll val = calc(ins(a,mid));
if(val <= k){
pos = l = mid;
mx = val;
}else{
r = mid;
}
}
G = mx;
a = ins(a,pos);
}
return a;
}
vector<int> construct_permutation(long long k)
{
K = k;
vector<int> ret;
if(k == 1) return ret;
auto upd = [&](vector<int> f){
if((int)ret.size() > (int)f.size()){
ret = f;
}
};
ret = solve(k,1ll,vector<int>());
vector<int> f(5);
iota(f.begin(),f.end(),0);
int it = 0;
do{
if(it > 50) break;
it++;
ll G = calc(f);
if(G <= k){
upd(solve(k,G,f));
}
}while(next_permutation(f.begin(),f.end()));
return ret;
}
Compilation message (stderr)
perm.cpp: In function 'std::vector<int> solve(ll, ll, std::vector<int>)':
perm.cpp:67:16: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | a = ins(a,pos);
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |