poj1821 Fence【队列优化线性DP】
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 6122 | Accepted: 1972 |
Description
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
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
Sample Input
8 4
3 2 2
3 2 3
3 3 5
1 1 7
Sample Output
17
Hint
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

当我们循环j,k时,可以把i看成是定值,于是对于这条方程我们可以进行一定的变形,将j和k分离。

#include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f int k, n;
struct node{
int l, s, p;
}worker[];
int que[], dp[][];
bool cmp(node a, node b)
{
return a.s < b.s;
} int cal(int i, int k)
{
return dp[i - ][k] - worker[i].p * k;
} int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(scanf("%d%d", &n, &k)!= EOF){
for(int i = ; i <= k; i++){
scanf("%d%d%d", &worker[i].l, &worker[i].p, &worker[i].s);
//printf("%d%d%d\n", worker[i].l, worker[i].s, worker[i].p);
}
sort(worker + , worker + + k, cmp); /*for(int i = 1; i <= k; i++){
printf("%d%d%d\n", worker[i].l, worker[i].s, worker[i].p);
}*/
for(int i = ; i <= k; i++){
int l = , r = ;
for(int x = max(, worker[i].s - worker[i].l); x <= worker[i].s - ; x++){
while(l <= r && cal(i, que[r]) <= cal(i, x)){
r--;
}
que[++r] = x;
}
for(int j = ; j <= n; j++){
dp[i][j] = max(dp[i - ][j], dp[i][j - ]);
if(j >= worker[i].s){
while(l <= r && que[l] < j - worker[i].l)l++;
if(l <= r){
dp[i][j] = max(dp[i][j], cal(i, que[l]) + worker[i].p * j);
}
//cout<<dp[i][j]<<endl;
}
}
} //int ans = 0;
//for(int i = 1; i <= n; i++){
// ans = max(ans, dp[k][i]);
//}
printf("%d\n", dp[k][n]);
}
}
poj1821 Fence【队列优化线性DP】的更多相关文章
- 【NOIP2017】跳房子 题解(单调队列优化线性DP)
前言:把鸽了1个月的博客补上 ----------------- 题目链接 题目大意:机器人的灵敏性为$d$.每次可以花费$g$个金币来改造机器人,那么机器人向右跳的范围为$[min(d-g,1),m ...
- hdu-3401-Trade-单调队列优化的DP
单调队列入门题... dp[i][j]:第i天.手中拥有j个股票时,获得的最大利润. 若第i天不买不卖:dp[i][j]=max(dp[i][j],dp[i-1][j]); 若第i天买 ...
- 一类利用队列优化的DP
I.导入: 这是一个\(O(n^2)\)的状态和转移方程: \[f(i,j)=\left\{ \begin{aligned} f(i-1,j-1)+k \ (1\leq j)\\ \max_{k \i ...
- [bzoj 2726] 任务安排 (斜率优化 线性dp)
3月14日第三题!!!(虽然是15号发的qwq) Description 机器上有N个需要处理的任务,它们构成了一个序列.这些任务被标号为1到N,因此序列的排列为1,2,3-N.这N个任务被分成若干批 ...
- HDU1024 Max Sum Plus Plus (优化线性dp)
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- Trade-----HDU3401----单调队列优化的DP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题目意思: 有T天,你每天可以以API买进,BPI卖出,最多买ASI个,最多卖BSI个 最多只能 ...
- 洛谷p1725 露琪诺 单调队列优化的DP
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int ...
- 单调队列以及单调队列优化DP
单调队列定义: 其实单调队列就是一种队列内的元素有单调性的队列,因为其单调性所以经常会被用来维护区间最值或者降低DP的维数已达到降维来减少空间及时间的目的. 单调队列的一般应用: 1.维护区间最值 2 ...
- BestCoder Round #89 02单调队列优化dp
1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01 HDU 5944 水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...
随机推荐
- free 和delete 把指针怎么啦?
别看 free 和 delete 的名字恶狠狠的(尤其是 delete),它们只是把指针所指的内存给 释放掉,但并没有把指针本身干掉. 发现指针 p 被 free 以后其地址仍然不变(非 NULL), ...
- 用C语言实现解析简单配置文件的小工具
本文介绍作者写的一个小工具,简单的代码中包含了C语言对字符串的处理技巧,对文本文件的简单解析,二进制文件的数据复制的方法,以及格式化输出文本文件的示例. 工具的输入是如下内容的配置文件: ;资源管理器 ...
- jquery 中json数组的操作(转)
在jquery中处理JSON数组的情况中遍历用到的比较多,但是用添加移除这些好像不是太多. 今天试过json[i].remove(),json.remove(i)之后都不行,看网页的DOM对象中好像J ...
- shell脚本中特定符合变量的含义
shell脚本中特定符合变量的含义: $# 传递到脚本的参数个数 $* 以一个单字符串显示所有向脚本传递的参数.与位置变量不同,此选项参数可超过9个 $$ 脚本运行的当前进程PID号 ...
- 怎样批量修改MathType公式格式
MathType是一款数学公式编辑器,我们在写论文的时候常常会遇到,但是有时由于公式的样式.大小和间隔等不符合论文要求,这个时候我们如果一个个修改是很麻烦的,还容易出错.所以批量修改就非常的有必要了, ...
- 【matlab】使用VideoReader提取视频的每一帧,不能用aviread函数~
这个问题是matlab版本问题,已经不用aviread函数了~ VideoReader里面没有cdata这个函数! MATLAB不支持avireader了,而且没有cdata这个属性了,详情去官网ht ...
- css常用代码含义
1.font:12px Arial, Helvetica, sans-serif: 使用了缩写,完整的代码应该是:font-size:12px; font-family:Tahoma:说明字体为12像 ...
- linux环境中,查询网卡的速度(带宽)
需求描述: 今天一同事要整理测试环境的主机硬件配置信息,需要提供网卡的速度的信息, 所以,就查询了下,在此记录下. 操作过程: 1.首先通过ip a命令查询主机的网口名称 [root@redhat6 ...
- HTML&CSS精选笔记_HTML与CSS网页设计概述
HTML与CSS网页设计概述 Web基本概念 认识网页 网页主要由文字.图像和超链接等元素构成.当然,除了这些元素,网页中还可以包含音频.视频以及Flash等. 名词解释 Internet网络 就是通 ...
- ios 获取手机相关的信息
获取手机信息 应用程序的名称和版本号等信息都保存在mainBundle的一个字典中,用下面代码可以取出来 //获取版本号 NSDictionary *infoDict = [[NSBundl ...