当前位置:网站首页>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;
}
边栏推荐
- LeetCode 最大矩形,最大正方形系列 84. 85. 221. 1277. 1725. (单调栈,动态规划)
- OpenGL es: (4) detailed explanation of EGL API (Continued)
- C语言初阶——牛客网精选好题
- SSM的教务管理系统(免费源码获取)
- TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)
- rust猜数字游戏
- Vscode function annotation / file header annotation shortcut
- OpenGL es: (3) EGL, basic steps of EGL drawing, eglsurface, anativewindow
- Fixed height of the first column in El table dynamic header rendering
- boot+jsp的高校社團管理系統(附源碼下載鏈接)
猜你喜欢

HCM 初学 ( 四 ) - 时间

从MLPerf谈起:如何引领AI加速器的下一波浪潮

Brief description of activation function

Through cooperation with the University of international trade, we can increase efficiency for college students

El tooltip in the table realizes line breaking display

Huluer app help

rust猜数字游戏

亲爱的派盘用户,我要向您表白!

机械臂速成小指南(六):步进电机驱动器

健康照明中应用的LED照明灯
随机推荐
SystemVerilog学习-10-验证量化和覆盖率
FPGA - 7系列 FPGA内部结构之Clocking -01- 时钟架构概述
OpenGL es: (3) EGL, basic steps of EGL drawing, eglsurface, anativewindow
[medical segmentation] u2net
为什么用葫芦儿派盘取代U盘?
OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow
Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal
Data governance: data governance management (Part V)
Xuanyi maintenance manual
Idea start view project port
码蹄集 - MT3149 · AND - 数据不是很强,暴力剪枝就能骗AC
穿越派·派盘 + 思源笔记 = 私人笔记本
bat操作ftp上传下载命令
Excel dynamic chart
Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
OpenGL es: (4) detailed explanation of EGL API (Continued)
葫芦儿 APP 使用帮助
OpenGL ES: (1) OpenGL ES的由来 (转)
基于微信小程序的青少年生理健康知识小助手(免费获取源码+项目介绍+运行介绍+运行截图+论文)
Wild melon or split melon?