Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2120 Accepted Submission(s): 771
as x[i1], x[i2],...,x[ik], which satisfies follow conditions:
1) x[i1] < x[i2],...,<x[ik];
2) 1<=i1 < i2,...,<ik<=n
As an excellent program designer, you must know how to find the maximum length of the
increasing sequense, which is defined as s. Now, the next question is how many increasing
subsequence with s-length can you find out from the sequence X.
For example, in one case, if s = 3, and you can find out 2 such subsequence A and B from X.
1) A = a1, a2, a3. B = b1, b2, b3.
2) Each ai or bj(i,j = 1,2,3) can only be chose once at most.
Now, the question is:
1) Find the maximum length of increasing subsequence of X(i.e. s).
2) Find the number of increasing subsequence with s-length under conditions described (i.e. num).
have n numbers.
4 3 6 2 5
2 2题意:1,求最长上升子序列长度 , 2 求最长上升子序列个数二分查找法求取;#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int maxn=100005,inf=0x3f3f3f3f; int a[maxn],dp[maxn]; int mark[maxn]; int find_(int l,int r,int x) { int mid; while(l<=r) { mid=(l+r)/2; if(dp[mid]==x) return mid; else if(dp[mid]<=x) l=mid+1; else r=mid-1; } return l; } int main() { int n,j; while(~scanf("%d",&n)) { for(int i=0; i<n; i++) scanf("%d",&a[i]); int len; int maxlen=0; int ans1=0; memset(mark,0,sizeof(mark)); while(1) { len=0; memset(dp,0,sizeof(dp)); dp[0]=-inf; for(int i=0; i<n; i++) { if(mark[i]) continue; j=find_(0,len,a[i]); dp[j]=a[i]; if(j>len) { mark[i]=1; len++; } } if(maxlen<len) maxlen=len,ans1++; else if(maxlen==len)ans1++; else break; } printf("%d\n%d\n",maxlen,ans1); } return 0; }
题意:求二路最长上升子序列,并且输出子序列的序号: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
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 — S i and B i 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
4 1 1 1 2 2 1 2 2Sample Output
2 1 4
#include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> using namespace std; const int inf = 0x3f3f3f3f; const int N = 110000; struct Node { int x,y; int num,count; }; Node a[N]; int cmp(Node a,Node b) { if(a.x!=b.x) return a.x<b.x; if(a.y!=b.y) return a.y>b.y; } int dp[N],mark[N]; int Bin_Search(int l,int r,int x) { while(l<=r) { int mid = (l+r)/2; //假如要求相等的情况下,返回较小的值。 if(dp[mid]==x) return mid; else if(dp[mid]<=x) l=mid+1; else r=mid-1; } return l; } int main() { int n; while(~scanf("%d",&n)) { memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { scanf("%d%d",&a[i].x,&a[i].y); a[i].num=i; } sort(a+1,a+n+1,cmp); memset(dp,inf,sizeof(dp)); int ans=0; int len = 1; for(int i=1;i<=n;i++) { int tmp=Bin_Search(1,len,a[i].y); //lower_bound(dp+1,dp+1+n,a[i].y)-dp; if(tmp==len) len++; dp[tmp] = a[i].y; mark[i] = tmp; ans = max(ans,tmp); } printf("%d\n",ans); for(int i=n;i>=1;i--) { if(mark[i]==ans) { printf("%d",a[i].num); if(ans!=1) printf(" "); ans--; } } printf("\n"); } return 0; }