poj2376(贪心)

11 篇文章 0 订阅
5 篇文章 0 订阅

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

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

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

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

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 25005
struct 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值