一个与相对大小关系相关的$DP$
设$f_{i,j,0/1}$表示放了$i$个,其中最后一个数字在$i$个中是第$j$大,且最后一个是极大值($1$)或极小值时($0$)的方案数。转移:
$$f_{i+1,j,1}=\sum\limits_{k=1}^{j-1} f_{i,k,0},f_{i+1,j,0} = \sum\limits_{k=j}^{i} f_{i,k,1}$$
发现转移可以前缀和优化,优化后复杂度为$O(n^2)$可以通过此题。
#include<bits/stdc++.h> //This code is written by Itst using namespace std; inline int read(){ ; ; char c = getchar(); while(c != EOF && !isdigit(c)){ if(c == '-') f = ; c = getchar(); } while(c != EOF && isdigit(c)){ a = (a << ) + (a << ) + (c ^ '); c = getchar(); } return f ? -a : a; } ; ]; int main(){ #ifndef ONLINE_JUDGE freopen("2467.in" , "r" , stdin); //freopen("2467.out" , "w" , stdout); #endif N = read(); MOD = read(); dp[][][] = dp[][][] = ; ; i <= N ; ++i){ ; j <= i ; ++j){ dp[i][j][] = dp[i - ][j][]; dp[i][j][] = dp[i - ][j - ][]; } ; j <= i ; ++j) dp[i][j][] = (dp[i][j][] + dp[i][j - ][]) % MOD; for(int j = i ; j ; --j) dp[i][j][] = (dp[i][j][] + dp[i][j + ][]) % MOD; } cout << (dp[N][N][] + dp[N][][]) % MOD; ; }