Fence(poj1821)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 4705 | Accepted: 1489 |
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
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<stack>
8 using namespace std;
9 typedef long long LL;
10 typedef struct node
11 {
12 int cost;
13 int id;
14 bool operator<(const node &cx)const
15 {
16 if(cx.cost == cost)return cx.id < id;
17 else return cx.cost>cost;
18 }
19 } ak;
20 typedef struct pp
21 {
22 int l,p,s;
23 } ss;
24 bool cmp(pp p,pp q)
25 {
26 return p.s<q.s;
27 }
28 priority_queue<ak>que;
29 ss ans[105];
30 int dp[105][16005];
31 ak quq[2*16005];
32 int main(void)
33 {
34 int n,m;
35 while(scanf("%d %d",&n,&m)!=EOF)
36 {
37 int j;
38 int i;
39 int maxx = 0;
40 for(i = 1; i <= m; i++)
41 scanf("%d %d %d",&ans[i].l,&ans[i].p,&ans[i].s);
42 sort(ans+1,ans+1+m,cmp);
43 memset(dp,0,sizeof(dp));
44 for(i = 1; i <= m; i++)
45 {
46 int head = 16001;
47 int rail = 16000;
48 for(j = 0; j < ans[i].s; j++)
49 {
50 dp[i][j] = dp[i-1][j];
51 ak acc;
52 acc.cost = dp[i-1][j]-j*ans[i].p;
53 acc.id = j;
54 if(head>rail)
55 quq[--head] = acc;
56 else
57 {
58 ak cpp = quq[rail];
59 while(cpp.cost < acc.cost)
60 {
61 rail--;
62 if(rail<head)
63 {
64 break;
65 }
66 cpp = quq[rail];
67 }
68 quq[++rail] = acc;
69 }
70 maxx = max(maxx,dp[i][j]);
71 }
72 for(j = ans[i].s; j <= min(n,ans[i].l+ans[i].s-1); j++)
73 {
74 dp[i][j] = max(dp[i-1][j],dp[i][j]);
75 int minn = max(0,j-ans[i].l);
76 while(head<=rail)
77 {
78 ak acc = quq[head];
79 if(acc.id < minn)
80 {
81 head++;
82 }
83 else
84 {
85 dp[i][j] = max(dp[i][j],acc.cost+j*ans[i].p);
86 break;
87 }
88 }
89 maxx = max(maxx,dp[i][j]);
90 }
91 for(j = min(n,ans[i].l+ans[i].s-1)+1; j <= n; j++)
92 {
93 dp[i][j] = dp[i-1][j];
94 maxx = max(maxx,dp[i][j]);
95 }}
96 printf("%d\n",maxx);
97 }
98 return 0;}
Fence(poj1821)的更多相关文章
- DP重开
颓了差不多一周后,决定重开DP 这一周,怎么说,学了学trie树,学了学二叉堆,又学了学树状数组,差不多就这样,然后和cdc一番交流后发现,学这么多有用吗?noip的范围不就是提高篇向外扩展一下,现在 ...
- 【学习笔记】动态规划—各种 DP 优化
[学习笔记]动态规划-各种 DP 优化 [大前言] 个人认为贪心,\(dp\) 是最难的,每次遇到题完全不知道该怎么办,看了题解后又瞬间恍然大悟(TAT).这篇文章也是花了我差不多一个月时间才全部完成 ...
- [POJ1821]Fence(单调队列优化dp)
[poj1821]Fence 有 N 块木板从左至右排成一行,有 M 个工匠对这些木板进行粉刷,每块木板至多被粉刷一次.第 i 个工匠要么不粉刷,要么粉刷包含木板 Si 的,长度不超过Li 的连续一段 ...
- POJ1821 Fence
题意 Language:Default Fence Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6478 Accepted: ...
- poj1821 Fence【队列优化线性DP】
Fence Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6122 Accepted: 1972 Description ...
- POJ1821 Fence 题解报告
传送门 1 题目描述 A team of $k (1 <= K <= 100) $workers should paint a fence which contains \(N (1 &l ...
- poj1821 Fence(单调队列优化dp)
地址 一排N个木板,M个工匠站在不同位置$S_i$,每个人可以粉刷覆盖他位置的.最长长度为$L_i$木板段,每刷一个有$P_i$报酬.同一木板只刷一次.求最大报酬. 根据每个人的位置dp,设$f[i] ...
- $Poj1821\ Fence\ $单调队列优化$DP$
Poj Acwing Description 有N块木板等待被M个工匠粉刷,每块木板至多被刷一次.第i个工匠要么不粉刷,要么粉刷包含木块Si的,长度不超过Li的连续的一段木板,每粉刷一块可以得到P ...
- poj1821 Fence(dp,单调队列优化)
题意: 由k(1 <= K <= 100)个工人组成的团队应油漆围墙,其中包含N(1 <= N <= 16 000)个从左到右从1到N编号的木板.每个工人i(1 <= i ...
随机推荐
- python基础实战
字符串的互相转换 字典的排序 字典的排序可以直接把,key值或者,values拿出来排序 也可以用dict.items拿出所有的key,value的值再加key=lambda x:x[1] 来排序. ...
- 逻辑学与Prolog学习笔记
int a = 3 + 5; 很自然.如果Matrix a, b要加呢?没有运算符重载,a + b是不行的,只能add(a, b). int a = add(3, 5)也行.如果函数名可以用+呢?+( ...
- 关于java中的安全管理器
最近再查看java的源码的时候看见了这一类代码 final SecurityManager sm = System.getSecurityManager(); 想要了解这个是为了做什么,查看资料之后发 ...
- 源码分析-Producer
消息生产者的代码都在client模块中,相对于RocketMQ来讲,消息生产者就是客户端,也是消息的提供者. 方法和属性 主要方法介绍 //创建主题 void createTopic(final St ...
- 利用extern共享全局变量
方法: 在xxx.h中利用extern关键字声明全局变量 extern int a; 在xxx.cpp中#include<xxx.h> 再定义 int a; 赋不赋初值无所谓,之后该全局变 ...
- Linux系统分区及挂载点
一.关于Linux的分区情况 虽然硬盘分区表中最多能存储四个分区,但我们实际使用时一般只分为两个分区,一个是主分区(Primary Partion)一个是扩展分区(extended partition ...
- 【C/C++】最大连续子序列和/动态规划
思路主要是看了晴神的算法笔记,实现是自己重新用vector实现了一下,对付逗号隔开的输入 #include <iostream> #include <string> #incl ...
- C#深入理解多态
1.里氏替换原则 1.里氏替换原则:在一个软件系统中,如果子类出现在父类出现的位置,而整个软件功能又没有影响,那么咱们称为里氏替换. 2. 考试题:父类变量指向子类对象!! 3.里氏替换 是 ...
- Android CameraX ImageAnalysis 获取视频帧
CameraX使用ImageAnalysis分析器,可以访问缓冲区中的图像,获取视频帧数据. 准备工作 准备工作包括gradle,layout,动态申请相机权限,外部存储权限等等,大部分设置与Came ...
- QT QApplication干了啥?
------------恢复内容开始------------ QCoreApplicationPrivate 会取得current thread; 在windows平台创建TLS变量,记录线程信息,并 ...