제출 #737101

#제출 시각아이디문제언어결과실행 시간메모리
737101becaido순열 (APIO22_perm)C++17
71.25 / 100
223 ms1600 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "perm.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int MAX = 1000;

mt19937 rng(time(NULL));
int rnd(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

ll cal(vector<int> v) {
    int n = v.size();
    vector<ll> dp(n);
    FOR (i, 0, n - 1) {
        dp[v[i]] = 1;
        FOR (j, 0, v[i] - 1) dp[v[i]] += dp[j];
    }
    return accumulate(dp.begin(), dp.end(), 0ll);
}

vector<int> construct_permutation(ll k) {
	k--;
	vector<pair<ll, vector<int>>> all;
	FOR (i, 1, 62) {
        vector<int> v(i);
        iota(v.begin(), v.end(), 0);
        ll val = cal(v);
        reverse(v.begin(), v.end());
        all.pb(val, v);
	}
	while (all.size() < MAX) {
        int len = rnd(1, 90);
        vector<int> v(len);
        iota(v.begin(), v.end(), 0);
        shuffle(v.begin(), v.end(), rng);
        ll val = cal(v);
        reverse(v.begin(), v.end());
        all.pb(val, v);
	}
	sort(all.begin(), all.end());
	all.erase(unique(all.begin(), all.end()), all.end());
	decltype(all) tmp;
	for (auto [val, v] : all) {
        while (tmp.size() && v.size() < tmp.back().S.size()) tmp.pop_back();
        tmp.pb(val, v);
	}
	all = tmp;
	vector<int> ans;
	for (int i = all.size() - 1; k > 0;) {
        auto [val, v] = all[i];
        if (val > k) {
            i--;
            continue;
        }
        k -= val;
        int s = ans.size();
        for (int x : v) ans.pb(s + x);
	}
	reverse(ans.begin(), ans.end());
	return ans;
}

#ifdef WAIMAI
static long long MX=1e18;

static bool check_permutation(vector<int> v)
{
	sort(v.begin(),v.end());
	for(int i=0;i<v.size();i++)
		if(v[i]!=i) return 0;
	return 1;
}

long long count_increasing(const vector<int>& v) {
  vector<long long> dp(v.size() + 1, 0);
  dp[0] = 1;
  for (int x : v)
  {
  	for (int i = 0; i <= x; i++)
  	{
  		dp[x+1] += dp[i];
  		dp[x+1] = min(dp[x+1],MX+1);
  	}
  }
  long long result = 0;
  for (int i = 0; i <= (int)v.size(); i++){
  	result += dp[i];
  	result = min(result,MX+1);
  }
  return result;
}

int main() {
	int t;
	assert(1 == scanf("%d", &t));
	while(t--)
	{
		long long k;
		assert(1 == scanf("%lld",&k));
		vector<int> ret=construct_permutation(k);
		if(!check_permutation(ret))
		{
			printf("WA: Returned array is not a permutation\n");
			exit(0);
		}
		long long inc=count_increasing(ret);
		if(inc!=k)
		{
			if(inc==MX+1)
				printf("WA: Expected %lld increasing subsequences, found more than %lld\n",k, MX);
			else
				printf("WA: Expected %lld increasing subsequences, found %lld\n",k,inc);
			exit(0);
		}
		printf("%d\n",(int)ret.size());
		for(int i=0;i<ret.size();i++)
		{
			printf("%d",ret[i]);
			if(i+1==ret.size())
				printf("\n");
			else
				printf(" ");
		}
	}
	return 0;
}
#endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...