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 ...
随机推荐
- MySQL - 日常操作二 备份还原
登录mysql的命令 # 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 mysql -h 110. -P3306 -uroot -p mysql -uroot -p -S /dat ...
- Linux下创建C函数库
http://blog.163.com/hitperson@126/blog/static/130245975201151552938133 http://blog.sina.com.cn/s/blo ...
- Mybatis进阶学习笔记——输出映射
输出映射(例如一个方法的返回至使用什么类型去接收) 1.基本类型 <!-- 统计记录数 --> <select id="queryTotalCount" resu ...
- 【Udacity并行计算课程笔记】- Lesson 3 Fundamental GPU Algorithms (Reduce, Scan, Histogram)
本周主要内容如下: 如何分析GPU算法的速度和效率 3个新的基本算法:归约.扫描和直方图(Reduce.Scan.Histogram) 一.评估标准 首先介绍用于评估GPU计算的两个标准: ste ...
- Android BroadcastReceiver解析
目录 示意图 1. 定义 即 广播,是一个全局的监听器,属于Android四大组件之一 Android 广播分为两个角色:广播发送者.广播接收者 2. 作用 监听 / 接收 应用 App 发出的广 ...
- spring整合ehcache2.5.2缓存异常-- net.sf.ehcache.CacheException
报错如下: The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcach ...
- Pytorch 之 backward
首先看这个自动求导的参数: grad_variables:形状与variable一致,对于y.backward(),grad_variables相当于链式法则dz/dx=dz/dy × dy/dx 中 ...
- 同步阿里云镜像到本地,在本地搭建YUM仓库
1.下载阿里云镜像repo文件 项目使用CentOS6系统,因此我下载的文件是: # CentOS-Base.repo # # The mirror system uses the connectin ...
- freeRTOS中文实用教程3--中断管理之中断嵌套
1.前言 最新的 FreeRTOS 移植中允许中断嵌套.中断嵌套需要在 FreeRTOSConfig.h 中设置configKERNEL_INTERRUPT_PRIORITY 和configMAX_S ...
- Delphi中的动态包,有详细建立包的步骤(答案很简单:因为包的功能强大)
为什么要使用包? 答案很简单:因为包的功能强大.设计期包(design-time package)简化了自定义组件的发布和安装:而运行期包(run-time package)则更是给传统的程序设计注入 ...