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 ...
随机推荐
- DOM盒子模型
获取行间样式 style.height //获取行间样式高度 xx.currentStyle.height //计算后的样式高度 IE低版本 getComputedStyle().height //获 ...
- ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password:NO)
转自:http://blog.sina.com.cn/s/blog_586a1f3e01000b82.html 刚使用mysql, 就老是碰到这个问题,真是郁闷, 终于找到原因.. C:\Progra ...
- axios - 基于 Promise 的 HTTP 异步请求库
axios 是基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用.Vue 更新到2.0之后,作者就宣告不再对 vue-resource 模块更新,而是推荐使用 a ...
- 理解django框架中的MTV与MVC模式
1.Models:一个抽象层,用来构建和操作你的web应用中的数据,模型是你的数据的唯一的.权威的信息源.它包含你所储存数据的必要字段和行为.通常,每个模型对应数据库中唯一的一张表. from dja ...
- Python2和Python3中print的不同点
在Python2和Python3中都提供print()方法来打印信息,但两个版本间的print稍微有差异 主要体现在以下几个方面: 1.python3中print是一个内置函数,有多个参数,而pyth ...
- 运维数据库平台~inception测试脚本
一 简介:今天咱们来聊聊inception的测试脚本 二 范例: #!/usr/bin/python import MySQLdb sql='/*--user=;--password=;--host= ...
- python 入门基础21 --面向对象_多态、内置方法、反射
内容: 1.接口思想 2.抽象类思想 3.多态 4.内置方法 5.反射 1.接口思想 建立关联的桥梁,方便管理代码 接口类:用来定义功能的类,位继承它的子类提供功能 该类的功能方法一般不需要实现体,实 ...
- plist反序列化
// // ViewController.m // 03-plist文件的一个反序列化 // // Created by jerry on 15/9/28. // Copyright (c) ...
- select 1
select 1 from mytable;与select anycol(目的表集合中的任意一行) from mytable;与select * from mytable 作用上来说是没有差别的,都是 ...
- 使用Idea初始化SpringMvc项目
(1) (2) (3) (4) (5)感谢http://www.cnblogs.com/feiyujun/p/6537510.html (6)