POJ2376 Cleaning Shifts(贪心)

给出每头奶牛的覆盖区间,求最少几头奶牛可以覆盖[1,T]这个区间。(有个问题需要注意一下,比如T=10,1-5,6-10这就算全部覆盖了,覆盖的是点)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(i,n) for(int (i)=0;(i)<n;(i)++)
using namespace std;

struct Node
{
    int l,r;
} cow[25010];

bool cmp(Node a,Node b)
{
    if(a.r==b.r) return a.l>b.l;
    return a.r>b.r;
}

int main()
{
    //freopen("d:\\Test.txt","r",stdin);
    int n,m;
    scanf("%d%d",&n,&m);
    rep(i,n) scanf("%d%d",&cow[i].l,&cow[i].r);
    sort(cow,cow+n,cmp);
    int ncount=1;
    int j=0;
    for(int i=0; i<n; i++)
    {
        if(cow[i].r<m) break;
        j++;
    }
    if(j==0)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    int k=cow[j-1].l;
    int L=k;
    int i=j;
    while(i<n)
    {
        bool flag=false;
        if(cow[i].r<k-1) {cout<<"-1"<<endl;return 0;}
        while(i<n&&cow[i].r>=k-1)
        {
            if(L>cow[i].l)
            {
                L=cow[i].l;
                flag=true;
            }
            i++;
        }
        if(flag) ncount++;
        k=L;
        if(k==1) break;
    }
    if(k>1) cout<<"-1"<<endl;
    else cout<<ncount<<endl;
    return 0;
}
时间: 2024-09-30 09:33:43

POJ2376 Cleaning Shifts(贪心)的相关文章

POJ2376 Cleaning Shifts 【贪心】

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11542   Accepted: 3004 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

POJ 2376 Cleaning shifts 贪心 基础题

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13042   Accepted: 3373 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000

poj2376 Cleaning Shifts【线段树】【DP】

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32561   Accepted: 7972 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co

poj2376 Cleaning Shifts 区间贪心

题目大意: (不说牛了) 给出n个区间,选出个数最少的区间来覆盖区间[1,t].n,t都是给出的. 题目中默认情况是[1,x],[x+1,t]也是可以的.也就是两个相邻的区间之间可以是小区间的右端与大区间的左端相差1.这个是看题解才知道的. 解题思路: 贪心题的关键是找到贪心策略.但是这题的贪心策略没那么明显.并且贪心策略没有特定地去选择某一区间.这一题最重要的是要知道在什么情况下才需要增加一个区间. 首先是进行排序,按照区间的左端从小到大排序,左端相同的按照右端从小到大排. 从头开始遍历(只能

poj2376 Cleaning Shifts(区间贪心,理解题意)

https://vjudge.net/problem/POJ-2376 题意理解错了!!真是要仔细看题啊!! 看了poj的discuss才发现,如果前一头牛截止到3,那么下一头牛可以从4开始!!! 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7

POJ2376 Cleaning Shifts

http://poj.org/problem?id=2376 类似于工作排序问题 贪心策略:在符合时间情况的选项中 选择结束时间最迟的牛 具体步骤: 按照开始时间升序排列  如果 开始时间相同 按照结束时间升序排列 设t为最终结束时间 区间[1, t]为最终区间 一次1 to n的循环 同时 扩大区间 最后比较t和T的值即可 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 5 usi

POJ 2376 Cleaning Shifts (贪心,区间覆盖)

题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小于1的部分(如果有的话),再找最长的区间,然后把这个区间的右端点作为下次寻找的起点, 再找最大区间,直到覆盖到最后. 注意:首先要判断好能不能覆盖,不能覆盖就结束,有可能会提前结束,也要做好判断,我就在这WA了好几次, 悲剧...其他的就比较简单了,不用说了. 代码如下: #include <ios

Cleaning Shifts(POJ 2376 贪心)

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15143   Accepted: 3875 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one co