当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
01-01-osg GL3 environment setup
Cluster usage specification
Day6: use PHP to write file upload page
【Transformer】ATS: Adaptive Token Sampling For Efficient Vision Transformers
正则表达式校验版本号
Osgsimplegl3 combined with renderdoc tool
【OpenCV】-算子(Sobel、Canny、Laplacian)学习
Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT
Flask reports an error runtimeerror: the session is unavailable because no secret key was set
Chrony 时间同步
随机推荐
Deep learning (2): image and character recognition
HC-SR04超声波测距模块使用方法和例程(STM32)
Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql
Flask reports an error runtimeerror: the session is unavailable because no secret key was set
Gan: generate adversarial networks
Markdown concise grammar manual
Day5: PHP simple syntax and usage
Intel will gradually end the optane storage business and will not develop new products in the future
Virtual augmentation and reality Part 2 (I'm a Firebird)
Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"
Transaction management in SQL Server
Centos7/8 command line installation Oracle11g
2022 R2 mobile pressure vessel filling test question simulation test platform operation
OSG advanced sequence
Sword finger offer 32 - ii Print binary tree II from top to bottom
2022 electrician (elementary) test question simulation test platform operation
Component transfer participation lifecycle
Crawling JS encrypted data of playwright actual combat case
7.1-default-arguments
Chrony 时间同步