-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path12484.cpp
52 lines (45 loc) · 1.08 KB
/
12484.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
math > game theory > minimax
difficulty: medium
date: 28/Mar/2020
problem: alberto and wanderley take one of two cards at the edges of the cards sequence, alberto want maximize it
hint: fill memo table in row-major order
by: @brpapa
*/
#include <iostream>
using namespace std;
typedef long long ll;
const int pALB = 0;
const int pWAN = 1;
int A[10010];
ll memo[10010][10010];
ll minimax(bool p, int l, int r) {
// cartas atuais A[l:r], jogador atual p
if (l > r) return 0ll;
ll &ans = memo[r][l]; // r >= l
if (ans != -1) return ans;
if (p == pALB) {
return ans = max(
(ll)A[l] + minimax(!p, l+1, r),
(ll)A[r] + minimax(!p, l, r-1)
);
}
else {
// pWAN escolhe estado em que pALB recebe menos
return ans = min(
minimax(!p, l+1, r),
minimax(!p, l, r-1)
);
}
}
int main() {
int N;
while (cin >> N) {
for (int i = 0; i < N; i++) {
cin >> A[i];
for (int j = 0; j <= i; j++) memo[i][j] = -1;
}
cout << minimax(pALB, 0, N-1) << endl;
}
return 0;
}