Bubble Sort Graph

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 a1, a2, ..., an in ascending order. He is bored of thisso simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n verticesand 0 edges. During Bubble Sort execution, edges appear as described in thefollowing algorithm (pseudocode).


procedure bubbleSortGraph()
    build a graph G with n vertices and 0edges
    repeat
        swapped = false
        for i = 1 to n - 1 inclusive do:
            if a[i] > a[i + 1] then
                add an undirected edge inG 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 asswapped value is true. */
end procedure

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

Input

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

Output

Output a single integer — the answer tothe problem.

Sample test(s)

input

3
3 1 2

output

2

Note

Consider the first example. Bubble sortswaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Thenbubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is nowsorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Itsmaximal independent set is [1, 2].

 

題解

這是一道尋找最長非遞減子序列的題,如果按照最原始的dp做法是會超時的,所以不超時的可以用二分查找加dp。

我們必須建立以個數組c[k],k表示長度爲k的子序列,它的最後一個元素的最小值(如果一開始的c[k],中的值大於後來的值v,而v大於c[k-1],則修改c[k] =  v),通過二分法+dp,時間複雜度爲O(lgn),比原來的的O(n)快。

 

源代碼:

#include <iostream>

#include <stdio.h>

#include <algorithm>

#include <cstring>

 

using namespace std;

 

int a[100500];

int c[100500],n;

 

int binary_search(int c[],int len,int v)

{

int

 left=1,right=len,mid=left+(right-left)/2;

 

while(left<=right)

{

       if(v>= c[mid])  

              left=mid+1;

       else

              right=mid-1;

       mid=left+(right-left)/2;

}

 

return left;

}

int LIS()

int i,j,len=1;     //j指的是i之前的最長遞增子序列len指的是當前的最長遞增子序列

c[0]=-1,c[1]=a[0];

 for(i=1;i<n;i++)

 {

       j=binary_search(c,len,a[i]);

       c[j]=a[i];          //c[j]=a[i]指的是長度爲j的子序列中的最大的元素的值

       if(j>len)

              len=j;

 }

return len;

}

 

int main()

{

       while(cin>> n)

       {

              for(inti = 0;i < n;i++)

                     scanf("%d",&a[i]);

 

              intmx = LIS();            

              cout<< mx << endl;

       }

 

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章