Advertisement
Guest User

Easy Game

a guest
Sep 8th, 2016
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. #define f(i,a,b) for(int i = (a); i <= (b); i++)
  3.  
  4. using namespace std;
  5.  
  6. const int INF = 1000000005;
  7. int N, A[105], DP[105][105][2], S[105][105], T;
  8.  
  9. int dp(int l, int r, int turn)
  10. {
  11.     if(l+r==N) return 0;
  12.     if(DP[l][r][turn] != INF) return DP[l][r][turn];
  13.     int ret = turn ? INF : -INF;
  14.     f(take,1,N-l-r)
  15.     {
  16.         // Take from the left
  17.  
  18.         if(turn) ret = min(ret, -S[l+1][l+take] + dp(l+take,r,1-turn));
  19.         else ret = max(ret, S[l+1][l+take] + dp(l+take,r,1-turn));
  20.  
  21.         // Take from the right
  22.  
  23.         if(turn) ret = min(ret, -S[N-r-take+1][N-r] + dp(l,r+take,1-turn));
  24.         else ret = max(ret, S[N-r-take+1][N-r] + dp(l,r+take,1-turn));
  25.     }
  26.     return DP[l][r][turn] = ret;
  27. }
  28.  
  29. int main()
  30. {
  31.     cin >> T;
  32.     f(tt,1,T)
  33.     {
  34.         cin >> N;
  35.         f(i,1,N) cin >> A[i];
  36.         f(l,0,N) f(r,0,N) S[l][r] = 0;
  37.         f(l,1,N) f(r,l,N) S[l][r] = S[l][r-1] + A[r];
  38.         f(l,0,N) f(r,0,N) f(t,0,1) DP[l][r][t] = INF;
  39.         cout << "Case " << tt << ": " << dp(0,0,0) << "\n";
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement