【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 2002k个人要刷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; }
相关文章推荐
- css实现高度不固定的div元素模块在页面中水平垂直居中
- jQuery中ready与load事件的区别
- JS获取IP、MAC和主机名的几种方法
- [Offer收割]编程练习赛2 hihocoder 1273 (DFS + 状压)
- jquery table的隔行变色 鼠标事件
- @import
- 用javascript插入样式
- CSS实现垂直居中的常用方法
- [Offer收割]编程练习赛2 hihocoder 1272 买零食 (DFS 或 dp 水题)
- 浅谈各种浏览器下的CSS Hack兼容性写法
- no datanode to stop
- 【JS-01】正则表达式获取url的参数值
- extjs 中绑定事件是.on('事件',function(){}),那如果是想移除已绑定的事件该怎么写啊?
- 【读书笔记】CSS3学习(三)
- HTML--JS练习小游戏(别踩白块儿)
- 使用selenium+phantomJS实现网页爬取
- javascript小实例,PC网页里的拖拽
- forever nodejs管理进程
- JS 高级总结
- CSS体系总结