九度OJ 1453 Greedy Tino -- 动态规划
题目地址: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 -- 动态规划的更多相关文章
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1499 项目安排 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1499 题目描述: 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时 ...
- 九度OJ 1547 出入栈 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1547 题目描述: 给定一个初始为空的栈,和n个操作组成的操作序列,每个操作只可能是出栈或者入栈. 要求在操作序列的 ...
- 九度OJ 1410 垒积木 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1410 题目描述: 给你一些长方体的积木,问按以下规则能最多垒几个积木. 1 一个积木上面最多只能垒另一个积木. 2 ...
- 九度OJ 1131 合唱队形 -- 动态规划(最长递增子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1131 题目描述: N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学不交换位置就能排成合 ...
- 九度OJ 1452 搬寝室 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1452 题目描述: 搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3 ...
- 九度OJ 1086 最小花费--动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1086 题目描述: 在某条线路上有N个火车站,有三种距离的路程,L1,L2,L3,对应的价格为C1,C2,C3.其对 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 【九度OJ】题目1170:找最小数 解题报告
[九度OJ]题目1170:找最小数 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1170 题目描述: 第一行输入一个数n,1 < ...
随机推荐
- hdoj 5522 Numbers
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5522 水题:暴力过 #include<stdio.h> #include<strin ...
- [html]js打开指定页面
1.在当前窗口打开 location.href = "http://www.baidu.com"; 2.可以设置开发方式 window.open("http://www. ...
- MSSQLSERVER数据库- 字符串分割函数返回类型表
遇到这样一个问题,存储在数据库的数据是一串字符串如:1,2,3,4,5,6.想把这串字符串进行转变成一个表格,如下: 1 2 3 4 5 6 就是这样一个问题,有人同事,写了一个这样的封装函数,这样就 ...
- Java调用yahoo!API获取天气数据
先把代码复制上来,以后再做补充 package com.weather.test; import java.io.InputStream; import java.net.URL; import ja ...
- phpcms 源码分析五:文件缓存实现
这次是逆雪寒的文件缓存实现代码分析: /* [/php] PHPCMS的文本缓存实现: [php] <?php /* 这个文件里面全是有关生成文本缓存的函数.文本缓存是个好东西.一般的项目,我们 ...
- 从 setNeedsLayout 说起
本文从 setNeedsLayout 这个方法说起,分享与其相关的 UIKit 视图交互.使用场景等内容. UIKit 为 UIView 提供了这些方法来进行视图的更新与重绘: public func ...
- android开发之shape详解
很多时候,使用shape能够实现的效果,你用一张图片也能够实现,但问题是一张图片无论你怎么压缩,它都不可能比一个xml文件小,因此,为了获得一个高性能的手机App,我们在开发中应该遵循这样一个原则:能 ...
- 下载好一个android软件之后,怎样自动提示安装?
最近在做毕设,里面牵涉到版本更新,当有新版本时可以下载新版本,下载完成之后提示安装.那么怎么实现下载完成之后提示安装呢? 直接上代码吧: File mFile = new File(Environme ...
- 如何运用管理员身份运行cmd窗口?
所有程序 → 附件 → 命令行提示符 → 鼠标右键“以管理员身份运行”.
- 从数组中随机取n条不重复的数据
工作中经常遇到有关数组的一些操作 1. 从数据中随机取n条不重复的数据 (PS:下面的S.each是KISSY.each方法,大家可以改为for循环) /* 1 从数组arr中随机取n条不重复的数据 ...