Submission #958222

# Submission time Handle Problem Language Result Execution time Memory
958222 2024-04-05T07:41:16 Z pragmatist Permutation (APIO22_perm) C++17
10 / 100
508 ms 444 KB
#include "perm.h"
#include<bits/stdc++.h>
 
using namespace std;
 
mt19937 rng(time(NULL));
 
long long dp[150], a[150];
int mn;
vector<int> ans;

void go(long long k, int n, int p[]) {
	for(int i = 1; i <= n; ++i) {
		a[i] = p[i-1];
	}
	long long cur = 1;
	for(int i = 1; i <= n; ++i) {
		dp[i] = 1;
		for(int j = 1; j < i; ++j) {
			if(a[j]<a[i]) {
				dp[i] += dp[j];
			}
		}
		cur += dp[i];
	}
	while(cur<k) {
		vector<pair<int, long long> > b;
		for(int i = 1; i <= n; ++i) {
			b.push_back({a[i], dp[i]});
		}
		sort(b.begin(), b.end());
		long long add = 1, need = k-cur;
		int nxt = 0;
		for(auto [x, y] : b) {
			if(add+y<=need) {
				nxt = x+1;
				add += y;								
			}
		}
		for(int i = 0; i <= n; ++i) {
			if(a[i] >= nxt) {
				a[i]++;
			}
		}			
		a[++n] = nxt;
		dp[n] = add;
		cur += add;
	}
	if(cur>k) {
		return;
	}
	vector<int> res;
	for(int i = 1; i <= n; ++i) {
		res.push_back(a[i]);
	}
	if(n<mn) {
		mn = n;
		ans = res;
	}
}

std::vector<int> construct_permutation(long long k) {
	mn = 1e9;
	for(int n = 1; n <= 5; ++n) {   
		int p[n];
		for(int i = 0; i < n; ++i) {
			p[i] = i;
		}
		do {
			go(k, n, p);	
		} while(next_permutation(p, p+n));
	}
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 9 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 9 ms 348 KB Output is correct
3 Correct 80 ms 428 KB Output is correct
4 Correct 114 ms 440 KB Output is correct
5 Incorrect 508 ms 444 KB Output isn't correct
6 Halted 0 ms 0 KB -