题目地址:http://ac.jobdu.com/problem.php?pid=1453

题目描述:

Tino wrote a long long story. BUT! in Chinese...
So I have to tell you the problem directly and discard his long long story. That is tino want to carry some oranges with "Carrying pole", and he must make two side of the Carrying pole are the same weight. Each orange have its' weight. So greedy tino want to
know the maximum weight he can carry.

输入:

The first line of input contains a number t, which means there are t cases of the test data.
for each test case, the first line contain a number n, indicate the number of oranges.
the second line contains n numbers, Wi, indicate the weight of each orange
n is between 1 and 100, inclusive. Wi is between 0 and 2000, inclusive. the sum of Wi is equal or less than 2000.

输出:

For each test case, output the maximum weight in one side of Carrying pole. If you can't carry any orange, output -1. Output format is shown in Sample Output.

样例输入:
1
5
1 2 3 4 5
样例输出:
Case 1: 7

状态转移方程:

dp[i][j]表示前i个柑橘被选择(每个柑橘可能放到第一堆或者第二堆)后,第一堆比第二堆中j时(当j为负数时表示第二堆比第一堆重),两堆的最大重量和。

#include <stdio.h>

#define OFFSET 2000
#define INF 0x7fffffff int main(void){
int test, num;
int weight[101];
int dp[2][4001];
int i, j;
int zero, cnt;
int tmp1, tmp2;
int cas = 0; scanf ("%d", &test);
while (test-- != 0){
scanf ("%d", &num);
cnt = 0;
zero = 0;
for (i=1; i<=num; ++i){
scanf ("%d", &weight[++cnt]);
if (weight[cnt] == 0){
--cnt;
zero = 1;
}
}
num = cnt;
for (i=-2000; i<=2000; ++i){
dp[0][i+OFFSET] = -INF;
}
dp[0][0+OFFSET] = 0;
for (i=1; i<=num; ++i){
for (j=-2000; j<=2000; ++j){
tmp1 = -INF;
tmp2 = -INF;
if (j + weight[i] <= 2000 && dp[(i-1)%2][j+weight[i]+OFFSET] != -INF){
tmp1 = dp[(i-1)%2][j+weight[i]+OFFSET] + weight[i];
}
if (j - weight[i] >= -2000 && dp[(i-1)%2][j-weight[i]+OFFSET] != -INF){
tmp2 = dp[(i-1)%2][j-weight[i]+OFFSET] + weight[i];
}
if (tmp1 < tmp2)
tmp1 = tmp2;
if (tmp1 < dp[(i-1)%2][j+OFFSET])
tmp1 = dp[(i-1)%2][j+OFFSET];
dp[i%2][j+OFFSET] = tmp1;
}
}
printf ("Case %d: ", ++cas);
if (dp[num%2][0+OFFSET] == 0){
puts (zero == 1 ? "0" : "-1");
}
else
printf ("%d\n", dp[num%2][0+OFFSET]/2);
} return 0;
}

九度OJ 1453 Greedy Tino -- 动态规划的更多相关文章

  1. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  2. 九度OJ 1499 项目安排 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1499 题目描述: 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时 ...

  3. 九度OJ 1547 出入栈 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1547 题目描述: 给定一个初始为空的栈,和n个操作组成的操作序列,每个操作只可能是出栈或者入栈. 要求在操作序列的 ...

  4. 九度OJ 1410 垒积木 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1410 题目描述: 给你一些长方体的积木,问按以下规则能最多垒几个积木. 1 一个积木上面最多只能垒另一个积木. 2 ...

  5. 九度OJ 1131 合唱队形 -- 动态规划(最长递增子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1131 题目描述: N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学不交换位置就能排成合 ...

  6. 九度OJ 1452 搬寝室 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1452 题目描述: 搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3 ...

  7. 九度OJ 1086 最小花费--动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1086 题目描述: 在某条线路上有N个火车站,有三种距离的路程,L1,L2,L3,对应的价格为C1,C2,C3.其对 ...

  8. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  9. 【九度OJ】题目1170:找最小数 解题报告

    [九度OJ]题目1170:找最小数 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1170 题目描述: 第一行输入一个数n,1 < ...

随机推荐

  1. HW3.28

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  2. Delphi- 数据加密和解密

    Delphi进行数据加密,在数据库方面经常要使用到.从网上转载过来的,以后会经常会用到. 一.MD5加密算法 在C#/.Net里提供了MD5加密的类库.在Delphi中没有.只能自己建一个新的单位,将 ...

  3. MySQL 统计信息

    200 ? "200px" : this.width)!important;} --> 介绍 数据库维护统计信息的目的主要是为了优化器进行更好的执行优化,首先统计信息是建立在 ...

  4. springboot 详细配置2

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

  5. get-random生成电话号码

    "138"+((0..9|Get-Random -count 10) -join $null) From:http://blog.csdn.net/shrekz/article/d ...

  6. iOS设计模式之生成器

    iOS设计模式之生成器 1.生成器模式的定义 (1): 将一个复杂的对象的构件与它的表示分离,使得相同的构建过程能够创建不同的表示 (2): 生成器模式除了客户之外还包括一个Director(指导者) ...

  7. hi3531 SDK 编译 uboot, 改动PHY地址, 改动 uboot 參数 .

    一,编译uboot SDK文档写得比較清楚了,写一下须要注意的地方吧. 1. 之前用SDK里和别人给的已经编译好的uboot,使用fastboot工具都刷不到板子上.最后自己用SDK里uboot源代码 ...

  8. 职责链模式vs状态模式区别

    状态模式在具体状态里设置了下一状态. 而职责链模式是在客户端代码里设置了下一状态的处理对象. 如果状态模式里的任何一环缺失,将导致事情无法进行下去.职责链模式的链式在客户端连接的,也就是说,如果我们请 ...

  9. java08 Set

    Set: 无序不可重复,重复则覆盖,判断是不是重复也是通过equals方法判断的.HashSet和TreeSet,HashSet底层是HashMap. public static void main( ...

  10. ltrace killed by SIGTRAP

    [Ltrace-devel] ltrace stucks with pthreads Heiko Carstens heiko.carstens at de.ibm.comFri Apr 14 11: ...