当前位置:网站首页>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;
}
边栏推荐
- ICMP message analysis
- Lesson 3 threejs panoramic preview room case
- Reading of false news detection papers (3): semi supervised content-based detection of misinformation via tensor embeddings
- 用户身份标识与账号体系实践
- node:文件写入数据(readFile、writeFile),覆盖与增量两种模式
- Day4: SQL server is easy to use
- Use SQL client How can the job generated by SH achieve breakpoint continuation after cancle?
- Classic interview question: = = the difference between equals
- Play Parkour with threejs Technology
- Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"
猜你喜欢

2022电工(初级)考题模拟考试平台操作

Application of matrix transpose

Deep learning (1): prediction of bank customer loss

ROS common instructions

Qpalette learning notes

2022 question bank and answers of operation certificate examination for main principals of hazardous chemical business units

Eggjs create application knowledge points

Simple operation of SQL server data table

Deep learning (2): image and character recognition
![[from_bilibili_dr_can][[advanced control theory] 9_ State observer design] [learning record]](/img/9d/d9a4a3091c00ec65a9492ca49267db.png)
[from_bilibili_dr_can][[advanced control theory] 9_ State observer design] [learning record]
随机推荐
Arfoundation Getting Started tutorial 7-url dynamically loading image tracking Library
WQS binary learning notes
node:文件写入数据(readFile、writeFile),覆盖与增量两种模式
完全背包问题 从朴素到终极
Data warehouse layered design and data synchronization,, 220728,,,,
【Transformer】ATS: Adaptive Token Sampling For Efficient Vision Transformers
Deep learning (2): image and character recognition
A little knowledge [synchronized]
RESTful 风格详解
C language -- 23 two-dimensional array
集群使用规范
Squareline partners with visual GUI development of oneos graphical components
Day5: PHP simple syntax and usage
Analysis of zorder sampling partition process in Hudi - "deepnova developer community"
Normal visualization
Chrony 时间同步
Intel will gradually end the optane storage business and will not develop new products in the future
C language sorts n integers with pointers pointing to pointers
【OpenCV】-算子(Sobel、Canny、Laplacian)学习
深度学习(1):银行客户流失预测