Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列
Bubble Sort Graph CodeForces - 340D
题意:
给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间连一条边。排序完后,求得到图的最大独立集。
解释:
最初想到的是图的最大独立集,认为不能解,于是就没辙了...
然而应当仔细分析题目(不容易想到):题意很容易知道是在每一对逆序对间连一条边。也就是说,对于a[i],向a[i]右侧比其小的和a[i]左侧比其大的都要连一条边。也就是说,只要选了结点i,那么a[i]右侧比其小的和a[i]左侧比其大的结点都不能选。也就是说,对于某一个选出的数a[i],左侧只可能选出比其小的,右侧只可能选出比其大的。也就是要求最长上升子序列。
ans[i]记录长度为k的LIS最末元素位置的最小值
2 1 5 3 6 4 8 9 7 8 9
1:
1
2:
2
3:
2,3(1,5)
4:
2,4(1,3)
5:
2,4,5(1,3,6)
6:
2,4,6(1,3,4)
7:
2,4,6,7(1,3,4,8)
8:
2,4,6,7,8(1,3,4,8,9)
9:
2,4,6,9,8(1,3,4,7,9)
10:
2,4,6,9,10(1,3,4,7,8)
11:
2,4,6,9,11(1,3,4,7,8,9)
#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[];
int s[];
int main()
{
int i;
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n;i++)
{
if(a[i]>s[s[]])
s[++s[]]=a[i];
else
*upper_bound(s+,s+s[]+,a[i])=a[i];
}
printf("%d",s[]);
return ;
}
补一份最长严格上升子序列(倒序输出)的:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,a[];
int s[],f[],len;
int main()
{
int i,j,t;
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
memset(s,0x3f,sizeof(s));
for(i=;i<=n;i++)
{
t=lower_bound(s+,s+len+,a[i])-s;
s[t]=a[i];
f[i]=t;
len=max(len,t);
}
printf("%d\n",len);
for(i=n,j=len;i>=;i--)
if(f[i]==j)
{
printf("%d ",a[i]);
j--;
}
return ;
}
不下降:将lower_bound改成upper_bound
upd 2018-3-30:
https://blog.csdn.net/u013665921/article/details/39856659
例如最长不下降子序列:
如果存在j<i且a[j]<=a[i]
则dp[i]=max{dp[j]}(j<i且a[j]<=a[i])+1
否则dp[i]=1
所以对值域开一个线段树就可以O(nlogn)了,输出方案也容易
Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列的更多相关文章
- 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 ...
- codeforces 340D Bubble Sort Graph(dp,LIS)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Bubble Sort Graph Iahub recently has lea ...
- Codeforces Round #323 (Div. 2) Once Again... CodeForces - 582B 最长非下降子序列【dp】(不明白)
B. Once Again... time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 【Codeforces 340D】Bubble Sort Graph
[链接] 我是链接,点我呀:) [题意] 让你根据冒泡排序的规则 建立一张图 问你这张图的最大独立子集的大小 [题解] 考虑a[i]会和哪些点连边? 必然是在a[i]左边且比它大的数字以及在a[i]右 ...
- cf340D Bubble Sort Graph
link:http://codeforces.com/problemset/problem/340/D 感觉很好的一道题目. 认真思考,发现,逆序的数字对一定有边相连.所以,题目要求没有边相连的最大的 ...
- [CF340D]Bubble Sort Graph/[JZOJ3485]独立集
题目大意: 给你一个序列,对序列中所有逆序对之间连一条边,问图中最大独立集为多大,有哪些点一定在最大独立集中. 思路: 在纸上画一下发现最大独立集一定是元序列的一个LIS,最大独立集必经点就是所有LI ...
- POJ3761 Bubble Sort (组合数学,构造)
题面 Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be ...
- Java中的经典算法之冒泡排序(Bubble Sort)
Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...
- Bubble Sort (5775)
Bubble Sort Problem Description P is a permutation of the integers from 1 to N(index starting from ...
随机推荐
- 在CenOS7.5里安装Redis
一.系统环境 操作系统:CentOS 7.5 Redis版本:redis3.2.8 登录账号:Frank 二.安装过程 A.预安装,安装gcc 1.进入终端,切换到root账号 2.输入指令: yum ...
- Mac下切换Python版本
Mac下有多个版本的Python时,需要进行版本切换.我使用的是anaconda,在终端下进行包安装时,默认Python版本是MacOS自带的Python,需要进行手动的版本切换. # 将anacon ...
- Luogu 3957 [NOIP2017]普及组 跳房子
写了好久,感觉自己好菜,唉…… 首先发现这个$g$的取值具有单调性,可以想到二分答案,然后考虑用$dp$来检验,这样子可以写出朴素的转移方程: 设$f_i$表示以$i$结尾的最大价值,那么有$f_i ...
- Luogu 4159 [SCOI2009]迷路
BZOJ 1297 应当是简单题. 发现边权的数量很小,所以我们暴力把一个点拆成$9$个点,然后把$(x, i)$到$(x, i + 1)$连边,代表转移一次之后可以走回来:对于每一条存在的边$(i, ...
- PHP加密与解密
password_hash ( string $password , integer $algo [, array $options ] ) 加密,生成60位得字符串 $algo:一个用来在散列密码时 ...
- inux内核的编译与安装 (转)
1.查看ubuntu版本号: xdj@xdj-MS-:~$ sudo lsb_release -a [sudo] password for xdj: No LSB modules are availa ...
- numpy.loadtxt() 出现codecError_____ Excel 做矩阵乘法
1) 用 numpy读入csv文件是报错 UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal m ...
- Azure:Manage anonymous read access to containers and blobs
Grant anonymous users permissions to containers and blobs By default, a container and any blobs with ...
- Regularization and model selection
Suppose we are trying select among several different models for a learning problem.For instance, we ...
- js $.inArray
var arr = [ "xml", "html", "css", "js" ]; $.inArray(" ...