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 ...
随机推荐
- 383. Ransom Note 在字典数组中查找笔记数组
[抄题]: Given an arbitrary ransom note string and another string containing letters from all the magaz ...
- 面试题:MySQL性能调优——索引详解与索引的优化 没用
——索引优化,可以说是数据库相关优化.理解尤其是查询优化中最常用的优化手段之一.所以,只有深入索引的实现原理.存储方式.不同索引间区别,才能设计或使用最优的索引,最大幅度的提升查询效率! 一.BTre ...
- Docker ubuntu镜像更换apt-get源
在Dockerfile中添加 RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list RUN ap ...
- 2.sql分类
SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法.但是 SQL 语言也包含用于更新.插 ...
- vuex 数据绑定
操作文档: 安装vuex: cnpm install vuex --save 文档介绍: https://vuex.vuejs.org/guide/modules.html import Vu ...
- hdu 4681 String(转载)
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream& ...
- 对C#泛型讲的很好的一篇文章
请参考 https://www.cnblogs.com/kissdodog/archive/2013/01/27/2879185.html
- 彻底清除Window7下的360注册表
当流氓360遇到强迫症,将注定有一场厮杀... 今天装了个虚拟机,其中win7系统是在网上随便下载了一个,是非纯净版的,自带了360在内的好多软件,其他软件都轻松删掉,但查看注册表时发现竟然有360残 ...
- UVa 10245 The Closest Pair Problem (分治)
题意:给定 n 个点,求最近两个点的距离. 析:直接求肯定要超时的,利用分治法,先把点分成两大类,答案要么在左边,要么在右边,要么一个点在左边一个点在右边,然后在左边或右边的好求,那么对于一个在左边一 ...
- DataTable中的select()用法
1.在DataTable中执行DataTable.Select("条件")返回DataTable // <summary> // 执行DataTable中的查询返回新的 ...