HDU 4960 Another OCD Patient 区间dp

区间dp。。

T^T一直感觉是n^3,看了题解看来是数据水了么。。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string.h>
#define ll long long
#define inf 1e8
inline int min(int a, int b){return a<b?a:b;}
inline void rdl(ll &n){
    n = 0;
    char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') n *= 10, n += (c - '0'),c = getchar();
}
inline void rd(int &n){
    n = 0;
    char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') n *= 10, n += (c - '0'),c = getchar();
}
#define N 5005
int dp[N][N], cost[N];
ll sum[N], a[N];
int n;
int dfs(int l, int r){
    if(dp[l][r] != -1)return dp[l][r];
    if(l < 1 || r > n)return dp[l][r] = inf;
    if(sum[l] != sum[n] - sum[r-1])return inf;
    int s = l, t = r;
    int ans = inf;
    while(1 <= s && t <= n) {
        if(sum[l] - sum[s-1] == sum[t] - sum[r-1])
        {
            int tmp = cost[l-s+1] + cost[t-r+1];
            tmp += dfs(s-1, t+1);
            ans = min(ans, tmp);
            if(s>=2)s--;
            else t++;
        }
        else if(sum[l] - sum[s-1] > sum[t] - sum[r-1])
            t++;
        else if(sum[l] - sum[s-1] < sum[t] - sum[r-1])
            s--;
    }

    return dp[l][r] = ans;
}

int main(){
    int i, j, T, k;
    while(scanf("%d",&n), n){
        sum[0] = sum[n+1] = 0;
        for(i = 1; i <= n; i++)rdl(a[i]), sum[i] = sum[i-1] + a[i];
        for(i = 1; i <= n; i++)rd(cost[i]);
        memset(dp, -1, sizeof dp);
        dp[0][n+1] = 0;
        int ans = cost[n];
        for(i = 1; i < n; i++)
            ans = min(ans, dfs(i,i+1));
        for(i = 2; i < n; i++)
            ans = min(ans, dfs(i-1,i+1));
        int s = 1, t = n;
        while( s < t){
            if(sum[s] == sum[n] - sum[t-1])
            {
                ans = min(ans, dfs(s,t) + cost[t-s-1]);
                s++;
            }
            else if(sum[s] < sum[n] - sum[t-1])
                s++;
            else
                t--;
        }
        printf("%d\n", ans);
    }
    return 0;
}
/*
5
27 9 9 9 27
0 1000 1000 1 1000

5
27 9 8 10 27
0 1000 1000 1 1000

5
27 9 8 10 27
0 1000 1 1000 1000

*/

HDU 4960 Another OCD Patient 区间dp,布布扣,bubuko.com

时间: 2024-08-02 06:58:37

HDU 4960 Another OCD Patient 区间dp的相关文章

HDU 4960 Another OCD Patient(区间dp记忆化搜索)

题目大意:给你一串数字让你判断经过若干次合并,使得这个数字串变成回文串的最小成本是多少.第一行是数字串,第二行是合并连续i个数字的成本是多少. 解题思路:区间dp,可以进行记忆化搜索,如果左边比右边和大那么右边一定是小了,右边比左边大那么左边一定小了.因为保证有解.具体不太好说,直接看代码吧. Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe

HDU 4960 Another OCD Patient 简单DP

思路: 因为是对称的,所以如果两段是对称的,那么一段的前缀和一定等于另一段的后缀和.根据这个性质,我们可以预处理出这个数列的对称点对.然后最后一个对称段是从哪里开始的,做n^2的DP就可以了. 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algori

hdu 4960 Another OCD Patient(dp)2014多校训练第9场

Another OCD Patient                                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) pat

hdu 4960 Another OCD Patient(dp)

Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 645    Accepted Submission(s): 238 Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This mornin

HDU 4960 Another OCD Patient(记忆化搜索)

HDU 4960 Another OCD Patient 题目链接 记忆化搜索,由于每个碎片值都是正数,所以每个前缀和后缀都是递增的,就可以利用twopointer去找到每个相等的位置,然后下一个区间相当于一个子问题,用记忆化搜索即可,复杂度接近O(n^2) 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3f3f3f

hdu 4960 Another OCD Patient(记忆化)

题目链接:hdu 4960 Another OCD Patient 题目大意:给定一个长度为n的序列,然后再给出n个数ai,表示合成i个数的代价.每次可以将连续的子序列和成一个数,即为序列中各个项的和.要求将给定长度n的序列变成一个回文串,一个数字只能被合成一次. 解题思路:dp[l][r]表示从l到r被和成回文串的最小代价,dp[l][r]=min(val(r?l+1),val(r?i+1)+val(j?l+1)+dp[j+1][i?1]),当i每减少1,对应的j一定变大,这一点可以减少大部分

hdu 4960 Another OCD Patient

Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 87    Accepted Submission(s): 24 Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning

hdu 5151 Sit sit sit(区间dp+排列组合)

题目链接:hdu 5151 Sit sit sit 题意: 一共有并排N个椅子, N个学生依次去坐,同时满足3个条件就不能坐下去: 1,该椅子不在最左,不在最右. 2,该椅子左右都有人坐了. 3,左右的椅子不同颜色.求最后N个人都能坐下去,有多少不同的情况. 题解: 考虑区间dp,dp[i][j] = sum(dp[i][k-1] * dp[k+1][j] * c[j - i][k - i])其中满足(v[k-1]==v[k+1]) 表示i到j区间最后来坐k位置,乘组合是因为合并这两段区间的时候

HDU 2476 String painter(区间DP啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 Problem Description There are two strings A and B with equal length. Both strings are made up of lower case letters. Now you have a powerful string painter. With the help of the painter, you can cha