当前位置:网站首页>多重背包,朴素及其二进制优化
多重背包,朴素及其二进制优化
2022-07-29 08:32: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;
}*/
/*
好,现在进行优化:
首先,我们用完全背包 的优化方式来优化,因为max无法做减操作所以是不行的.(太晚了)
好的现在让我们开始二进制优化:
假设i有n个,那么我们现在把这n个分成几组,每组的的个数分别为
0,1,2,4,8,...,2^(logn-1)(其中logn下取整),c;
其中1,2,4,8,... ,2^(logn-1)的和要求小于等于n,若是小于(也
就是说n不是2的整次幂c=n-sum( 1,2,4,8,... ,2^(logn-1));
而通过倍增的思想 0,1,2,4,8,...,2^(logn-1),c这些数,是可以
将0~n的每一个数都表示出来的。所以当我们把各个种类的物品都各自拆开,
我们就得到了许多组,每一组都有自己的体积、价值。由此就把多重背包的问题
转化成了01背包的问题。
代码实现:
*/
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;
}
边栏推荐
- Simple operation of SQL server data table
- Squareline partners with visual GUI development of oneos graphical components
- ML.NET相关资源整理
- LeetCode力扣题目总结(题目编号:53、3、141、面试题022、剑指offer链表中环的入口节点、20、19、牛客NC1、103、1143、牛客127)
- Proteus simulation based on msp430f2491 (realize water lamp)
- 6.3 references
- Clickhouse learning (III) table engine
- Count the list of third-party components of an open source project
- Background management system platform of new energy charging pile
- Back up Google or other browser plug-ins
猜你喜欢

Day6: use PHP to write file upload page

Smart energy management system solution

User identity identification and account system practice

PostgreSQL manually creates hikaridatasource to solve the error cannot commit when autocommit is enabled

7.3-function-templates

The first week of postgraduate freshman training: deep learning and pytorch Foundation

New energy shared charging pile management and operation platform

Ga-rpn: recommended area network for guiding anchors

数学建模——微分方程

Basic shell operations (Part 2)
随机推荐
Basic shell operations (Part 1)
A little knowledge [synchronized]
leetcode hot 100(刷题篇9)(301/45/517/407/offer62/MST08.14/)
(视频+图文)机器学习入门系列-第3章 逻辑回归
Proteus simulation based on msp430f2491
[from_bilibili_dr_can][[advanced control theory] 9_ State observer design] [learning record]
Clickhouse learning (III) table engine
Eggjs create application knowledge points
C language function output I love you
01背包关于从二维优化到一维
7.1-default-arguments
Leetcode Hot 100 (brush question 9) (301/45/517/407/offer62/mst08.14/)
MySQL statement mind map
为了速率创建线程池,启动核心线程
PostgreSQL手动创建HikariDataSource解决报错Cannot commit when autoCommit is enabled
What constitutes the smart charging pile system?
The first week of postgraduate freshman training: deep learning and pytorch Foundation
DC motor speed regulation system based on 51 single chip microcomputer (use of L298)
commonjs导入导出与ES6 Modules导入导出简单介绍及使用
Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"