poj2376 Cleaning Shifts "segment tree" "DP"

Source: Internet
Author: User

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

Description

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

Each cow is a available at some interval of times during the day for work on cleaning. Any cow that's selected for cleaning duty would work for the entirety of her interval.

Your job is-to-help Farmer John Assign some cows-shifts so (i) every shift have at least one cow assigned to it, an D (ii) as few cows as possible is 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 isn't possible to assign a cow to each shift.

Sample Input

3 101 73) 66 10

Sample Output

2

Hint

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

INPUT DETAILS:

There is 3 cows and 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 is covered. There is no-to-cover all the shifts using fewer than 2 cows.

Source

Usaco 2004 December Silver

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

And poj3171 Basically, change the input and range.

1#include <iostream>2#include <Set>3#include <cmath>4#include <stdio.h>5#include <cstring>6#include <algorithm>7 using namespacestd;8typedefLong LongLL;9 #defineINF 0x7f7f7f7fTen  One Const intMAXN =25000+5; A Const intMaxTime = 1e6 +5; - structnode{ -     intSt, Ed, cost; the }COW[MAXN]; - BOOLCMP (Node A, Node B) - { -     returnA.ed <B.Ed; + } -LL Tree[maxtime <<2];//interval f[] min. + intN, L, R; A  at voidPushup (intRT) - { -TREE[RT] = min (tree[rt <<1], Tree[rt <<1|1]); - } -  - voidBuildintRtintLintR) in { -     if(L = =R) { toTREE[MAXN] =inf; +         return; -     } the     intMid = (L + r)/2; *Build (rt<<1, L, mid); $Build (rt<<1|1, Mid +1, R);Panax Notoginseng pushup (RT); - } the  + voidUpdateintX, LL Val,intLintRintRT) A { the     if(L = =R) { +TREE[RT] =min (Tree[rt], Val); -         return; $     } $     intm = (L + r)/2; -     if(x <=m) { -Update (x, Val, L, M, rt<<1); the     } -     Else{WuyiUpdate (x, Val, M +1, R, rt<<1|1); the     } - pushup (RT); Wu } -  AboutLL Query (intLintRintLintRintRT) $ { -     if(l <= l && R >=R) { -         returnTree[rt]; -     } A     intm = (L + r)/2; +LL ans =inf; the     if(L <=m) { -ans = min (ans, query (L, R, L, M, rt<<1)); $     } the     if(R >m) { theans = min (ans, query (L, R, M +1, R, rt<<1|1)); the     } the pushup (RT); -     returnans; in } the  the intMain () About { the      while(SCANF ("%d%d", &n, &r)! =EOF) { ther+=1; thememset (Tree,0x7f,sizeof(tree)); +          for(inti =1; I <= N; i++){ -scanf"%d%d", &cow[i].st, &Cow[i].ed); thecow[i].st+=1; cow[i].ed+=1;BayiCow[i].cost =1; the         } theSort (cow +1, Cow +1+N, CMP); -  -Build1,1, R); the  theUpdate1,0,1R1); the         //cout<< "yes" <<endl; the         //int far = L; -         BOOLFlag =true; the          for(inti =1; I <= N; i++){ the             /*if (Cow[i].st > far + 1) { the flag = false;94 //break; the             }*/ the             intA = max (1, Cow[i].st-1); the             intb =min (R, cow[i].ed);98             //cout<<a<< "" <<b<<endl; AboutLL f = Query (A, B,1R1); -F + =Cow[i].cost;101             //cout<<f<<endl;102Update (b, F,1R1);103             //Far = max (far, Cow[i].ed);104             //cout<<far<<endl; the         }106         //cout<< "yes" <<endl;107 108LL ans = query (R, R,1R1);109         if(Ans >=inf) { theprintf"-1\n");111         } the         Else{113printf"%lld\n", ans); the  the         //else{ the         //printf (" -1\n");117         }118 119     } - 121}

poj2376 Cleaning Shifts "segment tree" "DP"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.