比赛 |
国庆欢乐赛3 |
评测结果 |
AAAAAAAAAA |
题目名称 |
毛二琛 |
最终得分 |
100 |
用户昵称 |
梦那边的美好ET |
运行时间 |
0.714 s |
代码语言 |
C++ |
内存使用 |
4.08 MiB |
提交时间 |
2025-10-05 11:07:09 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
// #define LOCAL
#define ll long long
#define db double
#define Ciallo true
#define pir pair <ll,ll>
#define fs first
#define sc second
#define mod 1000000007
#define MAXN 5010
inline ll read(){
ll f = 1, num = 0;
char c = getchar();
while(c < '0' || c > '9'){
if(c=='-') f = -1;c = getchar();
}
while(c>='0'&&c<='9') num = num * 10 + c - '0', c = getchar();
return num * f;
}
int typ[MAXN],p[MAXN];
int n;
ll f[2][MAXN], pre[2][MAXN], suf[2][MAXN];
int main(int argc, char* argv[]){
freopen("swap.in", "r", stdin);
freopen("swap.out", "w", stdout);
#ifdef FS
freopen(argv[1], "r", stdin);
freopen(argv[2], "w", stdout);
#elif defined(LOCAL)
freopen("w.in", "r", stdin);
freopen("w.out", "w", stdout);
#endif
n = read();
for(int i = 0;i < n; ++i){
p[i] = read();
if(i == p[i]){
cout << "0\n";
return 0;
}
auto col1 = [&](int x){// 限制 pos_x > pos_x - 1
if(x <= 0 || x >= n)return;
if(typ[x] == 2){
cout << "0\n";
exit(0);
}
typ[x] = 1;
};
auto col2 = [&](int x){// 限制 pos_x < pos_x - 1
if(x <= 0 || x >= n)return;
if(typ[x] == 1){
cout << "0\n";
exit(0);
}
typ[x] = 2;
};
if(p[i] > i){
col2(i);
for(int j = i + 1;j < p[i]; ++j)col1(j);
}else{
col1(i);
for(int j = p[i] + 1;j < i; ++j)col2(j);
}
}
f[0][1] = 1;
pre[0][1] = suf[0][1] = 1;
for(int i = 1;i < n - 1; ++i){
int now = i & 1, lst = now ^ 1;
memset(f[now], 0, sizeof(f[now]));
memset(pre[now], 0, sizeof(pre[now]));
memset(suf[now], 0, sizeof(suf[now]));
for(int j = 1;j <= i + 1; ++j){
if(typ[i] == 1)f[now][j] = pre[lst][j - 1];
if(typ[i] == 2)f[now][j] = suf[lst][j];
}
for(int j = 1;j <= i + 1; ++j){
pre[now][j] = (pre[now][j - 1] + f[now][j]) % mod;
}
for(int j = i + 1; j; --j){
suf[now][j] = (suf[now][j + 1] + f[now][j]) % mod;
}
}
cout << pre[(n - 2) & 1][n - 1] << endl;
cerr << "Time : " << 1.0 * clock() / CLOCKS_PER_SEC << "s \n";
return 0;
}