当前位置:网站首页>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;
}
边栏推荐
- OpenGL es: (4) detailed explanation of EGL API (Continued)
- Vscode function annotation / file header annotation shortcut
- 2022.6.30-----leetcode. one thousand one hundred and seventy-five
- Data governance: data governance framework (Part I)
- linux 关闭redis 进程 systemd+
- 运行时候的导包搜索路径虽然pycharm中标红但不影响程序的执行
- He struggled day and night to protect his data
- Summary of common components of applet
- C language beginner level - realize the minesweeping game
- 教务管理系统(免费源码获取)
猜你喜欢

linux 关闭redis 进程 systemd+

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

Summary of common components of applet

What is the at instruction set often used in the development of IOT devices?

不是你脑子不好用,而是因为你没有找到对的工具

excel高级绘图技巧100讲(一)-用甘特图来展示项目进度情况

C语言初阶——牛客网精选好题

Call us special providers of personal cloud services for College Students

Build 2022 上开发者最应关注的七大方向主要技术更新

el-table 动态表头渲染 固定第一列 高度问题
随机推荐
Qt编写自定义控件-自绘电池
Crossing pie · pie pan + Mountain duck = local data management
OpenGL ES: (4) EGL API详解 (转)
关于一道01背包问题的·拓展题的思考
SSM的教务管理系统(免费源码获取)
机械臂速成小指南(六):步进电机驱动器
SystemVerilog学习-09-进程间同步、通信和虚方法
OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow
从诺奖知“边缘计算”的未来!
加密狗资料搜集
Code shoe set - mt3114 · interesting balance - explain it with examples
LED lighting used in health lighting
json数据比较器
JSON data comparer
MySQL数据迁移遇到的一些错误
Dear pie users, I want to confess to you!
Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal
Advanced drawing skills of Excel lecture 100 (1) - use Gantt chart to show the progress of the project
libpng12.so.0: cannot open shared object file: No such file or directory 亲测有效
OpenGL ES: (2) OpenGL ES 与 EGL、GLSL的关系