<dp - 부분합이용>
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
|
#include <iostream>
#include <vector>
#include <cstring>
#define MAXX 1001
using namespace std;
int dp[MAXX][MAXX];
int card[MAXX] = { 0 };
//
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n; //카드의 개수
for (int i = 1; i <= n; i++) cin >> card[i];
memset(dp, 0, sizeof(dp));
//cout << playGame(0, 1, n) << '\n';
if (n % 2 == 1) {
for (int i = 1; i <= n; i++) {
dp[i][i] = card[i];
}
}
for (int i = 1; i <= n; i++) {
//명우 순서와 근우 순서를 나눠서 계산한다.
for (int j = 1; j <= n - i; j++) {
int k = j + i;
//cout << j << " " << k << " " << card[j] << " " << card[k] << '\n';
if (abs(n - (j + k)) % 2) {
//근우의 턴
dp[j][k] = max(dp[j + 1][k] + card[j], dp[j][k - 1] + card[k]);
}
else
dp[j][k] = min(dp[j + 1][k], dp[j][k - 1]); //근우가 받는 최대 점수이기 때문에 명우일 대는 점수를 더하지 않는다.
}
}
cout << dp[1][n] << '\n';
//for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) cout << dp[i][j] << ' ';
// cout << '\n';
//}
}
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
<dp- 재귀이용 >
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
|
#include <iostream>
#include <vector>
#include <cstring>
#define MAXX 1001
using namespace std;
int dp[MAXX][MAXX];
int card[MAXX] = { 0 };
int playGame(bool player, int start, int end) {
if (start == end) {
if (player) return dp[start][end];
return dp[start][end] = card[start];
}
int &ret = dp[start][end];
if (ret) return ret; //이미 고른 범위
if (!player) {
//그누의 순서
ret = max(playGame(!player, start + 1, end) + card[start], playGame(!player, start, end - 1) + card[end]);
}
else {
//명우는 점수를 더하지 않고 그냥 범위만 줄여준다.
ret = min(playGame(!player, start + 1, end), playGame(!player, start, end - 1));
}
//cout << "debug " << player << " " << dp[start][end] << '\n';
return ret;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n; //카드의 개수
for (int i = 1; i <= n; i++) cin >> card[i];
memset(dp, 0, sizeof(dp));
//cout << playGame(0, 1, n) << '\n';
cout << playGame(0, 1, n) << '\n';
}
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|