题意

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;
}

最新文章

  1. libevent源码分析:hello-world例子
  2. Ubuntu下安装boost
  3. JAVA上百实例源码以及开源项目
  4. CSS中相对定位与绝对定位
  5. 安卓开发_浅谈Android动画(四)
  6. push to deploy
  7. Bean实例化(Spring源码阅读)-我们到底能走多远系列(33)
  8. python读取数据库数据,读取出的中文乱码问题
  9. sql openxml sp_xml_preparedocument xml 中文乱码
  10. BZOJ 3406 乳草的入侵
  11. spring框架中一个跟String的trim方法一样的方法
  12. swift switch语句
  13. GNU INET SOCKET
  14. ASP.NET 表单认证与角色授权
  15. 设计模式 - Abstract Factory模式(abstract factory pattern) 详细说明
  16. [js高手之路]深入浅出webpack教程系列9-打包图片(file-loader)用法
  17. hihocoder Challenge 29 D. 不上升序列
  18. C++STL二维vector指定位置排序
  19. hdu 1276士兵队列问题【queue】
  20. vuex中的辅助函数 mapState,mapGetters, mapActions, mapMutations

热门文章

  1. 源码mysql-5.7.23在cmake时出现的小问题
  2. ES5 map循环一大坑:循环遍历竟然出现逗号!
  3. mysql数据库的基础操作
  4. volatile关键字的作用
  5. 关于wincc与博图的安装问题
  6. 命令行听歌http://www.linuxsir.org/bbs/thread280142.html?pageon=1#1584689
  7. 浮点型的三个特殊值 Double.NEGATIVE_INFINITY Double.POSITIVE_INFINITY Double.NaN
  8. mysql sql文件批量处理
  9. day32-python阶段性复习六
  10. 大数据面试题——如何从大量的url中找出相同的url