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
每一个点有两个属性 s,b;当一个的s,b都大于另外一个的时候才不会有矛盾,现在求没有矛盾的最大个数,并且输出这些数的序号(序号可以是乱的)
这个题有两个难点,第一个就是这么把它转化成一般的LIS,我看别的代码,先把s升序,如果s相同,按b降序,这样每次seach函数找到的f[i].b,的位置就是以这一个点为结尾的最长长度,就可以转化成一般的LIS了
还有一个难点就是保存路径,这个看别人博客居然是简单的mark[i]记下以i为结尾的最长上升字串的长度就可以了,真的是长见识了,换而言之 如果数据
3
1 1
2 2
2 3
那么排序后就是1 3 2 (这是原来的序号)
mark[3]=2,mark[2]=2;mark[1]=1;//排序后的标号
这时候认真看我的print()函数想想吧
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define eps 1e-8
using namespace std;
#define N 100005
int dp[N],mark[N];
struct stud{
int s,b;
int pos;
}f[N];
int n,len;
int cmp(stud x,stud y)
{
if(x.s==y.s)
return x.b>y.b;
return x.s<y.s;
}
int seach(int len,int x)
{
int le,ri,mid,ans;
le=0;ri=len;
while(le<=ri)
{
mid=(le+ri)>>1;
if(dp[mid]<x)
{
ans=mid;
le=mid+1;
}
else
ri=mid-1;
}
return ans+1; //ans是找到的不大于x的坐标,应该返回比它大一的坐标
}
void print()
{
int j=0;
printf("%d\n",len);
for(int i=n;i>=1;i--)
{
if(mark[i]==len) //d对于s相同的有可能有几个的mark相等,但是输出一个后len--了,所以只会输出一个
{
if(j) printf(" ");
j=1;
printf("%d",f[i].pos);
len--;
}
}
printf("\n");
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%d",&f[i].s,&f[i].b);
f[i].pos=i;
}
sort(f+1,f+n+1,cmp);
len=1;
dp[1]=f[1].b;
mark[1]=1;
for(i=2;i<=n;i++)
{
j=seach(len,f[i].b);
dp[j]=f[i].b;
mark[i]=j;
if(j>len)
len=j;
}
print();
}
return 0;
}