【CodeForces - 340D】Bubble Sort Graph (思维,nlogn最长上升子序列类问题)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/84744212

题干:

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 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 a1, a2, ..., an (1 ≤ ai ≤ n).

Output

Output a single integer — the answer to the problem.

Examples

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]就要交换。问你最大不相连的点有多少个。

解题报告:

    类似于逆序对的概念,逆序对之间一定有边 所以这题就转化成类似于求非逆序对个数了,不一样的地方在于这题不是求个数,而是求最多的这样的点数,很明显转化成lis问题、、

   话说训练的时候没想到这么多人会nlogn的lis。。。2333

   虽然这是我做到过的第五个吧,,需要用到nlogn的、、这里总结一下分别是:

    ZOJ 2319,51nod1134,HDU1025,codeforce270D

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int a[MAX],b[MAX];
int n;
int DP() {
	int len = 0;
	b[++len] = a[1];
	for(int i = 2; i<=n; i++) {
		if(a[i] >= b[len]) b[++len] = a[i];
		else {
			int pos = lower_bound(b+1,b+len+1,a[i]) - b;
			b[pos] = a[i];
		}
	}
//	for(int i = 1; i<=len; i++) printf("%d ",b[i]);
	return len;
}
int main()
{
	cin>>n;
	for(int i = 1; i<=n; i++) {
		scanf("%d",a+i);
	}
	printf("%d\n",DP());

	return 0 ;
 }

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/84744212