跳到正文

题解

题解:P2467

wangyizhi约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月21日
篇幅
约 8 分钟
题目
P2467

版本

  1. v1当前

提供一种类似连续段 dp 的做法。

题目分析

称一个比两边都大的数为峰,一个比两边都小的数为谷。则我们就要求峰谷交错的排列的个数。

显然谷只可能在两个峰之间。所以我们可以记录当前有多少个空可以插入谷。

考虑从大到小枚举数 ii 插入原先的排列。若 ii 为峰,则插入后可以增加一个能放谷的位置;若 ii 为谷,则插入后可以减少一个能放谷的位置。

然后考虑转移时的系数。记当前空的个数为 kk。若 nn 为偶数,则初始状态应为 峰 ... ... 谷(此处峰谷指排列两侧假定的状态以限制排列),插入峰时有 k+1k+1 个合法的空,插入谷时有 kk 个合法的空。但这样排列最左边就是谷,左右翻转一下的排列没有被计入,答案应乘 22。若 nn 为奇数,则初始状态应为 峰 ... ... 峰,初始有一个空,插入峰时有 kk 个合法的空,插入谷时有 kk 个合法的空。但这样排列两边就是谷,排列 pi′=n+1−pip'_i=n+1-p_i 没有被计入,答案应乘 22。

于是就做完了。时间复杂度 O(n2)O(n^2),滚一下空间复杂度 O(n)O(n)。

AC Code
C++
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//bool Mst;
using namespace std;
namespace wyzfastio
{
//#define usefio
#ifdef usefio
	namespace __getchar{const int bufsize=1<<20;char buf[bufsize<<1],*p1=buf,*p2=buf;inline char getchar(){return (p1==p2&&(p2=(p1=buf)+fread(buf,1,bufsize,stdin),p1==p2))?EOF:(*p1++);}}using __getchar::getchar;
	namespace __putchar{const int bufsize=1<<20;char buf[bufsize<<1],*p=buf;inline void putchar(const char ch){if(p-buf==bufsize) fwrite(buf,1,bufsize,stdout),p=buf;*p++=ch;}inline void flush(){fwrite(buf,1,p-buf,stdout);}}using __putchar::putchar;using __putchar::flush;
#endif
	/*---input---*/
	inline void read(unsigned long long &x){char ch=getchar();unsigned long long res=0;while(ch<'0'||ch>'9')ch=getchar();while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res;}
	inline void read(unsigned int &x){char ch=getchar();unsigned int res=0;while(ch<'0'||ch>'9')ch=getchar();while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res;}
	inline void read(long long &x){char ch=getchar();long long f=1;unsigned long long res=0;while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res*f;}
	inline void read(int &x){char ch=getchar();int f=1;unsigned res=0;while(ch<'0'||ch>'9'){if(ch=='-')f=-f;;ch=getchar();}while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res*f;}
	inline void read(char &s){s=getchar();while(s==' '||s=='\n'||s=='\r') s=getchar();}
	inline int read(char *s){int i=0;char ch=getchar();while(ch==' '||ch=='\n'||ch=='\r') ch=getchar();while(!(ch==' '||ch=='\n'||ch=='\r'||ch==EOF)) s[++i]=ch,ch=getchar();s[i+1]='\0';return i;}
	inline void read(std::string &s){char ch=getchar();s.clear();while(ch==' '||ch=='\n'||ch=='\r') ch=getchar();while(!(ch==' '||ch=='\n'||ch=='\r'||ch==EOF)) s.push_back(ch),ch=getchar();}
	template<typename _Tp,typename ...Args>inline void read(_Tp &x,Args &...args){read(x),read(args...);}
	/*---output---*/
	inline void write(const unsigned long long x){if(x<10) putchar(x+'0');else write(x/10),putchar(x%10+'0');}
	inline void write(const long long x){unsigned long long t=x;if(x<0)putchar('-'),t=-x;write(t);}
	inline void write(const unsigned int x){if(x<10) putchar(x+'0');else write(x/10),putchar(x%10+'0');}
	inline void write(const int x){unsigned int t=x;if(x<0)putchar('-'),t=-x;write(t);}
	inline void write(const char x){putchar(x);}
	inline void write(const char *s){while(*s!='\0'&&*s!=EOF)putchar(*s),s++;}
	template<typename _Tp,typename ...Args>inline void write(_Tp x,Args ...args){write(x),write(args...);}
}
using namespace wyzfastio;
using ll=long long;
using ld=long double;
//#define int ll
using pii=pair<int,int>;
const int N=5e3+5;
int f[N],g[N];
//bool Med;
signed main()
{
//	cerr<<"Memory Size: "<<abs((&Med)-(&Mst))/1024.0/1024<<" MB\n";
//	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//	freopen("in.in","r",stdin);
//	freopen("out./out","w",stdout);
	int n,mod;
	read(n,mod);
	if(n==1) return write(1%mod,'\n'),0;
	f[n%2]=1;
	for(int i=n;i>=1;i--)
	{
		for(int j=0;j<=n;j++) g[j]=f[j],f[j]=0;
		for(int j=0;j<=n;j++)
		{
			if(j) f[j-1]=(f[j-1]+1ll*(j)*g[j])%mod;
			if(j!=n) f[j+1]=(f[j+1]+1ll*(j+!(n%2))*g[j])%mod;
		}
	}
//	for(int i=0;i<=n;i++) write(f[i],' ');
//	write('\n');
	write(f[0]*2%mod,'\n');
#ifdef usefio
	flush();
#endif
	return 0;
}
原文