当前位置:网站首页>01 knapsack about from two-dimensional optimization to one-dimensional optimization
01 knapsack about from two-dimensional optimization to one-dimensional optimization
2022-07-29 08:41: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;
}*/
/*
From two-dimensional optimization to one-dimensional optimization
After deleting all equivalents
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]);
// In this case , It is equivalent to
//dp[i][j]=max(dp[i][j],dp[i][j-vi[i])
// Not equivalent to
//dp[i][j]=max(dp[i][j],dp[i-1][j-vi[i])
/// because j-vi[i] Is strictly less than j Of , When we j From small to large
When , Current i Layer of [j-vi[i]] In fact, it has been calculated , in other words
The first i-1 Layer of [j-vi[i]] Has been i Layer of [j-vi[i]] Updated .
///
And how to solve this problem , Just count the number i Layer of [j-vi[i]] When
dp[j-vi[i]] It's still the first time to save i-1 Layer data is enough , We will j Enumeration from large to small
When enumeration to j When , Because it is strictly greater than j-vi[i]], And we enumerate from large to small
therefore dp[j-vi[i]] The saved data has not been updated to , That is, the first i-1 Layer data .
}
}
*/
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;
}
边栏推荐
- Demonstration and solution of dirty reading, unrepeatable reading and unreal reading
- SAP ooalv-sd module actual development case (add, delete, modify and check)
- Segment paging and segment page combination
- Selenium actual combat case crawling JS encrypted data
- [from_bilibili_dr_can][[advanced control theory] 9_ State observer design] [learning record]
- Sword finger offer 27. image of binary tree
- ADB common command list
- Arfoundation starts from scratch 5-ar image tracking
- Google browser cross domain configuration free
- MFC integration QT verification and problem handling
猜你喜欢

MySQL statement mind map

数学建模——聚类

Virtual augmentation and reality Part 2 (I'm a Firebird)

Lesson 3 threejs panoramic preview room case

数学建模——微分方程

Ga-rpn: recommended area network for guiding anchors

C language watch second kill assist repeatedly

Count the list of third-party components of an open source project

The computer video pauses and resumes, and the sound suddenly becomes louder

Clion+opencv+aruco+cmake configuration
随机推荐
Cloud security daily 220712: the IBM integration bus integration solution has found a vulnerability in the execution of arbitrary code, which needs to be upgraded as soon as possible
Arfoundation Getting Started tutorial 7-url dynamically loading image tracking Library
A little knowledge [synchronized]
Simple operation of SQL server data table
数学建模——微分方程
Amazfit dial toolbox Online
Chrony time synchronization
Osgsimplegl3 combined with renderdoc tool
Sword finger offer 27. image of binary tree
ICMP message analysis
Curl -v | JQ
Demonstration and solution of dirty reading, unrepeatable reading and unreal reading
Clickhouse learning (I) Clickhouse?
Day6: use PHP to write file upload page
01-01-osg GL3 environment setup
数学建模——聚类
Cmake setting vs Startup running environment path
Clickhouse learning (II) Clickhouse stand-alone installation
LeetCode力扣题目总结(题目编号:53、3、141、面试题022、剑指offer链表中环的入口节点、20、19、牛客NC1、103、1143、牛客127)
Cluster usage specification