博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DP Big Event in HDU
阅读量:5066 次
发布时间:2019-06-12

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

Big Event in HDU

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 84   Accepted Submission(s) : 38

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).

Input

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.

Output

For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.

Sample Input

210 120 1310 1 20 230 1-1

Sample Output

20 1040 40
   
  题目的意思就是把这些设备尽量的平分成两份,先输出大的,再输出小的。
     总价值(所有v[i]*m[i]之和)的一半作为背包的容量,把每一种中的每一个设备都看成一块石头。按01背包的解法,打表更新。使答案非常接近总质量的一半(就是在不超过总质量一半的前提下,dp【k】越大就是越接近)。
     外面的两个循环就是遍历每一个设备,相当于01背包的外循环:for(i=1;i<=石头的数量;i++)。最里面的循环让容量从sum到v【i】(可以不到0,因为0到v【i】这部分,放不了任何石头,不更新,也就不需要判断v【i】是否大于k了)
#include 
#include
#include
#include
using namespace std; int main() { int T; int v[55], m[55]; while (cin >> T && T >= 0) { int i; int s = 0; for (i = 1; i <= T; i++) { cin >> v[i] >> m[i]; s = s + v[i] * m[i]; } int sum = s / 2; int dp[250005]; memset(dp, 0, sizeof(dp)); int j; for (i = 1; i <= T; i++)//对每一种遍历 { for (j = 1; j <= m[i]; j++)//有几个就遍历几个 {
//把它变成01背包,总共有T*m[i]个石头,遍历 int k; for (k = sum; k >= v[i]; k--)//最小的石头就是v[i]大,只要到v[i]就可以了 { dp[k] = max(dp[k], dp[k - v[i]] + v[i]); } } } if (dp[sum] > s - dp[sum]) cout << dp[sum] << " " << s - dp[sum] << endl; else cout << s - dp[sum] << " " << dp[sum] << endl; } return 0; }

 

 

转载于:https://www.cnblogs.com/caiyishuai/p/8325880.html

你可能感兴趣的文章
Ubuntu 14.04下安装CUDA8.0
查看>>
跨平台开发 -- C# 使用 C/C++ 生成的动态链接库
查看>>
C# BS消息推送 SignalR介绍(一)
查看>>
WPF星空效果
查看>>
WPF Layout 系统概述——Arrange
查看>>
PIGOSS
查看>>
几款Http小服务器
查看>>
iOS 数组排序
查看>>
第三节
查看>>
PHP结合MYSQL记录结果分页呈现(比较实用)
查看>>
Mysql支持的数据类型
查看>>
openSuse beginner
查看>>
Codeforces 620E(线段树+dfs序+状态压缩)
查看>>
Windows7中双击py文件运行程序
查看>>
Market entry case
查看>>
css3动画属性
查看>>
Mongodb 基本命令
查看>>
控制文件的备份与恢复
查看>>
软件目录结构规范
查看>>
mysqladmin
查看>>