POJ_2376_Cleaning Shifts【贪心】【区间覆盖】

题目链接

 

题目大意:

有一些奶牛,每只奶牛负责一个时间段。问覆盖完全部的时间段最少需要多少只奶牛。若不能全部覆盖,输出-1.

 

 
  

#include <cstdio>
#include <algorithm>
using namespace std;

 
  

typedef long long ll;

const int maxn = 25000 + 5;
const int maxd = 1000000 + 5;

struct Edge
{
    int x;
    int y;
}cow[maxn];

int N, T;
int X, Y;

bool cmp(Edge a, Edge b) 
{
    if (a.x == b.x) return a.y>b.y;
    return a.x<b.x;
}

void input() 
{
    X = 0; Y = 0;
    scanf("%d%d", &N, &T);
    for (int i = 0; i<N; i++) 
    {
        scanf("%d%d", &cow[i].x, &cow[i].y);
        if (cow[i].x == 1) X = 1;
        if (cow[i].y == T) Y = 1;
    }
}

void solve() 
{
    sort(cow, cow + N, cmp);
    //若所有的牛的工作时间的起始时间和结束时间都没有覆盖掉区间的起始时间和结束时间  
    if (X == 0 || Y == 0) 
    {
        printf("-1\n");
        return;
    }
    int ans = 1, End = 0, sum = 0;
    int Start = cow[0].y;
    int maxy = cow[0].y;
    while (true) 
    {
        while (End + 1<N&&cow[End + 1].x <= Start + 1)    //End代表当前点的坐标
        {
            End++;
            if (cow[End].y>maxy) 
            {
                maxy = cow[End].y;
            }
        }
        if (maxy != Start)          //start为当前有效区间的最远距离
        {
            ans++;
            Start = maxy;
        }
        else 
        {
            if (End == N - 1)  //已覆盖掉区间
            {  
                break;
            }
            else //说明中间有的时间点覆盖不到  
            {  
                printf("-1\n");
                return;
            }
        }
    }
    printf("%d\n", ans);
    return;
}

int main() {
    input();
    solve();
    return 0;
}

 

 

2018-04-01

 

转载于:https://www.cnblogs.com/00isok/p/8687894.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值