poj 2376 Cleaning Shifts

来源:互联网 发布:淘宝等级排行 编辑:程序博客网 时间:2024/05/18 13:26
Cleaning Shifts
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9568 Accepted: 2545

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. 
农民约翰让他的N(1<= N<=25,000)头牛做一些周围的谷仓清洁杂务(智能芯的牛)。他总是希望总能有一头牛在清理,这一天已经划分为T个部分(1<= T<=1,000,000),第一个是部分1,最后一个部分是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.
你的任务是帮助农民约翰分配一些奶牛的工作时间,使(i)每轮班分配了至少一头牛,(ii)尽可能少的奶牛参与清洁。如果不是每个shift都能被分配到奶牛,打印-1。(好吧,到底没翻译明白这个shift。。。大概脑袋转不过筋了)

Input

* Line 1: Two space-separated integers: N and T 
俩输入N,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.
2...N+1行,每头牛的空闲时间,可以用来工作

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

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 
输入蛮大,用scanf()进行输入操作
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

这道题是个快排加贪心的思路,先排序,然后包括还未添加的起点位置,右边界最大的那个,往里面加呀加,加不满就-1,或者中间断了就-1.
然后奇葩事件又出现了,我的快排超时了。。。各种数据都过,用sort系统函数就不会超时,而且才100ms左右。
确实了解sort函数是采用不同阶段的不同算法,好像超过某个数据量用堆排,小数据用什么排序也记不得了,但是我的也不至于超时那么可怕吧╮(╯▽╰)╭
总之总也找不到问题,还是妥协官方sort函数了。。。
那满满的注释都是泪啊。。。这tle查了我一天。。。还一直以为是贪心算法写错了。。。
#include <iostream>#include<algorithm>#include <stdio.h>#include <string.h>using namespace std;void q_sort(int l, int r);int partition_q(int l, int r);struct Cow{    int bg;    int ed;}cow[25001];bool cmp(Cow a, Cow b){    return a.bg < b.bg;}int main(){    int N, T;    int i;    int start, end_s, cnt, top;    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);        scanf("%d%d", &N, &T);        for (i = 0;i < N;i ++){            scanf("%d%d", &cow[i].bg, &cow[i].ed);        }//        q_sort(0, N-1);        sort(cow, cow + N, cmp);//        for (i = 0;i < N;i ++){//            printf("bg = %d, ed = %d\n", cow[i].bg, cow[i].ed);//        }        start = 0;        end_s = 0;        cnt = 0;        top = 0;        while(end_s < T){            start = end_s + 1;            for (i = top;i < N;i ++){                if(cow[i].bg <= start && cow[i].ed > end_s){                    end_s = cow[i].ed;                }else if(cow[i].bg > start){                    top = i;                    break;                }            }            if (start > end_s){                break;            }else{                cnt ++;            }        }//        printf("ends = %d, cnt = %d\n", end_s, cnt);        if (end_s == T){            printf("%d\n",cnt);        }else{            printf("-1\n");        }    return 0;}//void q_sort(int l, int r){//    int q;//    if(l < r){//        q = partition_q(l, r);//        q_sort(l, q-1);//        q_sort(q+1, r);//    }//}////int partition_q(int l, int r){//    int j;//    int i = l - 1;//    Cow temp;//    for (j = l;j < r;++ j){//        if(cow[j].bg <= cow[r].bg){//            i ++;//            temp = cow[j]; cow[j] = cow[i]; cow[i] = temp;//        }//    }//    temp = cow[i + 1]; cow[i + 1] = cow[r]; cow[r] = temp;//    return i + 1;//}


0 0