hdu 4960(预处理+DP)

Another OCD Patient

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2259    Accepted Submission(s): 795


Problem Description
Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning, his children played with plasticene. They broke the plasticene into N pieces, and put them in a line. Each piece has a volume Vi. Since Xiaoji is an OCD patient, he can't stand with the disorder of the volume of the N pieces of plasticene. Now he wants to merge some successive pieces so that the volume in line is symmetrical! For example, (10, 20, 20, 10), (4,1,4) and (2) are symmetrical but (3,1,2), (3, 1, 1) and (1, 2, 1, 2) are not.

However, because Xiaoji's OCD is more and more serious, now he has a strange opinion that merging i successive pieces into one will cost ai. And he wants to achieve his goal with minimum cost. Can you help him?

By the way, if one piece is merged by Xiaoji, he would not use it to merge again. Don't ask why. You should know Xiaoji has an OCD.
 
Input
The input contains multiple test cases.

The first line of each case is an integer N (0 < N <= 5000), indicating the number of pieces in a line. The second line contains N integers Vi, volume of each piece (0 < Vi <=10^9). The third line contains N integers ai (0 < ai <=10000), and a1 is always 0.

The input is terminated by N = 0.
 
Output
Output one line containing the minimum cost of all operations Xiaoji needs.
 
Sample Input
5
6 2 8 7 1
0 5 2 10 20 0
 
Sample Output
10
 
题意:一个长为n的序列,可对任意长度的序列进行合并,代价为ai,要求花最小代价将其构造成回文序列
  对于回文序列,可以找到两个点i,j(i < j)使1-i的和与j-n的和相等,那么就可以预处理出这样的点的对数,存在 i' < i < j < j' ,L[i] = R[j],而L[i'] = R[j'],所以有L[i]-L[i'] = R[j]-R[j'],于是序列被分为5段区间
  可以从中间向两边进行dp,转移方程为 dp[i] = min(dp[i],dp[j]+p[len(i',i)]+p[len(j,j')])
 
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 5005;
int t, n, m, k, ans;
ll v[N], p[N], lt[N], rt[N];
ll L[N], R[N], dp[N];
int main(){
    while(~scanf("%d", &n) && n){
        L[0] = R[n+1] = 0;
        for(int i = 1; i <= n; i ++){
            scanf("%I64d", &v[i]);
            L[i] = L[i-1]+v[i];
        }
        for(int i = 1; i <= n; i ++){
            scanf("%I64d", &p[i]);
        }
        for(int i = n; i; i --){
            R[i] = R[i+1]+v[i];
        }
        ans = 0;
        int l = 1, r = n;
        while(l <= r+1){    //WA点,注意最中间一段区域的处理
            if(L[l-1] == R[r+1]){
                lt[++ans] = l;
                rt[ans] = r;
                l ++, r --;
            }
            while(l <= r+1 && L[l-1] < R[r+1])
                l ++;
            while(l <= r+1 && L[l-1] > R[r+1])
                r --;
        }
        for(int i = ans; i; i --){
            dp[i] = p[rt[i]-lt[i]+1];
            for(int j = i+1; j <= ans; j ++){
                dp[i] = min(dp[i], dp[j]+p[lt[j]-lt[i]]+p[rt[i]-rt[j]]);
            }
        }
        printf("%I64d
", dp[1]);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/microcodes/p/12877892.html