๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/16639
16639๋ฒ: ๊ดํธ ์ถ๊ฐํ๊ธฐ 3
๊ธธ์ด๊ฐ N์ธ ์์์ด ์๋ค. ์์์ 0๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ , 9๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์ ์์ ์ฐ์ฐ์(+, -, ×)๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. ๊ณฑํ๊ธฐ์ ์ฐ์ฐ์ ์ฐ์ ์์๊ฐ ๋ํ๊ธฐ์ ๋นผ๊ธฐ๋ณด๋ค ๋๊ธฐ ๋๋ฌธ์, ๊ณฑํ๊ธฐ๋ฅผ ๋จผ์ ๊ณ
www.acmicpc.net
์ฃผ์ด์ง ์์ ๊ดํธ๋ฅผ ์ถ๊ฐํด์ ๊ฐ์ฅ ํฐ ๊ฐ์ ๋ง๋ค์ด๋ด๋ ๋ฌธ์ .
ํ์ด
DP๋ฅผ ์ด์ฉํด์ ๊ตฌ๊ฐ๋ณ๋ก ๊ฐ์ฅ ํฐ ๊ฐ์ ๊ณ์ ๊ตฌํ๋ค์ ๋ง์ง๋ง์ 1๋ถํฐ N๊น์ง ๋ฒ์์์ ๊ฐ์ฅ ํฐ ๊ฐ์ ์ถ๋ ฅํด์ฃผ๋ฉด ๋๋ค.
์์์ ๋ค์ด๊ฐ๋ ์ซ์๋ 0~9์ฌ์ด์ ์ซ์์ด๋ฏ๋ก ์์๋ ์์ ์ ๋ ฅ๋์ง ์๋๋ค.
๋จผ์ ๋ด๊ฐ ํ๋ ์ค์๋ "๊ฐ์ฅ ํฐ ๊ฐ" ์ ์ถ๋ ฅํ๋ค๋ ํค์๋์๋ง ์ง์คํด์ ๊ตฌ๊ฐ๋ณ๋ก ์ต๋๊ฐ๋ง ๊ณ์ ๊ตฌํ๊ณ ์์๋ค. ๋๋ถ๋ถ ์์ ๋ ์ ๋์์ง๋ง "-" ๊ธฐํธ๊ฐ ๋ค์ด๊ฐ ์์ ๋ง ๋ง๋๋ฉด ๊ฐ์ด ์ด์ํ๊ฒ ์ถ๋ ฅ๋์๋ค.
+, -, * ๊ธฐํธ๋ณ๋ก ์ต๋๊ฐ์ด ๋์ค๋ ์ํฉ์ ์๊ฐํด๋ณด์.
1. + : ์ต๋๊ฐ + ์ต๋๊ฐ = ์ต๋๊ฐ
2. - : ์ต๋๊ฐ - ์ต์๊ฐ = ์ต๋๊ฐ
3. * : ์์ * ์์ ๋๋ ์์ * ์์ ๋ฉด ๊ฐ์ฅ ํฌ๊ฒ ์ง๋ง ์ฌ๊ฑด์ด ์๋๋ค๋ฉด ์์ * ์์๋ฅผ ํด์ผํ๋ ์ํฉ์ด ์๊ธด๋ค.
์ฆ , * ๊ธฐํธ์ผ๋๋ ์ต๋ * ์ต์, ์ต์ * ์ต๋, ์ต๋ * ์ต๋, ์ต์ * ์ต์ ๋ชจ๋ ํด๋ณด๊ณ ๊ฐ์ฅ ํฐ ๊ฐ์ ์ฐพ์์ผ ํ๋ค.
๋ฐ๋ผ์ ๊ตฌ๊ฐ๋ณ๋ก "์ต์๊ฐ"๋ ์ ์ฅํ๊ณ ์์ด์ผ ์ต๋๊ฐ์ ์ฐพ์ ์ ์๋ค.
๊ทธ๋์ DP๋ฐฐ์ด์ DP[์ต๋ or ์ต์][๊ตฌ๊ฐ ์์์ ][๊ตฌ๊ฐ ๋์ ] ์ผ๋ก ์ค์ ํด์ฃผ์๋ค.
DP[0][i][j] ๋ i~j ๊ตฌ๊ฐ์ ์ต๋๊ฐ์ ์ ์ฅํ๊ณ DP[1][i][j] ๋ i~j ๊ตฌ๊ฐ์ ์ต์๊ฐ์ ์ ์ฅํ๋ค.
๊ดํธ๋ฅผ ์์ฐ๋ ๊ตฌ๊ฐ์ ์์์ ๊ธธ์ด๊ฐ ๊ทธ๋ ๊ฒ ๊ธธ์ง ์๊ธฐ๋๋ฌธ์ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ๋ค ๊ณ ๋ คํด์ฃผ์๋ค.
๋ง์ฝ ํ์ฌ ํ์์ค์ธ ๊ตฌ๊ฐ์ด 1 + 3 * 5 - 4 ๋ผ๋ ์์ด๋ผ๊ณ ํ๋ฉด (1) + (3 * 5 - 4) , (1 + 3) * (5 - 4) , (1 + 3 * 5) - (4) ์ด๋ ๊ฒ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํด์ ์ต๋๊ฐ๊ณผ ์ต๋๊ฐ์ ์ฐพ์์ค๋ค. ์ฌ๊ธฐ์ (3 * 5 - 4) ๋๋ (1 + 3 * 5) ์ ์ต๋, ์ต์๊ฐ์ ์ด๋ฏธ ์์ ์ฐพ์๋์์ ๊ฒ์ด๋ฏ๋ก ๊ฐ์ ธ์์ ์ฐ๋ฉด๋๋ค.
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> Priority_queue;
const int INF_MAX = INT_MAX;
const int INF_MIN = INT_MIN;
const int MAX_N = 20;
int N;
int Num[MAX_N];
char Operator[MAX_N];
int DP[2][MAX_N][MAX_N];
int main(void)
{
cin >> N;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) {
DP[0][i][j] = INF_MIN;
DP[1][i][j] = INF_MAX;
}
for (int i = 1; i <= N; i++) {
if (i % 2 == 1)
cin >> Num[(i + 1) / 2];
else if (i % 2 == 0)
cin >> Operator[i / 2];
}
for (int i = 1; i < (N + 1) / 2; i++) {
if (Operator[i] == '-') {
DP[0][i][i + 1] = Num[i] - Num[i + 1];
DP[1][i][i + 1] = Num[i] - Num[i + 1];
}
else if (Operator[i] == '+') {
DP[0][i][i + 1] = Num[i] + Num[i + 1];
DP[1][i][i + 1] = Num[i] + Num[i + 1];
}
else if (Operator[i] == '*') {
DP[0][i][i + 1] = Num[i] * Num[i + 1];
DP[1][i][i + 1] = Num[i] * Num[i + 1];
}
}
for (int i = 1; i <= N; i++)
DP[0][i][i] = DP[1][i][i] = Num[i];
for (int i = 2; i < (N + 1) / 2; i++) {
for (int j = 1; j <= (N + 1) / 2 - i; j++) { // j ~ j + i ๊น์ง
for (int t = 0; t < i; t++) { // t๋ ๊ดํธ๋ฅผ ์์ธ ๊ตฌ๊ฐ์ ์ ํ๋ ๊ฒฝ๊ณ์
if (Operator[j + t] == '+') {
DP[0][j][j + i] = max(DP[0][j][j + i], DP[0][j][j + t] + DP[0][j + t + 1][j + i]);
DP[1][j][j + i] = min(DP[1][j][j + i], DP[1][j][j + t] + DP[1][j + t + 1][j + i]);
}
else if (Operator[j + t] == '-') {
DP[0][j][j + i] = max(DP[0][j][j + i], DP[0][j][j + t] - DP[1][j + t + 1][j + i]);
DP[1][j][j + i] = min(DP[1][j][j + i], DP[1][j][j + t] - DP[0][j + t + 1][j + i]);
}
else if (Operator[j + t] == '*') {
DP[0][j][j + i] = max(DP[0][j][j + i], max(DP[0][j][j + t] * DP[0][j + t + 1][j + i],
max(DP[0][j][j + t] * DP[1][j + t + 1][j + i], max(DP[1][j][j + t] * DP[1][j + t + 1][j + i], DP[1][j][j + t] * DP[0][j + t + 1][j + i]))));
DP[1][j][j + i] = min(DP[1][j][j + i], min(DP[0][j][j + t] * DP[0][j + t + 1][j + i],
min(DP[0][j][j + t] * DP[1][j + t + 1][j + i], min(DP[1][j][j + t] * DP[1][j + t + 1][j + i], DP[1][j][j + t] * DP[0][j + t + 1][j + i]))));
}
}
}
}
cout << DP[0][1][(N + 1) / 2] << "\n";
return 0;
}
|
cs |
์ด ๋ฌธ์ ๋ฅผ ๋ํ๋ ์ฝ๋ฉํ ์คํธ์์ ๋ง๋ฌ์ผ๋ฉด ๋ชปํ์๊ฑฐ๋ ์๊ฐ์ ์์ฒญ ์ก์๋จน์์ ๊ฒ ๊ฐ๋ค. ์์ง DP๋ ์ด๋ ต๋ค.
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 9874 : Wormholes (0) | 2022.04.22 |
---|---|
[BOJ] 1800 : ์ธํฐ๋ท ์ค์น (0) | 2022.04.20 |
[BOJ] 17780 : ์๋ก์ด ๊ฒ์ (0) | 2022.04.15 |
[BOJ] 10021 : Watering the Fields (0) | 2022.04.14 |
[BOJ] 15684 : ์ฌ๋ค๋ฆฌ ์กฐ์ (0) | 2022.04.14 |