当前位置:网站首页>POJ 1742 Coins ( 单调队列解法 )「建议收藏」
POJ 1742 Coins ( 单调队列解法 )「建议收藏」
2022-07-07 18:10:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
博客。
再加上这题体积与价值相等那么就更好做了。仅仅有 j %v[ i ] 余数同样的才干够同一时候处理(j 指的是某个体积的值),在计算某个数的时候,仅仅要计算前面的同样的余数中(在个数限制内)是否有 true(有放满的) 就能够了。
代码:
#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 背包
{
for(int i = W ;i >= v ; --i)
if(!dp[i] && dp[i-v])
dp[i] = true ,ans++ ;
}
else if(num * v >= W) // 全然背包
{
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) // 同样余数一块处理
{
int front =0 ,end = 0 ,sum = 0 ;
for(int j = a ;j <= W ; j += v)
{
if(end - front-1 == num) // 去除过时元素 ,由于最多选择num[i] 个
sum -= deq[front++] ;
deq[end++] = dp[j] ; // 存入
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 ;
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116515.html原文链接:https://javaforall.cn
边栏推荐
- Opencv learning notes high dynamic range (HDR) imaging
- 使用高斯Redis实现二级索引
- 力扣599. 两个列表的最小索引总和
- torch.nn.functional.pad(input, pad, mode=‘constant‘, value=None)记录
- 力扣 1037.有效的回旋镖
- pom.xml 配置文件标签作用简述
- Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記
- CSDN syntax description
- openEuler 有奖捉虫活动,来参与一下?
- 一键部署Redis任意版本
猜你喜欢
Data island is the first danger encountered by enterprises in their digital transformation
How to cooperate among multiple threads
Simulate the implementation of string class
The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared
数据孤岛是企业数字化转型遇到的第一道险关
【STL】vector
Force buckle 2319 Judge whether the matrix is an X matrix
openEuler 有奖捉虫活动,来参与一下?
有了ST7008, 蓝牙测试完全拿捏住了
Flink并行度和Slot详解
随机推荐
[solution] package 'XXXX' is not in goroot
c语言如何判定是32位系统还是64位系统
Oracle 存储过程之遍历
Ways to improve the utilization of openeuler resources 01: Introduction
国家网信办公布《数据出境安全评估办法》:累计向境外提供10万人信息需申报
pom.xml 配置文件标签:dependencies 和 dependencyManagement 区别
【解决】package ‘xxxx‘ is not in GOROOT
Gorilla official: sample code for golang to open websocket client
JVM GC垃圾回收简述
Implement secondary index with Gaussian redis
Force buckle 459 Duplicate substring
Yolov6:yolov6+win10--- train your own dataset
YoloV6:YoloV6+Win10---训练自己得数据集
The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
Force buckle 599 Minimum index sum of two lists
力扣 459. 重复的子字符串
关于cv2.dnn.readNetFromONNX(path)就报ERROR during processing node with 3 inputs and 1 outputs的解决过程【独家发布】
大厂经典指针笔试题
JVM GC garbage collection brief