<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. flask数据库迁移

    实际操作顺序:1.python 文件 db init2.python 文件 db migrate -m"版本名(注释)"3.python 文件 db upgrade 然后观察表结构 ...

  2. python基础知识0-3

    一.根据用户输入内容输出其权限 # 根据用户输入内容打印其权限    # FYT --> 超级管理员 # eric --> 普通管理员 # tony,rain --> 业务主管 # ...

  3. JavaScript设计模式(装饰者模式)

    一.模拟传统面向对象语言的装饰者模式: 假设我们在编写一个飞机大战的游戏,随着经验值的增加,我们操作的飞机对象可以升级成更厉害的飞机,一开始这些飞机只能发射普通的子弹,升到第二级时可以发射导弹,升到第 ...

  4. C# 常用类库说明

    Array类 用括号声明数组是C#中使用Array类的记号.在后台使用C#语法,会创建一个派生于抽象基类Array的新类.这样,就可以使用Array类为每个C#数组定义的方法和属性了. Array类实 ...

  5. C# WPF集中引用图片等资源的路劲方式

    第一内部资源: pack://application:,,,/images/my.jpg 第二 外部程序资源: pack://SiteOfOrigin:,,,/images/my.jpg 需要将资源放 ...

  6. 手把手教你搭建FastDFS集群(中)

    手把手教你搭建FastDFS集群(中) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/u0 ...

  7. 帝国cms 加载更多的实现(父栏目以及子栏目都可以实现)

    1. <div class="pagelist"> <span id="loadmore" class="btn" sty ...

  8. vue入门:(底层渲染实现render函数、实例生命周期)

    vue实例渲染的底层实现 vue实例生命周期 一.vue实例渲染的底层实现 1.1实例挂载 在vue中实例挂载有两种方法:第一种在实例化vue时以el属性实现,第二种是通过vue.$mount()方法 ...

  9. jQuery快速入门专题

    jQuery入门专题 本人博客特点:最高重要等级为*****(五红星),依次减少代表重要性相对较低! 一.jQuery简介 jQuery 是一个 JavaScript的一个库,也就是说jQuery是基 ...

  10. OSCP-Kioptrix2014-2 漏洞利用

    pChart 2.1.3 文件包含漏洞 搜索漏洞 查看漏洞理由代码: hxxp://localhost/examples/index.php?Action=View&Script=%2f..% ...