当前位置:网站首页>01背包关于从二维优化到一维
01背包关于从二维优化到一维
2022-07-29 08:32:00 【蒟】
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int wi[N], vi[N];
int n, v;
/*int dp[N][N];
int main() {
cin >> n >> v;
for (int i = 1; i <= n; i++) {
cin >> vi[i] >> wi[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= v; j++) {
dp[i][j]=dp[i-1][j];
if(j>=vi[i])
{
dp[i][j]=max(dp[i-1][j-vi[i]]+wi[i],dp[i][j]);
}
}
}
cout<<dp[n][v];
return 0;
}*/
/*
从二维优化到一维
全部等价删除之后
for (int i = 1; i <= n; i++) {
for (int j = vi[i]; j <= v; j++) {
dp[j]=max(dp[j-vi[i]]+wi[i],dp[j]);
//这样的话,其等价于
//dp[i][j]=max(dp[i][j],dp[i][j-vi[i])
//而非等价于
//dp[i][j]=max(dp[i][j],dp[i-1][j-vi[i])
///因为j-vi[i]是严格小于j的,当我们j是从小到大枚举的
时候,当前第i层的[j-vi[i]]其实已经被算过了,也就是说
第i-1层的[j-vi[i]]已经被第i层的 [j-vi[i]]给更新掉了。
///
而如何解决这个问题,只需要在算第i层的[j-vi[i]]的时候
dp[j-vi[i]]还是保存的第i-1层的数据即可,我们将j从大到小枚举
当枚举到j的时候,因为其严格大于j-vi[i]],而我们是从大到小枚举的
所以dp[j-vi[i]]保存的数据还未被更新到,即还是第i-1层的数据。
}
}
*/
int dp[N];
int main() {
cin >> n >> v;
for (int i = 1; i <= n; i++) {
cin >> vi[i] >> wi[i];
}
for (int i = 1; i <= n; i++) {
for (int j = v; j >= vi[i]; j--) {
dp[j] = max(dp[j - vi[i]] + wi[i], dp[j]);
}
}
cout << dp[v];
return 0;
}
边栏推荐
- GBase 8s数据库有哪些备份恢复方式
- commonjs导入导出与ES6 Modules导入导出简单介绍及使用
- 01-01-osg GL3 environment setup
- Osgsimplegl3 example analysis
- Play Parkour with threejs Technology
- Simple operation of SQL server data table
- Common query optimization technology of data Lake - "deepnova developer community"
- Data warehouse layered design and data synchronization,, 220728,,,,
- Transaction management in SQL Server
- Low cost 2.4GHz wireless transceiver chip -- ci24r1
猜你喜欢
WQS binary learning notes
Normal visualization
C language calculates the length of string
Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql
Deep learning (2): image and character recognition
Reading of false news detection papers (3): semi supervised content-based detection of misinformation via tensor embeddings
DAC0832 waveform generator based on 51 single chip microcomputer
A little knowledge [synchronized]
C language macro define command exercise
Virtual augmentation and reality Part 2 (I'm a Firebird)
随机推荐
Week 1 task deep learning and pytorch Foundation
What is the working principle of the noise sensor?
Hal library learning notes - 8 concept of serial communication
Centos7/8 command line installation Oracle11g
Pnpm install appears: err_ PNPM_ PEER_ DEP_ ISSUES Unmet peer dependencies
Flask reports an error runtimeerror: the session is unavailable because no secret key was set
Charging pile charging technology new energy charging pile development
DAC0832 waveform generator based on 51 single chip microcomputer
Clickhouse learning (II) Clickhouse stand-alone installation
WQS binary learning notes
Stm32ff030 replaces domestic MCU dp32g030
Cmake setting vs Startup running environment path
7.3-function-templates
Data warehouse layered design and data synchronization,, 220728,,,,
集群使用规范
[from_bilibili_dr_can][[advanced control theory] 9_ State observer design] [learning record]
Phy6252 is an ultra-low power Bluetooth wireless communication chip for the Internet of things
Qpalette learning notes
2022 Teddy cup data mining challenge C project and post game summary
AES 双向加密解密工具