CodeForces 340D

来源:互联网 发布:魔兽世界源码 编辑:程序博客网 时间:2024/06/06 10:54
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 withn elementsa1,a2, ...,an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call itG) initially hasn 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 graphG, if we use such permutation as the premutationa in procedure bubbleSortGraph.

Input

The first line of the input contains an integern (2 ≤ n ≤ 105). The next line containsn distinct integersa1, a2, ...,an(1 ≤ ai ≤ n).

Output

Output a single integer — the answer to the problem.

Examples
Input
33 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].



看懂题意很重要:  所以好好看题。

题意是:找一个最长 的序列,使他们之间没有直接相连的边

找规律发现就是   求LIS最长非递减子序列。

然后o(n*n)的做法会超时,所以要dp+二分解决


代码:

#include#include#include#include#define maxn 100100using namespace std;int dp[maxn],A[maxn];int n,cnt;int LS(){    for(int i=1;i<=n;i++)    {        if(A[i]>=dp[cnt])            dp[++cnt]=A[i];        else        {            int tt=upper_bound(dp,dp+cnt+1,A[i])-dp;   //二分操作,即返回第一个大于A[i]的下标            dp[tt]=A[i];        }    }}int main(){    scanf("%d",&n);    dp[0]=0;    for(int i=1;i<=n;i++)    {        scanf("%d",&A[i]);        dp[i]=0;    }    cnt=0;    LS();    printf("%d\n",cnt);    return 0;}


原创粉丝点击