zoukankan      html  css  js  c++  java
  • Cleaning Shifts(POJ 2376 贪心)

    Cleaning Shifts
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15143   Accepted: 3875

    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

    区间覆盖问题,贪心

     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cstdio>
     5 using namespace std;
     6 struct node
     7 {
     8     int x,y;
     9 }cow[25000+5];
    10 int cmp(node a,node b)
    11 {
    12     if(a.x==b.x)    return a.y>=b.y;
    13     return a.x<b.x;
    14 }
    15 int main()
    16 {
    17     int T,N;
    18     int i,j;
    19     freopen("in.txt","r",stdin);
    20     while(scanf("%d%d",&N,&T)!=EOF)
    21     {
    22         for(i=0;i<N;i++)
    23             scanf("%d%d",&cow[i].x,&cow[i].y);
    24         sort(cow,cow+N,cmp);
    25         int c=0,coun=1,j;
    26         bool ans=1;
    27         if(cow[0].x>1)
    28         {
    29             printf("-1
    ");
    30             continue;
    31         }
    32         int t=cow[0].y+1;
    33         while(t<=T)
    34         {
    35             int MaxLen=t;
    36             bool flag=0;
    37             int k=c;
    38             for(j=c+1;cow[j].x<=t&j<N;j++)
    39             {
    40 
    41                 if(cow[j].y>=MaxLen)
    42                 {
    43                     k=j;
    44                     MaxLen=cow[j].y;
    45                     flag=1;
    46                 }
    47             }
    48             if(flag==0)
    49             {
    50                 ans=0;
    51                 break;
    52             }
    53             c=k;
    54             t=MaxLen+1;
    55             coun++;
    56         }
    57         if(ans==0)    {printf("-1
    ");continue;}
    58         printf("%d
    ",coun);
    59     }
    60 }
  • 相关阅读:
    【FastJSON序列化】FastJSON序列化
    【EsayExcel】EsayExcel生成自定义表头
    时间序列的基础频率
    问题记录 --> 未定义的引用 ||| not found
    linux shell find 用法大全
    pgrep -f abc匹配出含abc的进程 ||||||| pid pkill -f abc
    >/dev/null 2>&1
    编译,发版,链接库问题
    shell 问题 source project_env.sh
    linux/ubuntu 压缩与解压
  • 原文地址:https://www.cnblogs.com/a1225234/p/5164726.html
Copyright © 2011-2022 走看看