CF 1677D(Tokitsukaze and Permutations-冒泡排序)

Posted nike0good

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF 1677D(Tokitsukaze and Permutations-冒泡排序)相关的知识,希望对你有一定的参考价值。

已知长度为n的排列,经过k次冒泡(每次把最大的数交换到最后)后,得到的新序列为 a i a_i ai.
v i = ∑ j = 1 i − 1 = [ a i < a j ] vi=\\sum_j=1^i−1=[a_i<a_j] vi=j=1i1=[ai<aj]
现在已知 v i v_i vi的某些地方的值,不知道的记 v i = − 1 v_i=-1 vi=1
求合法原排列数。

考虑 v i v_i vi和排列达成双射关系。
且1次冒泡会导致 v i v_i vi序列整体左移,并减1(若为0则不减)。最后添1位0
也即是

for(int i=1;i<n;i++)
	v[i]=min(v[i+1]-1,0);
v[n]=0;

因此 v v v序列对应的原排列数为 k ! ∗ ( ∑ i = 1 n − k [ v [ i ] = = 0 ] ( k + 1 ) ) ∗ ( ∑ i = 1 n − k [ v [ i ] = = − 1 ] ( k + i ) ) k! *(\\sum_i=1^n-k[v[i]==0] (k+1))*(\\sum_i=1^n-k[v[i]==-1] (k+i)) k!(i=1nk[v[i]==0](k+1))(i=1nk[v[i]==1](k+i))
无解条件为
( ∀ i ≤ n − k , v [ i ] > i − 1 ) ∨ ( ∀ i > n − k , v [ i ] > 0 ) (\\forall i \\le n-k,v[i]>i-1)\\lor(\\forall i\\gt n-k,v[i]>0) (ink,v[i]>i1)(i>nk,v[i]>0)

#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F ( 998244353)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n)  \\
						For(j,m-1) cout<<a[i][j]<<' ';\\
						cout<<a[i][m]<<endl; \\
						 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b)return (a*b)%F;
ll add(ll a,ll b)return (a+b)%F;
ll sub(ll a,ll b)return ((a-b)%F+F)%F;
void upd(ll &a,ll b)a=(a%F+b%F)%F;
inline int read()

	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) if (ch=='-') f=-1; ch=getchar();
	while(isdigit(ch))  x=x*10+ch-'0'; ch=getchar();
	return x*f;
 
int n,m;
int v[1123456];
#define MAXN (1123456)
ll inj[MAXN],jie[MAXN];
inline int C(int a,int b) 
	return (ll)jie[a]*inj[b]%F*inj[a-b]%F;
inline int P(int a,int b) 
	return (ll)jie[a]*inj[a-b]%F;

ll p=F;
inline int pow2(int a,ll b)  //a^b mod p 
  
    if (b==0) return 1%p;  
    int c=pow2(a,b/2);  
    c=(ll)c*c%p;  
    if (b&1) c=(ll)c*a%p;  
    return c;  
  
void pre(int n) 
	jie[0]=1;For(i,n) jie[i]=mul(jie[i-1],i);
	inj[0]=inj[1]=1;Fork(i,2,n) inj[i]=(F-(F/i))*inj[F%i]%F;
	For(i,n) inj[i]=mul(inj[i],inj[i-1]);  
 
ll a[1123456];
ll calc() 
	int n=read(),k=read();
	For(i,n) v[i]=read();
	Fork(i,n-k+1,n) if(v[i]>0) return 0;
	For(i,n-k) if(v[i]>i-1) return 0;
	ll p=jie[k];
	For(i,n-k) if(v[i]==0) p=p*(k+1)%F;
	else if(v[i]==-1) p=p*(i+k)%F;
	return p;


int main()

//	freopen("D.in","r",stdin);
//	freopen(".out","w",stdout);
	pre(1e6);
	int T=read();
	while(T--) 
		cout<<calc()<<endl;
	
	return 0;

以上是关于CF 1677D(Tokitsukaze and Permutations-冒泡排序)的主要内容,如果未能解决你的问题,请参考以下文章

CF 1677D(Tokitsukaze and Permutations-冒泡排序)

CF1190E Tokitsukaze and Explosion 二分贪心倍增ST表

HDU 6797 Tokitsukaze and Rescue

Codeforces Round #573 (Div. 2) A. Tokitsukaze and Enhancement

Tokitsukaze and Duel CodeForces - 1191E (博弈论)

Codeforces Round #573 (Div. 2) E. Tokitsukaze and Duel (博弈)