一路二路最长单调递增子序列 hdu3998 + ACdream 1216

来源:互联网 发布:淘宝开店保证金交成功 编辑:程序博客网 时间:2024/06/08 19:33

Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2120    Accepted Submission(s): 771


Problem Description
There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequence of X 
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).
 

Input
The input file have many cases. Each case will give a integer number n.The next line will 
have n numbers.
 

Output
The output have two line. The first line is s and second line is num.
 

Sample Input
43 6 2 5
 

Sample Output
22
题意: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;}

G - Beautiful People
Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu
Submit Status Practice ACdream 1216

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 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

41 11 22 12 2

Sample Output

21 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;}



0 0
原创粉丝点击