<pre name="code" class="html">

Cleaning Shifts

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14425   Accepted: 3700

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.
区间问题,,典型的贪心   63ms
<span style="font-size:18px;color:#3366ff;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct Node{
int s;
int e;
}node[25005];
int cmp(Node a,Node b)
{
if(a.s!=b.s)
return a.s<b.s;
else
return a.e>b.e;
}
int main()
{
int n,q;
while(~scanf("%d %d",&n,&q))
{
for(int i=1;i<=n;i++)
scanf("%d %d",&node[i].s,&node[i].e);
sort(node+1,node+n+1,cmp);
if(node[1].s!=1)
{
printf("-1\n");
continue;
}
int cnt=1,i=1,r=node[1].e,temp=0;
while(r<q)
{
temp=0;
for(int j=i+1;node[j].s<=node[i].e+1&&j<=n;j++)
if(node[j].e>r)
{
temp=j;
r=node[j].e;
}
if(temp==0)
break;
else
{
cnt++;
r=node[temp].e;
i=temp;
}
}
if(r<q)
printf("-1\n");
else
printf("%d\n",cnt);
}
return 0;
}
</span><span style="font-size: 14px;">
</span>


poj 2376 Cleaning Shifts 贪心 区间问题的更多相关文章

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

    Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...

  2. POJ 2376 Cleaning Shifts 贪心

    Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...

  3. poj 2376 Cleaning Shifts 最小区间覆盖

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40751   Accepted: 9871 ...

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

    题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...

  5. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

  6. poj 2376 Cleaning Shifts

    http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  7. POJ 2376 Cleaning Shifts【贪心】

    POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...

  8. 【原创】poj ----- 2376 Cleaning Shifts 解题报告

    题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K ...

  9. POJ 2376 Cleaning Shifts 区间覆盖问题

    http://poj.org/problem?id=2376 题目大意: 给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间. 思路: 贪心,按区间的起点找满足条件的并且终点尽量大的. 一开始 ...

随机推荐

  1. T100 事务的开始与结束

    例如: IF cl_ask_confirm('apm-00801') THEN CALL s_transaction_begin() IF NOT axmt500_change_xmdc015('u' ...

  2. nginx-consul-template

    概述Consul-template 是 HashiCorp 基于 Consul 所提供的可扩展的工具,通过监听 Consul中的数据变化,动态地修改一些配置文件中地模板.常用于在 Nginx.HAPr ...

  3. Spring实战(七)Bean 的作用域

    1.Spring中bean 的多种作用域 单例(Singleton):整个应用中只创建一个bean 的实例,Spring默认创建单例的bean: 原型(Prototype):每次注入or通过Sprin ...

  4. 安装jenkins时出现 No such plugin: cloudbees-folder的解决办法

    今天安装了一下jenkins,在初始化安装插件时出现" No such plugin: cloudbees-folder"错误,根据网上的教程: 1.打开链接"http: ...

  5. c# winform 窗体间的传值

    1.父窗体传值给子窗体: 1) 父窗体: FrmXX frm = ,); frm.Owner = this; frm.ShowDialog(); 子窗体: ; public FrmXX(int ty, ...

  6. c++ 使用 gsoap 调用 WebService 中文乱码

    c++ 使用 gsoap 调用  WebService 中文乱码   问题产生: 使用gsoap时,如果WebService服务端及客户调用端都使用 C++ , 再传递中文时不会存在乱码问题, 当客户 ...

  7. 日常开发用Windows 好还是 Ubuntu好?

    最近打算给电脑重新装系统,纠结了很久,不知道应该是换Windows还是Ubuntu,今天通过我自身的体验,来为大家分析一下,日常开发环境到底是用Windows和Ubuntu. [系统介绍] Windo ...

  8. Windows 系统自动登录配置

    1. open regedit 2. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon 3.在打开的项右 ...

  9. centos8/redhat8 无法上网,通过启动systemctl start NetworkManger搞定

    在systemd里面,可以直接使用systemctl进行管理 启动:systemctl start NetworkManger 关闭:systemctl stop NetworkManager 开机启 ...

  10. python 匿名函数lambda使用

    lambda函数语法格式: lambda函数 后面参数可以有一个或多个,冒号后面是python表达式: lambda 参数1,参数2,参数3...:表达式 # 一个参数情况: a = lambda x ...