HDU 4283 You Are the One (interval dp)
??
N people are arranged in a row to perform on stage. Each person has a pig value. The first person to perform on stage, his dissatisfaction was (I-1) * pi.
Now there is a black room similar to a stack. You can let some people enter this black room. These people are arranged in the order of ranking,
There are two options:
(1) Let him directly perform on stage;
(2) Let him enter the dark room temporarily.
Now, please select a reasonable scheduling order to minimize the total dissatisfaction?
When I was training, I thought about greed. I thought about it later. It was not right. After reading the question, I realized that I could use the range dp to do it, or I still had insufficient posture.
Train of Thought: interval dp. For a range [I, j], the first element I in the interval may come to power at any time. If he takes power at k, no matter what
Range [I + 1, I + k-1] This K-1 element must come on stage before I,
The interval [I + k, j] must come on stage after I.
Then we can get the state transition equation.
Dp (L, R) = min (dp (L + 1, I) + (I-L) * ds [L] + dp (I + 1, R) + (su [R]-su [I]) * (I + 1-L ))
#include
#include
#include
#include
#include
#include #include
#include
#include
#include
#include
#include
#include
#define eps 1e-6 #define LL long long using namespace std; //const int maxn = 100 + 5;const int INF = 0x3f3f3f3f;int n;int d[105][105], su[105];int ds[105];int dp(int L, int R) {if(L >= R) return 0;if(d[L][R] != -1) return d[L][R];d[L][R] = INF;for(int i = L; i <= R; i++) {d[L][R] = min(d[L][R], dp(L+1, i)+(i-L)*ds[L]+dp(i+1, R)+(su[R]-su[i])*(i+1-L)); }return d[L][R];}int kase;int main() {//freopen(input.txt, r, stdin);int t; cin >> t;while(t--) {memset(d, -1, sizeof(d));scanf(%d, &n);for(int i = 1; i <= n; i++) {scanf(%d, &ds[i]);su[i] = su[i-1] + ds[i];}printf(Case #%d: %d, ++kase, dp(1, n));}return 0;}