Fence

Time Limit: 1000ms
Memory Limit: 30000KB

This problem will be judged on PKU. Original ID: 1821
64-bit integer IO format: %lld      Java class name: Main

 
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

 
解题:单调队列优化dp
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = ;
struct Worker{
int L,S,P;
bool operator<(const Worker &rhs) const{
return S < rhs.S;
}
}a[maxn];
int n,m,L[maxn],R[maxn],dp[][maxn],q[maxn]; int main(){
while(~scanf("%d%d",&n,&m)){
for(int i = ; i <= m; ++i)
scanf("%d%d%d",&a[i].L,&a[i].P,&a[i].S);
sort(a + , a + m + );
for(int i = ; i <= m; ++i){
L[i] = max(,a[i].S - a[i].L);
R[i] = min(n,a[i].S + a[i].L - );
}
for(int i = ; i <= m; ++i){
for(int j = ; j <= R[i]; ++j)
dp[i][j] = dp[i-][j];
int hd = ,tl = ;
for(int j = L[i]; j < a[i].S; ++j){
while(hd < tl && dp[i-][j] - j*a[i].P >= dp[i-][q[tl-]] - q[tl-]*a[i].P) --tl;
q[tl++] = j;
}
for(int j = a[i].S; j <= R[i]; ++j){
while(hd < tl && j - q[hd] > a[i].L) ++hd;
dp[i][j] = max(dp[i-][j],dp[i][j-]);
dp[i][j] = max(dp[i][j],dp[i-][q[hd]] + (j - q[hd])*a[i].P);
}
for(int j = R[i] + ; j <= n; ++j)
dp[i][j] = max(dp[i-][j],dp[i][j-]);
}
int ret = ;
for(int i = ; i <= n; ++i)
ret = max(ret,dp[m][i]);
printf("%d\n",ret);
}
return ;
}

POJ 1821 Fence的更多相关文章

  1. poj 1821 Fence(单调队列优化DP)

    poj 1821 Fence \(solution:\) 这道题因为每一个粉刷的人都有一块"必刷的木板",所以可以预见我们的最终方案里的粉刷匠一定是按其必刷的木板的顺序排列的.这就 ...

  2. poj 1821 Fence 单调队列优化dp

    /* poj 1821 n*n*m 暴力*/ #include<iostream> #include<cstdio> #include<cstring> #incl ...

  3. poj 1821 Fence(单调队列)

    题目链接:http://poj.org/problem?id=1821 题目分析来自:http://blog.csdn.net/tmeteorj/article/details/8684453 连续的 ...

  4. POJ 1821 Fence (算竞进阶习题)

    单调队列优化dp 我们把状态定位F[i][j]表示前i个工人涂了前j块木板的最大报酬(中间可以有不涂的木板). 第i个工人不涂的话有两种情况: 那么F[i - 1][j], F[i][j - 1]就成 ...

  5. POJ 1821 Fence(单调队列优化DP)

    题解 以前做过很多单调队列优化DP的题. 这个题有一点不同是对于有的状态可以转移,有的状态不能转移. 然后一堆边界和注意点.导致写起来就很难受. 然后状态也比较难定义. dp[i][j]代表前i个人涂 ...

  6. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  7. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  8. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  9. POJ 3253 Fence Repair (贪心)

    Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

随机推荐

  1. [Codeforces Round495A] Sonya and Hotels

    [题目链接] https://codeforces.com/contest/1004/problem/A [算法] 直接按题意模拟即可 时间复杂度 :O(NlogN) [代码] #include< ...

  2. 【BZOJ 3032】 七夕祭

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3032 [算法] 交换左右两个相邻格子的摊点,不会改变这一行的摊点个数 交换上下两个相 ...

  3. P3258 [JLOI2014]松鼠的新家 树链剖分

    这个题就是一道树剖板子题,就是每走一步就把所有的经过点加一就行了.还有,我的树剖板子没问题!!!谁知道为什么板子T3个点!我不管了!反正这道题正常写A了. 题干: 题目描述 松鼠的新家是一棵树,前几天 ...

  4. [luogu_U15118]萨塔尼亚的期末考试

    https://zybuluo.com/ysner/note/1239615 题面 \(T\)次询问,求出\[\sum_{i=1}^n\frac{i}{\frac{n(n+1)}{2}}fib_i\] ...

  5. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  6. python lambda表达式&map/filter/reduce

    习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: 1 2 3 4 5 6 7 8 # 普通条件语句 if 1 == 1:     name = 'wupeiqi' else ...

  7. 如何在vue项目中引入阿里巴巴的iconfont图库

    1. 打开 http://www.iconfont.cn/ 2. 选择我们喜欢的图标,点击上面的小车,加入图标库,即右侧的购物车 3.点击购物车,点击下载代码 4.解压下载的文件夹,将文件夹复制到 a ...

  8. 最全的C/C++入门到进阶的书籍推荐,你需要嘛?

    编程是操作性很强的一门知识,看书少不了,但只有学习和实践相结合才能起到很好的效果,一种学习方法是看视频->看书->研究书中例子->自己做些东西->交流->看书. 研究经典 ...

  9. JVM-垃圾回收器

    目录 垃圾收集器 Serial收集器 Serial Old 收集器 ParNew 收集器 Parallel Scavenge 收集器 (并行清除) /'pærəlɛl/ /'skævɪndʒ/ Par ...

  10. Appium Appium 链接夜神模拟器

    在此之前,已经安装Appium,参考第一部分在 Windows7 搭建 Appium (一) https://testerhome.com/topics/8004 第一步安装Android开发环境 下 ...