POJ 2376 Cleaning Shifts 区间覆盖问题
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 区间覆盖问题的更多相关文章
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- poj 2376 Cleaning Shifts
http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)
Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...
- poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
- POJ 2376 Cleaning Shifts (贪心,区间覆盖)
题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...
- poj 2376 Cleaning Shifts 贪心 区间问题
<pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS Memory ...
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- 【原创】poj ----- 2376 Cleaning Shifts 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- poj 3171 Cleaning Shifts(区间的最小覆盖价值)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2743 Accepted: 955 De ...
随机推荐
- struts.xml配置action没用,任意href自动跳到主页,在action中print没用?????
今晚弄了好久都搞不清楚,晕死我了. 上网找也没找到解决办法. 然后看了Build Path.好吧,有几个没用的jar包,remove之.我去,马上正常了,具体原因未知. 总结:删除jar包不代表Bui ...
- 讲的好,php后端模式,php-fpm以及php-cgi, fast-cgi,以及与nginx的关系
关于cgi是什么,fast-cgi是什么,php-cgi是什么,fast-cgi是什么,下面这篇讲的很清楚: https://segmentfault.com/q/1010000000256516 另 ...
- OcadeToolkit - From 2D CAD to PDMS
OcadeToolkit - From 2D CAD to PDMS eryar@163.com Abstract. 基于开源二维CAD软件QCAD开发的插件可以将DXF文件中直线.圆弧转换到PDMS ...
- DSP开发中遇到的问题 - 类指针未初始化后果
收到RECEIVE_REQ_MSG消息时会运行以下的代码,这里由于某种原因m_receiverSlaverController的值仍为NULL,并没有指向详细的CReceiverSlaverContr ...
- Android 学习笔记之Bitmap位图的缩放
位图的缩放也可以借助Matrix或者Canvas来实现. 通过postScale(0.5f, 0.3f)方法设置旋转角度,然后用createBitmap方法创建一个经过缩放处理的Bitmap对象,最后 ...
- button按钮怎么实现超链接
button按钮怎么实现超链接 一.总结 1.我的按钮实现超链接是通过button内嵌a标签来实现的 <button class="am-btn am-btn-default am-b ...
- 洛谷——P3178 [HAOI2015]树上操作
https://www.luogu.org/problem/show?pid=3178#sub 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 ...
- 洛谷——P2093 零件分组
https://www.luogu.org/problem/show?pid=2093 题目描述 某工厂生产一批棍状零件,每个零件都有一定的长度(Li)和重量(Wi).现在为了加工需要,要将它们分成若 ...
- HDU 5063 Operation the Sequence(暴力 数学)
题目链接:pid=5063" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=5063 Prob ...
- HDU 5188 zhx and contest(带限制条件的 01背包)
Problem Description As one of the most powerful brushes in the world, zhx usually takes part in all ...