poj 3190 Stall Reservations

http://poj.org/problem?id=3190ios

Stall Reservations
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3567   Accepted: 1276   Special Judge

Descriptionui

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.

Inputthis

Line 1: A single integer, N 

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

Outputspa

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 Inputcode

5
1 10
2 4
3 6
5 8
4 7

Sample Outputblog

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.

Source隊列

 

 

分析:ip

這個題是說一些奶牛要在指定的時間內擠牛奶,而一個機器只能同時對一個奶牛工做。給你每頭奶牛的指定時間的區間,問你最小須要多少機器。ci

最開始想的是以奶牛要求時間的結束點從小到大進行排序,但後來發現這樣的想法是錯誤的。

1 6
2 8 8
3 4 8
4 6 10
5 1 3
6 2 4
7 4 7

 

後來通過調整,應該先按奶牛要求的時間起始點進行從小到大排序,而後維護一個優先隊列,裏

面以已經開始擠奶的奶牛的結束時間早爲優先。而後每次只須要檢查當前是否有奶牛的擠奶工做已經完成的機器便可,如有,則換那臺機器進行工做。若沒有,則加一臺新的機器。

 

 

 

AC代碼:

 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 }    

 

 

 

AC代碼:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<queue>
 5 #include<cstdio>
 6 using namespace std;
 7 struct TT
 8 {
 9     int x, y, id;
10 }a[50010];
11 int vis[50010];
12 bool cmp( TT m, TT n)
13 {
14     if((m.x<n.x) || (m.x==n.x && m.y<n.y)) return true;
15     return false;
16 }
17 bool operator<( TT a, TT b ){
18     return a.y>b.y;
19 }
20 int main()
21 {
22     int T,i;
23     while(~scanf("%d",&T))
24     {
25         priority_queue<TT>pp;
26         memset(vis,0,sizeof(vis));
27         for(i=1;i<=T;i++)
28         {
29             scanf("%d %d",&a[i].x,&a[i].y);
30             a[i].id = i;
31         }
32     TT now;
33     sort(a+1,a+1+T,cmp);
34     int ans = 1;
35     vis[a[1].id]=ans;
36     pp.push(a[1]);
37     for(i=2; i<=T; i++)
38     {
39          now = pp.top();
40          if(a[i].x>now.y)
41          {
42              vis[a[i].id] = vis[now.id];
43              pp.pop();//取出該點;
44              now.y = a[i].y;//更新末尾點;
45              pp.push(now);
46          }
47          else
48          {
49              vis[a[i].id] = ++ans;
50              pp.push(a[i]);
51          }
52     }
53         printf("%d\n",ans);
54         for(int i=1;i<=T;i++)
55         {
56             printf("%d\n",vis[i]);
57         }
58    }
59     return 0;
60 }
相關文章
相關標籤/搜索