POJ 2376 Cleaning Shifts【贪心】
题意:
给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间。
分析:
贪心法。设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+1)中右端点最大的值,然后更新最右端点ans++。初始时t=0
注:所谓衔接不是[0,1][1,2]这样首尾相接,而是[0,1][2,3]即可,故为 t+1
#include<iostream>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<vector>
#include<set>
#include<stdio.h>
#include<stack>
#include<bitset>
using namespace std; int n,T;
struct P
{
int x,y;
}s[25002];
int cmp(P a,P b)
{
return a.x<b.x;
}
int main()
{
scanf("%d%d",&n,&T);
for(int i=0;i<n;i++)scanf("%d%d",&s[i].x,&s[i].y);
sort(s,s+n,cmp);
s[n].x=0x7fffffff; //边界处要考虑
int t=0,temp=0,ans=0;
bool f=0;
for(int i=0;i<n;i++)
{
if(s[i].x<=t+1)
{
if(s[i].y>temp)temp=s[i].y,f=1;
if(s[i+1].x>t+1&&f)
{
ans++;
t=temp;
f=0;
}
}
}
if (t<T) printf("-1\n"); else printf("%d\n",ans);
return 0;
}
POJ 2376 Cleaning Shifts【贪心】的更多相关文章
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- 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 贪心 区间问题
<pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS Memory ...
- POJ 2376 Cleaning Shifts (贪心,区间覆盖)
题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...
- 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 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
- ACM学习历程——POJ 2376 Cleaning Shifts(贪心)
Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning ...
随机推荐
- bzoj千题计划303:bzoj4827: [Hnoi2017]礼物
https://www.lydsy.com/JudgeOnline/problem.php?id=4827 式子化简一下,发现最后只跟 Σ xi*yi 有关 第二个序列反转,就可以用FFT优化 注意: ...
- mysql的事件
mysql的事件定时器的使用: SHOW VARIABLES LIKE 'event_scheduler' --查询event_scheduler开启状态 SET GLOBAL event_sched ...
- 词典的实现(1)--Map的底层实现
1,词典是这样的一种数据结构:它能根据给定的键(索引值,key)来查找其对应的值(value)是否存在,在JAVA中主要由java.util.HashMap来完成该功能.如电话本就是词典的一个具体实例 ...
- Python分析网页中的<a>标签
soup = BeautifulSoup(html,"html.parser") html=soup.select("table a") for k in ht ...
- Composer 安装和使用
1.linux下安装 curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer ...
- C# print2flash3文件转化
1.下载print2flash3 并且安装print2flash3 2.转换工具类 (1)需要导入using Print2Flash3; 这个程序集 using System; using Syste ...
- JavaScript之子类构建工具
(function(){ var initializing = false; var superPattern = /xyz/.test(function(){ xyz; }) ? /\b_super ...
- OO第三阶段总结
软件形式化方法历史 形式化方法的研究高潮始于20世纪60年代后期,针对当时所谓"软件危机",人们提出种种解决方法,归纳起来有两类:一是采用工程方法来组织.管理软件的开发过程:二是深 ...
- CNN可解释
1 http://bindog.github.io/blog/2018/02/10/model-explanation/ http://www.sohu.com/a/216216094_473283 ...
- Java导出List集合到txt文件中——(四)
有时候,需要将数据以一定格式导出到txt文件中.利用Java的IO可以轻松的导出数据到txt中. package Action.txt; import java.io.BufferedWriter; ...