POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts
题目连接:
http://poj.org/problem?id=2376
Description
Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T.
Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval.
Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.
Input
Line 1: Two space-separated integers: N and T
Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.
Output
- Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.
Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
Hint
题意
给你n个区间,要求你选出最少的区间,使得能够覆盖[1,t]
题解:
dp很简单,dp[i] = min(dp[j])+1,其中需满足a[j].r+1>=a[i].l
贪心也很简单,每次我们扫的时候,直接扫到能够转移到的最右边就好了(其实也是DP思想..
期末考试前写道水题攒攒RP T T
代码
#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
struct node
{
int x,y;
};
bool cmp(node a,node b)
{
if(a.x==b.x)return a.y>b.y;
return a.x<b.x;
}
node p[25005];
int main()
{
int n,t;
scanf("%d%d",&n,&t);
for(int i=1;i<=n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
sort(p+1,p+1+n,cmp);
int l = 0;
int ans = 0;
int i = 1;
while(i<=n)
{
if(p[i].x>l+1)return puts("-1");
int tmp = l;
while(i<=n&&p[i].x<=l+1)
{
tmp = max(tmp,p[i].y);
i++;
}
l = tmp;ans++;
if(l>=t)return printf("%d\n",ans);
}
return puts("-1");
}
POJ 2376 Cleaning Shifts 贪心的更多相关文章
- 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【贪心】
POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...
- 【原创】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 ...
随机推荐
- 360网站卫士推出google字体加速方案
最近,有网友反映称谷歌官网域名google.com.谷歌香港google.com.hk都打不开, ping了一下google.com和google.com.hk两个域名的服务器情况,最后ping出来的 ...
- JAVA多线程二
Thread.Join() join()函数表示等待当前线程结束,然后返回. public final synchronized void join(long millis) throws Inter ...
- 解决:cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
在win下用Go语言的cgo时(比如下面场景)我们会用到的GCC编译器,Win下我们一般用MinGW. Golang连接Oracle数据库:win下 golang 跨平台编译 MinGW全称Minim ...
- Java之--Java语言基础组成(关键字、标识符、注释、常量和变量、运算符)
Java语言基础组成-关键字.标识符.注释.常量和变量.运算符 Java语言由8个模块构成,分别为:1.关键字:2.标识符(包名.类名.接口名.常量名.变量名等):3.注释:4.常量和变量:5.运算符 ...
- 可进行JavaScript代码测试与调试的12个网站
概述:JavaScript是网站前端开发最为重要的一门编程语言,本文收集了能够在线测试与调试JavaScript代码的12个网站 1.JS Bin JS bin是一个为JavaScript和CSS爱好 ...
- 一个有趣的模拟光照的shader(类似法线贴图)
最近使用unity,碰到到一个很有趣的例子.场景无光线,却模拟出了光照,效果挺好.其思路与法线贴图原理异曲同工. 原作者提供的效果印象深刻. 模型除了使用原来的diffuse贴图外,还用到了一张模拟记 ...
- docker集成管理工具-shipyard的开发环境搭建笔记
前段时间一直在研究openstack,后来老师告诉我需要用docker容器来搭建hadoop集群,所以就将战场转移到docker上来了,话说docker最近这段时间太火了,但是说实话我觉得应用起来还不 ...
- 使用MockMvc测试Spring mvc Controller
概述 对模块进行集成测试时,希望能够通过输入URL对Controller进行测试,如果通过启动服务器,建立http client进行测试,这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不方便 ...
- error while loading shared libraries: lib******: cannot open shared object file: No such file or directory
程序编译成功后,运行时错误: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object fi ...
- 【转】17种常用的JS正则表达式 非负浮点数 非负正数.
<input type='text' id='SYS_PAGE_JumpPage' name='SYS_PAGE_JumpPage' size='3' maxlength='5' onkeyup ...