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

题目大意:

给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间。

思路:

贪心,按区间的起点找满足条件的并且终点尽量大的。

一开始排序还考虑了起点一样终点要大的,想了想没必要,因为只后都是直接扫描满足条件的。。

注意的是比如【4.5】下一次可以直接【6,10】这样。。。这一步坑了我好久。。。

后来一直WA,改了老半天。。。发现是大于等于少写了个等号,,,我去面壁。。。我要蹲墙角。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=25000+10;
struct cow{
int s,e;//start time and end time
bool operator <(const cow &x)const{
// if(s==x.s) // 也没必要这么排序。。
// return e>x.e;
return s<x.s;
}
}a[MAXN]; int main()
{
int n,T;
while(~scanf("%d%d",&n,&T))
{
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].s,&a[i].e); sort(a,a+n);
int s=0,e=0; //s必须从0开始,因为下面判断是s+1
int ans=0;
for(int i=0;i<n;i++)
{
if(s>=T) break; //少写了个等于。改了一小时。。。。。我去面壁
bool find=false;
for(int j=i;j<n;j++) //找到满足条件的结束时间最大的
{
if(a[j].s>s+1) break;
if(a[j].e>e) {
e=a[j].e;
i=j;
}
find=true;
}
if(!find)
{
ans=-1;
break;
}
ans++;
s=e;
}
if(s<T) ans=-1;
printf("%d\n",ans);
}
return 0;
}

POJ 2376 Cleaning Shifts 区间覆盖问题的更多相关文章

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

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

  2. poj 2376 Cleaning Shifts

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

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

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

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

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

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

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

  6. poj 2376 Cleaning Shifts 贪心 区间问题

    <pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS   Memory ...

  7. POJ 2376 Cleaning Shifts 贪心

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

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

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

  9. poj 3171 Cleaning Shifts(区间的最小覆盖价值)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2743   Accepted: 955 De ...

随机推荐

  1. CSUOJ 1551 Longest Increasing Subsequence Again

    1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 75  Solved ...

  2. 安装Apache PHP MySQL PHPMyAdmin

    视频教程:https://www.youtube.com/watch?v=FJC2iGt_2bc,Youtube看不了的FQ吧-3- 本人参考这篇文章:http://blog.csdn.net/kno ...

  3. 程序猿的量化交易之路(13)--Cointrader类图(1)

    转载须注明出处:http://blog.csdn.net/minimicall? viewmode=contents, htpp://cloudtrader.top 今天開始正式切入到Cointrad ...

  4. [Android]新建项目继承Activity不继承ActionBarActivity

    在SDK更新后,在eclipse新建Android项目时.我们常常会碰到这样一种事情:新建的MainActivity不再继承Activity而是继承ActionBarActivity,因为一些人的开发 ...

  5. json和XML

    发请求(url) 1.client  ---------------->服务端                发送数据(Json/xml)                      < - ...

  6. Windows学习总结(5)——【IIS建站】Windows10怎么打开站点服务?

    从Windows8开始,界面发生了很大的变动,再到Windows10,仍然有不小的变动,鉴于以后Windows10会成为主流,我们姑且介绍下Windows10建站的方法,虽然它并不是专业的服务器系统, ...

  7. 计算机视觉(ComputerVision, CV)相关领域的站点链接

    关于计算机视觉(ComputerVision, CV)相关领域的站点链接,当中有CV牛人的主页.CV研究小组的主页,CV领域的paper,代码.CV领域的最新动态.国内的应用情况等等. (1)goog ...

  8. cocoapod卡在了analyzing dependencies

    尽管公司的项目没有使用cocoapod,可是有一些第三方库本身依赖其它第三方的库,而且是用cocoapod来管理这些依赖的.所以在使用某些第三方库时.还是须要用到cocoapod的.今天在github ...

  9. Qt自定义类型使用QHash等算法(Qt已经自定义了34种类型,包括int, QString, QDate等基本数据类型)

    自定义类型 #include <QCoreApplication> #include <QSet> #include <QDebug> class testCust ...

  10. softInputMode- 软件盘的设置

    今天遇到一个问题,就是软件盘弹出来以后,会把之前的布局界面整个的挤到屏幕的外面,而且按下返回建以后,这个软件盘占据的空间会留下一个黑色的背景.在网上查找了很多的方法,刚开始都是说,如下方法 <a ...