poj3171 Cleaning Shifts【线段树(单点修改区间查询)】【DP】

Cleaning Shifts

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4422   Accepted: 1482

Description

Farmer John‘s cows, pampered since birth, have reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer John, the most obliging of farmers, has no choice but hire some of the cows to clean the barn.

Farmer John has N (1 <= N <= 10,000) cows who are willing to do some cleaning. Because dust falls continuously, the cows require that the farm be continuously cleaned during the workday, which runs from second number M to second number E during the day (0 <= M <= E <= 86,399). Note that the total number of seconds during which cleaning is to take place is E-M+1. During any given second M..E, at least one cow must be cleaning.

Each cow has submitted a job application indicating her willingness to work during a certain interval T1..T2 (where M <= T1 <= T2 <= E) for a certain salary of S (where 0 <= S <= 500,000). Note that a cow who indicated the interval 10..20 would work for 11 seconds, not 10. Farmer John must either accept or reject each individual application; he may NOT ask a cow to work only a fraction of the time it indicated and receive a corresponding fraction of the salary.

Find a schedule in which every second of the workday is covered by at least one cow and which minimizes the total salary that goes to the cows.

Input

Line 1: Three space-separated integers: N, M, and E.

Lines 2..N+1: Line i+1 describes cow i‘s schedule with three space-separated integers: T1, T2, and S.

Output

Line 1: a single integer that is either the minimum total salary to get the barn cleaned or else -1 if it is impossible to clean the barn.

Sample Input

3 0 4
0 2 3
3 4 2
0 0 1

Sample Output

5

Hint

Explanation of the sample:

FJ has three cows, and the barn needs to be cleaned from second 0 to second 4. The first cow is willing to work during seconds 0, 1, and 2 for a total salary of 3, etc.

Farmer John can hire the first two cows.

Source

USACO 2005 December Silver

题意:

一个[L, R]的区间,有n头牛,每头牛可以清理一个固定区间,需要花费一定的价格。现在要清理[L,R]这个区间,需要花费最少的价格是多少。

思路:

用f[x]表示清理区间[L, x]需要花费的最小价格。

对于一头牛,他可以清理的区间是[a,b],那么因为中间不能间断,所以f[b] = min(f[x]) + c其中x是属于[a-1,b]区间的。

每次状态转移都要取一个区间最小值,并且更新一个点的值,所以用上线段树来维护。

首先将所有的牛按照结束的区间进行排序,然后DP。

因为时间是从0开始,所以我给所有的时间都加了2,这样a-1就还是从1开始。

要注意的是update()时,并不是直接将值改为val,而是取较小值,这里WA了一会。

  1 #include <iostream>
  2 #include <set>
  3 #include <cmath>
  4 #include <stdio.h>
  5 #include <cstring>
  6 #include <algorithm>
  7 using namespace std;
  8 typedef long long LL;
  9 #define inf 0x7f7f7f7f
 10
 11 const int maxn = 1e5 + 5;
 12 const int maxtime = 86500;
 13 struct node{
 14     int st, ed, cost;
 15 }cow[maxn];
 16 bool cmp(node a, node b)
 17 {
 18     return a.ed < b.ed;
 19 }
 20 LL tree[maxn << 2];//区间中f[]最小值
 21 int n, L, R;
 22
 23 void pushup(int rt)
 24 {
 25     tree[rt] = min(tree[rt << 1], tree[rt << 1|1]);
 26 }
 27
 28 void build(int rt, int l, int r)
 29 {
 30     if(l == r){
 31         tree[maxn] = inf;
 32         return;
 33     }
 34     int mid = (l + r) / 2;
 35     build(rt<<1, l, mid);
 36     build(rt<<1|1, mid + 1, r);
 37     pushup(rt);
 38 }
 39
 40 void update(int x, LL val, int l, int r, int rt)
 41 {
 42     if(l == r){
 43         tree[rt] = min(tree[rt], val);
 44         return;
 45     }
 46     int m = (l + r) / 2;
 47     if(x <= m){
 48         update(x, val, l, m, rt<<1);
 49     }
 50     else{
 51         update(x, val, m + 1, r, rt<<1|1);
 52     }
 53     pushup(rt);
 54 }
 55
 56 LL query(int L, int R, int l, int r, int rt)
 57 {
 58     if(L <= l && R >= r){
 59         return tree[rt];
 60     }
 61     int m = (l + r) / 2;
 62     LL ans = inf;
 63     if(L <= m){
 64         ans = min(ans, query(L, R, l, m, rt<< 1));
 65     }
 66     if(R > m){
 67         ans = min(ans, query(L, R, m + 1, r, rt<<1|1));
 68     }
 69     pushup(rt);
 70     return ans;
 71 }
 72
 73 int main()
 74 {
 75     while(scanf("%d%d%d", &n, &L, &R) != EOF){
 76         L+=2;R+=2;
 77         memset(tree, 0x7f, sizeof(tree));
 78         for(int i = 1; i <= n; i++){
 79             scanf("%d%d%d", &cow[i].st, &cow[i].ed, &cow[i].cost);
 80             cow[i].st+=2;cow[i].ed+=2;
 81         }
 82         sort(cow + 1, cow + 1 + n, cmp);
 83
 84         build(1, 1, R);
 85
 86         update(L - 1, 0, 1, R, 1);
 87         //cout<<"yes"<<endl;
 88         int far = L;
 89         bool flag = true;
 90         for(int i = 1; i <= n; i++){
 91             if(cow[i].st > far + 1){
 92                 flag = false;
 93             //    break;
 94             }
 95             int a = max(L - 1, cow[i].st - 1);
 96             int b = min(R, cow[i].ed);
 97             //cout<<a<<" "<<b<<endl;
 98             LL f = query(a, b, 1, R, 1);
 99             f += cow[i].cost;
100             //cout<<f<<endl;
101             update(b, f, 1, R, 1);
102             far = max(far, cow[i].ed);
103             //cout<<far<<endl;
104         }
105         //cout<<"yes"<<endl;
106
107         LL ans = query(R, R, 1, R, 1);
108         if(ans >= inf){
109             printf("-1\n");
110         }
111         else{
112                 printf("%lld\n", ans);
113
114         //else{
115         //    printf("-1\n");
116         }
117
118     }
119
120 }

原文地址:https://www.cnblogs.com/wyboooo/p/9808378.html

时间: 2024-10-07 14:18:01

poj3171 Cleaning Shifts【线段树(单点修改区间查询)】【DP】的相关文章

HDU 1754 I Hate It 【线段树单点修改 区间查询】

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 102825    Accepted Submission(s): 38669 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,

HDU 3874 Necklace (线段树单点更新+区间查询+离线操作)

Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count

【模板】线段树-单点修改,区间查询

容易理解但是难打(又长又难调)------仅代表个人观点 (能别打就别打) 线段树是什么? 大概长这样?(表示区间1到6) 线段树是一颗二叉树,是通过二分思想建立的一颗表示区间关系的树形结构.(总之记住它很好用就对了) 怎样建一颗线段树 大概思路: 二分+递归 没什么好讲的,具体看代码吧.. //建树 struct node { int a,b; }tree[100001]; void make_tree(int p,int x,int y)//p为当前节点编号,x,y为区间的左右端点 { tr

ZOJ 3632 Watermelon Full of Water(dp+线段树 单点修改)

题意: 一共有n天 每天西瓜售价为dp[i]元 该天的西瓜能吃v[i]天 而且这天如果买了西瓜之前的西瓜就要扔掉 问每天都吃到西瓜的最小花费是多少 思路: 从最后一天开始dp最小花费 并用线段树单点更新来维护 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; #define ll long long #def

hdu1698 Just a Hook(线段树+区间修改+区间查询+模板)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 54923    Accepted Submission(s): 25566 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of

hdu1166 线段树单点修改与区间查询

基本上就是个简单的线段树的单点修改(update)与区间查询(query) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1166 连Lazy标记都不用 附上代码 1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=50000+1

HDU - 1166 敌兵布阵 (线段树+单点修改,区间查询和)

C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视. 中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇

CDOJ 1073 线段树 单点更新+区间查询 水题

H - 秋实大哥与线段树 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submit Status Practice UESTC 1073 Appoint description:  System Crawler  (2016-04-24) Description “学习本无底,前进莫徬徨.” 秋实大哥对一旁玩手机的学弟说道. 秋实大哥是一个爱学习的人,今天他刚刚学习了线段树这个数据结构. 为

敌兵布阵 HDU - 1166 (线段树单点修改)

敌兵布阵 HDU - 1166 题目链接:https://vjudge.net/problem/HDU-1166 题目: C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视. 中央情报局要研究敌人

HDU1166(线段树单点更新区间查询)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 62483    Accepted Submission(s): 26386 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就