【POJ】2376 - Cleaning Shifts 贪心

http://poj.org/problem?id=2376

给定N个小区间以及区间起点终点,求能用它们覆盖区间[1,T]的最小组合。

从左往右,尽量选择长度最大的区间。

首先对所有奶牛按照开始时间排序。

然后更新起点=终点+1,搜索剩下的奶牛中能够覆盖这个起点同时终点最远的那一头,更新终点。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;

int n,t;
struct E{
    int l,r;
}e[25005];

bool cmp(struct E a,struct E b){
    return a.l<b.l;
}

int main(){
    cin >> n >> t;
    for (int i=0;i<n;i++){
        cin >> e[i].l >> e[i].r;
    }
    sort(e,e+n,cmp);

    int Left=1;
    int Right=0;
    int ans=0;
    int i=0;
    while (Left<=t){
        while (i<n&&e[i].l<=Left){
            if (e[i].l<=Left&&e[i].r>Right){
                Right=e[i].r;
            }
            i++;
        }
        if (Right>=Left){
            Left=Right+1;
            ans++;
        }
        else break;
    }
    if (Left<=t) cout << "-1" << endl;
    else cout << ans << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值