博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 340D Bubble Sort Graph(dp,LIS)
阅读量:4584 次
发布时间:2019-06-09

本文共 2903 字,大约阅读时间需要 9 分钟。

转载请注明出处:           ——by fraud

 

 Bubble Sort Graph

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.

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(nlogn)搞一下就好

1 //##################### 2 //Author:fraud 3 //Blog: http://www.cnblogs.com/fraud/ 4 //##################### 5 #include 
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 #include
22 #include
23 #include
24 #include
25 using namespace std;26 #define XINF INT_MAX27 #define INF 0x3FFFFFFF28 #define MP(X,Y) make_pair(X,Y)29 #define PB(X) push_back(X)30 #define REP(X,N) for(int X=0;X
=L;X--)33 #define CLR(A,X) memset(A,X,sizeof(A))34 #define IT iterator35 typedef long long ll;36 typedef pair
PII;37 typedef vector
VII;38 typedef vector
VI;39 #define MAXN 10001040 int a[MAXN];41 int dp[MAXN];42 int main()43 {44 ios::sync_with_stdio(false);45 int n;46 cin>>n;47 for(int i=0;i
>a[i];49 fill(dp,dp+MAXN,INF);50 for(int i=0;i
代码君

 

转载于:https://www.cnblogs.com/fraud/p/4376961.html

你可能感兴趣的文章
webstorm快捷键大全
查看>>
SQL Server 语法大全
查看>>
MySQL存储过程
查看>>
HttpContext是干什么的
查看>>
线程安全
查看>>
原形模式
查看>>
iOS开发笔记5:多线程之NSThread、NSOperation及GCD
查看>>
php中curl的详细解说【转】
查看>>
Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
查看>>
hdu 6069 Counting Divisors 筛法
查看>>
codeforces gym 100971 K Palindromization 思路
查看>>
各个控件说明
查看>>
鼠标事件(jQuery)
查看>>
delete指针时coredump的分析之旅
查看>>
openoffice+pdf2swf+FlexPaper在线显示office和pdf
查看>>
24-React Components组件
查看>>
[BZOJ 1188] [HNOI2007] 分裂游戏 【博弈论|SG函数】
查看>>
[BZOJ - 2631] tree 【LCT】
查看>>
ASP.NET Core管道深度剖析(2):创建一个“迷你版”的管道来模拟真实管道请求处理流程...
查看>>
JS实现数组排序:升序和降序
查看>>