poj 1821(DP+单调队列优化)

Fence
Time Limit: 1000MS   Memory Limit: 30000K
     

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 


解题思路:这题一眼就可以知道是用动态规划的思想,而且是属于比较常见的类型。。。。
设dp[i][j]表示前i个工人完成j块木板所获的最大价值,所以可以分析一下,dp[i][j]可以由两个状态转移过来:
1)dp[i][j] = dp[i-1][j] 不需要第i个工人,直接由前i-1个工人把j块木板完成了。
2)dp[i][j] = max{dp[i][j],dp[i-1][t-1]+(j-t+1)*p[i]} 即前i-1个工人完成了t-1块木板,剩下的j-t+1给第i个工人完成。
这样定义状态的话,最优值保存在dp[1....k][1....n]当中,所以每次循环都直接把最大值保存起来就OK了。
但是数据量较大,TLE。。

TLE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 16005;
struct node
{
	int l,p,s;
}worker[110];
int n,k,dp[110][maxn]; //dp[i][j]表示前i个工人完成j块木板所得到的最大价值

int cmp(node a, node b)
{
	if(a.s != b.s)
		return a.s < b.s;
	return a.p > b.p;
}

int main()
{	
	while(scanf("%d%d",&n,&k)!=EOF)
	{	
		for(int i = 1; i <= k; i++)
			scanf("%d%d%d",&worker[i].l,&worker[i].p,&worker[i].s);
		sort(worker+1,worker+1+k,cmp);
		memset(dp,0,sizeof(dp));
		int ans = 0;
		for(int i = 1; i <= k; i++)
			for(int j = 1; j <= n; j++)
			{
				dp[i][j] = dp[i-1][j];
				if(j < worker[i].s) continue;
				for(int t = j; t >= j - worker[i].l + 1 && t > 0; t--)
				{
					if(worker[i].s < t) continue;
					dp[i][j] = max(dp[i][j],dp[i-1][t-1] + (j-t+1)*worker[i].p);
					ans = max(ans,dp[i][j]);
				}
			}
		printf("%d\n",ans);
	}
	return 0;
}


我看了别人的解题报告,确实感觉差距不小。。。。我把大神的copy过来,然后按照自己的状态方程把他的修改了。。。

第一种状态直接转移,第二种必须要用些优化才能节约时间dp[i-1][k]+p[i]*(j-k)=dp[i-1][k]-p[i]*k+p[i]*j,其中p[i]*j对固定dp[i][j]是固定的,即dp[i-1][k]-p[i]*k越大越好,所以可以用优先队列将所有能够通过第三种方式更新dp[i][j]储存起来,能够更新需要满足两个条件k<Si且k+Li>=j,所以可以首先将[Si-Li,Si-1]区间的值预处理出来,并在每次选取优先队列中元素时判断它是否满足k+Li>=j即可(如果不满足,因为j是递增的,它以后也不会满足,所以可以直接pop掉)

用我自己的状态方程修改的代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct Data
{
    int val,pos;
    bool operator<(const Data &ne)const
    {
        if(val!=ne.val)
            return val<ne.val;
        else
            return pos>ne.pos;
    }
    Data(){}
    Data(int _val,int _pos){val=_val;pos=_pos;}
}temp[16002];
struct People
{
    int l,p,s;
    bool operator<(const People &ne)const
    {
        return s < ne.s;
    }
}po[102];
int dp[102][16002];
int main()
{
    int n,m,top;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=m;i++)
            scanf("%d%d%d",&po[i].l,&po[i].p,&po[i].s);
        po[m+1].p=0;
        sort(po+1,po+m+1);
        memset(dp,0,sizeof(dp));
		int ans = 0;
        for(int i=1;i<=m;i++)
        {
            int ll=po[i].l,pp=po[i].p,ss=po[i].s,pr=po[i+1].p;
            priority_queue<Data> Q;
            for(int j=max(0,ss-ll);j<ss;j++)
                Q.push(Data(dp[i-1][j]-j*pp,j));
            for(int j=1;j<=n;j++)
            {
                dp[i][j]=dp[i-1][j];
                if(j<ss||ss+ll-1<j)
                    continue;
                while(!Q.empty()&&Q.top().pos+ll<j)
                    Q.pop();
                if(Q.empty())
                    continue;
                dp[i][j]=max(dp[i][j],Q.top().val+pp*j);
				ans = max(ans,dp[i][j]);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(dp)