ACM刷题之路(二十二)多重背包转01背包 HDU 1171

简介: ACM刷题之路(二十二)多重背包转01背包 HDU 1171

题目链接:传送门

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


                 

2 10 1 20 1 3 10 1 20 2 30 1 -1

 

Sample Output


                 

20 10 40 40

题意:

样例:

2

10 1

20 1

意思是两种物品,质量为10的物品有一个,质量为20的物品有一个,求如何分使得最大程度的两等分?

答案当然是  : 20   10   (题目规定大的放前面)

3

10 1

20 2

30 1

意思是三种物品,质量为10的物品有一个,质量为20的物品有两个,,质量为30的物品有一个,求如何分使得最大程度的两等分?

答案当然是  : 40   40  即20 20放一起   、10 30 放一起即可

分析:本题我采用多重背包转01背包的做法,把输入的物品质量放入a数组,如果物品数量大于1,就放多次,当然可以二进制优化,但在本题中不优化也可以AC。

另外注意判断输入结束必须是t<0 ,而不是t==-1!!!

另外注意判断输入结束必须是t<0 ,而不是t==-1!!!

另外注意判断输入结束必须是t<0 ,而不是t==-1!!!

当然下面就是01背包的模板

1. for i=1..N
2. for v=V..0
3.         f[v]=max{f[v],f[v-c[i]]+w[i]};

在本题中,第一层遍历物品的个数N,第二层遍历背包容量sum/2,状态转移方程一套就可以了。

注意dp数组的初始化,还有输出大的放前面即可。

1. #include<iostream>
2. #include<algorithm>
3. #include<cstring>
4. using namespace std;
5. int a[5002];
6. int dp[500002];
7. int maxx(int x, int y) {
8.  return x > y ? x : y;
9. }
10. int main()
11. {
12.   int t;
13.   while (~scanf_s("%d", &t) && t >= 0) {
14.     int len = 0;
15.     int sum = 0;
16.     while (t--) {
17.       int x, y;
18.       scanf_s("%d%d", &x, &y);
19.       sum += x * y;
20.       while (y--) {
21.         a[len++] = x;
22.       }
23.     }
24.     int summ = sum;
25.     sum /= 2;
26.     memset(dp, 0, sizeof(dp));
27.     for (int i = 0; i < len; i++) {
28.       for (int j = sum; j >= a[i]; j--) {
29.         dp[j] = maxx(dp[j], dp[j - a[i]] + a[i]);
30.       }
31.     }
32.     printf("%d %d\n", summ - dp[sum], dp[sum]);
33.   }
34.   return 0;
35. }

 


相关文章
C++访问类的私有成员变量的方法(友元函数、友元类)
C++访问类的私有成员变量的方法(友元函数、友元类)
986 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8052 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18184 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
2天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
770 0
|
8天前
|
云安全 数据采集 人工智能
|
9天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1471 5
|
15天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2596 8