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 ...
随机推荐
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- 转--python 面试题
# 每一题都值得好好琢磨钻透 [原文地址](http://www.cnblogs.com/Allen-rg/p/7693394.html)1.Python是如何进行内存管理的? 答:从三个方面来说,一 ...
- linux僵尸进程产生的原因以及如何避免产生僵尸进程
给进程设置僵尸状态的目的是维护子进程的信息,以便父进程在以后某个时间获取.这些信息包括子进程的进程ID.终止状态以及资源利用信息(CPU时间,内存使用量等等).如果一个进程终止,而该进程有子进程处于僵 ...
- JavaScript 无刷新修改浏览器URL地址栏
//发现地址栏已改为:newUrlvar stateObject = {}; var title = "Wow Title"; var newUrl = "/my/awe ...
- C#窗口防止闪烁两种方法
public class PaintIncrease { public static void SetDoubleBuffered(object obj) { Type type = obj.GetT ...
- 八、IIC 接口
8.1 IIC接口介绍 8.1.1 IIC 总线的概念 I2C总线是由Philips公司开发的一种简单.双向二线制同步串行总线.它只需要两根线即可在连接于总线上的器件之间传送信息. 主器件用于启动总线 ...
- Hyper-V和其他虚拟机共存 【转】
由于Windows中Hyper-V的实现方式在系统中为独占,所以其不能与其他的虚拟机共存. 下面的方法是在系统中新增一个启动项,在这个启动项中关闭Hyper-V,这样就可以安装并使用其他虚拟机了.需要 ...
- crontab在/var/log/目录下没有cron.log文件
1.修改rsyslog文件: /etc/rsyslog.d/50-default.conf 将 rsyslog 文件中的 #cron.* 前的 # 删掉: 2.重启rsyslog服务: s ...
- WPF通过DynamicResource的用法
1.先在资源类库中编写:style.xaml,如下: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2 ...
- MySQL各个版本区别及问题总结
参考:http://www.admin10000.com/document/62.html 一.简介 MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个 ...