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, 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

Hint

In the sample, there is two ways to achieve Xiaoji‘s goal.
[6 2 8 7 1] -> [8 8 7 1] -> [8 8 8] will cost 5 + 5 = 10.
[6 2 8 7 1] -> [24] will cost 20.

Source

2014 Multi-University Training Contest
9

题解及代码:

官方题解:

思路和官方是一样的,但是比赛时还是没 写出来,比赛后修修改改就过了,好忧桑啊.....

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef __int64  ll;
ll org[5010],l[5010],r[5010];
ll val[5010],d[2510];

struct node
{
    int l,r;
} pos[5010];

map<ll,int>m;
int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {
        m.clear();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&org[i]);
            if(i==1)
                l[i]=org[i];
            else l[i]=l[i-1]+org[i];
            m[l[i]]=i;
        }

        int k=1,flag=1;
        pos[0].l=pos[0].r=0;
        for(int i=n; i>=1; i--)
        {
            if(i==n)
                r[i]=org[i];
            else r[i]=r[i+1]+org[i];
            if(flag&&m.find(r[i])!=m.end())
            {
                pos[k].l=m[r[i]];
                pos[k].r=n-i+1;
                if(pos[k].l+pos[k].r>=n)
                {
                    flag=0;
                }
                k++;
            }
        }
        k--;

        /*for(int i=0;i<=k;i++)
        {
            printf("l:%d r:%d\n",pos[i].l,pos[i].r);
        }
         */
        int mid;
        if(pos[k].l+pos[k].r==n)
        {
            mid=0;
        }
        else
        {
            k--;
            mid=n-pos[k].r-pos[k].l;
        }
        //printf("k:%d mid:%d\n",k,mid);

        val[0]=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%I64d",&val[i]);
        }

        ll ans=-1;
        d[0]=0;
        for(int i=1; i<=k; i++)
        {
            d[i]=val[pos[i].l]+val[pos[i].r];
            for(int j=1; j<i; j++)
            {
                d[i]=min(d[j]+val[pos[i].l-pos[j].l]+val[pos[i].r-pos[j].r],d[i]);
            }
        }

        for(int i=0; i<=k; i++)
        {
            if(ans==-1||ans>d[i]+val[mid+pos[k].l-pos[i].l+pos[k].r-pos[i].r])
                ans=d[i]+val[mid+pos[k].l-pos[i].l+pos[k].r-pos[i].r];
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

hdu 4960 Another OCD Patient

时间: 2024-10-18 11:33:33

hdu 4960 Another OCD Patient的相关文章

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 简单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

区间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

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记忆化搜索)

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

2014多校训练九(HDU 4960 HDU 4961 HDU 4965 HDU 4968 HDU 4969 HDU 4970)

HDU 4960 Another OCD Patient 题意:给你一串数字  相邻x个数字合并成一个数字(相加)有一定代价  问  最少花费多少使得串变成回文串 思路: 读完题感觉像dp  数据范围也像  就开始想怎么表示状态  最简单的应该想到dp[i][j]表示i到j区间变成回文串的最小花费  状态想好了想做法  考虑将串分成AAAABBBBBBBCCC三段  即所有A合成一个数字  C也是  而且A和C相等  那么B串就变成了子问题  但是A和C是不是都要枚举呢?  这个串所有元素都是正

HDU 4960 (水dp)

Another OCD Patient 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 Xi