poj 2376

http://acm.pku.edu.cn/JudgeOnline/problem?id=2376

在T天里干完一件事,有多只牛,每只牛负责连续几天,日期可能重叠。找出最少数量的牛干完这件事。(可能讲的不清楚)

 

#include<stdio.h> 

#include<algorithm> 

using namespace std; 

struct ORDER 

{ 

    int left; 

    int right; 

}order[25001]; 



bool cmp(ORDER a, ORDER b) 

{ 

    return a.left < b.left; 

} 



int main() 

{ 

    int n_cow, time, i, step, find, tot, ok, temp_right; 

    while(scanf("%d%d", &n_cow, &time)!=EOF) 

    { 

        for(i=0; i<n_cow; i++) 

            scanf("%d%d", &order[i].left, &order[i].right); 

        sort(order, order + n_cow, cmp); 



        if(order[0].left >1) 

        { 

            printf("-1\n"); 

            continue; 

        } 

         

        step = 0; 

        i = 0; 

        ok = 1; 

        tot = 0; 



        while(step < time && i < n_cow) 

        { 

            find = 0; 

            temp_right = 0; 

            while(order[i].left <= step + 1 && i < n_cow ) 

            { 

                find = 1; 

                if(order[i].right > temp_right) 

                    temp_right = order[i].right; 

                i++; 

            } 

            if(!find) 

            { 

                ok = 0; 

                break; 

            } 

            step = temp_right; 

            tot++; 

        } 

        if(!ok || step < time) 

            printf("-1\n"); 

        else 

            printf("%d\n", tot); 

    } 

    return 0; 

}  

你可能感兴趣的:(poj)