当前位置:网站首页>(dp+ group backpack) acwing 9 Group knapsack problem
(dp+ group backpack) acwing 9 Group knapsack problem
2022-06-11 23:34:00 【Age worry】
9. Group knapsack problem
Topic link https://www.acwing.com/problem/content/description/9/
subject :

Method 1 : There is no memory optimization , A two-dimensional . The idea is to compare each item in each group , But make two comparisons . The first time is with f[ k-1 ][ j ], The second time is with f[k][j].
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int f[110][110],k=1;
int main(){
int n,v;
cin>>n>>v;
for(int i=0;i<n;i++){
int s,x,y;
scanf("%d",&s);
for(int j=1;j<=s;j++){
scanf("%d%d",&x,&y);
for(int z=v;z>=0;z--){
f[k][z]=max(f[k][z],f[k-1][z]);
if(z>=x)
f[k][z]=max(f[k][z],f[k-1][z-x]+y);
}
}
k++;
}
cout<<f[k-1][v];
return 0;
}
Method 2 : To optimize the f, Into one dimension . The idea is , Take each group in a v Inside for comparison , Take the optimal value
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int f[110],v[110][110],w[110][110],s[110];
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>s[i];
for(int j=1;j<=s[i];j++){
cin>>v[i][j]>>w[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=m;j>=0;j--){
for(int z=1;z<=s[i];z++){
if(j>=v[i][z])
f[j]=max(f[j],f[j-v[i][z]]+w[i][z]);
}
}
}
cout<<f[m];
return 0;
}
边栏推荐
- 一文读懂Logstash原理
- 【Day2 文献精读】Time in the mind: Using space to think about time
- Teacher lihongyi, NTU -- tips for DNN regulation
- What is bom? And knowledge points
- 2022 operation of simulation examination platform for safety officer C certificate
- 抗原产品进入家庭,中国医疗器械企业迎来新蓝海
- C language simple exercise No.17, about the combination of for and while loops
- 2022 safety officer-b certificate theoretical question bank and simulation test
- Lake Shore - supertran continuous flow cryogenic thermostat system
- HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
猜你喜欢

【Day4 文献精读】Space–time interdependence: Evidence against asymmetric mapping between time and space

Two ways of using reuqests in RF

Altium designer工程下多个原理图和PCB图的一一对应

Lake shore vnf series low temperature thermostat system - sample in flowing steam

Jenkins基本配置
![[Day2 intensive literature reading] time in the mind: using space to think about time](/img/7a/b155ee0c136f911a7e99e9633af246.png)
[Day2 intensive literature reading] time in the mind: using space to think about time

Introduction aux bases de SOLR
![[day15 literature extensive reading] numerical magnetic effects temporary memories but not time encoding](/img/57/9ce851636b927813a55faedb4ecd48.png)
[day15 literature extensive reading] numerical magnetic effects temporary memories but not time encoding

一文读懂Logstash原理

显示商品详情【项目 商城】
随机推荐
[day4 literature intensive reading] space – time interdependence: evidence against Asymmetric mapping between time and space
mysql——find_in_set用法
Pad printing process flow and application precautions
Vs code writing assembly code [microcomputer principle]
Cisco private dynamic routing protocol: EIGRP
[Delphi] determine the encoding method of the file (ANSI, Unicode, utf8, unicodebig)
【自然语言处理】【多模态】ALBEF:基于动量蒸馏的视觉语言表示学习
My creation anniversary
05 classification learning notes lihongyi's in-depth study 2021
2022年安全员-B证理论题库及模拟考试
Jetpack architecture component learning (3) -- activity results API usage
2022 high place installation, maintenance and removal of simulated examination platform for operation certificate examination question bank
sonarqube介绍和安装步骤
2022 safety officer-a certificate test question simulation test platform operation
Altium designer工程下多个原理图和PCB图的一一对应
Application of Lora technology in long distance wireless transmission of water meter reading
Leetcode must review 20 lintcode (5466421166978227)
Si4432 RF chip scheme typical application of data transmission of wireless communication module of Internet of things
oracle中dblink操作
Introduction to Solr Basics