当前位置:网站首页>Multiple knapsack, simplicity and binary optimization
Multiple knapsack, simplicity and binary optimization
2022-07-29 08:41:00 【蒟】
#include<bits/stdc++.h>
using namespace std;
/*int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>v[i]>>w[i]>>s[i];
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
for(int k=0;k<=s[i]&&k*v[i]<=j;k++)
{
dp[i][j]=max(dp[i][j],dp[i-1][j-k*v[i]]+k*w[i]);
}
}
}
cout<<dp[n][m];
return 0;
}*/
/*
good , Now optimize :
First , We use full backpacks To optimize , because max It's impossible to do subtraction .( It's too late )
OK, now let's start binary optimization :
hypothesis i Yes n individual , Now let's put this n Divide into groups , The number of each group is
0,1,2,4,8,...,2^(logn-1)( among logn Round down ),c;
among 1,2,4,8,... ,2^(logn-1) The sum of is less than or equal to n, If it is less than ( also
That is to say n No 2 The whole power of c=n-sum( 1,2,4,8,... ,2^(logn-1));
And through the thought of multiplication 0,1,2,4,8,...,2^(logn-1),c These numbers , Yes.
take 0~n Every number of is expressed . So when we separate all kinds of objects ,
We got many groups , Each group has its own volume 、 value . Therefore, the problem of multiple backpacks
Transformed into 01 The problem with backpacks .
Code implementation :
*/
const int N = 2e5+ 10;
int dp[N];
int v[N],w[N],cnt=0;
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
int a,b,c,k=1;
cin>>a>>b>>c;
while(k<=c)
{
v[++cnt]=a*k;
w[cnt]=b*k;
c-=k;
k*=2;
}
if(c>0)
{
v[++cnt]=a*c;
w[cnt]=b*c;
}
}
for(int i=1;i<=cnt;i++)
{
for(int j=m;j>=v[i];j--)
{
dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
}
}
cout<<dp[m];
return 0;
}
边栏推荐
- [opencv] - Operator (Sobel, canny, Laplacian) learning
- Arfoundation Getting Started tutorial 7-url dynamically loading image tracking Library
- Chrony 时间同步
- 01背包关于从二维优化到一维
- Markdown concise grammar manual
- Day15: the file contains the vulnerability range manual (self use file include range)
- 分组背包
- Reading of false news detection papers (3): semi supervised content-based detection of misinformation via tensor embeddings
- 深度学习(2):图片文字识别
- 集群使用规范
猜你喜欢
随机推荐
7.2-function-overloading
What if official account does not support markdown format file preparation?
Common query optimization technology of data Lake - "deepnova developer community"
Markdown concise grammar manual
Several ways of debugging support under oneos
Intel将逐步结束Optane存储业务 未来不再开发新产品
SAP ooalv-sd module actual development case (add, delete, modify and check)
01-01-osg GL3 environment setup
Day15 (day16 extension): file contains vulnerability
【OpenCV】-算子(Sobel、Canny、Laplacian)学习
Osgsimplegl3 example analysis
Collation of ml.net related resources
Cmake setting vs Startup running environment path
Basic shell operations (Part 1)
(视频+图文)机器学习入门系列-第3章 逻辑回归
Sudoku (DFS)
Cluster usage specification
Use SQL client How can the job generated by SH achieve breakpoint continuation after cancle?
Week 1 task deep learning and pytorch Foundation
Simple operation of SQL server data table









