Poj 2376 cleaning shifts (Greedy)

Source: Internet
Author: User

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 101 73 66 10

Sample output

2

The interval with the minimum cost is covered.

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>#include<map>using namespace std;typedef long long LL;const int maxn=25000+100;struct node{    int st,ed;}e[maxn];int n,m;int cmp(node l1,node l2){    if(l1.st==l2.st)        return  l1.ed>l2.ed;    return l1.st<l2.st;}int main(){    int ed,temp;    while(~scanf("%d%d",&n,&m))    {        for(int i=0;i<n;i++)            scanf("%d%d",&e[i].st,&e[i].ed);        sort(e,e+n,cmp);        if(e[0].st!=1)        {            printf("-1\n");            continue;        }        int ans=1;        ed=e[0].ed;        temp=0;        for(int i=1;i<n;i++)        {            if(e[i].st>ed+1)            {                ed=temp;                ans++;            }            if(e[i].st<=ed+1)            {                if(e[i].ed>temp)                    temp=e[i].ed;                if(e[i].ed==m)                {                    ans++;                    ed=m;                    break;                }            }        }        if(ed==m)   printf("%d\n",ans);        else   printf("-1\n");    }    return 0;}


Poj 2376 cleaning shifts (Greedy)

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.