Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud
Bubble Sort Graph
Iahub recently have learned Bubble sort, an algorithm that's used to Sort a permutation with n elements a 1, a 2, ..., a n in ascending order. He is bored of the algorithm, so and 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 does:
if a[i] > a[i + 1] Then
Add an und Irected 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 I S true. */
End procedure
For a graph, the independent set is a set of vertices in a graph, no the which is adjacent (so there be no edges betwe En vertices of an independent set). A Maximum independent set is an independent set which have 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 a1, a2, ..., a n (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].
Considering the problem that can be converted into the longest ascending subsequence ... And then I'll just have to do it for O (Nlogn).
1 //#####################2 //Author:fraud3 //Blog:http://www.cnblogs.com/fraud/4 //#####################5#include <iostream>6#include <sstream>7#include <ios>8#include <iomanip>9#include <functional>Ten#include <algorithm> One#include <vector> A#include <string> -#include <list> -#include <queue> the#include <deque> -#include <stack> -#include <Set> -#include <map> +#include <cstdio> -#include <cstdlib> +#include <cmath> A#include <cstring> at#include <climits> -#include <cctype> - using namespacestd; - #defineXinf Int_max - #defineINF 0X3FFFFFFF - #defineMP (x, y) make_pair (x, y) in #definePB (x) push_back (x) - #defineREP (x,n) for (int x=0; x<n; X + +) to #defineREP2 (X,L,R) for (int x=l; x<=r; X + +) + #defineDEP (x,r,l) for (int x=r; x>=l; x--) - #defineCLR (a,x) memset (a,x,sizeof (A)) the #defineIT iterator *typedefLong Longll; $typedef pair<int,int>PII;Panax Notoginsengtypedef vector<pii>VII; -typedef vector<int>VI; the #defineMAXN 100010 + intA[MAXN]; A intDP[MAXN]; the intMain () + { -Ios::sync_with_stdio (false); $ intN; $Cin>>N; - for(intI=0; i<n;i++) -Cin>>A[i]; theFill (dp,dp+maxn,inf); - for(intI=0; i<n;i++)Wuyi*lower_bound (dp,dp+n,a[i]) =A[i]; theCout<<lower_bound (Dp,dp+n,inf)-dp<<Endl; - Wu - return 0; About}
code June
Codeforces 340D Bubble Sort Graph (Dp,lis)