您的位置:首页 > Web前端

【poj1821】Fence 单调队列优化DP

2016-03-14 10:47 295 查看

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]=max⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪dp[i−1][j]第i个人不刷 1<=j<=ndp[i][j−1]第j个木板不刷 1<=j<=nmax{dp[i−1][k]+(j−k)∗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[i−1][k]−k∗p[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
[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
[i]);
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: