题目地址: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. poj 2367 Genealogical tree (拓扑排序)

    火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...

  2. Linux内存寻址之分页机制

    在上一篇文章Linux内存寻址之分段机制中,我们了解逻辑地址通过分段机制转换为线性地址的过程.下面,我们就来看看更加重要和复杂的分页机制. 分页机制在段机制之后进行,以完成线性—物理地址的转换过程.段 ...

  3. hdoj 2955 Robberies

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. Ubuntu16.04编译安装php

    #Ubuntu16.04编译安装php Ubuntu16.04上面搭建基于Nginx的php服务.Nginx使用apt直接安装的. sudo apt install nginx php的安装部署步骤主 ...

  5. git 秘钥的生成

    在命令查看自己的秘钥还是公钥 cat .ssh/id_rsa.pub/cat .ssh/id_rsa

  6. [ES6] 22. Const

    'const' keyword is for creating a read only variable, something you can never change once created. ' ...

  7. zoj 3672 思考题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4915 真是老了.脑子不会动了,可是事实上就算如今搜了题解A了,还是没总结出思 ...

  8. ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限

    开发了一个导入TXT文件的功能,执行过程中出错.提示:.....ASP.NET 未被授权访问所请求的资源.请考虑授予 ASP.NET 请求标识访问此资源的权限.ASP.NET 有一个在应用程序没有模拟 ...

  9. debian安装mono环境

    官网 http://pkg-mono.alioth.debian.org/ 在/etc/apt/sources.list 加上 deb http://debian.meebey.net/pkg-mon ...

  10. 手机端轻应用模拟原生的下拉刷新效果(JavaScript)

    方案一:使用iscoll等有下拉功能的框架. 分析:因为项目的结构已经基本完成,再使用框架,会与原来的结构互相影响: 方案二:用JavaScript.Jquery写. 分析:可能没有直接使用框架的效果 ...