-
Notifications
You must be signed in to change notification settings - Fork 160
/
Mergestones.cpp
85 lines (68 loc) · 2.42 KB
/
Mergestones.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
73
74
75
76
77
78
79
80
81
82
83
/*
There are n stacks of stones on a playgroud. We want to merge all stones together.
The rule is that you need to merge two adjacent stacks together every time and get a
score the same as the number of stones in the new stack. Please design a
algorithm to get the maximal and minimal scores after the n stacks are merged to
one stack.
*/
/*
solution: dynamical programming. Because there is a cycle, we change the cycle to an array num[] by copying the cycle again
to the end of first cycle.
dpMin[i][j] denotes the minimal score by merging stack i to stack j, dpMax[i][j] denotes the maximal score by merging
stack i to stack j.
Transition function:
dpMin[i][j] = min(dpMin[i][j] , dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]);
dpMax[i][j] = max(dpMax[i][j] , dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1]);
here sum[] is the num[] prefix sum array.
O((2*n)^3) time, O((2n)^2) space
*/
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 20;
int dpMax[N][N] , dpMin[N][N];
void StoneMinMaxScores(int num[], int len) {
int sum[N];
memset(sum , 0 , sizeof(sum));
for (int i = 1 ; i <= 2 * len ; i++) {
dpMax[i][i] = dpMin[i][i] = 0;
sum[i] = sum[i-1] + num[i];
}
for (int k = 1 ; k < len ; k++) {
for (int i = 1 ; i <= 2*len-k ; i++) {
//k is the interval length
//i is the interval begin point
//j is the interval end point
int j = i+k;
dpMax[i][j] = INT_MIN;
dpMin[i][j] = INT_MAX;
//p is the inner interval points
for (int p = i ; p < j ; p++){
//score equals num[i] + num[i+1] + ... + num[j]
int score = sum[j] - sum[i-1];
dpMax[i][j] = max(dpMax[i][j],dpMax[i][p] + dpMax[p+1][j]+score);
dpMin[i][j] = min(dpMin[i][j],dpMin[i][p] + dpMin[p+1][j]+score);
}
}
}
int minSco = INT_MAX;
int maxSco = INT_MIN;
for (int i = 1 ; i <= len ; i++) { // note the index range here
minSco = min(minSco , dpMin[i][i+len-1]);
maxSco = max(maxSco , dpMax[i][i+len-1]);
}
cout<<minSco<<" " <<maxSco<<endl;
}
int main(){
int org[] = {-1, 4, 4, 5, 9}; //does not use index 0
int len = sizeof(org)/sizeof(org[0]);
int num[N];
for (int i = 1; i <=len; i++) {
num[i] = org[i];
}
for (int i = 1; i <=len; i++) {
num[i+len] = org[i];
}
StoneMinMaxScores(num, len);
return 0;
}