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 with n elements a1a2, ..., 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 a1a2, ..., an (1 ≤ ain).

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].

思路:

问题等价于找一个最长非降子序列。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100005
using namespace std; int n,m,ans,cnt;
int a[maxn],dp[maxn]; void solve()
{
int i,j,pos;
dp[0]=0;
cnt=0;
for(i=1; i<=n; i++)
{
if(a[i]>=dp[cnt]) dp[++cnt]=a[i];
else
{
pos=upper_bound(dp,dp+cnt+1,a[i])-dp; // 找到>a[i]的第一次出现的位置
printf("i:%d pos:%d\n",i,pos);
dp[pos]=a[i];low
}
}
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{
for(i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
solve();
printf("%d\n",cnt); // 长度即为cnt 但序列不是dp保存的序列 要输出序列的话应在更新ant时记录序列
}
return 0;
}
/*
7
2 3 3 5 3 2 4
*/

Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)的更多相关文章

  1. Codeforces Round #581 (Div. 2)D(思维,构造,最长非递减01串)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[100007];int main ...

  2. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  3. 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph

    题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...

  4. Codeforces Round #198 (Div. 2)

    A.The Wall 题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数. 分析:求出x和y的最小公倍数,然后做一 ...

  5. Codeforces Round #198 (Div. 2)C,D题解

    接着是C,D的题解 C. Tourist Problem Iahub is a big fan of tourists. He wants to become a tourist himself, s ...

  6. Codeforces Round #198 (Div. 1 + Div. 2)

    A. The Wall 求下gcd即可. B. Maximal Area Quadrilateral 枚举对角线,根据叉积判断顺.逆时针方向构成的最大面积. 由于点坐标绝对值不超过1000,用int比 ...

  7. Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理

    题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ...

  8. Codeforces Round #198 (Div. 2)E题解

    E. Iahub and Permutations Iahub is so happy about inventing bubble sort graphs that he's staying all ...

  9. Codeforces Round #486 (Div. 3)-B. Substrings Sort

    B. Substrings Sort time limit per test 1 second memory limit per test 256 megabytes input standard i ...

随机推荐

  1. armv7a-mediatek451_001_vfp-linux-gnueabi-gcc: directory: No such file or directory 编译error

    release/vm_linux/output/hisense_android/mt5399_cn_android_JB/rel/obj/oss/source/arm_mali_ko/mali400- ...

  2. 15个Docker基本命令及用法

    Docker入门教程:15个Docker基本命令及用法   本文中,我们将学习15个Docker命令以及命令的用法和功能,并通过实践学习它是如何工作的. AD:51CTO 网+ 第十二期沙龙:大话数据 ...

  3. 双积分式(A/D)转换器电路结构及工作原理

    1.转换方式 V-T型间接转换ADC. 2.  电路结构 图1是这种转换器的原理电路,它由积分器(由集成运放A组成).过零比较器(C).时钟脉冲控制门(G)和计数器(ff0-ffn)等几部分组成 图1 ...

  4. OC中格式化输出符号

    定义 说明 %@ Objective-C object, printed as the string returned by descriptionWithLocale: if available, ...

  5. 简谈Comparable和Comparator区别

    对于Comparable和Comparator这连个相似的接口,还是做一下比较比较好: Comparable Comparator (1)只包含一个compareTo()方法,此方法可以给两个对象排序 ...

  6. C++返回引用的函数

    要以引用返回函数值,则函数定义时的格式如下: 类型标识符&类型名 (形参列表及类型说明) { 函数体 } 用const限定引用的声明方式为: const 类型标识符&引用名=目标变量名 ...

  7. JavaScript自学代码--(三)

    //通过 id 查找 HTML 元素 var x = document.getElementById("demo"); //通过标签名查找 HTML 元素 //本例查找 id=&q ...

  8. OneAlert 入门(四)——事件分派和通知必达

    OneAlert 是国内首个 SaaS 模式的云告警平台,集成国内外主流监控/支撑系统,实现一个平台上集中处理所有 IT 事件,提升 IT 可靠性.有了 OneAlert,你可以更快更合理地为事件划分 ...

  9. POJ 2750 Potted Flower(线段树的区间合并)

    点我看题目链接 题意 : 很多花盆组成的圆圈,每个花盆都有一个值,给你两个数a,b代表a位置原来的数换成b,然后让你从圈里找出连续的各花盆之和,要求最大的. 思路 :这个题比较那啥,差不多可以用DP的 ...

  10. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: SELECT command denied to user’

    Linux环境 Mysql+Hibernate command denied to user 错误 错误信息 如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSynt ...