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

Stall Reservations(POJ 3190 贪心+优先队列)

时间:2016-01-28 13:57:50      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

Stall Reservations
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4434   Accepted: 1588   Special Judge

Description

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows. 

Help FJ by determining:
  • The minimum number of stalls required in the barn so that each cow can have her private milking period
  • An assignment of cows to these stalls over time
Many answers are correct for each test dataset; a program will grade your answer.

Input

Line 1: A single integer, N 

Lines 2..N+1: Line i+1 describes cow i‘s milking interval with two space-separated integers.

Output

Line 1: The minimum number of stalls the barn must have. 

Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.

Sample Input

5
1 10
2 4
3 6
5 8
4 7

Sample Output

4
1
2
3
2
4

Hint

Explanation of the sample: 

Here‘s a graphical schedule for this output: 

Time     1  2  3  4  5  6  7  8  9 10

Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>
Stall 2 .. c2>>>>>> c4>>>>>>>>> .. ..
Stall 3 .. .. c3>>>>>>>>> .. .. .. ..
Stall 4 .. .. .. c5>>>>>>>>> .. .. ..
Other outputs using the same number of stalls are possible.

这个题是说一些奶牛要在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作。给你每头奶牛的指定时间的区间,问你最小需要多少机器。

先按奶牛要求的时间起始点进行从小到大排序,然后维护一个优先队列,里面以已经开始挤奶的奶牛的结束时间早为优先。然后每次只需要检查当前是否有奶牛的挤奶工作已经完成的机器即可,若有,则换那台机器进行工作。若没有,则加一台新的机器。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <queue>
 6 using namespace std;
 7 const int maxn=60000;
 8 int n,use[maxn];
 9 struct Node
10 {
11     int l;
12     int r;
13     int pos;
14     bool operator <(const Node &a)const 
15     {
16     if(r==a.r)
17         return l>a.l;
18     return r>a.r;
19     }
20 }a[maxn];
21 priority_queue<Node> q;
22 bool cmp(Node a,Node b)
23 {
24     if(a.l==b.l)
25     return a.r<b.r;
26     return a.l<b.l;
27 }
28 int main()
29 {
30     while(scanf("%d",&n)!=EOF)
31     {
32     for(int i=0;i<n;i++)
33     {
34         scanf("%d%d",&a[i].l,&a[i].r);
35         a[i].pos=i;
36     }
37     sort(a,a+n,cmp);
38     q.push(a[0]);
39     int now=0,ans=1;
40     use[a[0].pos]=1;
41     for(int i=1;i<n;i++)
42     {
43         if(!q.empty()&&q.top().r<a[i].l)
44         {
45         use[a[i].pos]=use[q.top().pos];
46         q.pop();
47         }
48         else
49         {
50         ans++;
51         use[a[i].pos]=ans;
52         }
53         q.push(a[i]);
54     }
55     printf("%d\n",ans);
56     for(int i=0;i<n;i++)
57         printf("%d\n",use[i]);
58     while(!q.empty())
59         q.pop();
60     }
61     return 0;
62 }    

TLE CODE:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 struct node
 7 {
 8     int x1,x2;
 9     int num;
10 }a[50000+10];
11 bool cmp(node a,node b)
12 {
13     if(a.x1==b.x1)
14         return a.x2<b.x2;
15     return a.x1<b.x1;
16 }
17 bool vis[50000+10];
18 int pos[50000+10];
19 int n;
20 int search(int m)
21 {
22     int l=0,r=n-1,mid,k=-1;
23     while(l<=r)
24     {
25         mid=(l+r)/2;
26         if(a[mid].x1>=m)
27         {
28             
29             k=mid;
30             r=mid-1;
31         }
32         else
33             l=mid+1;
34     }
35     return k;
36 }
37 int main()
38 {
39     int i,j;
40     freopen("in.txt","r",stdin);
41     while(scanf("%d",&n)!=EOF)
42     {
43         int count=0;
44         memset(vis,0,sizeof(vis));
45         fill(pos,pos+n,0);
46         for(i=0;i<n;i++)
47         {
48             scanf("%d%d",&a[i].x1,&a[i].x2);
49             a[i].num=i;
50         }
51         sort(a,a+n,cmp);
52         int last,coun=0,p;
53         for(i=0;i<n;i++)
54         {
55             if(vis[i])
56                 continue;
57             last=a[i].x2+1;
58             vis[i]=1;
59             pos[a[i].num]=++coun;
60             while(1)
61             {
62                 p=search(last);
63                 if(p==-1)
64                     break;
65                 if(vis[p])
66                     last=a[p].x1+1;
67                 else
68                 {
69                     last=a[p].x2+1;
70                     vis[p]=1;
71                     pos[a[p].num]=coun;
72                 }
73             }
74         }
75         printf("%d\n",coun);
76         for(i=0;i<n;i++)
77             printf("%d\n",pos[i]);
78     }
79 }

 

Stall Reservations(POJ 3190 贪心+优先队列)

标签:

原文地址:http://www.cnblogs.com/a1225234/p/5165864.html

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