POJ 2376

连接点应该是只需连接而不必覆盖。

按开始时间排序。

假设当前最远为t,则按序枚举在t+1范围内开始的cow即可,再求出最远点。注意break条件。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cctype>
 5 #include <algorithm>
 6 #define LL unsigned __int64
 7 using namespace std;
 8
 9 const int N= 25100;
10
11 struct Cow{
12     int s,e;
13     bool operator < (const Cow &a)const{
14         if(s<a.s) return true;
15         return false;
16     }
17 }cow[N];
18 int n,T;
19
20 int main(){
21     while(scanf("%d%d",&n,&T)!=EOF){
22         for(int i=0;i<n;i++){
23             scanf("%d%d",&cow[i].s,&cow[i].e);
24         }
25         sort(cow,cow+n);
26         int i=0,t=0,j;
27         int ans=0;
28         if(cow[i].s>1){
29             puts("-1");
30             continue;
31         }
32         while(i<n&&t<T){
33             j=-1;
34             while(i<n&&cow[i].s<=t+1){
35                 j=max(j,cow[i].e);
36                 i++;
37             }
38             if(j<=t) break;
39             t=j;
40         //    cout<<t<<endl;
41             ans++;
42         }
43         if(t<T){
44             printf("-1\n");
45         }
46         else printf("%d\n",ans);
47     }
48     return 0;
49 }

时间: 2024-10-12 16:20:08

POJ 2376的相关文章

POJ 2376 (区间问题,贪心)

题目链接:http://poj.org/problem?id=2376 题目大意:选择一些区间使得能够覆盖1-T中的每一个点,并且区间数最少 题目分析:这道题目很明显可以用贪心法来解决.但题目没有看起来那么简单,有许多的坑. 我的贪心策略如下: 1.将区间按照起点排序,并且保证起点相同的,终点大的排在前边 2.在前一个选取的区间范围[L0,R0+1]中,选取起点在此范围但终点最靠右的一个区间 3.重复这个过程 另外,还有几点需要注意的地方: 1.要保证第一个区间起点和最后一个区间终点符合1-L的

贪心问题 区间覆盖 —— POJ 2376 Cleaning Shift

题目:http://poj.org/problem?id=2376 题意:就是 N 个区间, 输入 N 个区间的 [begin, end],求能用它们覆盖区间[1,T]的最小组合. 题解: 1. 首先对所有奶牛的排序,按照开始时间升序排序. 2. 更新 起点 为 上一次的终点 + 1,并寻找覆盖起点,且终点最远的区间 #include <iostream> #include <cstdlib> #include <cstdio> #include <algorit

POJ 2376 Cleaning Shifts(轮班打扫)

Time Limit: 1000MS   Memory Limit: 65536K [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 t

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

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 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12874   Accepted: 3331 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

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

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

poj 2376 Cleaning Shifts(贪心)

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), t