题意

Language:Default
Fence
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6478 Accepted: 2129

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

K个人对N块木板涂色,每个人初始站在一块木板前(不重复),每人最多只能涂包含所站木板的连续l个木板或一个木板也不涂。给出每人最多涂的木块数l,涂一快木板的工钱p,站的木板s。求这群人最多共获得多少工钱。

分析

参照mousehao001的题解。

dp[i][j]表示前i个人对前j块木板操作的最大收益。

核心状态转移方程:

dp[i][j]=max(dp[i][j-1],dp[i-1][k]+P[i].p*(j-k),dp[i-1][j])

max(0,P[i].s-P[i].l)<=k<min(P[i].s-1,j)  k=0表示前i-1个人在玩泥巴。。

显然直接做就是枚举i,j,k。

观察dp[i-1][k]+P[i].p*(j-k)=(dp[i-1][k]-P[i].p*k)+P[i].p*j

在枚举k的时候,P[i].p*j的值已经确定不用考虑,只需要找出使(dp[i-1][k]-P[i].p*k)最大的k就行了,又观察到这个式子的值不与j相关,也就是说在枚举k之前我们就可以通过某种方法找出这个k,即:构造递减的 单调队列 维护k值。

时间复杂度\(O(MN)\)

代码

#include<iostream>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std; co int N=16001,M=101;
struct rec{int L,P,S;}a[M];
bool operator<(co rec&a,co rec&b) {return a.S<b.S;}
int n,m,f[M][N],q[N];
int calc(int i,int k){
return f[i-1][k]-a[i].P*k;
}
int main(){
read(n),read(m);
for(int i=1;i<=m;++i) read(a[i].L),read(a[i].P),read(a[i].S);
sort(a+1,a+m+1);
for(int i=1;i<=m;++i){
int l=1,r=0;
for(int k=max(0,a[i].S-a[i].L);k<=a[i].S-1;++k){
while(l<=r&&calc(i,q[r])<=calc(i,k)) --r;
q[++r]=k;
}
for(int j=1;j<=n;++j){
f[i][j]=max(f[i-1][j],f[i][j-1]);
if(j>=a[i].S){
while(l<=r&&q[l]<j-a[i].L) ++l;
if(l<=r) f[i][j]=max(f[i][j],calc(i,q[l])+a[i].P*j);
}
}
}
printf("%d\n",f[m][n]);
return 0;
}

POJ1821 Fence的更多相关文章

  1. [POJ1821]Fence(单调队列优化dp)

    [poj1821]Fence 有 N 块木板从左至右排成一行,有 M 个工匠对这些木板进行粉刷,每块木板至多被粉刷一次.第 i 个工匠要么不粉刷,要么粉刷包含木板 Si 的,长度不超过Li 的连续一段 ...

  2. poj1821 Fence【队列优化线性DP】

    Fence Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6122   Accepted: 1972 Description ...

  3. POJ1821 Fence 题解报告

    传送门 1 题目描述 A team of $k (1 <= K <= 100) $workers should paint a fence which contains \(N (1 &l ...

  4. poj1821 Fence(单调队列优化dp)

    地址 一排N个木板,M个工匠站在不同位置$S_i$,每个人可以粉刷覆盖他位置的.最长长度为$L_i$木板段,每刷一个有$P_i$报酬.同一木板只刷一次.求最大报酬. 根据每个人的位置dp,设$f[i] ...

  5. $Poj1821\ Fence\ $单调队列优化$DP$

    Poj   Acwing Description 有N块木板等待被M个工匠粉刷,每块木板至多被刷一次.第i个工匠要么不粉刷,要么粉刷包含木块Si的,长度不超过Li的连续的一段木板,每粉刷一块可以得到P ...

  6. poj1821 Fence(dp,单调队列优化)

    题意: 由k(1 <= K <= 100)个工人组成的团队应油漆围墙,其中包含N(1 <= N <= 16 000)个从左到右从1到N编号的木板.每个工人i(1 <= i ...

  7. poj1821——Fence

    题意: 一个栅栏一共有n(从1--n)个木板,我们找k个工人去粉刷它,li表示每个人有限制粉刷木板数量,pi表示粉刷一个木板得到的钱,si表示他开始在那个木板前面 如果一个工人要粉刷,那么他必须粉刷s ...

  8. 单调队列与DP

    算是一个总结吧! 先来一个模板: TYVJ 1305 最大子序和 题目描述 输入一个长度为n的整数序列,从中找出一段不超过M的连续子序列,使得整个序列的和最大. 例如 1,-3,5,1,-2,3 当m ...

  9. Fence(poj1821)

    Fence Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4705   Accepted: 1489 Description ...

随机推荐

  1. 解决github访问及上传慢的问题

    在本地host文件中添加映射 http://tool.chinaz.com/dns , 查询 github.global.ssl.fastly.net 和 assets-cdn.github.com ...

  2. 03.v-cloak、v-v-text、v-html、v-bind、v-on

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 在pycharm中运行python程序

    安装PyCharm 安装过程取决于您的操作系统: 在Windows上安装PyCharm 运行.exe您已下载的文件,并按照PyCharm安装向导的说明进行操作. 在macOS上安装PyCharm 打开 ...

  4. nodejs web API 相关杂项

    场景是这样的: docker-compose中起多个服务,其中有一个是nodejs写的作为web API. 这个API 的使用者有2类: 1 docker-compose网络内其他特定服务访问,作为C ...

  5. vue-先渲染dom载执行js

    价格判断v-if=“dataList”有数据的时候才渲染

  6. Katy Perry - E.T.

    作曲 : Katy Perry, Łukasz Gottwald, Max Martin, Joshua Coleman 作词 : Katy Perry, Łukasz Gottwald, Max M ...

  7. 【异常】idea执行Main方法出现 Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest

    一.异常复现步骤 1)首先得是一个Spring MVC项目 注:Spring Boot项目有内置的web 容器,不会出现该问题 2)main方法存在于使用HttpServletRequest类的类中 ...

  8. python和jupyter安装

    python官网:https://www.python.org/ 进去之后选择适合自己电脑的系统类型,安装,我的是windows  下载之后,双击打开 在安装时请勾选上add to path 选项,安 ...

  9. vue 日历组件只显示本月和下个月 -- 多选日期

    效果就是上图 代码贴出 1.在components > calander > index.vue <template> <div class="page&quo ...

  10. nlp基础(一)基本应用

    1.问答系统,它主要是针对那些有明确答案的用户问题,而且通常面向特定的领域,比如金融,医疗,这一类的机器人.它的技术实现方案分为基于检索和基于知识库两大类. 2.第二个任务型对话系统,大家看论文的时候 ...