当前位置:网站首页>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学习笔记 高动态范围 (HDR) 成像
- Force buckle 643 Subarray maximum average I
- Gorilla official: sample code for golang to open websocket client
- 力扣 989. 数组形式的整数加法
- School 1 of vulnhub
- Interpretation of transpose convolution theory (input-output size analysis)
- MRS离线数据分析:通过Flink作业处理OBS数据
- pom.xml 配置文件标签作用简述
- 841. 字符串哈希
- Force buckle 2319 Judge whether the matrix is an X matrix
猜你喜欢
随机推荐
第二十章 使用工作队列管理器(三)
Traversée des procédures stockées Oracle
Equals method
vulnhub之tre1
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
LeetCode_7_5
MRS离线数据分析:通过Flink作业处理OBS数据
Flink并行度和Slot详解
Mongodb由浅入深学习
Mysql, sqlserver Oracle database connection mode
Precautions for cjson memory leakage
最多可以参加的会议数目[贪心 + 优先队列]
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
MIT science and technology review article: AgI hype around Gato and other models may make people ignore the really important issues
R language dplyr package mutate_ At function and min_ The rank function calculates the sorting sequence number value and ranking value of the specified data column in the dataframe, and assigns the ra
力扣 459. 重复的子字符串
CSDN语法说明
gorilla官方:golang开websocket client的示例代码
国家网信办公布《数据出境安全评估办法》:累计向境外提供10万人信息需申报
机器学习笔记 - 使用Streamlit探索对象检测数据集









