比赛 20120707 评测结果 AAAAATTTTT
题目名称 奇怪的棋盘 最终得分 50
用户昵称 Citron酱 运行时间 10.113 s
代码语言 C++ 内存使用 2.20 MiB
提交时间 2012-07-07 12:04:29
显示代码纯文本
  1. #include <cstdio>
  2.  
  3. #define I_F "boarda.in"
  4. #define O_F "boarda.out"
  5.  
  6. const int MAXn=500;
  7. const int MAXm=500+1;
  8. const long MAXh=1000000;
  9. const long P=1000000007;
  10.  
  11. int n, m;
  12. long h[MAXn];
  13. int maxh[MAXn][MAXn], minh[MAXn][MAXn];
  14. long long c[MAXm+1];
  15. long ans;
  16.  
  17. void Input();
  18. void Getc();
  19. void Rmq();
  20. long Dfs(const int, const int, const long, const long, const int);
  21. void Output();
  22.  
  23. int main()
  24. {
  25. Input();
  26. Getc();
  27. Rmq();
  28. ans=Dfs(0,n-1,1,h[maxh[0][n-1]],m);
  29. Output();
  30. return 0;
  31. }
  32.  
  33. void Input()
  34. {
  35. freopen(I_F,"r",stdin);
  36. scanf("%d%d",&n,&m);
  37. for (int i=0; i<n; ++i)
  38. scanf("%ld",&h[i]);
  39. }
  40.  
  41. void Getc()
  42. {
  43. c[0]=1;
  44. for (int i=1; i<MAXn; ++i)
  45. c[i]=c[i-1]*i%P;
  46. }
  47.  
  48. void Rmq()
  49. {
  50. for (int i=0; i<n; ++i)
  51. {
  52. maxh[i][i]=minh[i][i]=i;
  53. for (int j=i+1; j<n; ++j)
  54. {
  55. maxh[i][j]=(h[j]>h[maxh[i][j-1]])?j:maxh[i][j-1];
  56. minh[i][j]=(h[j]<h[minh[i][j-1]])?j:minh[i][j-1];
  57. }
  58. }
  59. }
  60.  
  61. long Dfs(const int l, const int r, const long d, const long u, const int m)
  62. {
  63. if (m==0)
  64. return 1;
  65. if (m>r-l+1 || u<d)
  66. return 0;
  67. if (h[minh[l][r]]>=u)
  68. {
  69. long long re=1;
  70. int i=r-l+1, j=u-d+1;
  71. for (int k=m-1; k>=0; --k)
  72. re*=((i-k)*(j-k));
  73. re=(re/c[m])%P;
  74. return re;
  75. }
  76. long long re=0;
  77. if (h[minh[l][r]]<d)
  78. {
  79. long long a, b;
  80. for (int k=0; k<=m; ++k)
  81. {
  82. a=Dfs(l,minh[l][r]-1,d,h[maxh[l][minh[l][r]-1]],k);
  83. if (a>0)
  84. {
  85. b=Dfs(minh[l][r]+1,r,d,h[maxh[minh[l][r]+1][r]],m-k);
  86. re=(re+a*b)%P;
  87. }
  88. }
  89. }
  90. else
  91. {
  92. long long a, b;
  93. for (int k=0; k<=m; ++k)
  94. {
  95. b=Dfs(l,r,h[minh[l][r]]+1,h[maxh[l][r]],k);
  96. if (b>0)
  97. {
  98. a=Dfs(l,r-k,d,h[minh[l][r]],m-k);
  99. re=(re+a*b)%P;
  100. }
  101. }
  102. }
  103. return re;
  104. }
  105.  
  106. void Output()
  107. {
  108. freopen(O_F,"w",stdout);
  109. printf("%ld\n",ans);
  110. }
  111.