POJ2376 Cleaning Shifts 【贪心】

Cleaning Shifts

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11542   Accepted: 3004

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

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

INPUT DETAILS:

There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10.

OUTPUT DETAILS:

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

Source

USACO 2004 December Silver

用最少的线段覆盖所有的点

#include <stdio.h>
#include <string.h>
#include <algorithm>

#define maxn 25005

struct Node {
	int u, v;
} E[maxn];

bool cmp(Node a, Node b) {
	return a.u < b.u;
}

int main() {
	int N, T, flag, ans, right, i;
	while(scanf("%d%d", &N, &T) == 2) {
		for(i = 0; i < N; ++i)
			scanf("%d%d", &E[i].u, &E[i].v);
		std::sort(E, E + N, cmp);
		ans = right = 0;
		flag = 1;
		i = 0;
		while(flag <= T) {
			for( ; i < N && E[i].u <= flag; ++i)
				if(E[i].u <= flag && E[i].v > right)
					right = E[i].v;
			if(right >= flag) flag = right + 1, ++ans;
			else break;
		}
		if(flag <= T) ans = -1;
		printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-10-18 17:41:27

POJ2376 Cleaning Shifts 【贪心】的相关文章

POJ2376 Cleaning Shifts(贪心)

给出每头奶牛的覆盖区间,求最少几头奶牛可以覆盖[1,T]这个区间.(有个问题需要注意一下,比如T=10,1-5,6-10这就算全部覆盖了,覆盖的是点) #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define rep(i,n) for(int (i)=0;(i)<n;(i)++) using namespace std; struct Node {

POJ 2376 Cleaning shifts 贪心 基础题

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13042   Accepted: 3373 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 co

POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

Cleaning Shifts 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

poj2376 Cleaning Shifts【线段树】【DP】

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32561   Accepted: 7972 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 co

poj2376 Cleaning Shifts 区间贪心

题目大意: (不说牛了) 给出n个区间,选出个数最少的区间来覆盖区间[1,t].n,t都是给出的. 题目中默认情况是[1,x],[x+1,t]也是可以的.也就是两个相邻的区间之间可以是小区间的右端与大区间的左端相差1.这个是看题解才知道的. 解题思路: 贪心题的关键是找到贪心策略.但是这题的贪心策略没那么明显.并且贪心策略没有特定地去选择某一区间.这一题最重要的是要知道在什么情况下才需要增加一个区间. 首先是进行排序,按照区间的左端从小到大排序,左端相同的按照右端从小到大排. 从头开始遍历(只能

poj2376 Cleaning Shifts(区间贪心,理解题意)

https://vjudge.net/problem/POJ-2376 题意理解错了!!真是要仔细看题啊!! 看了poj的discuss才发现,如果前一头牛截止到3,那么下一头牛可以从4开始!!! 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7

POJ2376 Cleaning Shifts

http://poj.org/problem?id=2376 类似于工作排序问题 贪心策略:在符合时间情况的选项中 选择结束时间最迟的牛 具体步骤: 按照开始时间升序排列  如果 开始时间相同 按照结束时间升序排列 设t为最终结束时间 区间[1, t]为最终区间 一次1 to n的循环 同时 扩大区间 最后比较t和T的值即可 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 5 usi

POJ 2376 Cleaning Shifts (贪心,区间覆盖)

题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小于1的部分(如果有的话),再找最长的区间,然后把这个区间的右端点作为下次寻找的起点, 再找最大区间,直到覆盖到最后. 注意:首先要判断好能不能覆盖,不能覆盖就结束,有可能会提前结束,也要做好判断,我就在这WA了好几次, 悲剧...其他的就比较简单了,不用说了. 代码如下: #include <ios

Cleaning Shifts(POJ 2376 贪心)

Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15143   Accepted: 3875 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 co