poj2376(贪心)

来源:互联网 发布:初中全程辅导软件下载 编辑:程序博客网 时间:2024/05/07 20:51

题意:问有N头牛,每头牛的工作时间不同,要工作T小时,最少需要几头牛工作。

题意:即是:输入 n 个区间和 t,接着输入 n 个区间[s, e],

题意:要求找出最少的区间数覆盖区间目标区间[1, t];

贪心区间问题。。。。。。

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;#define N 25005struct Range{    int l,r;    bool operator <(const Range temp)const{        return l<temp.l;    }}ra[N];int main(){    int n,t;    while(scanf("%d%d",&n,&t)!=EOF){        for(int i=0;i<n;i++) scanf("%d%d",&ra[i].l,&ra[i].r);        sort(ra,ra+n);        if(ra[0].l>1){//之前写成if(ra[i].l>1||ra[i].r<t),WA了n次都没发现。。。。终于找出来了            printf("-1\n");            continue;        }        int i=0,j,pos,ok=0,cnt=1;        while(i<n){            if(ra[i].r>=t) {ok=1;break;}            pos=i;            for(j=i+1;j<n;j++){                if(ra[i].r+1<ra[j].l) break;                if(ra[pos].r<ra[j].r) pos=j;            }if(i==pos) break;            i=pos;            cnt++;        }        printf("%d\n",ok?cnt:-1);    }//while    return 0;}


0 0