【LIS变形】 zoj 2319 Beautiful People

来源:互联网 发布:nginx body filter 编辑:程序博客网 时间:2024/04/30 08:09
Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge

The most prestigious sports club in one city has exactly N members. Each of its members is strong and beautiful. More precisely, i-th member of this club (members being numbered by the time they entered the club) has strength Si and beauty Bi. Since this is a very prestigious club, its members are very rich and therefore extraordinary people, so they often extremely hate each other. Strictly speaking, i-th member of the club Mr X hates j-th member of the club Mr Y if Si <= Sj and Bi >= Bj or if Si >= Sj and Bi <= Bj (if both properties of Mr X are greater then corresponding properties of Mr Y, he doesn��t even notice him, on the other hand, if both of his properties are less, he respects Mr Y very much).

To celebrate a new 2005 year, the administration of the club is planning to organize a party. However they are afraid that if two people who hate each other would simultaneouly attend the party, after a drink or two they would start a fight. So no two people who hate each other should be invited. On the other hand, to keep the club prestige at the apropriate level, administration wants to invite as many people as possible.

Being the only one among administration who is not afraid of touching a computer, you are to write a program which would find out whom to invite to the party.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Input

The first line of the input file contains integer N - the number of members of the club. (2 <= N <= 100 000). Next N lines contain two numbers each - Si and Bi respectively (1 <= Si, Bi <= 109).


Output

On the first line of the output file print the maximum number of the people that can be invited to the party. On the second line output N integers - numbers of members to be invited in arbitrary order. If several solutions exist, output any one.


Sample Input

1

4
1 1
1 2
2 1
2 2


Sample Output

2
1 4


LIS模板:http://blog.csdn.net/baidu_35643793/article/details/53197107  o(nlogn)的时间复杂度;

题意:有n个人,每个人有一个Si和Bi,如果Si < Sj && Bi < Bj,则 i 和 j 不互相讨厌。问从这n个人中最多可以选出多少个人使得任意两个人都不会互相讨厌。

分析:因为一个人的S和B都严格小于另外一个人的S和B时,这连个人才不会互相讨厌。所以可以先对S从小到大排序,如果S相同则把B从大到小排序,这样问题就转化为了求最长上升子序列。当S相同时之所以要对B从大到小排序,是因为如果S递增的话,那么选出的方案中会将S相同的几个人都包含进去,不符合题目要求。然后就用O(nlogn)的算法求最长上升子序列的同时,记录到达每一个数时的最大长度。最后从最大长度向下输出路径即可。

将S,B都排好序了,就不用再考虑S了,就是B的LIS的长度并输出就好;


代码:

[cpp] view plain copy
  1. #include<cstdio>  
  2. #include<cstring>  
  3. #include<algorithm>  
  4. using namespace std;  
  5. const int maxn=100005;  
  6. int D[maxn];  
  7. int F[maxn];  
  8. struct node  
  9. {  
  10.     int s, b;  
  11.     int id;  
  12.     bool operator < (const node &x) const  
  13.     {  
  14.         if(x.s!=s)  
  15.             return x.s>s;  
  16.         else  
  17.             return x.b<b;  
  18.     }  
  19. } A[maxn];  
  20.   
  21. int main()  
  22. {  
  23.     int t;  
  24.     while(~scanf("%d",&t))  
  25.     {  
  26.         while(t--)  
  27.         {  
  28.             int n;  
  29.             scanf("%d",&n);  
  30.             for(int i=1; i<=n; i++)  
  31.             {  
  32.                 scanf("%d%d",&A[i].s,&A[i].b);  
  33.                 A[i].id=i;  
  34.             }  
  35.   
  36.             sort(A+1,A+n+1);  
  37.             int max_len=0;  
  38.             D[++max_len]=A[1].b;  
  39.             F[1]=max_len;  
  40.             for(int i=2; i<=n; i++)  
  41.             {  
  42.                 if(D[max_len]<A[i].b)  
  43.                 {  
  44.                     D[++max_len]=A[i].b;  
  45.                     F[i]=max_len;  
  46.                 }  
  47.                 else  
  48.                 {  
  49.                     int k=lower_bound(D+1,D+1+max_len,A[i].b)-D;  
  50.                     D[k]=A[i].b;  
  51.                     F[i]=k;  
  52.                 }  
  53.             }  
  54.   
  55.             printf("%d\n",max_len);  
  56.             for(int i=n; i>=1; i--)  
  57.             {  
  58.                 if(F[i]==max_len)  
  59.                 {  
  60.                     printf("%d",A[i].id);  
  61.                     if(max_len>1)  printf(" ");  
  62.                     max_len--;  
  63.                 }  
  64.             }  
  65.             printf("\n");  
  66.         }  
  67.     }  
  68.     return 0;  
  69. }  


定义结构体优先级的方法:http://blog.csdn.net/baidu_35643793/article/details/70173709


0 0