比赛 |
20120707 |
评测结果 |
AAAAATTTTT |
题目名称 |
奇怪的棋盘 |
最终得分 |
50 |
用户昵称 |
Citron酱 |
运行时间 |
10.113 s |
代码语言 |
C++ |
内存使用 |
2.20 MiB |
提交时间 |
2012-07-07 12:04:29 |
显示代码纯文本
- #include <cstdio>
-
- #define I_F "boarda.in"
- #define O_F "boarda.out"
-
- const int MAXn=500;
- const int MAXm=500+1;
- const long MAXh=1000000;
- const long P=1000000007;
-
- int n, m;
- long h[MAXn];
- int maxh[MAXn][MAXn], minh[MAXn][MAXn];
- long long c[MAXm+1];
- long ans;
-
- void Input();
- void Getc();
- void Rmq();
- long Dfs(const int, const int, const long, const long, const int);
- void Output();
-
- int main()
- {
- Input();
- Getc();
- Rmq();
- ans=Dfs(0,n-1,1,h[maxh[0][n-1]],m);
- Output();
- return 0;
- }
-
- void Input()
- {
- freopen(I_F,"r",stdin);
- scanf("%d%d",&n,&m);
- for (int i=0; i<n; ++i)
- scanf("%ld",&h[i]);
- }
-
- void Getc()
- {
- c[0]=1;
- for (int i=1; i<MAXn; ++i)
- c[i]=c[i-1]*i%P;
- }
-
- void Rmq()
- {
- for (int i=0; i<n; ++i)
- {
- maxh[i][i]=minh[i][i]=i;
- for (int j=i+1; j<n; ++j)
- {
- maxh[i][j]=(h[j]>h[maxh[i][j-1]])?j:maxh[i][j-1];
- minh[i][j]=(h[j]<h[minh[i][j-1]])?j:minh[i][j-1];
- }
- }
- }
-
- long Dfs(const int l, const int r, const long d, const long u, const int m)
- {
- if (m==0)
- return 1;
- if (m>r-l+1 || u<d)
- return 0;
- if (h[minh[l][r]]>=u)
- {
- long long re=1;
- int i=r-l+1, j=u-d+1;
- for (int k=m-1; k>=0; --k)
- re*=((i-k)*(j-k));
- re=(re/c[m])%P;
- return re;
- }
-
- long long re=0;
- if (h[minh[l][r]]<d)
- {
- long long a, b;
- for (int k=0; k<=m; ++k)
- {
- a=Dfs(l,minh[l][r]-1,d,h[maxh[l][minh[l][r]-1]],k);
- if (a>0)
- {
- b=Dfs(minh[l][r]+1,r,d,h[maxh[minh[l][r]+1][r]],m-k);
- re=(re+a*b)%P;
- }
- }
- }
- else
- {
- long long a, b;
- for (int k=0; k<=m; ++k)
- {
- b=Dfs(l,r,h[minh[l][r]]+1,h[maxh[l][r]],k);
- if (b>0)
- {
- a=Dfs(l,r-k,d,h[minh[l][r]],m-k);
- re=(re+a*b)%P;
- }
- }
- }
- return re;
- }
-
- void Output()
- {
- freopen(O_F,"w",stdout);
- printf("%ld\n",ans);
- }
-