题目链接:http://115.28.76.232/problem?pid=1216
Problem Description
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 2003 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.
Input
Output
Sample Input
4 1 1 1 2 2 1 2 2
Sample Output
2 1 4
有n个人每人有一个力气值Si,美丽值Bi,满足Bi>Bj&&Si>Sj 或者 Bi<Bj&&Si<Sj 的人可以一起参见晚会,问最多有多少人可以一起参见晚会。
类似于HDU1025 Constructing Roads In JGShining's Kingdom
我们根据S从小到大将所有人排序,然后看B最长的上升子序列的长度求出来即可,中间有些变形 ,需要记录路径,详情看注释
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdlib>
using namespace std;
const int maxn = 100010;
struct people {
int s,b,id;
};
people p[maxn];
int q[maxn],path[maxn],tot;
//q[]来保存最长的递增序列中的元素在排序后的序列中的位置;
//path[]用来保存排好序的元素在最长的递增序列中的位置。
bool cmp (const struct people &A,const struct people &B){
if(A.s==B.s)
return A.b > B.b;
return A.s < B.s;
}
int bin_search(int val,int l,int r)//二分过程中根据题目中的要求不能取等
{
while(l<=r){
int mid=(l+r)>>1;
if(p[q[mid]].b<val) l=mid+1;
else r=mid-1;
}
return l;
}
int main() {
int n,len;
while(~scanf("%d",&n)){
for(int i = 0; i < n; i++){
scanf("%d %d",&p[i].s,&p[i].b);
p[i].id = i+1;
}
sort(p,p+n,cmp);
len=0;
q[len++] = 0;
path[0] = 0;
for(int i = 1; i < n; i++){
if(p[i].b > p[q[len-1]].b){//如果大于当前的递增数列的最后一项了,直接方到最后
path[i] = q[len-1];//记录它前面的元素的位置
q[len++] = i;
}else{
int index = bin_search(p[i].b,0,len-1);
path[i] = index ? q[index-1] : 0;
q[index] = i;
}
}
printf("%d\n",len);
printf("%d",p[q[len-1]].id);
int tmp = q[len-1];
while(--len){
tmp = path[tmp];
printf(" %d",p[tmp].id);
}
puts("");
}
return 0;
}