当前位置:网站首页>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
边栏推荐
- Interpretation of transpose convolution theory (input-output size analysis)
- Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
- MSE API学习
- 一. 基础概念
- ASP. Net learning & ASP's one word
- 力扣 88.合并两个有序数组
- Gorilla official: sample code for golang to open websocket client
- 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
- 数据孤岛是企业数字化转型遇到的第一道险关
- 【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
猜你喜欢
随机推荐
[résolution] le paquet « xxxx» n'est pas dans goroot
About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]
使用高斯Redis实现二级索引
力扣 2315.统计星号
【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
Boot 和 Cloud 的版本选型
多个线程之间如何协同
Open source heavy ware! Chapter 9 the open source project of ylarn causal learning of Yunji datacanvas company will be released soon!
关于自身的一些安排
Force buckle 599 Minimum index sum of two lists
Machine learning notes - explore object detection datasets using streamlit
pom. XML configuration file label: differences between dependencies and dependencymanagement
数据孤岛是企业数字化转型遇到的第一道险关
openEuler 资源利用率提升之道 01:概论
微信公众号OAuth2.0授权登录并显示用户信息
最多可以参加的会议数目[贪心 + 优先队列]
mock.js从对象数组中任选数据返回一个数组
AIRIOT助力城市管廊工程,智慧物联守护城市生命线
equals 方法
Traversée des procédures stockées Oracle