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

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

  1. 8 4
  2. 3 2 2
  3. 3 2 3
  4. 3 3 5
  5. 1 1 7

Sample Output

  1. 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 
思路:dp+单调队列;
首先我们要对原来的点按顺序排,然后dp[i][j]表示前i个人喷漆到j个位置结束的最大值,那么转移方程是dp[i][j] = max(dp[i-1][j],dp[i-1][j-s]+s*ans.p);这样n^3肯定不行,然后方程可写为dp[i-1][k]+(j-k)*ans.p=dp[i-1][k]-k*ans.p+j*ans.p,因为第二层循环中的j是不变的,(max(0,j-ans.l)<=k<ans.s),那么ans.l定,ans.s定,当j增大时区间范围减小,然后单调队列维护下最大值即可。复杂度O(n*m);
  1. 1 #include<stdio.h>
  2. 2 #include<algorithm>
  3. 3 #include<iostream>
  4. 4 #include<string.h>
  5. 5 #include<stdlib.h>
  6. 6 #include<queue>
  7. 7 #include<stack>
  8. 8 using namespace std;
  9. 9 typedef long long LL;
  10. 10 typedef struct node
  11. 11 {
  12. 12 int cost;
  13. 13 int id;
  14. 14 bool operator<(const node &cx)const
  15. 15 {
  16. 16 if(cx.cost == cost)return cx.id < id;
  17. 17 else return cx.cost>cost;
  18. 18 }
  19. 19 } ak;
  20. 20 typedef struct pp
  21. 21 {
  22. 22 int l,p,s;
  23. 23 } ss;
  24. 24 bool cmp(pp p,pp q)
  25. 25 {
  26. 26 return p.s<q.s;
  27. 27 }
  28. 28 priority_queue<ak>que;
  29. 29 ss ans[105];
  30. 30 int dp[105][16005];
  31. 31 ak quq[2*16005];
  32. 32 int main(void)
  33. 33 {
  34. 34 int n,m;
  35. 35 while(scanf("%d %d",&n,&m)!=EOF)
  36. 36 {
  37. 37 int j;
  38. 38 int i;
  39. 39 int maxx = 0;
  40. 40 for(i = 1; i <= m; i++)
  41. 41 scanf("%d %d %d",&ans[i].l,&ans[i].p,&ans[i].s);
  42. 42 sort(ans+1,ans+1+m,cmp);
  43. 43 memset(dp,0,sizeof(dp));
  44. 44 for(i = 1; i <= m; i++)
  45. 45 {
  46. 46 int head = 16001;
  47. 47 int rail = 16000;
  48. 48 for(j = 0; j < ans[i].s; j++)
  49. 49 {
  50. 50 dp[i][j] = dp[i-1][j];
  51. 51 ak acc;
  52. 52 acc.cost = dp[i-1][j]-j*ans[i].p;
  53. 53 acc.id = j;
  54. 54 if(head>rail)
  55. 55 quq[--head] = acc;
  56. 56 else
  57. 57 {
  58. 58 ak cpp = quq[rail];
  59. 59 while(cpp.cost < acc.cost)
  60. 60 {
  61. 61 rail--;
  62. 62 if(rail<head)
  63. 63 {
  64. 64 break;
  65. 65 }
  66. 66 cpp = quq[rail];
  67. 67 }
  68. 68 quq[++rail] = acc;
  69. 69 }
  70. 70 maxx = max(maxx,dp[i][j]);
  71. 71 }
  72. 72 for(j = ans[i].s; j <= min(n,ans[i].l+ans[i].s-1); j++)
  73. 73 {
  74. 74 dp[i][j] = max(dp[i-1][j],dp[i][j]);
  75. 75 int minn = max(0,j-ans[i].l);
  76. 76 while(head<=rail)
  77. 77 {
  78. 78 ak acc = quq[head];
  79. 79 if(acc.id < minn)
  80. 80 {
  81. 81 head++;
  82. 82 }
  83. 83 else
  84. 84 {
  85. 85 dp[i][j] = max(dp[i][j],acc.cost+j*ans[i].p);
  86. 86 break;
  87. 87 }
  88. 88 }
  89. 89 maxx = max(maxx,dp[i][j]);
  90. 90 }
  91. 91 for(j = min(n,ans[i].l+ans[i].s-1)+1; j <= n; j++)
  92. 92 {
  93. 93 dp[i][j] = dp[i-1][j];
  94. 94 maxx = max(maxx,dp[i][j]);
  95. 95 }}
  96. 96 printf("%d\n",maxx);
  97. 97 }
  98. 98 return 0;}

Fence(poj1821)的更多相关文章

  1. DP重开

    颓了差不多一周后,决定重开DP 这一周,怎么说,学了学trie树,学了学二叉堆,又学了学树状数组,差不多就这样,然后和cdc一番交流后发现,学这么多有用吗?noip的范围不就是提高篇向外扩展一下,现在 ...

  2. 【学习笔记】动态规划—各种 DP 优化

    [学习笔记]动态规划-各种 DP 优化 [大前言] 个人认为贪心,\(dp\) 是最难的,每次遇到题完全不知道该怎么办,看了题解后又瞬间恍然大悟(TAT).这篇文章也是花了我差不多一个月时间才全部完成 ...

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

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

  4. POJ1821 Fence

    题意 Language:Default Fence Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6478 Accepted: ...

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

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

  6. POJ1821 Fence 题解报告

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

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

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

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

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

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

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

随机推荐

  1. 搭建zabbix服务器常见问题解析处理

    1. 找不到url 2. 服务器无法处理当前请求,PHP解析出错 3. 服务器无法处理当前请求,权限不足 1. 找不到url 浏览器报错:The requested URL /zabbix/ was ...

  2. CPF C#跨平台UI框架发布安卓端预览版

    CPF的安卓端适配采用Xamarin的安卓绑定库,而不是Xamarin.Form.CPF和flutter差不多,完全由skia绘制,基本不依赖原生控件. 当前还只是预览版,不建议用在正式项目中. 可能 ...

  3. Shell 输出第五行的内容

    目录 Shell 输出第五行的内容 题目 题解-awk 题解-sed Shell 输出第五行的内容 题目 写一个 bash脚本以输出一个文本文件 nowcoder.txt 中第5行的内容. 示例: 假 ...

  4. 用户体验再升级!Erda 1.2 版本正式发布

    来源|尔达 Erda 公众号 Erda v1.2 Changelog: https://github.com/erda-project/erda/blob/master/CHANGELOG/CHANG ...

  5. doy05循环语法学习笔记

    doy05循环语法学习笔记 一.while循环语法: 1.基本用法示例 x = 1 while x <= 5: print(x) x += 1 2.死循环:永远不结束的循环 如:while Tr ...

  6. python web工程师跳巢攻略

    python web工程师跳巢攻略 流程 一面问基础 二面问项目 三面问设计(经验) web请求的流程 浏览器 负载均衡 web框架 业务逻辑 数据库缓存 后端技术栈 python语言基础 语言特点 ...

  7. Hive(十一)【压缩、存储】

    目录 一.Hadoop的压缩配置 1.MR支持的压缩编码 2.压缩参数配置 3.开启Mapper输出阶段压缩 4.开启Reduceer输出阶段 二.文件存储 1.列式存储和行式存储 2.TextFil ...

  8. 【Python】【Module】time

    #_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了 ...

  9. Docker从入门到精通(一)——初识

    1.Docker 是什么? Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容 ...

  10. Linkerd Service Mesh 服务配置文件规范

    服务配置文件 为 Linkerd 提供有关服务的附加信息. 以下是可以使用服务配置文件完成的所有操作的参考. 系列 中文手册(https://linkerd.hacker-linner.com) Sp ...