博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 10688 - The Poor Giant(区间DP,较难,题目难懂,状态转移难。。。)
阅读量:4037 次
发布时间:2019-05-24

本文共 2870 字,大约阅读时间需要 9 分钟。

1、

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

52 03 04 05 010 20
Case 1: 2Case 2: 6Case 3: 13Case 4: 22Case 5: 605


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

 

5、Ac代码:

#include
#include
#include
using namespace std;#define INF 0x7ffffffint 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;}

 

转载地址:http://egddi.baihongyu.com/

你可能感兴趣的文章
springboot+springsecurity+jwt进行系统权限开发
查看>>
使用轻量级工具emoji-java处理emoji表情字符
查看>>
排序算法的C语言实现C代码
查看>>
c语言快排函数调用方法模板
查看>>
c语言实现多行输入输出数据
查看>>
查找算法
查看>>
C语言单链表实现
查看>>
SQL基本命令集合整理
查看>>
QT中json的生成和解析
查看>>
std::function 和 std::bind 的简单例子
查看>>
CFormView简介
查看>>
Visual Studio 2010 与 VC++ 6.0 的操作差异(一)之对话框中添加OnInitDialog()函数
查看>>
VC的MFC里面控件的ID使用ID_XXXXX和IDR_XXXXX的区别
查看>>
VC++ 获取ListControl选中行
查看>>
用VC++实现应用程序窗口的任意分割(2)
查看>>
“class”类型重定义,include(头文件)重复加载 QT /c++
查看>>
MFC框架类、文档类、视图类相互访问的方法
查看>>
<转>文档视图指针互获
查看>>
C++中头文件相互包含的几点问题
查看>>
内存设备描述表
查看>>