#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int > pp;
typedef long long ll;
int const N=1e4+2,oo=1e9,M=1e6;
ll const OO=1e18;
double const eps=1e-6,PI=acos(-1);
int mod=M+7;
int n,aa[N],dp[2][N][2];
int rec(int idx,int mx,int l){
int &ret=dp[idx][mx][l];
if(ret!=-1)return ret;
if(idx==n-1)return ret=1;
if(idx==0){
if(aa[1]>1)return ret=(rec(idx+1,2,0)+rec(idx+1,1,1))%mod;
return ret=rec(idx+1,1,0);
}
if(l)return ret=(rec(idx+1,mx,l)*1LL*mx+rec(idx+1,mx+1,l))%mod;
else{
if(aa[idx+1]>mx)ret=(rec(idx+1,mx,1)*1LL*mx+rec(idx+1,aa[idx+1],0))%mod;
else ret=(rec(idx+1,mx,1)*1LL*(aa[idx+1]-1)+rec(idx+1,mx,0))%mod;
return ret;
}
return ret;
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",&aa[i]);
/* memset(dp,-1,sizeof dp);
cout<<rec(0,0,0)<<endl;*/
int f=0,an=0;
for(int i=1;i<=n;i++)dp[f][i][0]=dp[f][i][1]=1;
for(int i=n-2;i>-1;i--){
f=1-f;
for(int j=1;j<=n;j++){
dp[f][j][1]=(dp[!f][j][1]*1LL*j+dp[!f][j+1][1])%mod;
if(aa[i+1]>j)dp[f][j][0]=(dp[!f][j][1]*1LL*j+(aa[i+1]==j+1)*dp[!f][aa[i+1]][0])%mod;
else dp[f][j][0]=(dp[!f][j][1]*1LL*(aa[i+1]-1)+dp[!f][j][0])%mod;
}
}
printf("%d\n",dp[f][1][0]);
// printf("%d\n",an);
return 0;
}
Compilation message
teams.cpp: In function 'int main()':
teams.cpp:35:13: warning: unused variable 'an' [-Wunused-variable]
int f=0,an=0;
^~
teams.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
teams.cpp:32:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=0;i<n;i++)scanf("%d",&aa[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
376 KB |
Output is correct |
2 |
Correct |
7 ms |
376 KB |
Output is correct |
3 |
Correct |
7 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
376 KB |
Output is correct |
2 |
Correct |
22 ms |
292 KB |
Output is correct |
3 |
Correct |
23 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1066 ms |
504 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
505 ms |
504 KB |
Output is correct |
2 |
Correct |
508 ms |
604 KB |
Output is correct |
3 |
Correct |
517 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1081 ms |
504 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |