POJ 2376 Cleaning Shifts [贪心]

Description

给一个1..T的区间,然后N个子区间,取最少的把这个区间填满,求这个最小值

Algorithm

以区间开始为关键字升序排序,如果左边不能覆盖,那肯定就不行。这样就选好了最开始的一块。那当然是选所以1开头的右边区间最大的一块。第二块就是在这1..y+1中再找个右边最大的一块,然后不断更新。如果找不到,那就不能填满,当全部找完以后,再判断能不能填满

Code

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 20000;
class V
{
public:
    int x, y;
};
bool compare(V a, V b)
{
    if (a.x < b.x) return true;
    if (a.x > b.x) return false;
    if (a.x == b.x)
        if (a.y > b.y) return true;
    return false;
}
int main()
{
    int n, t;
    scanf("%d%d", &n, &t);
    V a[maxn];
    for (int i=0;i<n;i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        a[i].x = x;
        a[i].y = y;
    }
    sort(a, a+n, compare);
    if (a[0].x!=1)
    {
        puts("-1");
        return 0;
    }
    int last = a[0].y;
    int last_max = last;
    int i = 0;
    int ans = 1;
    do
    {
        if (last >= t) break;
        if (a[i].x>last+1)
        {
            ans = -1;
            break;
        }
        while (a[i].x<=last+1 && i<n)
        {
            if (a[i].y > last_max) last_max = a[i].y;
            i++;
        }
        last = last_max;
        ans++;
    }while(i<n);
    if (last_max < t) ans = -1;
    printf("%d", ans);
    return 0;
}

新技能GET

用sort函数进行多关键字排序

生词

  • assign 分配
  • chore 家务
  • barn 谷仓
  • interval 区间
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值