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

Fence
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3485   Accepted: 1047

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

[Submit]   [Go Back]   [Status]   [Discuss]

题目地址:http://poj.org/problem?id=1821

最近做了一些单调优化dp.发现此类dp题目的优化体现在状态转移方程里面,即计算dp时的优化,一般能将部分O(n)的复杂度降到O(1)

以此题为例:

【题意】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

显然直接做就是枚举i,j,k。

观察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
#include
#include
#include
using namespace std;
int dp[110][16000+50];

struct person
{
    int l,p,s;
}P[110];
int cmp(person p1,person p2)
{
    return p1.s=P[i].s+P[i].l)//这些木块涂不了
                    continue;
                while(front

没考虑清楚k的范围WA了好长时间=_=

你可能感兴趣的:(poj 1821 Fence(dp+单调队列优化))