UVA 10688 - The Poor Giant(区间DP)
来源:互联网 发布:excel数据分列怎么用 编辑:程序博客网 时间:2024/05/21 22:41
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
52 03 04 05 010 20
Case 1: 2Case 2: 6Case 3: 13Case 4: 22Case 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 0x3f3f3f3fconst 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;}
- Uva 10688 The Poor Giant (区间DP)
- UVA 10688 The Poor Giant (区间DP)
- uva 10688 - The Poor Giant(区间dp)
- UVA 10688 - The Poor Giant(区间DP)
- UVa 10688 The Poor Giant(区间DP)
- uva 10688 - The Poor Giant(区间DP,较难,题目难懂,状态转移难。。。)
- UVA - 10688 The Poor Giant
- uva 10688 - The Poor Giant(记忆化搜索)
- UVA - 10688 —— The Poor Giant
- UVA -10688(区间dp)
- UVa 10891 (区间DP)
- UVA 1336 Fixing the Great Wall(区间DP)
- uva 10003 Cutting Sticks (区间dp)
- uva 10003 Cutting Sticks(区间DP)
- UVA 10003 - Cutting Sticks(区间DP)
- UVA 1351 - String Compression(区间DP)
- Uva 10003-Cutting Sticks(区间DP)
- UVA 10617 Again Palindromes (区间dp)
- 来电归属地数据查询Java实现
- 办公室减肥小妙招
- 办公室减肥操
- XAMPP的常用路径
- C# 生成数据字典(Word 表格)
- UVA 10688 - The Poor Giant(区间DP)
- Linux进程间通信
- 基础练习 数列特征
- Mybatis 示例之 Association (2)
- [MFC]ListControl控件列表report类型添加列表图标
- excel VBA学习
- 【面试那些事】-S2SH三大框架的一些面试题收集
- testHello
- IE6/IE7中li底部4px的Bug