Codeforces Round #198 (Div. 2) D. Bubble Sort Graph poj2533Longest Ordered Subsequence

D. Bubble Sort Graph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode).

procedure bubbleSortGraph()
    build a graph G with n vertices and 0 edges
    repeat
        swapped = false
        for i = 1 to n - 1 inclusive do:
            if a[i] > a[i + 1] then
                add an undirected edge in G between a[i] and a[i + 1]
                swap( a[i], a[i + 1] )
                swapped = true
            end if
        end for
    until not swapped 
    /* repeat the algorithm as long as swapped value is true. */ 
end procedure

For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph.

Input

The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1a2, ..., an (1 ≤ ai ≤ n).

Output

Output a single integer — the answer to the problem.

Sample test(s)
input
3
3 1 2
output
2
Note

Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].

其实就是求最长公共子序列的长度,因为冒泡排序,一定是将,i<j a[i]>a[j]的相连的了,那么,答案不就是求,i<j a[i]<a[j]的最大长度么,这样就成功的转化成了求最长公共子序列,这样,问题,就很清楚了!

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N=100050;
int a[N],f[N],d[N];
int bsearch(const int *f,int size,const int &a){
    int l=0,r=size-1;
    while(l<=r){
        int mid=(l+r)/2;
        if(a>f[mid-1]&&a<=f[mid])return mid;
        else if(a<f[mid])r=mid-1;
        else l=mid+1;
    }
}
int LIS(const int *a,const int &n){
    int i,j,size=1;
    f[0]=a[0];d[0]=1;
    for(i=1;i<n;i++){
        if(a[i]<=f[0])j=0;
        else if(a[i]>f[size-1])j=size++;
        else j=bsearch(f,size,a[i]);
        f[j]=a[i];d[i]=j+1;
    }
    return size;
}
int main()
{
    int i,n;
    while(scanf("%d",&n)!=EOF){
        for(i=0;i<n;i++)scanf("%d",&a[i]);
        printf("%d\n",LIS(a,n));
    }
    return 0;
}
Longest Ordered Subsequence
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 28869   Accepted: 12551

Description

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence ( ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4

Source

Northeastern Europe 2002, Far-Eastern Subregion
这两题 是一样的,用一个代码!

你可能感兴趣的:(Codeforces Round #198 (Div. 2) D. Bubble Sort Graph poj2533Longest Ordered Subsequence)