HDU 3578 Greedy Tino(双塔DP)
Greedy Tino
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1194 Accepted Submission(s): 393
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.
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.
1
5
1 2 3 4 5
Case 1: 7双塔DP注意如果有一个橘子是0,那么就符合条件dp[i][j] 表示第i个橘子,两边差值#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
int n;
int a[105];
int dp[105][4005];
int main()
{
int t;
scanf("%d",&t);
int cas=0;
while(t--)
{
scanf("%d",&n);
int num=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==0)
i--,n--,num++; }
memset(dp,-1,sizeof(dp));
dp[0][0+2000]=0;
for(int i=1;i<=n;i++)
{
memcpy(dp[i],dp[i-1],sizeof(dp[i]));
for(int j=-2000;j<=2000;j++)
{ if(dp[i-1][j+2000]==-1) continue; if(j<0)
{
dp[i][j-a[i]+2000]=max(dp[i][j-a[i]+2000],dp[i-1][j+2000]+a[i]);
dp[i][j+a[i]+2000]=max(dp[i][j+a[i]+2000],dp[i-1][j+2000]+max(0,j+a[i]));
}
else
{ dp[i][j+a[i]+2000]=max( dp[i][j+a[i]+2000],dp[i-1][j+2000]+a[i]);
dp[i][j-a[i]+2000]=max(dp[i][j-a[i]+2000],dp[i-1][j+2000]+max(0,a[i]-j));
}
}
} if(dp[n][2000])
printf("Case %d: %d\n",++cas,dp[n][2000]);
else
{
if(num>=1)
printf("Case %d: %d\n",++cas,0);
else
printf("Case %d: %d\n",++cas,-1);
}
}
return 0;
}
HDU 3578 Greedy Tino(双塔DP)的更多相关文章
- 题目1453:Greedy Tino(dp题目)
题目链接:http://ac.jobdu.com/problem.php?pid=1453 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- 九度OJ 1453 Greedy Tino -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1453 题目描述: Tino wrote a long long story. BUT! in Chinese... ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
- hdu 4568 Hunter 最短路+dp
Hunter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- ZOJ2401 Zipper 双塔式 DP(双塔DP)
第二次遇到双塔DP,再写一下. (flag是为了避免memset多次导致的时间浪费) #include<cstdio> #include<cstdlib> #include&l ...
- HDU 1231.最大连续子序列-dp+位置标记
最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1078 FatMouse and Cheese ( DP, DFS)
HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...
随机推荐
- PHPCMS V9使用中的一些心得体会
官方演示站:http://v9.demo.phpcms.cn/ 在线帮助文档:http://v9.help.phpcms.cn/ 案例展示:互联网 http://www.i-busi.com 1. ...
- 64位Windows系统如何配置32位ODBC数据源
在64位Windows系统中,默认“数据源(ODBC)”是64位的,包括“控制面板->管理工具->数据源 ”或在“运行”中直接运行“ODBCAD32”程序.如果客户端是32位应用程序,仍然 ...
- 点滴积累【JS】---JS小功能(checkbox实现全选和全取消)
效果: 代码: <head runat="server"> <title></title> <script type="text ...
- atitit.软件设计模式大的总结attialx总结
atitit.软件设计模式大的总结attialx总结 1. 设计模式的历史3 2. 设计模式的数量(253个)3 3. 设计模式的结构4 3.1. 应用场景and条件Context4 3.2. Pro ...
- KVC之-setValue:forKey:方法实现原理与验证
KVC之-setValue:forKey:方法实现原理与验证 - (void)setValue:(id)value forKey:(NSString *)key方法,实现原理与验证 功能:使用一个字符 ...
- node-nginx二级域名添加配置
首先在阿里云配置解析域名 指向端口为3200的nodejs服务,在nginx/conf.d下增加文件chat.conf,内容如下: server { listen 80; server_name ww ...
- 解决:std::ostream operator<< should have been declared inside 'xxx'
用VS的NMAKE构建,不会报错,但是用GNU MAKE构建,就会报错.(尝试删除Toast.h中第24行的声明) 因此在遇到类似的情况的时候,记得不仅class里面要有friend声明,namesp ...
- 完工尚需绩效指数 TCPI
转自:http://www.cnblogs.com/allenblogs/archive/2010/12/09/1901404.html TCPI To Complete Performance In ...
- java中for循环执行过程
for (int j = 0; j < newSize; j++) { //执行todo } 1.首先变量j初始化为0 2.然后j=0的值跟newSize进行比较,假如为true,则执行{}里面 ...
- linux重命名session和window
重命名 window title 最近想要给screen session中的每一个 窗口命名一个标识名字,而不是默认的 $ bash 相关命令: ctrl+z(我的screen配置的+z,默认是+a) ...