【poj1821】Fence 单调队列优化DP

该博客主要介绍了如何解决编程竞赛题目POJ1821 Fence,该问题涉及到k名工人最大化收益地粉刷包含n块木板的栅栏。每个工人能粉刷的区间有限,并且每块木板只能被刷一次。文章详细阐述了如何利用动态规划和单调队列进行优化,以实现更高效的解决方案。
摘要由CSDN通过智能技术生成

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块木板,第i个人站在第si块木板上,他可以粉刷长度为li的区间,可以不刷但如果刷的话必须包括si。每块木板只能刷一次,每个人刷一块木板可以获得pi元。求最大收益和。

先写朴素的状态转移方程,很明显要以人为阶段,要先按si排序。
dp[i][j] 表示前i个人刷前j块木板的最大收益。

dp[i][j]=maxdp[i1][j]i 1<=j<=ndp[i][j1]j 1<=j<=nmax{dp[i1][k]+(jk)p[i]}[k+1,j] s[i]-l[i]<=k<=s[i]-1,s[i]<=j<=s[i]+l[i]-1,j-k<=l[i]

我们发现,对于第三个式子,若固定i和j,则第三个式子的大小取决于 dp[i1][k]kp[i] ,由于k随着j的增大,下界也在增大。所以维护【入队时间单调递增(k),权值单调递减】的单调队列,每次取队头就可以 O(1) 更新了。

完了之后还要for一遍取max…因为这个调了半天

朴素版:

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

typedef long long LL;
const int SZ = 200010;
const int INF = 1000000010;

struct haha{
    int l,p,s;
}l[SZ];

bool cmp(haha a,haha b) { return a.s < b.s; }

int dp[110][SZ];
deque<int> q;

int main()
{
    int n,k;
    scanf("%d%d",&k,&n);
    for(int i = 1;i <= n;i ++)
        scanf("%d%d%d",&l[i].l,&l[i].p,&l[i].s);
    sort(l + 1,l + 1 + n,cmp);

    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= k;j ++)
            dp[i][j] = max(dp[i][j],max(dp[i - 1][j],dp[i][j - 1]));
        for(int j = l[i].s;j <= l[i].s + l[i].l - 1;j ++)
        {
            for(int k = j - l[i].l;k <= l[i].s - 1;k ++)
                if(k >= 0)  
                {
                    dp[i][j] = max(dp[i][j],dp[i - 1][k] + (j - k) * l[i].p);
            //      printf("i : %d  j : %d\n",i,j);
            //      printf("%d %d\n",dp[i][j],dp[i - 1][k] - k * l[i].p);
                }
        }
    }
    int ans = 0;
    for(int i = 1;i <= K;i ++)
        ans = max(ans,dp[n][i]);
    printf("%d\n",ans);
    return 0;
}





优化之后:

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

typedef long long LL;
const int SZ = 200010;
const int INF = 1000000010;

struct haha{
    int l,p,s;
}l[SZ];

bool cmp(haha a,haha b) { return a.s < b.s; }

int dp[110][SZ];

struct hoho{
    int k,x;
};

deque<hoho> q;

int main()
{
    int n,K;
    scanf("%d%d",&K,&n);
    for(int i = 1;i <= n;i ++)
        scanf("%d%d%d",&l[i].l,&l[i].p,&l[i].s);
    sort(l + 1,l + 1 + n,cmp);

    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= K;j ++)
            dp[i][j] = max(dp[i - 1][j],dp[i][j - 1]);

        while(q.size()) q.pop_back();
        for(int k = max(l[i].s - l[i].l,0);k <= l[i].s - 1;k ++)
        {
            int tmp = dp[i - 1][k] - k * l[i].p;
            while(q.size() && q.back().x < tmp) q.pop_back();
            q.push_back((hoho){k,tmp});
        }

        for(int j = l[i].s;j <= l[i].s + l[i].l - 1;j ++)
        {
            while(q.size() && q.front().k < j - l[i].l) q.pop_front();
            dp[i][j] = max(dp[i][j],q.front().x + j * l[i].p);
        }
    }
    int ans = 0;
    for(int i = 1;i <= K;i ++)
        ans = max(ans,dp[n][i]);
    printf("%d\n",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值