Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)
1 second
256 megabytes
standard input
standard output
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.
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 a single integer — the answer to the problem.
3
3 1 2
2
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 (转化为最长非降子序列)的更多相关文章
- Codeforces Round #581 (Div. 2)D(思维,构造,最长非递减01串)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;char s[100007];int main ...
- Codeforces Round #198 (Div. 2)A,B题解
Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...
- 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph
题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...
- Codeforces Round #198 (Div. 2)
A.The Wall 题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数. 分析:求出x和y的最小公倍数,然后做一 ...
- 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 ...
- Codeforces Round #198 (Div. 1 + Div. 2)
A. The Wall 求下gcd即可. B. Maximal Area Quadrilateral 枚举对角线,根据叉积判断顺.逆时针方向构成的最大面积. 由于点坐标绝对值不超过1000,用int比 ...
- 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 ...
- Codeforces Round #198 (Div. 2)E题解
E. Iahub and Permutations Iahub is so happy about inventing bubble sort graphs that he's staying all ...
- 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 ...
随机推荐
- CSS禅意
标题取自<css禅意花园>一书,还记得当年读此书时的情景,真的是内容和书名一样的优秀,就以此标题作为自己在该文的一种追求吧,尽管我的水平和见解都和Dave Shea相去甚远.该文算是对前两 ...
- python类的继承
继承一个类 如果已经定义了Person类,需要定义新的Student和Teacher类时,可以直接从Person类继承: class Person(object): def __init__(self ...
- HTML5 知识点
HTML5 知识点 (1)语义化标记 <header>,<footer>,<nav>,<article>,<section> ...
- Ubuntu配置apache
http://blog.csdn.net/ljchlx/article/details/21978431 http://www.2cto.com/os/201110/107283.html
- 今天,安装了一个GANGLIA玩玩,以后再测试NAGIOS吧。
说不定以后派得上用场呢.. 还有,NGINX也要学,不能老是凭站IIS,APACHE混饭吃吧,现在它都这么流行了..新浪,网易,腾讯.
- Hibernate 注解时 hibernate.hbm.xml的配置方法 以及与SSH整合里的配置方式
①纯Hibernate开发: 当你在Bean中写入注解后,需要告诉hibernate哪些类使用了注解. 方法是在hibernate.hbm.xml文件中配置 <!DOCTYPE hibernat ...
- Chapter 32:动态规划一 总结
今天做了四道dp题,分数306. 就从我的做题顺序开始讲.. 首先是第四题,明显的记录模数DP,很快打出来了. 后来做完之后回来拍第四题,也没有什么问题,就AC了. 然后是第三题,我的性子真是太急了, ...
- Automatic Code Generation-->Implement Interface
https://msdn.microsoft.com/en-us/library/hk90416s(v=vs.110).aspx VS中自带的只能提示,一个类继承自某一个接口. 由VS为类生成接口所要 ...
- android学习——ADT的离线安装
前一篇讲解了ADT的在线安装,不过有的时候在线在线安装的速度很慢.所以今天学习一下ADT的离线安装: 首先 下载与SDK相对应的ADT(如果SDK是最新版的就下最新版ADT否则就把SDK更新到最新版以 ...
- js数组如何去掉逗号
技术水平比较差,有错误的地方或者大神们有好的方法在介绍下,可以指点出来我加以改正! 1.join去掉逗号',' var a = ['1', '2', '3', '4', '5']; var c = a ...