poj 2376 最小区间覆盖问题

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

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 cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T. 

Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval. 

Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

Input

* Line 1: Two space-separated integers: N and T 

* Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.

Output

* Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 

INPUT DETAILS: 

There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10. 

OUTPUT DETAILS: 

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.
所有的活必须在1~T天里完成,每头牛可以 再a[i].begain~a[i].end时间内干活,现将活安排给牛去做,尽量安排最少的牛去完成。

贪心问题的典型例子,最小区间覆盖问题:以每头牛开始的时间为标准,从小到大为牛排序。然后,对于没有找过的牛,选取begin小于Max且end大于Max的最大区间(牛),直到end>=T为止。

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct A
{
    int left,right;
};
int cmp(const A &a,const A &b)
{
    if(a.left==b.left)
      return a.right<b.right;
    return a.left <b.left;
}
int main()
{
    A a[25005],b[25005];
    int m,n,c;
    while(~scanf("%d%d",&n,&c))
    {
        for(int i=0; i<n; i++)
        {
            cin >> a[i].left>>a[i].right;
            if(a[i].left>a[i].right)
                swap(a[i].left,a[i].right);
        }
        sort(a,a+n,cmp);
        /*for(int i=0;i<n;i++)
          cout << a[i].left <<" "<< a[i].right<<endl;*/
        int max=0;
        int sum=0;
        int top=0;
        while(max<c)
        {
            int min=max+1;
            for(int i=top; i<n; i++)
                if(a[i].left<=min&&a[i].right>=min)
                       max=max>a[i].right?max:a[i].right;
                else if(a[i].left>min)
                {
                    top=i;
                    break;
                }
            if(min>max)
               break;
            else
               sum++;
        }
        if(max>=c)
         cout << sum <<endl;
        else
         cout << "-1\n";
    }
    return 0;
}


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
贪心算法最小区间覆盖是指给定一个大区间和多个小区间,要求找到最少的小区间来完全覆盖区间问题。其中,每个小区间都有一个开始时间和结束时间。贪心算法的思路是按照开始时间递增排序,然后从第一个小区间开始,选择与当前区间结束时间不重叠且结束时间最早的下一个区间,直到覆盖了整个大区间为止。 具体的解法可以按照以下步骤进行: 1. 将所有小区间按照开始时间递增排序,如果开始时间相同,则按照结束时间递增排序。 2. 初始化一个变量count,用于记录覆盖区间所需要的小区间的数量。 3. 设定一个变量end,表示当前选择的小区间的结束时间。 4. 遍历排序后的小区间列表,对于每个小区间: - 如果小区间的开始时间大于end,则说明当前小区间与之前选择的小区间不重叠,可以选择该小区间覆盖区间。 将该小区间的结束时间赋值给end,同时将count加1。 - 如果小区间的开始时间小于等于end,说明当前小区间与之前选择的小区间重叠,需要进行下一次遍历。 5. 如果end大于大区间的结束时间,则表示已经找到了能够完全覆盖区间最小区间集合,返回count的值;否则,返回-1,表示无法找到满足条件的最小区间覆盖。 这个问题可以使用贪心算法来解决,通过排序和遍历的方式选择合适的小区间覆盖区间,以达到最小覆盖的目标。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [贪心算法.doc](https://download.csdn.net/download/hqztrue2/11338380)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [贪心算法 --- 最小区间覆盖问题(POJ2376)](https://blog.csdn.net/WSSB____/article/details/126682938)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [贪心算法——最小区间覆盖问题](https://blog.csdn.net/mashizuren/article/details/113345347)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值