fork download
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cctype>
  4. #define REP(i, l, r) for(int i = l; i <= r; i++)
  5. #define mp make_pair
  6. #define Jump(l, r) {\
  7.   ret += f[l][r].win;\
  8. if (f[l][r].left) {\
  9. tmp = Cal(it = (r) - f[l][r].left + 1); ret += tmp.first, it = tmp.second;\
  10. } else it = succ(r);\
  11. }
  12.  
  13. using namespace std;
  14. typedef pair<int, int> PII;
  15.  
  16. const int MAXN = 2012; int n, k, I = -1, J = -1, a[MAXN];
  17. inline int getC(void) {
  18. int c; while (!isalnum(c = getchar()));
  19. return isdigit(c) ? c - 48 : (c == 'A' ? 1 : 10);
  20. }
  21.  
  22. struct Node {
  23. int win, left;
  24. Node():win(0), left(0){}
  25. }f[MAXN][MAXN];
  26.  
  27. inline int succ(int now) {
  28. if (now == I - 1) return J + 1;
  29. else if (now == J) return 1;
  30. else return now + 1;
  31. }
  32.  
  33. inline PII Cal(int now, int end = MAXN) {
  34. int c[2] = {}, num[2] = {};
  35. REP(i, 0, 3) if (now != end) c[i&1] += a[now], num[i&1]++, now = succ(now);
  36. REP(i, 0, 1) {
  37. while (c[i] < 16 && now != end) {
  38. c[i] += a[now], num[i]++, now = succ(now);
  39. if (c[i] > 21) return mp(i, now);
  40. if (num[i] == 5) return mp(i^1, now);
  41. }
  42. }
  43. return c[0] >= 16 && c[1] >= 16 ? mp((int)(c[0] > c[1]), now) : mp(0, end + 2);
  44. }
  45.  
  46. inline void Process(int l) {
  47. int i = l;
  48. while (i <= n) {
  49. PII tmp = Cal(i, n + 1);
  50. REP(j, i, tmp.second - 2) f[l][j].win = f[l][i - 1].win, f[l][j].left = j - i + 1;
  51. f[l][tmp.second - 1].win = f[l][i - 1].win + tmp.first;
  52. f[l][tmp.second - 1].left = 0;
  53. i = tmp.second;
  54. }
  55. }
  56.  
  57. inline int Make(int l, int r) {
  58. int ret = 0, it = l, cut = n - k + 2; PII tmp;
  59. if (cut > r) {
  60. Jump(l, r);
  61. if (it < l) Jump(it, l - 1);
  62. if (it < cut) Jump(it, cut - 1);
  63. } else if (l - (r - cut + 1) >= 1) {
  64. cut = l - (r - cut + 1);
  65. Jump(l, r);
  66. if (it < cut) Jump(it, cut - 1);
  67. } else {
  68. cut = r - (n - cut + 1 - (n - (r - l + 1))) + 1;
  69. Jump(l, cut - 1);
  70. }
  71. return ret;
  72. }
  73.  
  74. int main(void) {
  75. int kase = 0;
  76. while (scanf("%d%d", &n, &k) == 2) {
  77. I = -1, J = -1;
  78. REP(i, 1, n) a[i] = getC();
  79. REP(i, 1, n) Process(i);
  80. int Ans = 0;
  81. REP(i, 2, n - 1) REP(j, i, n - 1) {
  82. I = i, J = j; int tmp = Make(i, j);
  83. Ans = max(tmp, Ans);
  84. }
  85. printf("Case %d: %d\n", ++kase, Ans);
  86. }
  87. return 0;
  88. }
  89.  
Success #stdin #stdout 0.04s 34320KB
stdin
20 10
8 4 7 8 8 K 5 A Q Q A Q 6 4 J 6 9 5
3 9
40 10
3 J 7 7 2 T J 6 A 4 4 8 J T 6 A 6 2 K 9
6 5 7 J T 3 5 5 3 7 7 J 5 3 A 5 9 Q 6 7
stdout
Case 1: 3
Case 2: 6