hdu4283 区间dp

 1 //Accepted    300 KB    0 ms
 2 //区间dp
 3 //dp[i][j] 表示i到j第一个出场的最小diaosizhi
 4 //对于i到j考虑元素i
 5 //(1)i第一个出场,diaosizhi为 dp[i+1][j]+sum(i+1--j)
 6 //(2)i不是第一个出场,而是第k个出场,则i+1到k+i-1这段区间第一个出场,k+i到j第k+1个出场
 7 //diaoshizhi为dp[i+1][i+k-1] + a[i]*(k-1) + (dp[i+k][j]+k*sum(i+k--j))
 8 //sum为一段区间的diaosizhi的和,考虑k+i到j第k+1个出场相当于k+i到j第一个出场再加上k*(sum(i+k--j))
 9 #include <cstdio>
10 #include <cstring>
11 #include <iostream>
12 using namespace std;
13 const int imax_n = 105;
14 int dp[imax_n][imax_n];
15 int a[imax_n],sum[imax_n];
16 int n;
17 int min(int a,int b)
18 {
19     return a<b?a:b;
20 }
21 void Dp()
22 {
23     memset(dp,0,sizeof(dp));
24     for (int l=2;l<=n;l++)
25     {
26         for (int i=1;i<=n;i++)
27         {
28             int j=i+l-1;
29             if (j>n) break;
30             dp[i][j]=dp[i+1][j]+sum[j]-sum[i];
31             for (int k=1;k<=l;k++)
32             {
33                 dp[i][j]=min(dp[i][j],dp[i+1][i+k-1]+a[i]*(k-1)+dp[i+k][j]+k*(sum[j]-sum[i+k-1]));
34             }
35         }
36     }
37 }
38 int main()
39 {
40     int T;
41     scanf("%d",&T);
42     for (int t=1;t<=T;t++)
43     {
44         scanf("%d",&n);
45         sum[0]=0;
46         for (int i=1;i<=n;i++)
47         {
48             scanf("%d",&a[i]);
49             sum[i]=sum[i-1]+a[i];
50         }
51         Dp();
52         printf("Case #%d: %d\n",t,dp[1][n]);
53     }
54     return 0;
55 }

 1 //Accepted    4792 KB    281 ms
 2 //区间dp
 3 //dp[i][j][k] i到j整段区间在第k个出去时的最小花费
 4 //考虑区间中的第一个元素i,有一下两种情况:
 5 //(1)i在第k个出去,则i+1到j在第k+1个出去即dp[i+1][j][k+1]
 6 //(2)i不在第k个出去,则i后必有一段在第k个出去,假设这段为i+1到m
 7 //则有dp[i+1][m][k]+a[i]*(k+m-i)+dp[m+1][j][k+m-i+1]
 8 #include <cstdio>
 9 #include <cstring>
10 #include <iostream>
11 using namespace std;
12 const int imax_n = 105;
13 const int inf = 100000000;
14 int dp[imax_n][imax_n][imax_n];
15 int a[imax_n];
16 int n;
17 int min(int a,int b)
18 {
19     return a<b?a:b;
20 }
21 void Dp()
22 {
23     memset(dp,0,sizeof(dp));
24     for (int i=1;i<=n;i++)
25     {
26         for (int k=1;k<=n;k++)
27         {
28             dp[i][i][k]=(k-1)*a[i];
29         }
30     }
31     for (int i=1;i<n;i++)
32     {
33         for (int k=1;k<=n;k++)
34         {
35             dp[i][i+1][k]=min((k-1)*a[i]+k*a[i+1],k*a[i]+(k-1)*a[i+1]);
36         }
37     }
38     for (int l=3;l<=n;l++)
39     {
40         for (int i=1;i<=n;i++)
41         {
42             int j=i+l-1;
43             if (j>n) break;
44             for (int k=1;k<=n;k++)
45             {
46                 dp[i][j][k]=inf;
47                 dp[i][j][k]=min(dp[i][j][k],dp[i+1][j][k+1]+(k-1)*a[i]);
48                 for (int m=i+1;m<=j;m++)
49                 {
50                     dp[i][j][k]=min(dp[i][j][k],dp[i+1][m][k]+a[i]*(k+m-i-1)+dp[m+1][j][k+1+m-i]);
51                 }
52             }
53         }
54     }
55 }
56 int main()
57 {
58     int T;
59     scanf("%d",&T);
60     for (int t=1;t<=T;t++)
61     {
62         scanf("%d",&n);
63         for (int i=1;i<=n;i++)
64         scanf("%d",&a[i]);
65         Dp();
66         printf("Case #%d: %d\n",t,dp[1][n][1]);
67     }
68     return 0;
69 }

时间: 2024-10-12 15:51:52

hdu4283 区间dp的相关文章

hdu4283(区间dp)

You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1585    Accepted Submission(s): 756 Problem Description The TV shows such as You Are the One has been very popular. In order to me

区间DP基础篇之 HDU4283——You Are the One(非诚勿扰)

题目大意: 有n个男屌丝事先按1,2,3,,,,,,n的顺序排好,每个人都有一个不开心值unhappy[i],如果第i个人第k个上台找对象,那么该屌丝男的不开心值就会为(k-1)*unhappy[i],因为在他前面有k-1个人嘛,导演为了让所有男屌的总不开心值最小,搞了一个小黑屋,可以通过小黑屋来改变男屌的出场顺序 注意:这个小黑屋是个栈,男屌的顺序是排好了的,但是可以通过入栈出栈来改变男屌的出场顺序 解题思路:(操度娘所知~度娘你好腻害) dp[i][j]表示区间[i,j]的最小总不开心值 把

HDU-4283 You Are the One (区间DP)

Problem Description The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there ar

HDU4283:You Are the One(区间DP)

Problem Description The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there ar

hdu4283---You Are the One(区间dp)

Problem Description The TV shows such as You Are the One has been very popular. In order to meet the need of boys who are still single, TJUT hold the show itself. The show is hold in the Small hall, so it attract a lot of boys and girls. Now there ar

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

黑书例题 Fight Club 区间DP

题目可以在bnuoj.soj等OJ上找到. 题意: 不超过40个人站成一圈,只能和两边的人对战.给出任意两人对战的输赢,对于每一个人,输出是否可能是最后的胜者. 分析: 首先序列扩展成2倍,破环成链. dp[i][j]表示i和j能够相遇对打,那么dp[i][i+n]为真代表可以成为最后胜者. 枚举中间的k,若i和j都能和k相遇,且i和j至少一人能打赢k,那么i和j可以相遇. 复杂度o(n^3) 1 #include<cstdio> 2 #include<cstring> 3 usi

算法复习——区间dp

感觉对区间dp也不好说些什么直接照搬讲义了2333 例题: 1.引水入城(洛谷1514) 这道题先开始看不出来到底和区间dp有什么卵关系···· 首先肯定是bfs暴力判一判可以覆盖到哪些城市····无解直接输出···有解得话就要想想了···· 这道题关键是要发现··如果一个蓄水池所在城市可以覆盖到一些沙漠城市···那么这些沙漠城市肯定是一段····不然假设有一个城市是断开的而两边都被同一个蓄水池流出的水覆盖,这个城市四周的城市都肯定比它矮···(不理解举个反例吧···反正我举不出来)···然后就

合并石子 区间dp水题

合并石子 链接: nyoj 737 描述: 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N-1次合并后成为一堆.求出总的代价最小值. tags:最基本的区间dp,这题范围小,如果n大一些,还是要加个平行四边行优化. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring&g