-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.cpp
72 lines (58 loc) · 1.3 KB
/
card.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <algorithm>
#include <fstream>
#include <iostream>
using namespace std;
ifstream input("card.inp");
ofstream output("card.out");
int DP[2][1001][1001];
int arr[1001];
int card(int turn, int x, int y) {
if (x == y) {
if (turn == 0)
return arr[x];
else
return 0;
}
int &answer = DP[turn][x][y];
if (answer != -1) {
return answer;
}
if (turn == 0) {
int t1 = card(!turn, x + 1, y) + arr[x];
int t2 = card(!turn, x, y - 1) + arr[y];
if (t1 > t2) {
answer = t1;
} else {
answer = t2;
}
} else {
int t1 = card(!turn, x + 1, y);
int t2 = card(!turn, x, y - 1);
if (t1 > t2) {
answer = t2;
} else {
answer = t1;
}
}
return answer;
}
int main() {
int count;
input >> count;
while (count != 0) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 1001; j++) {
for (int k = 0; k < 1001; k++) {
DP[i][j][k] = -1;
}
}
}
int n;
input >> n;
for (int i = 0; i < n; i++) {
input >> arr[i];
}
output << card(0, 0, n - 1) << "\n";
count--;
}
}