【2017 ICPC亚洲区域赛北京站 J】Pangu and Stones(区间dp)

In Chinese mythology, Pangu is the first living being and the creator of the sky and the earth. He woke up from an egg and split the egg into two parts: the sky and the earth.

At the beginning, there was no mountain on the earth, only stones all over the land.

There were N piles of stones, numbered from 1 to N. Pangu wanted to merge all of them into one pile to build a great mountain. If the sum of stones of some piles was S, Pangu would need S seconds to pile them into one pile, and there would be S stones in the new pile.

Unfortunately, every time Pangu could only merge successive piles into one pile. And the number of piles he merged shouldn‘t be less than L or greater than R.

Pangu wanted to finish this as soon as possible.

Can you help him? If there was no solution, you should answer ‘0‘.

Input

There are multiple test cases.

The first line of each case contains three integers N,L,R as above mentioned (2<=N<=100,2<=L<=R<=N).

The second line of each case contains N integers a1,a2 …aN (1<= ai  <=1000,i= 1…N ), indicating the number of stones of  pile 1, pile 2 …pile N.

The number of test cases is less than 110 and there are at most 5 test cases in which N >= 50.

Output

For each test case, you should output the minimum time(in seconds) Pangu had to take . If it was impossible for Pangu to do his job, you should output  0.

Sample Input

3 2 2
1 2 3
3 2 3
1 2 3
4 3 3
1 2 3 4

Sample Output

9
6
0

题意:

n个石子堆排成一排,每次可以将连续的[L,R]堆石子合并成一堆,花费为要合并的石子总数。求将所有石子合并成一堆的最小花费,如无法实现则输出0。

思路:

dp[i][j][k]表示区间[i, j]分成k堆的最小代价,转移有

k=1时:

dp[i][j][1]=min(dp[i][j][1],dp[i][j][q]+sum[j]-sum[i-1]) 

k>1时:

dp[i][j][q]=min(dp[i][j][q],dp[i][k][q-1]+dp[k+1][j][1]) 

#include<bits/stdc++.h>
using namespace std;
#define MAX 105
#define INF 0x3f3f3f3f
int sum[MAX],dp[MAX][MAX][MAX];
int main()
{
    int n,l,r,i,j,k;
    while(scanf("%d%d%d",&n,&l,&r)!=EOF)
    {
        memset(dp,INF,sizeof(dp));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&sum[i]);
            dp[i][i][1]=0;
            sum[i]+=sum[i-1];
        }
        int len;
        for(len=l;len<=r;len++)    //merge长度 len[l,r]
        {
            for(i=1;i+len-1<=n;i++)//merge范围 [i,i+len-1]
            {
                j=i+len-1;
                dp[i][j][len]=0;
                dp[i][j][1]=sum[j]-sum[i-1];
            }
        }

        int q;
        for(len=2;len<=n;len++)    //merge长度 len[2,n]
        {
            for(i=1;i+len-1<=n;i++)//merge范围 [i,i+len-1]
            {
                j=i+len-1;
                for(k=i;k<j;k++)
                    for(q=2;q<=len;q++)
                        dp[i][j][q]=min(dp[i][j][q],dp[i][k][q-1]+dp[k+1][j][1]);
                for(q=l;q<=r;q++)
                    dp[i][j][1]=min(dp[i][j][1],dp[i][j][q]+sum[j]-sum[i-1]);
            }
        }

        if(dp[1][n][1]<INF)
            printf("%d\n",dp[1][n][1]);
        else printf("0\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/kannyi/p/9962443.html

时间: 2024-10-09 01:39:08

【2017 ICPC亚洲区域赛北京站 J】Pangu and Stones(区间dp)的相关文章

【2016 ICPC亚洲区域赛北京站 E】What a Ridiculous Election(BFS预处理)

Description In country Light Tower, a presidential election is going on. There are two candidates,  Mr. X1 and Mr. X2, and both of them are not like good persons. One is called a liar and the other is called a maniac. They tear(Chinese English word,

2015 ACM / ICPC 亚洲区域赛总结(长春站&amp;&amp;北京站)

队名:Unlimited Code Works(无尽编码)  队员:Wu.Wang.Zhou 先说一下队伍阵容:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之前只学了大部分图论内容,以及一些数据结构.动态规划等等,但是还不能熟练运用... ... 先从长春站说起吧... ... 长春站是我加入ACM以来参加的第一场ICPC,因此无比的激动!从杭州出发,乘了整整24小时的火车,终于到达了长春:第一次到大东北,沿途的风景与南方有很大差异,体会到了东北的寒

2014ACM/ICPC亚洲区域赛牡丹江站总结

我在集训队里面也就一般水平,这次学校史无前例的拿到了8个名额,由于大三的只有两个队伍,所以我们13及能分到名额,由于13及人数很多,组长就按照谁在oj上面a的题多就让谁去,我和tyh,sxk,doubleq幸运的在大二就有机会参加亚洲现场赛,非常激动.牡丹江赛区是我和sxk和doubleq组成rainbow战队,我们对这次区域赛其实就是去张张见识,增加大赛经验(外加公费旅游2333),可是当我们真正来到赛场的时候不知道为上面我非常渴望拿一块牌子.第一天热身赛,double迅速切下水题,我一直再弄

2014ACM/ICPC亚洲区域赛牡丹江站汇总

球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doubleq运的在大二就有机会參加亚洲现场赛,非常激动.牡丹江赛区是我和sxk和doubleq组成rainbow战队,我们对这次区域赛事实上就是去张张见识,添加大赛经验(外加公费旅游2333),但是当我们真正来到赛场的时候不知道为上面我非常渴望拿一块牌子. 第一天热身赛,double迅速切下水题.我一直再

2014ACM/ICPC亚洲区域赛牡丹江站D和K题

Known Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expre

2017 ICPC西安区域赛 A - XOR (线段树并线性基)

链接:https://nanti.jisuanke.com/t/A1607 题面: Consider an array AA with n elements . Each of its element is A[i]A[i] (1 \le i \le n)(1≤i≤n) . Then gives two integers QQ, KK, and QQ queries follow . Each query , give you LL, RR, you can get ZZ by the foll

2014ACM/ICPC亚洲区域赛牡丹江现场赛总结

不知道如何说起-- 感觉还没那个比赛的感觉呢?现在就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,因为要来比赛,所以那些事情队友的国奖不能答辩,自己的励志奖班里乱搞要投票,自己又不在,真是无语了--烦得要死,然后在这些事情还没处理好之前我们就这样10号中午从地大去北京站上火车了--那时真感觉这场带着这种心情来现场赛感觉要打铁了-- 然后10号晚上队友的国奖让琦神帮答辩完了,得国奖无疑了,然后自己的励志奖也定下来一定得了,在火车上的我们也松了一口气,不能因为来比赛国奖励志奖都不得是不--

【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)

Problem Description Consider a un-rooted tree T which is not the biological significance of tree or plant, but a tree as an undirected graph in graph theory with n nodes, labelled from 1 to n. If you cannot understand the concept of a tree here, plea

Digit sum (第 44 届 ACM/ICPC 亚洲区域赛(上海)网络赛)进制预处理水题

131072K A digit sum S_b(n)Sb?(n) is a sum of the base-bb digits of nn. Such as S_{10}(233) = 2 + 3 + 3 = 8   S10?(233)=2+3+3=8, S_{2}(8)=1 + 0 + 0 = 1S2?(8)=1+0+0=1, S_{2}(7)=1 + 1 + 1 = 3S2?(7)=1+1+1=3. Given NN and bb, you need to calculate \sum_{n