codeforces 340D Bubble Sort Graph(最長非遞減子序列)

果7發表於2013-10-09
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].



題目大意:注意看定義,就是不斷的冒泡,然後排好序之後,停止。我們不斷在加邊,找一個最長的序列,使得他們之間沒有邊聯絡即可。最後可以找到規律,就是求最長非遞減子序列

      解題思路:先看下樸素的o(n*n)會超時,o(n*log(n))可以過,需要dp加上二分。具體見程式碼.

      題目地址:D. Bubble Sort Graph

o(n*n)樸素方法:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
int a[100005];
int dp[100005];

int main()
{
    int n,i,j,res;
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            dp[i]=1;
        }
        //dp[i]=max(max(dp[j])+1,dp[i]);  a[i]>=a[j]
        res=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<i;j++)
            {
                if(a[i]>=a[j]&&dp[j]+1>dp[i])
                    dp[i]=dp[j]+1;
            }
            if(dp[i]>res)
                res=dp[i];
        }

        printf("%d\n",res);
    }
    return 0;
}


AC程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
int a[100005];
int b[100005];
int n;

int findd(int len,int p)   //二分查詢<=p的位置
{
    int l,r,mid;
    l=1,r=len,mid=(l+r)>>1;
    while(l<=r)
    {
        if(p>b[mid]) l=mid+1;
        else if(p<b[mid]) r=mid-1;
        else return mid;
        mid=(l+r)>>1;
    }
    return l;
}

int LIS()
{
    int i,j,len=1;
    b[1]=a[0];
    for(i=1;i<n;i++)
    {
        j=findd(len,a[i]);
        b[j]=a[i];  //b[j]是指長度為j最大元素的值
        if(j>=len) len=j;
    }
    return len;
}

int main()
{
    int i;
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        printf("%d\n",LIS());
    }
    return 0;
}



相關文章