NYOJ 214: monotonically increasing sequence (B) (binary optimization)


Description
given an integer number sequence {a1, a2 ..., an} (0 <n <= 100000), to find the longest sequence monotonically increasing, and its length is obtained.

Such as: a monotonically increasing longest sub-910511213 is the sequence 19101113, a length of 5.

Input
a plurality of sets of test data (<= 7)
of the first row of each test is an integer and n represents an integer of the sequence there are n, then the next row, there are n integers representing the number of all elements in the column. Each shaping the middle number of spaced by spaces (0 <n <= 100000) .
EOF data to the end.
Input data to ensure that legitimate (all int type integer)!
Output
to the length of the longest increasing sequence number of the test data output shaping each column, each output per line.
Sample input
. 7
. 1. 5. 11. 9 10 13 is 2
2
2 -1
sample output
. 5
. 1

#include <stdio.h>
#include <algorithm>
using namespace std;
#define N 100050
int a[N], dp[N];
int main()
{
    int n, i, mid, l, r, len;
    while(scanf("%d", &n)!=EOF)
    {
        for(i=1; i<=n; i++)
            scanf("%d", &a[i]);
        dp[1]=a[1];
        len=1;
        for(i=2; i<=n; i++)
        {
            l=1;r=len;
            while(l<=r)
            {
                mid=(l+r)/2;
                if(dp[mid]>=a[i])
                    r=mid-1;
                else
                    l=mid+1;
            }
            dp[l]=a[i];
            len=max(len, l);
        }
        printf("%d\n", len);
    }
    return 0;
}

 

Published 463 original articles · won praise 122 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_41505957/article/details/95010051