POJ 1821 Fence 单调队列优化dp

Fence

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5189 Accepted: 1646

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

Source

Romania OI 2002

题意:
有k个人刷长为n的木板,每个人可以选择不刷,但如果要刷的话必须刷一个完整的区间,区间长度不能大于l[i]。且包含s[i]。每个人刷一块木板可以获得p[i]的收益,求最大的收益。

题解:
可以先写出最朴素的dp方程:
定义dp[i][j]代表前i个人刷前j块木板可以获得的最大收益。
dp[i][j]=max(dp[i-1][j],dp[i][j-1]),即前i个人涂前j块木板的最大收益为前i-1个人涂j块(第i个人不涂)或者第j块木板不涂的最大收益。
或者dp[i][j]=dp[i-1][k]+(j-k)*p[i],k< s[i]且k+l[i]>=j。
如果枚举i,j,k暴力转移肯定是会TLE的,而考虑最后一个dp方程:
dp[i][j]=dp[i-1][k]+j*p[i]-k*p[i],我们会发现dp[i-1][k]-k*p[i]是一个与k无关的量,可以在之前就把它预处理出来,所以维护一个单调递减的队列,每次从队首取出满足条件的最大的k,O(1)转移。
大概是第一道单调队列的题。。(其实应该可以用数据结构来水的)
注意要满足s比较小的先更新,所以先将人按s排一遍序。

朴素代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int K = 100 + 10;
const int N = 16000 + 10;

int n,k;
int dp[K][N];

struct node{
    int l,p,s;
}a[K];

bool operator < (node a,node b){
    return a.s<b.s;
}

int main(){
    n=read(),k=read();
    for(int i=1;i<=k;++i) a[i].l=read(),a[i].p=read(),a[i].s=read();
    sort(a+1,a+k+1);
    for(int i=1;i<=k;++i){
        for(int j=1;j<=n;++j){
            dp[i][j]=std::max(dp[i-1][j],dp[i][j-1]);
            if(j<a[i].s||j>=a[i].s+a[i].l) continue;
            for(int d=max(0,j-a[i].l);d<a[i].s;++d)
            dp[i][j]=max(dp[i][j],dp[i-1][d]+(j-d)*a[i].p);
        }
    }
    printf("%d\n",dp[k][n]);
    return 0;
}

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int K = 100 + 10;
const int N = 16000 + 10;

int n,k;
int dp[K][N];

struct node{
    int l,p,s;
}a[K];

bool operator < (node a,node b){
    return a.s<b.s;
}

deque<int> q;

int main(){
    n=read(),k=read();
    for(int i=1;i<=k;++i) a[i].l=read(),a[i].p=read(),a[i].s=read();
    sort(a+1,a+k+1);
    for(int i=1;i<=k;++i){
        while(!q.empty()) q.pop_back();
        q.push_back(std::max(a[i].s-a[i].l,0));
        for(int j=1;j<=n;++j){
            dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            if(j>=a[i].s+a[i].l) continue;
            while(!q.empty()&&q.front()<j-a[i].l) q.pop_front();
            if(j<a[i].s){//符合条件 入队
                int x=dp[i-1][j]-a[i].p*j;
                while(!q.empty()&&x>dp[i-1][q.back()]-a[i].p*q.back()) q.pop_back();
                q.push_back(j);
                continue;
            }
            dp[i][j]=max(dp[i][j],dp[i-1][q.front()]+(j-q.front())*a[i].p);
        }
    }
    printf("%d\n",dp[k][n]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值