POJ 1821 Fence

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

Romania OI 2002

解题:单调队列优化dp

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn = 20100;
 7 struct Worker{
 8     int L,S,P;
 9     bool operator<(const Worker &rhs) const{
10         return S < rhs.S;
11     }
12 }a[maxn];
13 int n,m,L[maxn],R[maxn],dp[110][maxn],q[maxn];
14
15 int main(){
16     while(~scanf("%d%d",&n,&m)){
17         for(int i = 1; i <= m; ++i)
18             scanf("%d%d%d",&a[i].L,&a[i].P,&a[i].S);
19         sort(a + 1, a + m + 1);
20         for(int i = 1; i <= m; ++i){
21             L[i] = max(0,a[i].S - a[i].L);
22             R[i] = min(n,a[i].S + a[i].L - 1);
23         }
24         for(int i = 1; i <= m; ++i){
25             for(int j = 0; j <= R[i]; ++j)
26                 dp[i][j] = dp[i-1][j];
27             int hd = 0,tl = 0;
28             for(int j = L[i]; j < a[i].S; ++j){
29                 while(hd < tl && dp[i-1][j] - j*a[i].P >= dp[i-1][q[tl-1]] - q[tl-1]*a[i].P) --tl;
30                 q[tl++] = j;
31             }
32             for(int j = a[i].S; j <= R[i]; ++j){
33                 while(hd < tl && j - q[hd] > a[i].L) ++hd;
34                 dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
35                 dp[i][j] = max(dp[i][j],dp[i-1][q[hd]] + (j - q[hd])*a[i].P);
36             }
37             for(int j = R[i] + 1; j <= n; ++j)
38                 dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
39         }
40         int ret = 0;
41         for(int i = 1; i <= n; ++i)
42             ret = max(ret,dp[m][i]);
43         printf("%d\n",ret);
44     }
45     return 0;
46 }

时间: 2024-08-01 15:32:39

POJ 1821 Fence的相关文章

poj 1821 Fence(单调队列)

题目链接:http://poj.org/problem?id=1821 题目分析来自:http://blog.csdn.net/tmeteorj/article/details/8684453 连续的N块木板,有K个粉刷匠,分别坐在第Si块木板前,每个粉刷匠不能移动位置,且最多能粉刷连续的Li块木板(必须包括Si或者不要该粉刷匠),每个粉刷匠粉刷一块木板可以得Pi块钱,求总共的最大利益. 题解:dp[i][j]代表前i个粉刷匠粉刷完成至多前j个木板的最大利益,状态转移有三种: 1.不需要第i个粉

poj 1821 Fence(单调队列+dp)

Language: Default Fence Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4161   Accepted: 1288 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 fro

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

题解 以前做过很多单调队列优化DP的题. 这个题有一点不同是对于有的状态可以转移,有的状态不能转移. 然后一堆边界和注意点.导致写起来就很难受. 然后状态也比较难定义. dp[i][j]代表前i个人涂完前j个位置的最大收益. 然后转移考虑 第i个人可以不刷.dp[i][j]=dp[i-1][j]; 第j个木板可以不刷dp[i][j]=dp[i][j-1]; 然后当c[i].s<=j<=s[i]+l[i]-1时 dp[i][j]=p[i]*j+max(dp[i-1][k]-p[i]*k)其中j-

POJ 3253 Fence Repair (优先队列)

POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He the

POJ 3253 Fence Repair 类似哈夫曼树的贪心思想

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24550   Accepted: 7878 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)

【优先队列/huffman】sdut 2848/poj 3253——Fence Repair

来源:点击打开链接 很久很久之前做过这个题,印象中是用优先队列来做,结果一写各种wa了..........翻之前的代码库,发现优先队列的定义出现了问题.. 因为数据很大需要每次都选取两个最短的进行拼装,所以用了优先队列,每两个小的构成父节点,然后把父节点放进去再找两个小的接起来.huffmanTree的逆向思维,接到最后那一个就是最后的答案了. #include <iostream> #include <queue> #include <vector> #include

poj 3253 Fence Repair(优先队列+哈夫曼树)

题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每一个父节点都是两个子节点的和.这个题就是可以从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个元素为止. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <cty

POJ 3253 Fence Repair(哈夫曼树)

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26167   Accepted: 8459 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)

哈夫曼树 POJ 3253 Fence Repair

竟然做过原题,一眼看上去竟然没感觉... 哈夫曼树定义:给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree).哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近. 1.路径和路径长度 在一棵树中,从一个结点往下可以达到的孩子或孙子结点之间的通路,称为路径.通路中分支的数目称为路径长度.若规定根结点的层数为1,则从根结点到第L层结点的路径长度为L-1. 2.结点的权及带权路径长度 若将树中结点赋给一个有着某