首页 > 代码库 > ZOJ3519-Beautiful People:最长上升子序列的变形
ZOJ3519-Beautiful People:最长上升子序列的变形
Beautiful People
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
41 11 22 12 2
Sample Output
21 4
Source
1 #include <iostream> 2 #include <memory.h> 3 #include <algorithm> 4 #include <stdio.h> 5 using namespace std; 6 #define INF 1000000000 7 #define MAXN 100010 8 class CT 9 {10 public:11 int x,y,num;12 bool operator <(const CT &c2)const13 {14 if(x!=c2.x)15 return x<c2.x;16 return y>c2.y;17 }18 };19 20 CT a[MAXN];21 int d[MAXN];22 int cnt[MAXN]={0};23 int fa[MAXN];24 25 int main()26 {27 #ifndef ONLINE_JUDGE28 freopen("in.txt","r",stdin);29 #endif30 int n;31 while(~scanf("%d",&n))32 {33 for(int i=1;i<=n;i++)34 {35 scanf("%d %d",&a[i].x,&a[i].y);36 a[i].num=i;37 }38 sort(a+1,a+1+n);39 40 d[0]=0;41 fill_n(d+1,n+5,INF);42 memset(cnt,0,sizeof cnt);43 memset(fa,-1,sizeof fa);44 int ans=0;45 46 for(int i=1;i<=n;i++)47 {48 int low=lower_bound(d,d+i,a[i].y)-d-1;49 cnt[i]=low+1;50 ans=max(ans,cnt[i]);51 d[low+1]=a[i].y;52 }53 54 printf("%d\n",ans);55 int u;56 for(int i=n;i>=1;i--)57 if(cnt[i]==ans)58 {59 u=i;60 break;61 }62 63 printf("%d",a[u].num);64 int pre=u;65 for(int i=u-1;i>=1;i--)66 {67 if(a[i].y<a[pre].y && cnt[i]==cnt[pre]-1)68 {69 printf(" %d",a[i].num);70 pre=i;71 }72 }73 printf("\n");74 }75 return 0;76 }
ZOJ3519-Beautiful People:最长上升子序列的变形