1. 程式人生 > >poj2376 Cleaning Shifts【線段樹】【DP】

poj2376 Cleaning Shifts【線段樹】【DP】

scan 區間 cin can pri sign interval divide poj2376

Cleaning Shifts
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 32561 Accepted: 7972

Description

Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T.

Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval.

Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

Input

* Line 1: Two space-separated integers: N and T

* Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.

Output

* Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

INPUT DETAILS:

There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10.

OUTPUT DETAILS:

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

Source

USACO 2004 December Silver

https://www.cnblogs.com/wyboooo/p/9808378.html

和poj3171基本一樣,改一下輸入和範圍即可。

  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 = 25000 + 5;
 12 const int maxtime = 1e6 + 5;
 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[maxtime << 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", &n, &R) != EOF){
 76         R+=1;
 77         memset(tree, 0x7f, sizeof(tree));
 78         for(int i = 1; i <= n; i++){
 79             scanf("%d%d", &cow[i].st, &cow[i].ed);
 80             cow[i].st+=1;cow[i].ed+=1;
 81             cow[i].cost = 1;
 82         }
 83         sort(cow + 1, cow + 1 + n, cmp);
 84 
 85         build(1, 1, R);
 86 
 87         update(1, 0, 1, R, 1);
 88         //cout<<"yes"<<endl;
 89         //int far = L;
 90         bool flag = true;
 91         for(int i = 1; i <= n; i++){
 92             /*if(cow[i].st > far + 1){
 93                 flag = false;
 94             //    break;
 95             }*/
 96             int a = max(1, cow[i].st - 1);
 97             int b = min(R, cow[i].ed);
 98             //cout<<a<<" "<<b<<endl;
 99             LL f = query(a, b, 1, R, 1);
100             f += cow[i].cost;
101             //cout<<f<<endl;
102             update(b, f, 1, R, 1);
103             //far = max(far, cow[i].ed);
104             //cout<<far<<endl;
105         }
106         //cout<<"yes"<<endl;
107 
108         LL ans = query(R, R, 1, R, 1);
109         if(ans >= inf){
110             printf("-1\n");
111         }
112         else{
113                 printf("%lld\n", ans);
114 
115         //else{
116         //    printf("-1\n");
117         }
118 
119     }
120 
121 }

poj2376 Cleaning Shifts【線段樹】【DP】