poj 1821 Fence(dp+单调队列优化)

Fence

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5830 Accepted: 1857

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. 

Input

The input contains: 
Input 

N K 
L1 P1 S1 
L2 P2 S2 
... 
LK PK SK 

Semnification 

N -the number of the planks; K ? the number of the workers 
Li -the maximal number of planks that can be painted by worker i 
Pi -the sum received by worker i for a painted plank 
Si -the plank in front of which sits the worker i 

Output

The output contains a single integer, the total maximal income.

Sample Input

8 4
3 2 2
3 2 3
3 3 5
1 1 7 

Sample Output

17

Hint

Explanation of the sample: 

the worker 1 paints the interval [1, 2]; 

the worker 2 paints the interval [3, 4]; 

the worker 3 paints the interval [5, 7]; 

the worker 4 does not paint any plank 

题意:K个人对N块木板涂色,每个人初始站在一块木板前(不重复涂),每人最多只能涂包含所站木板的连续l个木板或一个木板也不涂。给出每人最多涂的木块数l,涂一快木板的工钱p,站的木板s。求这群人最多共获得多少工钱。

解 :dp[i][j]表示前i个人对前j块木板操作的最大收益。

那么 dp[i][j]=max(dp[i][j-1],dp[i-1][k]+P[i].p*(j-k),dp[i-1][j])

         max(0,P[i].s-P[i].l) <= k< min(P[i].s,j)    ,因为必须涂包含所站木板,所以k得小于p[i].s

观察dp[i-1][k]+P[i].p*(j-k)=(dp[i-1][k]-P[i].p*k)+P[i].p*j

在枚举k的时候,P[i].p*j的值已经确定不用考虑,只需要找出使(dp[i-1][k]-P[i].p*k)最大的k就行了,又观察到这个式子的值不与j相关,也就是说在枚举k之前我们就可以通过某种方法找出这个k,即:构造递减的 单调队列 维护k值。

  

#include<map>
#include<vector>
#include<iostream>
#include<queue>
#include<deque>
#include<math.h>
#include<algorithm>
#include<stdio.h>
#include<string>
#include<string.h>
#define ll long long
const int maxn=1e7+10;
using namespace std;
int dp[110][16111];
struct node
{
    int l,p,s;
}p[110];
int q[16111];//单调队列,存单调递减的最优解的k值
int cmp(node n1,node n2)
{
    return n1.s<n2.s;
}
int main()
{
    int n,k,en,ed;
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=1;i<=k;i++)
            scanf("%d%d%d",&p[i].l,&p[i].p,&p[i].s);
        memset(dp,0,sizeof(dp));
        sort(p+1,p+1+k,cmp);
        for(int i=1;i<=k;i++)//枚举每个人
        {
            en=0,ed=1;
            q[0]=max(0,p[i].s-p[i].l);//这个人能涂的最前端
            for(int j=1;j<=n;j++)
            {
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                if(j>=p[i].s+p[i].l||j<p[i].s-p[i].l) continue;//如果不在这个人涂的范围内
                while(en<ed&&q[en]+p[i].l<j) en++;//如果这个人不能从存的k涂到j,换次优的k值
                if(j<p[i].s)//说明满足k的取值范围,能到这边肯定是在涂的范围内,且能涂到所站点
                {
                    int temp=dp[i][j]-p[i].p*j;
                    while(en<ed&&temp>dp[i][q[ed-1]]-p[i].p*q[ed-1])
                        ed--;//枚举的k值比上一个k值更优则一直去掉直到上一位比现在的k更优
                    q[ed++]=j;
                    continue;
                }
                dp[i][j]=max(dp[i][j],dp[i][q[en]]+p[i].p*(j-q[en]));
            }
        }
        printf("%d\n",dp[k][n]);

    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值