DP Big Event in HDU
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
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
A test case starting with a negative integer terminates input and this test case is not to be processed.
Output
Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1
Sample Output
20 10
40 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 <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
int main()
{
int T;
int v[], m[];
while (cin >> T && T >= )
{
int i;
int s = ;
for (i = ; i <= T; i++)
{
cin >> v[i] >> m[i];
s = s + v[i] * m[i];
}
int sum = s / ;
int dp[];
memset(dp, , sizeof(dp));
int j;
for (i = ; i <= T; i++)//对每一种遍历
{
for (j = ; 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 ;
}
DP Big Event in HDU的更多相关文章
- HDU 1171 Big Event in HDU dp背包
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...
- HDU-1171 Big Event in HDU
Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...
- Big Event in HDU
Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe ...
- Big Event in HDU(HDU1171)可用背包和母函数求解
Big Event in HDU HDU1171 就是求一个简单的背包: 题意:就是给出一系列数,求把他们尽可能分成均匀的两堆 如:2 10 1 20 1 结果是:20 10.才最均匀! 三 ...
- 组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1171 Big Event in HDU 多重背包二进制优化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...
- Big Event in HDU(HDU 1171 多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU1171-Big Event in HDU
描述: Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don ...
- HDU 1171 Big Event in HDU (多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- python------模块定义、导入、优化 ------time模块
1.模块介绍 2.time & datetime 3.random 4.os 5.sys 6.shutil 7.json & pickle 8.shelve 9.xml处理 10.ya ...
- 【liunx】时间处理函数
一.脚本示例 [mylinuxaccount@linux01 ~]$ date +%Y%m%d 20171224 [mylinuxaccount@linux01 ~]$ date +%F 2017-1 ...
- webpack中hash、chunkhash、contenthash区别
webpack中对于输出文件名可以有三种hash值: 1. hash 2. chunkhash 3. contenthash 这三者有什么区别呢? hash 如果都使用hash的话,因为这是工程级别的 ...
- oracle审计的激活与取消
审计audit用户见识用户所执行的操作,并且oracle会将审计跟踪结果存放到os文件或数据库中 激活审计 conn /as sysdba show parameter audit_sys_opera ...
- mysql配置my.cnf文件,以及参数优化提升性能
系统centos7 mariadb通过yum安装 mysql配置文件位于/etc/my.cnf 常用参数: 1)max_connections设置最大连接(用户)数,其默认值为100,设置太小会出现t ...
- Android.bp学习笔记
1.Android.bp简介 Android 7.0之后希望用Android.bp替换Android.mk,bp简单的配置更方便Ninja 文件的产生,而Blueprint和Soong 就此产生.An ...
- Oracle12C版本安装步骤
一.下载路径 http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html file1,file ...
- 递归和非递归分别实现strlen
思路:strlren主要是字符串是以'\0'为结尾标识来计算字符串的长度,所以要实现自己去写strlen也要从这方面下手. 非递归思想:应用循环的思路,以'\0'为循环结束的标识,每循环一次计数加一. ...
- 火狐浏览器firebug
1. 近日,Firebug团队在官网贴出了停止继续开发.更新维护Firebug的通知,邀请大家使用Firefox内置工具DevTools. 来自官网截图 Firebug是Firefox旗下的一款扩 ...
- Hessian总结
一.简介: Hessian是一个基于Binary-RPC 实现的远程通讯library,基于 Http 协议进行传输.通过其自定义的串行化机制将请求信息进行序列化,产生二进制流.响应端根据 Hessi ...