当前位置:网站首页>Backpack template
Backpack template
2022-07-02 20:32:00 【. dye】
01 knapsack
int dp[10000];
int v[100];
int p[100];
int main()
{ int n,w;cin>>n>>w;
for(int i=1;i<=n;i++){
cin>>v[i]>>p[i];
}
for(int i=1;i<=n;i++){
for(int j=w;j>=0;j--){
if(j>=v[i])dp[j]=max(dp[j],dp[j-v[i]]+p[i]);
}
}
cout<<dp[w];
}
Completely backpack
ll dp[60000];
ll v[60000];
ll p[20000];
int main()
{ ll n,w;scanf("%lld%lld",&n,&w);
for(ll i=1;i<=n;i++){
scanf("%lld%lld",&v[i],&p[i]);
}
for(ll i=1;i<=n;i++){
for(ll j=v[i];j<=w;j++){
if(j>=v[i])dp[j]=max(dp[j],dp[j-v[i]]+p[i]);
}
}
cout<<dp[w];
}
Multiple backpack ( A monotonous queue )
int dp[60000],zu[60000], x[60000],n,m;
int main() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
int v, w, s;
cin >> v >> w >> s;
memcpy(zu, dp, sizeof dp);
for (int j = 0; j < v; ++j) {
int a = 0, b = -1;
for (int k = j; k <= m; k += v) {
if (a <= b && x[a]<k-s* v) a ++;
while (a <= b && zu[x[b]]-(x[b] - j) / v * w <= zu[k] - (k - j) / v * w) b --;
if (a <= b) dp[k] = max(dp[k], zu[x[a]] + (k - x[a]) / v * w); x[++b] = k;
}
}
}
cout<<dp[m];
return 0;
}
边栏推荐
- 什么叫在线开户?现在网上开户安全么?
- Istio1.12: installation and quick start
- Detailed upgrade process of AWS eks
- Codeforces round 651 (Div. 2) (a thinking, B thinking, C game, D dichotomy, e thinking)
- Redis sentinel cluster working principle and architecture deployment # yyds dry goods inventory #
- 在消费互联网时代,诞生了为数不多的头部平台的话
- Sometimes only one line of statements are queried, and the execution is slow
- JASMINER X4 1U deep disassembly reveals the secret behind high efficiency and power saving
- Postman download and installation
- Data preparation for behavior scorecard modeling
猜你喜欢
pytorch 模型保存的完整例子+pytorch 模型保存只保存可训练参数吗?是(+解决方案)
笔记本安装TIA博途V17后出现蓝屏的解决办法
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
【实习】解决请求参数过长问题
八年测开经验,面试28K公司后,吐血整理出高频面试题和答案
I did a craniotomy experiment: talk about macromolecule coding theory and Lao Wang's fallacy from corpus callosum and frontal leukotomy
[real case] trap of program design - beware of large data
burp 安装 license key not recognized
One side is volume, the other side is layoff. There are a lot of layoffs in byte commercialization department. What do you think of this wave?
AMD's largest transaction ever, the successful acquisition of Xilinx with us $35billion
随机推荐
Database schema notes - how to choose the right database in development + who invented relational database?
B端电商-订单逆向流程
Wu Enda's machine learning mind mapping insists on clocking in for 23 days - building a knowledge context, reviewing, summarizing and replying
CRM客户关系管理系统
How to realize the function of detecting browser type in Web System
【每日一题】241. 为运算表达式设计优先级
I did a craniotomy experiment: talk about macromolecule coding theory and Lao Wang's fallacy from corpus callosum and frontal leukotomy
How can testers do without missing tests? Seven o'clock is enough
pytorch 模型保存的完整例子+pytorch 模型保存只保存可训练参数吗?是(+解决方案)
AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)
Google Earth engine (GEE) - Landsat 9 image full band image download (Beijing as an example)
【JS】获取hash模式下URL的搜索参数
Why do I have a passion for process?
现在券商的优惠开户政策什么?实际上网上开户安全么?
Is it safe to open an account for online stock speculation? I'm a novice, please guide me
Complete example of pytorch model saving +does pytorch model saving only save trainable parameters? Yes (+ solution)
Motivation! Big Liangshan boy a remporté le prix Zhibo! Un article de remerciement pour les internautes qui pleurent
Driverless learning (III): Kalman filter
JS modularization
After 65 days of closure and control of the epidemic, my home office experience sharing | community essay solicitation