UVA 10688 - The Poor Giant(区间DP)

Problem A

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

题意:

有n个苹果,其中一个是甜的,每个苹果质量为i + k (1 <= i <= n),甜的左边肯定是苦的,右边是酸,现在选择一个吃苹果的顺序,使得能找出甜的,要把所有位置可能是甜的情况算出来,求每种情况下吃的苹果质量总和的总和。求这个总和最小。

思路:

区间dp,对于吃一个苹果,可能有3种情况,苦的,酸的,甜的,吃到苦的就往左边区间找,吃到酸的就往右边区间找,吃到甜的就停止,然后对应一个区间[l,r],他的每种甜苹果出现的位置为l - r + 1种,所以你当前吃下这个苹果,最后算总质量会被加上 w * (l - r + 1);,所以状态转移方程为

dp[l][r] = dp[l][mid - 1] + dp[mid + 1][r] + w *(l - r + 1);

代码:

#include <stdio.h>
#include <string.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define INF 0x3f3f3f3f
const int N = 505;

int t, n, K, i, j, k, dp[N][N];

int main() {
	int sum = 0;
	int cas = 0;
	scanf("%d", &t);
	while (t--) {
		memset(dp, 0, sizeof(dp));
		printf("Case %d: ", ++cas);
		scanf("%d%d", &n, &K);
		for (i = n; i >= 1; i--) {
			for (j = i + 1; j <= n; j++) {
				dp[i][j] = INF;
				for (k = i; k <= j; k++) {
					int t = (j - i + 1) * (k + K) + dp[i][k - 1] + dp[k + 1][j];
					dp[i][j] = min(dp[i][j], t);
				}
			}
		}
		printf("%d\n", dp[1][n]);
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的精简博客系统,源码+数据库+毕业论文+视频演示 当下,正处于信息化的时代,许多行业顺应时代的变化,结合使用计算机技术向数字化、信息化建设迈进。以前企业对于博客信息的管理和控制,采用人工登记的方式保存相关数据,这种以人力为主的管理模式已然落后。本人结合使用主流的程序开发技术,设计了一款基于Springboot开发的精简博客系统,可以较大地减少人力、财力的损耗,方便相关人员及时更新和保存信息。本系统主要使用B/S开发模式,在idea开发平台上,运用Java语言设计相关的系统功能模块,MySQL数据库管理相关的系统数据信息,SpringBoot框架设计和开发系统功能架构,最后通过使用Tomcat服务器,在浏览器中发布设计的系统,并且完成系统与数据库的交互工作。本文对系统的需求分析、可行性分析、技术支持、功能设计、数据库设计、功能测试等内容做了较为详细的介绍,并且在本文中也展示了系统主要的功能模块设计界面和操作界面,并对其做出了必要的解释说明,方便用户对系统进行操作和使用,以及后期的相关人员对系统进行更新和维护。本系统的实现可以极大地提高企业的工作效率,提升用户的使用体验,因此在现实生活中运用本系统具有很大的使用价值。 关键词:博客管理;Java语言;B/S结构;MySQL数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值