当前位置: 代码迷 >> 综合 >> uva 10688 - The Poor Giant(区间DP,较难,题目难懂,状态转移难。。。)
  详细解决方案

uva 10688 - The Poor Giant(区间DP,较难,题目难懂,状态转移难。。。)

热度:96   发布时间:2024-01-13 20:30:11.0

1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1629

2、题目大意:

有n个苹果,和一个数k,第i个苹果的重量是k+i(1<=i<=n). 已知其中只有一个苹果是甜的,
所有比它重量轻的都是苦的,比它重的都是酸的。

为了要找出甜的苹果,就要去一个一个地吃它,且吃了咬了苹果就必须把它吃完,不管苹果是苦的还是酸的。
我们可以选择一个最佳策略,为了找到甜苹果吃总重量最少。
假设n=4, k=0,那么4个苹果的重量为1,2,3,4,假设先吃 #2个苹果,
如果#1是甜的,那么吃了2时就是酸的,那么就可以确定1是甜的了,共吃重量=2
如果#2是甜的,那么吃的重量=2
如果#3是甜的,那么2是酸的,可以推测甜的在(3,4)中的一个,然后吃3, 就可以确定哪个是甜的,共吃重量=2+3=5
如果#4是甜的,那么方案和上面一样,共吃重量=5
其实就类似二分法。
总共重量 = 2+2+5+5 = 14
3、分析

设dp[i][j]为i-j区间内的最佳方案下的重量

我们可以选择i-j中的一个k吃掉,那么对于i-----k-1,k+1-----j,每个吃的都得加上v[k],

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

4、题目:

The Poor Giant

Input: Standard Input
Output: Standard Output
Time Limit: 1 second

On a table, there are n apples, the i-th apple has the weight k+i(1<=i<=n). Exactly one of the apples is sweet, lighter apples are all bitter, while heavier apples are all sour. The giant wants to know which one is sweet, the only thing he can do is to eat apples. He hates bitter apples and sour apples, what should he do?
For examples, n=4, k=0, the apples are of weight 1, 2, 3, 4. The gaint can first eat apple #2.
if #2 is sweet, the answer is #2
if #2 is sour, the answer is #1
if #2 is bitter, the answer might be #3 or #4, then he eats #3, he'll know the answer regardless of the taste of #3
The poor gaint should be prepared to eat some bad apples in order to know which one is sweet. Let's compute the total weight of apples he must eat in all cases.
#1 is sweet: 2
#2 is sweet: 2
#3 is sweet: 2 + 3 = 5
#4 is sweet: 2 + 3 = 5
The total weights = 2 + 2 + 5 + 5 = 14.
This is not optimal. If he eats apple #1, then he eats total weight of 1, 3, 3, 3 when apple #1, #2, #3 and #4 are sweet respectively. This yields a solution of 1+3+3+3=13, beating 14. What is the minimal total weight of apples in all cases?

Input

The first line of input contains a single integer t(1<=t<=100), the number of test cases. The following t lines each contains a positive integer n and a non-negative integer k(1<=n+k<=500).

Output

For each test case, output the minimal total weight in all cases as shown in the sample output.

 

Sample Input

Sample Output

5
2 0
3 0
4 0
5 0
10 20
Case 1: 2
Case 2: 6
Case 3: 13
Case 4: 22
Case 5: 605


Problem setter: Rujia Liu, Member of Elite Problemsetters' Panel

 

5、Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x7ffffff
int v[505];
int dp[505][505];
int main()
{int t,n,k,cas=0;scanf("%d",&t);while(t--){cas++;scanf("%d%d",&n,&k);for(int i=1;i<=n;i++)v[i]=i+k;memset(dp,0,sizeof(dp));for(int d=2;d<=n;d++){for(int i=1;i+d-1<=n;i++){int s=i,e=i+d-1;dp[s][e]=INF;for(int j=s;j<=e;j++){dp[s][e]=min(dp[s][e],dp[s][j-1]+dp[j+1][e]+v[j]*d);}}}printf("Case %d: %d\n",cas,dp[1][n]);}return 0;
}