当前位置:网站首页>POJ 1742 coins (monotone queue solution) [suggestions collection]
POJ 1742 coins (monotone queue solution) [suggestions collection]
2022-07-07 20:16:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
Blog .
Plus, if the volume is equal to the value, it's better to do . Only j %v[ i ] The rest of the same ability can be handled at the same time (j It refers to the value of a certain volume ), When calculating a number , Just calculate the same remainder as before ( Within the number limit ) Is there a true( It's full ) You can .
Code :
#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<cctype>
#include<iomanip>
#include<algorithm>
using namespace std ;
#define INT long long int
#define L(x) (x * 2)
#define R(x) (x * 2 + 1)
const int INF = 0x3f3f3f3f ;
const double esp = 0.0000000001 ;
const double PI = acos(-1.0) ;
const int mod = 1000000007 ;
const int MY = (1<<5) + 5 ;
const int MX = 100010 + 5 ;
int n ,W ,ans ;
int v[MX] ,num[MX] ;
bool deq[MX] ,dp[MX] ;
void input()
{
memset(dp ,false ,sizeof(dp)) ;
for(int i = 1 ;i <= n ; ++i)
scanf("%d" ,&v[i]) ;
for(int i = 1 ;i <= n ; ++i)
scanf("%d" ,&num[i]) ;
}
void DP(int v ,int num)
{
if(!num || !v) return ;
if(num == 1) // 01 knapsack
{
for(int i = W ;i >= v ; --i)
if(!dp[i] && dp[i-v])
dp[i] = true ,ans++ ;
}
else if(num * v >= W) // It's all backpacks
{
for(int i = v ;i <= W ; ++i)
if(!dp[i] && dp[i-v])
dp[i] = true ,ans++ ;
}
else
{
num = min(num ,W/v) ;
for(int a = 0 ;a < v ; ++a) // Similarly, the remainder is processed together
{
int front =0 ,end = 0 ,sum = 0 ;
for(int j = a ;j <= W ; j += v)
{
if(end - front-1 == num) // Remove obsolete elements , Due to the most choices num[i] individual
sum -= deq[front++] ;
deq[end++] = dp[j] ; // Deposit in
sum += dp[j] ;
if(!dp[j] && sum)
dp[j] = true ,ans++ ;
}
}
}
}
int main()
{
//freopen("input.txt" ,"r" ,stdin) ;
while(scanf("%d%d" ,&n ,&W) ,n+W)
{
input() ;
dp[0] = true ;
ans = 0 ;
for(int i = 1 ;i <= n ; ++i)
DP(v[i] ,num[i]) ;
printf("%d\n" ,ans) ;
}
return 0 ;
}
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116515.html Link to the original text :https://javaforall.cn
边栏推荐
- 线性基
- Semantic SLAM源码解析
- Try the tuiroom of Tencent cloud (there is an appointment in the evening, which will be continued...)
- One click deployment of any version of redis
- CIS芯片测试到底怎么测?
- School 1 of vulnhub
- Boot 和 Cloud 的版本选型
- The boundary of Bi: what is bi not suitable for? Master data, Martech? How to expand?
- 831. KMP字符串
- kubernetes之创建mysql8
猜你喜欢
关于cv2.dnn.readNetFromONNX(path)就报ERROR during processing node with 3 inputs and 1 outputs的解决过程【独家发布】
Simulate the implementation of string class
School 1 of vulnhub
网络原理(1)——基础原理概述
Vulnhub's funfox2
力扣 599. 两个列表的最小索引总和
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
【mysql篇-基础篇】事务
整型int的拼接和拆分
YoloV6:YoloV6+Win10---训练自己得数据集
随机推荐
Chapter 20 using work queue manager (3)
PHP method of obtaining image information
力扣 1790. 仅执行一次字符串交换能否使两个字符串相等
Force buckle 599 Minimum index sum of two lists
[auto.js] automatic script
Force buckle 643 Subarray maximum average I
CIS芯片测试到底怎么测?
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
JVM 类加载机制
整型int的拼接和拆分
841. String hash
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
机器学习笔记 - 使用Streamlit探索对象检测数据集
【mysql篇-基础篇】事务
Some important knowledge of MySQL
rk3128投影仪lcd显示四周显示不完整解决
Leetcode force buckle (Sword finger offer 36-39) 36 Binary search tree and bidirectional linked list 37 Serialize binary tree 38 Arrangement of strings 39 Numbers that appear more than half of the tim
JVM GC garbage collection brief
基于深度学习的目标检测的更新迭代总结(持续更新ing)
编译器优化那些事儿(4):归纳变量