Final Exam Arrangement(ZOJ)

In Zhejiang University, there are N different courses labeled from 1 to N. Each course has its own time slot during the week. We can represent the time slot of a course by an left-closed right-open interval [s, t).

Now we are going to arrange the final exam time of all the courses.

The final exam period will contain multiple days. In each day, multiple final exams will be held simultaneously. If two courses' time slots are not overlapped, there may be students who are attending both of them, so we cannot arrange their final exams at the same day.

Now you're to arrange the final exam period, to make the total days as small as possible.

Input

There are multiple test cases separated by blank lines.

For each ease, the 1st line contains one integer N(1<=N<=100000).

Then N lines, the i+1th line contains s and t of the interval [s, t) for the ith course.(0<=s<t<=231-1)

There is a blank line after each test case.

Output

For each case, the 1st line contains the days P in the shortest final exam period.

Next P lines, the i+1th line contains the numbers of courses whose final exam is arranged on the ith day separated by one space.

Output a blank line after each test case.

Sample Input

4
0 1
1 2
2 3
3 4

4
0 2
1 3
2 4
3 5

4
0 4
1 5
2 4
3 6

Sample Output

4
1
2
3
4

2
1 2
3 4

1
1 2 3 4
 1 #include <stdio.h>
 2 #define N 100005
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 struct node
 7 {
 8     int a,b,num;
 9 } po[N];
10 int out[2*N+100];
11 int cmp(node x,node y)
12 {
13     if(x.a==y.a) return x.b>y.b;
14     return x.a<y.a;
15 }
16 int main()
17 {
18     int n,i,j;
19     while(scanf("%d",&n)!=EOF)
20     {
21         for(i=0; i<n; i++)
22         {
23             scanf("%d%d",&po[i].a,&po[i].b);
24             po[i].num=i;
25         }
26         sort(po,po+n,cmp);
27         int k,ans=0,c=0;
28         for(i=0; i<n; )
29         {
30             k=po[i].b;
31             for(j=i; ; j++)
32             {
33                 if(po[j].a>=k)
34                 {
35                     i=j;
36                     ans++;
37                     out[c++]=-1;
38                     break;
39                 }
40                 else
41                 {
42                     out[c++]=po[j].num;
43                     if(po[j].b<k) k=po[j].b;
44                 }
45                 if(j==n)
46                 {
47                     i=n;
48                     break;
49                 }
50             }
51         }
52         printf("%d\n",ans+1);
53         for(i=0; i<c-1; i++)
54         {
55             if(out[i]==-1) puts("");
56             else
57             {
58                 if(i && out[i-1]!=-1) putchar(' ');
59                 printf("%d",out[i]+1);
60             }
61         }
62         puts("\n");
63     }
64     return 0;
65 }
贪心

 

posted @ 2013-08-05 23:29  1002liu  阅读(216)  评论(0编辑  收藏  举报