POJ-2376 Cleaning Shifts---区间覆盖&贪心
题目链接:
https://vjudge.net/problem/POJ-2376
题目大意:
farmer John要安排他的牛清理牛棚,一共有T个牛棚要清理,每头牛可以清理相邻的牛棚。比如,一头牛可以清理4-7号牛棚。当然了,牛清理的牛棚可以重叠。现在要你求出可以完成牛棚的清理的最少头牛的个数,不可以就输出-1.
思路:
贪心题。题目意思很明显是求最少的区间覆盖掉大区间。先对这些时间段排好序(见代码),这个排序应该是没什么问题的。然后呢,第一头牛肯定要选,就从这头牛开始,选取下一头牛。下一头牛怎么选取呢?即在满足条件的牛里面(注意:满足条件的牛是只要开始工作时间 start>=cow[0].y+1 即可),选取右边界值最大的那个,因为这样子能够覆盖掉最多的时间段。以此类推,故贪心法求之。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
int n, t;
struct node
{
int l ,r;
bool operator < (const node a)const
{
return l < a.l || l == a.l && r > a.r;
}
};
node a[];
int main()
{
while(scanf("%d%d", &n, &t) != EOF)
{
for(int i = ; i < n; i++)
{
scanf("%d%d", &a[i].l, &a[i].r);
}
sort(a, a + n);
int ans = , end = , now = ;
while(end < t)
{
int begin = end + ;
for(int i = now; i < n; i++)
{
if(a[i].l <= begin)//要覆盖起点
{
end = max(end, a[i].r);//取最远的终点
}
else
{
now = i;//不能覆盖起点,说明之后的都不可以,直接跳出,记录下标
break;
}
}
if(end < begin)//没找到牛,直接跳出
{
ans = -;
break;
}
else ans++;
}
cout<<ans<<endl;
}
return ;
}
POJ-2376 Cleaning Shifts---区间覆盖&贪心的更多相关文章
- POJ 2376 Cleaning Shifts 区间覆盖问题
http://poj.org/problem?id=2376 题目大意: 给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间. 思路: 贪心,按区间的起点找满足条件的并且终点尽量大的. 一开始 ...
- 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 (贪心,区间覆盖)
题意:给定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 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- poj 2376 Cleaning Shifts(贪心)
Description Farmer John <= N <= ,) cows to <= T <= ,,), the first being shift and the la ...
- POJ 2376 Cleaning Shifts【贪心】
POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...
随机推荐
- LocalDate test
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.tim ...
- ScheduledThreadPoolDemo01
package com.zhy.concurrency.timer; import java.util.Date; import java.util.Timer; import java.util.T ...
- 微信小程序多图上传/朋友圈传图效果【附完整源码】
效果图 部分源代码 js文件: var uploadPicture = require('../Frameworks/common.js') //获取应用实例 const app = getApp() ...
- 6.SpringMVC2
1.视图解析 当客户端发出请求后,交由SpringMVC的DispatcherServlet处理,接着Spring会分析看哪一个HandlerMapping定义的所有请求映射中对该请求的最合理的映射, ...
- window.open()总结
今天找父页面和子页面调去问题. 父页面js 1.打开子页面 可以设置子页面长宽高和位置 win= window.open(url, "", "top=45,left=20 ...
- Android官方架构组件介绍之LiveData(二)
LiveData LiveData是一个用于持有数据并支持数据可被监听(观察).和传统的观察者模式中的被观察者不一样,LiveData是一个生命周期感知组件,因此观察者可以指定某一个LifeCycle ...
- bootstrap-datepicker控件中文 ,只显示年和只显示月份
插件下载地址 https://github.com/uxsolutions/bootstrap-datepicker html <link href="css/bootstrap-da ...
- Python collections
#count对象 Only 2.7 from collections import Counter #统计字母出现的次数 Counter('hello world') Counter(['red', ...
- linux_api之文件操作
本篇索引: 1.引言 2.文件描述符 3.open函数 4.close函数 5.read函数 6.write函数 7.lseek函数 8.i/o效率问题 9.内核用以维护打开文件的相关数据结构 10. ...
- C# 使用消息队列,包括远程访问
转:https://www.cnblogs.com/80X86/p/5557801.html 功能需求,用到了队列,用的时候出了很多问题,现在总结一下,希望能对有需要的人提供帮助. 我的需求很简单,就 ...