-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva10003.cpp
71 lines (54 loc) · 1.28 KB
/
uva10003.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
#include <limits.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <bitset>
//#include <unordered_map>
using namespace std;
typedef pair<int, int> ii;
#define traverse(c, it) \
for (map<ii, int>::iterator it = c.begin(); it != c.end(); it++)
int N, L;
vector<int> cuts;
int dp[1002][1002];
int getMinCuts(int left, int right) {
if(dp[left][right] != -1) {
return dp[left][right];
}
int bestCost = INT_MAX;
for(int i = 0; i < N; i++) {
if(left < cuts[i] && cuts[i] < right) {
int cost = getMinCuts(left, cuts[i]) + getMinCuts(cuts[i], right) +
(right - left);
bestCost = min(bestCost, cost);
}
}
if(bestCost == INT_MAX) {
bestCost = 0;
}
return dp[left][right] = bestCost;
}
int main() {
while(true) {
cin >> L;
if(L == 0) {
break;
}
cin >> N;
cuts.clear();
cuts.resize(N);
for(int i = 0; i < N; i++) {
cin >> cuts[i];
}
memset(dp, -1, sizeof(dp));
printf("The minimum cutting is %d.\n", getMinCuts(0, L));
}
return 0;
}