博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3578 Greedy Tino(双塔DP)
阅读量:4696 次
发布时间:2019-06-09

本文共 2242 字,大约阅读时间需要 7 分钟。

Greedy Tino

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1194    Accepted Submission(s): 393


Problem Description
  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.
 

Input
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.
 

Output
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.
 

Sample Input
 
1 5 1 2 3 4 5
 

Sample Output
 
Case 1: 7
双塔DP
注意如果有一个橘子是0,那么就符合条件
dp[i][j] 表示第i个橘子,两边差值
#include 
#include
#include
#include
#include
#include
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;}
 

转载于:https://www.cnblogs.com/dacc123/p/8228646.html

你可能感兴趣的文章
jquery 取id模糊查询
查看>>
解决在vue中,自用mask模态框出来后,下层的元素依旧可以滑动的问题
查看>>
修改node节点名称
查看>>
PAT(B) 1014 福尔摩斯的约会(Java)
查看>>
PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
查看>>
项目开发总结报告(GB8567——88)
查看>>
SSH加固
查看>>
端口扫描base
查看>>
iOS IM开发的一些开源、框架和教程等资料
查看>>
FansUnion:共同写博客计划终究还是“流产”了
查看>>
python 二维字典
查看>>
pip 警告!The default format will switch to columns in the future
查看>>
Arrays类学习笔记
查看>>
实验吧之【天下武功唯快不破】
查看>>
2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)...
查看>>
win7-64 mysql的安装
查看>>
dcm4chee 修改默认(0002,0013) ImplementationVersionName
查看>>
maven3在eclipse3.4.2中创建java web项目
查看>>
发布时间 sql语句
查看>>
黑马程序员 ExecuteReader执行查询
查看>>