QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#292648#5567. Balanced PermutationsRadewoosh#WA 89ms13836kbC++235.8kb2023-12-28 10:22:412023-12-28 10:22:41

Judging History

你现在查看的是最新测评结果

  • [2023-12-28 10:22:41]
  • 评测
  • 测评结果:WA
  • 用时:89ms
  • 内存:13836kb
  • [2023-12-28 10:22:41]
  • 提交

answer

//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
const int nax=1000*1007;
const int vax=107;
const ll inf=1007LL*1007LL*1007LL*1007LL*1007LL*1007LL;
using mag=__int128;

int n;

int koszt[nax];
pii prz[nax];
ll dp[nax];

ll kom[vax][vax];

int gdzdol[nax];
int gdzgor[nax];

ll mnoz(ll a, ll b)
{
	return min(a*(mag)b, (mag)inf);
}

ll pytkom(int a, int b)
{
	assert(b>=0 && b<=a);
	if (a<vax)
		return kom[a][b];
	return 1;
}

vi operator +(vi a, vi b)
{
	for (int i : b)
		a.push_back(i);
	return a;
}

vi lift(vi a, int b)
{
	for (int &i : a)
		i+=b;
	return a;
}

void wypisz(vi wek)
{
	for (int i : wek)
		printf("%d ", i);
	printf("\n");
}

ll pasuje(int k, vi per, int m)
{
	int r=per.size();
	if (m<k)
		return 0;
	if (!r)
		return mnoz(dp[k], kom[m][k]);
	ll ret=0;
	int g=0;
	for (int i=0; i<r; i++)
		if (per[i]>per[g])
			g=i;
	g++;
	for (int i=prz[k].first; i<=prz[k].second; i++)
	{
		ret=min(ret, inf);
		if (i<=r)
		{
			if (i!=g)
				continue;
			if (per[g-1]<k)
				continue;
			int ko=0;
			for (int a=1; a<i; a++)
			{
				int naj=0;
				for (int b=a; b<i; b++)
				{
					naj=max(naj, per[b-1]);
					if (naj==max(per[a-1], per[b-1]))
						ko++;
				}
			}
			if (ko!=koszt[i-1])
				continue;
			int j=per[g-1];
			vi napra(j+1, 1);
			napra[0]=napra[j]=0;
			for (int l=1; l<i; l++)
				napra[per[l-1]]=0;
			for (int l=2; l<j; l++)
				napra[l]+=napra[l-1];
			vi pra;
			for (int l=i+1; l<=r; l++)
				pra.push_back(napra[per[l-1]]);
			//~ ll staret=ret;
			ret+=pasuje(k-i, pra, j-1-(i-1));
			//~ debug() << imie(k) << " " << imie(m) << " " << i << " " << j << " z " << staret << " na " << ret << "     A";
		}
		else
		{
			for (int j=max(per[g-1]+1, k); j<=m; j++)
			{
				//~ ll staret=ret;
				ret+=mnoz(pasuje(i-1, per, j-1), dp[k-i]);
				//~ debug() << imie(k) << " " << imie(m) << " " << i << " " << j << " z " << staret << " na " << ret << "     B";
			}
		}
	}
	ret=min(ret, inf);
	//~ debug() << imie(k) << imie(per) << imie(m) << imie(ret);
	return ret;
}

vi solve_smallest(int k, ll kt)
{
	if (!k)
		return vi{};
	if (dp[k]<kt)
		return {-1};
	if (dp[k-gdzdol[k]]>=kt)
		return solve_smallest(gdzdol[k]-1, 1)+vi{k}+lift(solve_smallest(k-gdzdol[k], kt), gdzdol[k]-1);
	vi juz(k+1);
	vi tab;
	for (int i=1; i<=k; i++)
	{
		for (int j=1; j<=k; j++)
		{
			if (juz[j])
				continue;
			tab.push_back(j);
			ll x=pasuje(k, tab, k);
			if (x<kt)
			{
				kt-=x;
				tab.pop_back();
				continue;
			}
			juz[j]=1;
			break;
		}
	}
	return tab;
}

vi solve_largest(int k, ll kt)
{
	if (!k)
		return vi{};
	if (dp[k]<kt)
		return {-1};
	if (dp[k-gdzgor[k]]>=kt)
		return lift(solve_largest(gdzgor[k]-1, 1), gdzgor[k]-1)+vi{k}+solve_largest(k-gdzgor[k], kt);
	vi juz(k+1);
	vi tab;
	for (int i=1; i<=k; i++)
	{
		for (int j=k; j>=1; j--)
		{
			if (juz[j])
				continue;
			tab.push_back(j);
			ll x=pasuje(k, tab, k);
			if (x<kt)
			{
				kt-=x;
				tab.pop_back();
				continue;
			}
			juz[j]=1;
			break;
		}
	}
	return tab;
}

int main()
{
	for (int i=0; i<vax; i++)
	{
		kom[i][0]=1;
		for (int j=1; j<=i; j++)
			kom[i][j]=min(inf, kom[i-1][j-1]+kom[i-1][j]);
	}
	scanf("%d", &n);
	{
		gdzdol[1]=1;
		int v=2;
		int u=2;
		for (int i=1; v<=n; i<<=1)
		{
			for (int j=0; j<i; j++)
			{
				gdzdol[v]=u;
				v++;
			}
			for (int j=0; j<i; j++)
			{
				gdzdol[v]=u;
				v++;
				u++;
			}
		}
	}
	{
		//~ gdzgor[1]=1;
		int v=1;
		int u=1;
		for (int i=1; v<=n; i<<=1)
		{
			for (int j=0; j<i; j++)
			{
				gdzgor[v]=u;
				v++;
			}
			for (int j=0; j<i; j++)
			{
				gdzgor[v]=u;
				v++;
				u++;
			}
		}
	}
	dp[0]=1;
	for (int i=1; i<=n; i++)
	{
		koszt[i]=koszt[i/2]+koszt[(i-1)/2]+i;
		
		int bsa=1;
		int bsb=i;
		while(bsa<bsb)
		{
			int bss=(bsa+bsb)>>1;
			if (koszt[bss-1]+koszt[i-bss]+i==koszt[i])
				bsb=bss;
			else
				bsa=bss+1;
		}
		prz[i]={bsa, i+1-bsa};
		for (int j=prz[i].first; j<=prz[i].second && dp[i]<inf; j++)
			dp[i]=min(dp[i]+mnoz(mnoz(dp[j-1], dp[i-j]), pytkom(i-1, j-1)), inf);
	}
	ll kt;
	
	scanf("%lld", &kt);
	wypisz(solve_smallest(n, kt));
	scanf("%lld", &kt);
	wypisz(solve_largest(n, kt));
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 9928kb

input:

3 1 2

output:

1 3 2 
1 3 2 

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 1ms
memory: 10144kb

input:

4 9 13

output:

3 1 4 2 
-1 

result:

ok 5 number(s): "3 1 4 2 -1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 9892kb

input:

4 20 7

output:

-1 
2 3 4 1 

result:

ok 5 number(s): "-1 2 3 4 1"

Test #4:

score: -100
Wrong Answer
time: 89ms
memory: 13836kb

input:

99500 1000000000000000000 1000000000000000000

output:

1 2 4 3 8 5 7 6 16 9 11 10 15 12 14 13 32 17 19 18 23 20 22 21 31 24 26 25 30 27 29 28 64 33 35 34 39 36 38 37 47 40 42 41 46 43 45 44 63 48 50 49 54 51 53 52 62 55 57 56 61 58 60 59 128 65 67 66 71 68 70 69 79 72 74 73 78 75 77 76 95 80 82 81 86 83 85 84 94 87 89 88 93 90 92 91 127 96 98 97 102 99 ...

result:

wrong answer 99470th numbers differ - expected: '99458', found: '66717'