贪心 POJ2376 区间查最小覆盖
题目:https://vjudge.net/problem/POJ-2376
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.
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.
* 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.
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.
题意: 给定一个n和t n头牛和t时间
每头牛负责的开始和结束时间
求从1到t时间内至少需要几头牛 如果有时间点断开 输出-1
思路: 代码里面有 ,,主要是贪心思想
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<set> #include<algorithm> #include<map> #define maxn 200005 using namespace std; struct node //结构体记录开始时间和结束时间 { int st,en; }; node a[maxn]; bool cmp(node x,node y) //结构体排序,先按开始时间排序在按结束时间从小到大排序 { if(x.st!=y.st)return x.st<y.st; else x.en<y.en; } int main() { int n,t; while(scanf("%d%d",&n,&t)!=EOF) { for(int i=0;i<n;i++) { cin>>a[i].st>>a[i].en; if(a[i].st>a[i].en) swap(a[i].st,a[i].en); } sort(a,a+n,cmp); int sta=0; //开始时间 int enn=0; //结束时间 是去结束时间的最大值 int cnt=0; //计数 int flag=0; //跳跃 while(enn<t) { sta=enn+1; //开始时间一开始的时候是1开始 后面更新开始时间 for(int i=flag;i<n;i++) //从flag开始避免重复查询 { if(a[i].st<=sta&&a[i].en>=enn) //寻找符合条件且结束时间最大的值 { enn=max(a[i].en,enn); } else if(a[i].st>sta) //遇到不符合条件的结束查询,并记录flag { flag=i; break; } } if(sta>enn) //如果开始时间大于了结束时间说明无法继续查询下去 { break; } else cnt++; //循环次数 } if(enn>=t) //如果结束时间大于t了说明已经找到了需要的次数了 { printf("%d\n",cnt); } else { printf("-1\n"); } } return 0; }