码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2376

时间:2019-03-19 21:47:58      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:into   end   amp   hat   tac   which   efi   oss   fir   

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

 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <utility>
 7 #include <vector>
 8 #include <map>
 9 #include <queue>
10 #include <stack>
11 #include <cstdlib>
12 #include <cmath>
13 typedef long long ll;
14 #define lowbit(x) (x&(-x))
15 #define ls l,m,rt<<1
16 #define rs m+1,r,rt<<1|1
17 using namespace std;
18 #define pi acos(-1)
19 #define P pair<ll,ll>
20 const int N  = 2e6+200;
21 const ll mod= 1000000007;
22 ll n,t;
23 int main()
24 {
25     scanf("%lld%lld",&n,&t);
26     ll x,y;
27     priority_queue<P,vector<P>,greater<P> >Q;
28     for(int i=0;i<n;i++){
29         scanf("%lld%lld",&x,&y);
30         Q.push(P(x,y));
31     }    
32     int flag=1;
33     ll  en=0,cnt=0;
34     while(en<t&&flag){
35         flag=0;
36         int st = en+1;
37         while(!Q.empty()&&Q.top().first<=st){//和区间问题有差异,点是独立的。 
38             ll u = Q.top().second;
39             Q.pop();
40             if(u>en){
41                 en = u;
42                 flag=1;//只要flag =1,那么en 就在不断变大 
43                 // 找到的第一个的左一定是1,而右是符合条件里面的的最大值 
44             }
45         }
46         cnt++;
47     }
48     if(flag) printf("%lld\n",cnt);//只要flag==1,就一定可以找到满足题意的。 
49     else printf("-1\n");    
50     return 0;
51 }

 

poj 2376

标签:into   end   amp   hat   tac   which   efi   oss   fir   

原文地址:https://www.cnblogs.com/tingtin/p/10561388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!