当前位置:网站首页>Thoughts on a "01 knapsack problem" expansion problem
Thoughts on a "01 knapsack problem" expansion problem
2022-07-01 05:51:00 【Forgetting sleep】
[USACO03FALL]Cow Exhibition G
Background
Title Description
Cows want to prove that they are smart and funny . So , Bessie set up a dairy Fair , She has been right N N N A cow had an interview , The IQ and EQ of each cow were determined .
Bessie has the right to choose which cows to show . Because negative IQ or EQ can have negative effects , So Bessie doesn't want to show that the sum of cows' IQ is less than zero , Or the sum of EQ is less than zero . Under these two conditions , She hopes to show that the greater the sum of IQ and EQ of cows, the better , Please help Bessie find this maximum .
Input format
first line : A single integer N N N, 1 ≤ N ≤ 400 1 \le N \le 400 1≤N≤400.
Line two to line two N + 1 N+1 N+1 That's ok : The first i + 1 i+1 i+1 Line has two integers : S i S_i Si and F i F_i Fi, It means the first one i i i The IQ and EQ of a cow ,− 1000 ≤ S i ; F i ≤ 1000 1000 \le S_i;F_i \le 1000 1000≤Si;Fi≤1000.
Output format
Output a single integer : Indicates the maximum sum of EQ and IQ . Bessie can exclude any cows from the exhibition , If this is the best , Output 0 0 0.
Examples #1
The sample input #1
5
-5 7
8 -6
6 -3
2 1
-8 -5
Sample output #1
8
Tips
Select the first end , The third end , The fourth cow , IQ and −5+6+2 = 3, EQ and Wei 7−3+1 = 5. add
Entering the second cow can increase the total to 10, However, because of emotional intelligence and become negative , So it's not allowed
The question is 01 An extension of knapsack problem , It's hard to think of , Regard IQ as volume , Emotional intelligence as value ,
Because IQ is negative , According to the data, the sum of IQ can be calculated : -400*1000 <= sum <= 400 * 1000,.
So you can add an offset to your IQ 400*1000, In this way, as an array subscript, it will not be out of bounds
Draw the key : This question should be guaranteed dp[j] Is just IQ and for j-M, How do you guarantee that ? This is how the following code initializes all values to -0x3f3f3f3f, Double this number will not overflow , Then initialize f[M]=0, In this way, we finally get max The maximum values in are those from 0 Shift from the past , That is, it happens to go through many iterations dp[j] = max(dp[j], dp[j - s[i]] + f[i]); The source of the final maximum value is from dp[M] That is, IQ and for 0 Transfer the past , This ensures our final result dp[j] It must be just IQ and for j-M
Ideas refer to : Backpack problem review list explanation
The code is as follows :
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1e6 + 10, M = 400 * 1000;
int dp[N], n, s[N], f[N];
int main()
{
cin >> n;
for(int i = 0; i < n; i ++)
cin >> s[i] >> f[i];
memset(dp, -0x3f, sizeof dp);// Because EQ is negative , take max, To initialize negative infinity
dp[M] = 0;// These two lines guarantee dp[j] Is just IQ and for j-M
for(int i = 0; i < n; i ++){
// Yes 01 Backpack has been expanded , because j - s[i] May be greater than j It may also be less than j
// One dimensional processing , the truth is that dp[i][j], Once upon a time i Among them, the IQ is only j-M The greatest emotional intelligence selected
// So if j - s[i] Greater than j It is necessary to traverse the update in positive order , Because it is used behind the upper floor
// If j - s[i] Less than j It is necessary to traverse the update in reverse order , Because it uses the one in front of the upper floor
if(s[i] >= 0)
for(int j = 2 * M; j >= s[i]; j --)
dp[j] = max(dp[j], dp[j - s[i]] + f[i]);
else
for(int j = 0; j <= s[i] + 2 * M; j ++)
dp[j] = max(dp[j], dp[j - s[i]] + f[i]);
}
int res = 0;
for(int i = M; i <= 2 * M; i ++)
if(dp[i] > 0)
res = max(res, dp[i] + i - M);
cout << res << endl;
return 0;
}
边栏推荐
- Why use huluer pie disk instead of U disk?
- rust猜数字游戏
- Advanced cross platform application development (III): online resource upgrade / hot update with uni app
- Advanced cross platform application development (II): uni app practice
- OpenGL ES: (1) OpenGL ES的由来 (转)
- A little assistant for teenagers' physiological health knowledge based on wechat applet (free source code + project introduction + operation introduction + operation screenshot + Thesis)
- 【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图
- OneFlow源码解析:算子签名的自动推断
- Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
- SystemVerilog学习-06-类的封装
猜你喜欢

Know the future of "edge computing" from the Nobel prize!

Crossing sect · paipan + Siyuan notes = private notebook

Diagramme dynamique Excel

【知识点总结】卡方分布,t分布,F分布

boot+jsp的高校社团管理系统(附源码下载链接)

HCM 初学 ( 三 ) - 快速输入PA70、PA71 浏览员工信息PA10

Ssm+mysql second-hand trading website (thesis + source code access link)

扩展点系列之SmartInstantiationAwareBeanPostProcessor确定执行哪一个构造方法 - 第432篇

uniapp树形层级选择器

我从技术到产品经理的几点体会
随机推荐
栈题目:解析布尔表达式
bat操作ftp上传下载命令
Trust guessing numbers game
SystemVerilog学习-09-进程间同步、通信和虚方法
Code shoe set - mt3149 · and - the data is not very strong. Violent pruning can deceive AC
Know the future of "edge computing" from the Nobel prize!
Fixed height of the first column in El table dynamic header rendering
linux 关闭redis 进程 systemd+
TIDB数据库特性总结
芯片,建立在沙粒上的帝国!
Beauty of Mathematics - Application of Mathematics
Preliminary level of C language -- selected good questions on niuke.com
论文学习记录随笔 多标签之LIFT
Orcle创建用户+角色
千万不要把笔记视频乱放!
分片上传与断点续传
从MLPerf谈起:如何引领AI加速器的下一波浪潮
Looking for high school student developers with similar interests
How to transmit and share 4GB large files remotely in real time?
这才是大学生必备软件 | 知识管理