当前位置:网站首页>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
边栏推荐
- 九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
- 力扣 912.排序数组
- Semantic SLAM源码解析
- Machine learning notes - explore object detection datasets using streamlit
- Equals method
- 力扣 2319. 判断矩阵是否是一个 X 矩阵
- Kubernetes -- detailed usage of kubectl command line tool
- Openeuler prize catching activities, to participate in?
- 力扣 88.合并两个有序数组
- Force buckle 1037 Effective boomerang
猜你喜欢
随机推荐
微信公众号OAuth2.0授权登录并显示用户信息
841. 字符串哈希
Traversée des procédures stockées Oracle
The boundary of Bi: what is bi not suitable for? Master data, Martech? How to expand?
JVM GC垃圾回收简述
MSE API learning
php 获取图片信息的方法
CJSON内存泄漏的注意事项
Detailed explanation of Flink parallelism and slot
vulnhub之school 1
Force buckle 674 Longest continuous increasing sequence
openEuler 资源利用率提升之道 01:概论
About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]
力扣599. 两个列表的最小索引总和
Boot 和 Cloud 的版本选型
[sword finger offer] sword finger offer II 012 The sum of left and right subarrays is equal
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
ASP. Net learning & ASP's one word
My creation anniversary
Open source heavy ware! Chapter 9 the open source project of ylarn causal learning of Yunji datacanvas company will be released soon!