POJ1821 Fence【单调队列优化DP】

这是一个关于优化资源配置的问题,具体是确定K个工人如何在N块木板上粉刷以最大化总收入。每个工人有特定的起始位置Si、最大粉刷长度Li和每块木板的收入Pi。通过动态规划和单调队列,可以解决这个问题,确保每块木板最多被粉刷一次,并最大化总收入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

A team of k (1 <= K <= 100) workers should paint a fence which contains N (1 <= N <= 16 000) planks numbered from 1 to N from left to right. Each worker i (1 <= i <= K) should sit in front of the plank Si and he may paint only a compact interval (this means that the planks from the interval should be consecutive). This interval should contain the Si plank. Also a worker should not paint more than Li planks and for each painted plank he should receive Pi $ (1 <= Pi <= 10 000). A plank should be painted by no more than one worker. All the numbers Si should be distinct.

Being the team’s leader you want to determine for each worker the interval that he should paint, knowing that the total income should be maximal. The total income represents the sum of the workers personal income.

Write a program that determines the total maximal income obtained by the K workers.

题目分析

题意:有n块木板,m个工匠,第i个工匠要么不粉刷,要么粉刷包含木板Si的、长度不超过Li的连续的一段木板,每粉刷一块可以得到Pi的报酬,每块木板至多被粉刷一次,求工匠们能获得的最大总报酬

首先将工人按Si升序排序
设dp[i][j]表示前 i 个工人粉刷前 j 块木板能获得的最大总报酬
转移分为三种
dp[i][j]=dp[i−1][j]dp[i][j]=dp[i-1][j]dp[i][j]=dp[i1][j],即第i个工人不参与粉刷
dp[i][j]=dp[i][j−1]dp[i][j]=dp[i][j-1]dp[i][j]=dp[i][j1],第i个工人粉刷的区域不包含j
dp[i][j]=max(dp[i−1][k]+(j−k)Pi), (j−Li≤k<j且k<Si)dp[i][j]=max(dp[i-1][k]+(j-k)P_i),\ (j-L_i\leq k< j且k<S_i)dp[i][j]=max(dp[i1][k]+(jk)Pi), (jLik<jk<Si),即第i个工人粉刷的区域为k+1到j
其中仅当Si≤j≤Si+Li−1S_i\leq j\leq S_i+L_i-1SijSi+Li1时需要第三种转移

第三种转移是求每段长为Li的区间中的最大值,显然可以单调队列维护
而Si升序排序则保证在转移中不会出现一块木板被粉刷多次

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
 
int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=50010;
int n,m;
struct Worker{int L,P,S;}wrk[maxn];
int dp[110][maxn];
int q[maxn];

bool cmp(Worker a,Worker b){
	return a.S<b.S;
}

int calc(int i,int pos){
	return dp[i-1][pos]-pos*wrk[i].P;
}

int main()
{
    n=read(); m=read();
    for(int i=1;i<=m;++i)
    {
    	wrk[i].L=read();
    	wrk[i].P=read();
    	wrk[i].S=read();
	}
	
	sort(wrk+1,wrk+1+m,cmp);
	
	for(int i=1;i<=m;++i)
	{
		int Si=wrk[i].S, Li=wrk[i].L, Pi=wrk[i].P;
		
		int ll=1,rr=1;
		q[rr++]=0; // 来自dp[i-1][0]的转移
		
		for(int j=1;j<=n;++j)
		{	
			dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i][j-1]));
			
			while(ll<rr&&q[ll]<j-Li) ++ll;
			
			if(j>=Si&&j<=Si+Li-1){
				if(ll<rr) dp[i][j]=max(dp[i][j],calc(i,q[ll])+j*Pi);
			}
			
			if(j<Si) // 转移的位置必须小于Si
			{
				while(ll<rr&&calc(i,q[rr-1])<=calc(i,j)) --rr;
				q[rr++]=j;
			}
		}
	}
	
	printf("%d",dp[m][n]);
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值