当前位置:网站首页>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
边栏推荐
- pom. XML configuration file label: differences between dependencies and dependencymanagement
- Traversée des procédures stockées Oracle
- 力扣 2315.统计星号
- Interpretation of transpose convolution theory (input-output size analysis)
- 关于自身的一些安排
- PHP method of obtaining image information
- sql 常用优化
- 【mysql篇-基础篇】事务
- Force buckle 599 Minimum index sum of two lists
- Some arrangements about oneself
猜你喜欢

php 获取图片信息的方法

Force buckle 599 Minimum index sum of two lists

编译器优化那些事儿(4):归纳变量

ASP.NET学习& asp‘s one word

The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared

Cloud 组件发展升级

Mongodb由浅入深学习

Vulnhub tre1

Open source heavy ware! Chapter 9 the open source project of ylarn causal learning of Yunji datacanvas company will be released soon!

【STL】vector
随机推荐
How C language determines whether it is a 32-bit system or a 64 bit system
Welcome to the markdown editor
Version selection of boot and cloud
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
pom.xml 配置文件标签作用简述
数据孤岛是企业数字化转型遇到的第一道险关
TS quick start - Generic
Implement secondary index with Gaussian redis
Oracle 存储过程之遍历
kubernetes之创建mysql8
【Auto.js】自动化脚本
力扣 989. 数组形式的整数加法
Vulnhub's funfox2
CUDA versions are inconsistent, and errors are reported when compiling apex
MIT science and technology review article: AgI hype around Gato and other models may make people ignore the really important issues
torch.nn.functional.pad(input, pad, mode=‘constant‘, value=None)记录
Force buckle 2319 Judge whether the matrix is an X matrix
整型int的拼接和拆分
LeetCode_ 7_ five
LeetCode_7_5