转载请注明出处: http://www.cnblogs.com/fraud/          ——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)搞一下就好

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 100010
int a[MAXN];
int dp[MAXN];
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=;i<n;i++)
cin>>a[i];
fill(dp,dp+MAXN,INF);
for(int i=;i<n;i++)
*lower_bound(dp,dp+n,a[i])=a[i];
cout<<lower_bound(dp,dp+n,INF)-dp<<endl; return ;
}

代码君

codeforces 340D Bubble Sort Graph(dp,LIS)的更多相关文章

  1. Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)

    D. Bubble Sort Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Codeforces 840C - On the Bench(dp/容斥原理)

    Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2500 的 D1C,可个人认为难度堪比某些 *2700 *2800. 不过嘛,*2500 终究还是 *2500,还是被我自己 ...

  3. hdu----(1257)最少拦截系统(dp/LIS)

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. HDU 1087 Super Jumping! Jumping! Jumping! (DP+LIS)

    题意:给定一个长度为n的序列,让你求一个和最大递增序列. 析:一看,是不是很像LIS啊,这基本就是一样的,只不过改一下而已,d(i)表示前i个数中,最大的和并且是递增的, 如果 d(j) + a[i] ...

  5. codeforces 985E Pencils and Boxes(dp+思维)

    E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. HDU 5078 Revenge of LIS II(dp LIS)

    Problem Description In computer science, the longest increasing subsequence problem is to find a sub ...

  7. POJ 2533 Longest Ordered Subsequence(dp LIS)

    Language: Default Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  8. Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列

    Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间 ...

  9. 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)

    洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...

随机推荐

  1. 三组I/O复用模型的比较

    概论: select.poll和epoll三组I/O复用系统调用,这3组系统调用都能同时监听多个文件描述符.它们将等待由timeout参数指定的超时时间,直到一个或者多个文件描述符上有事件发生时返回. ...

  2. ashx中session的使用

    在平常的页面上是用是很容易就的到request,response对像,从而对其进行一些操作,但在ashx(一般处理程序)中却是有一点的不同, 在ashx你无法正常的使用session,即 contex ...

  3. POJ2446 二分图最大匹配

    问题:POJ2446 分析: 采用黑白相间的方法把棋盘分成两个点集,是否可以用1*2的卡片实现全覆盖等价于二分图是否有完全匹配. AC代码 //Memory: 172K Time: 32MS #inc ...

  4. NSThread 、NSRunLoop 和 Dispatch Queue

     iOS多线程编程中,NSOperation和NSOperationQueue无疑是最常用的,它们能满足绝大部分情况下的线程操作.但在完成一些特殊的任务时,我们还是要使用的NSThread和NSRun ...

  5. Block(二)内存管理与其他特性

    一.block放在哪里 我们针对不同情况来讨论block的存放位置: 1.栈和堆 以下情况中的block位于堆中: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  6. win7 x64 jdk1.7.0_51

    1:我的 jdk与jre默认安装在:D:\Program Files\Java 2:配置环境变量(系统变量): (1)新建JAVA_HOME (2)新建CLASSPATH (3)编辑Path,%JAV ...

  7. 关于Android界面编程与视图(View)组件

    UI组件--------------->android.widget.* View组件------------->android.view.* 视图(View)组件 所有UI组件都是建立在 ...

  8. QProcess进程间双向通信

    记得以前写过Linux的C程序, 里面用popen打开一个子进程, 这样可以用read/write和子进程通讯, 而在子进程里则是通过从stdin读和向stdout写实现对父进程的通讯. QProce ...

  9. 无人参与安装IIS 6.0

    使用脚本安装 IIS 从“开始”菜单,单击“运行”. 在“打开”框中,键入 cmd,然后单击“确定”. 在命令提示符下,键入 Sysocmgr.exe /i:sysoc.inf /u:%path_to ...

  10. CPU的物理限制

    Quote from Solidot 密歇根大学的Igor Markov在上周的<自然>期刊上发表了一篇文章,谈论了CPU面临的基本物理限制.有学者曾估计预言CPU晶体管数目和性能增长的摩 ...