POJ 1821--Fence(单调队列优化dp,总结一下)

Fence

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5185 Accepted: 1643

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




题意:长度为n的墙,k个粉刷匠。第 i 个粉刷匠在 si ,他最多可以刷包含 si 的长度为 li 的区间,他刷单位长度获得钱 pi 。求k个粉刷匠最多能赚多少钱?


最近写了一些单调队列优化dp的题目,被这个题卡了一天,写一下题解和单调队列优化dp的看法.

其实能用单调队列优化dp的问题很统一,都能化成dp[i]=max/min(f[k])+g[j]的形式,其中f[k]是只和k有关的函数,g[j]和k无关,这样的问题由于f[0]-f[j-1]的值可以用单调队列保存,每次更新取出队首元素即可,所以可以省掉一维的枚举.

最简单的题目当然是f[0]-f[j-1]都可以用来更新dp[j],但有的题目不是,比如这个题,这个题目是更新的k的范围必须在j-l~s-1之间,所以单调队列中还需要记录k的值的信息,因为k的范围是上届不变,下届递增的,所以可以用单调队列保存,更新的时候让不符合条件的出队即可.

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int MAXN=20000;
struct men{
  int s,p,l;
  void input(){
    scanf("%d%d%d",&l,&p,&s);
  }
}a[110];
int cmp(men x,men y){
    return x.s<y.s;
}

int dp[110][MAXN];
struct node{
    int id;
    int val;
}q[MAXN];
int main(){
    int n,k;
    int cas=0;
    while(scanf("%d%d",&n,&k)!=EOF){
        if(k==0){
            puts("0");
            continue;
        }
        int head,tail;
        for(int i=1;i<=k;i++){
            a[i].input();
            for(int j=1;j<=n;j++)
                dp[i][j]=0;
        }
        sort(a+1,a+k+1,cmp);
        for(int i=1;i<=k;i++){
            head=tail=0;
            int s=a[i].s,l=a[i].l,p=a[i].p;
            //先初始化
            for(int j=1;j<=n;j++){
                dp[i][j]=dp[i-1][j];
                if(j<s)
                    dp[i][j]=max(dp[i][j-1],dp[i][j]);
            }
            head=tail=0;
            for(int j=0;j<=n;j++){
                if(j)
                    dp[i][j]=max(dp[i][j],dp[i][j-1]);
                while(tail>head&&(q[head].id<j-l||q[head].id>=s))
                    head++;
                if(head<tail&&j>=s){
                    dp[i][j]=max(dp[i][j],q[head].val+j*p);
                }
                int x=dp[i-1][j]-j*p;
                if(j<s){
                    while(tail>head&&q[tail-1].val<x){
                        tail--;
                    }
                    q[tail].id=j;
                    q[tail++].val=x;
                }
            }
            /*
            for(int j=1;j<=n;j++)
                printf("%d ",dp[i][j]);
            printf("\n");
            */

        }
        int res=0;
        for(int i=1;i<=n;i++){
            res=max(res,dp[k][i]);
        }
        printf("%d\n",res);
    }
}




点赞