[CF340D]Bubble Sort Graph/[JZOJ3485]独立集
题目大意:
给你一个序列,对序列中所有逆序对之间连一条边,问图中最大独立集为多大,有哪些点一定在最大独立集中。
思路:
在纸上画一下发现最大独立集一定是元序列的一个LIS,最大独立集必经点就是所有LIS的公共部分。
考虑把所有的LIS记录下来,然后构建一个DAG,DAG的割点即为LIS的公共部分。
由于我们需要先知道所有的LIS的具体方案,只能写O(n^2)的DP记录转移,能拿60分。
不过事实上我们也不需要知道具体的状态。
我们可以记录一下所有LIS中的每个点在LIS上的哪个位置,看一下这个位置是否只有它一个点的。
如果不,那么肯定可以被别的点替代。
如果是,那么它肯定是LIS的公共部分。
LIS可以O(n log n)求,最后判断是O(n)的,总的时间复杂度是O(n log n)。
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=;
int w[N],id[N],f[N],g[N],h[N];
int n;
class FenwickTree {
private:
int val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
void reset() {
memset(val,,sizeof val);
}
void modify(int p,const int &x) {
while(p<=n) {
val[p]=std::max(val[p],x);
p+=lowbit(p);
}
}
int query(int p) const {
int ret=;
while(p) {
ret=std::max(ret,val[p]);
p-=lowbit(p);
}
return ret;
}
};
FenwickTree t;
int main() {
n=getint();
for(register int i=;i<=n;i++) {
const int v=getint();
w[i]=v;
id[v]=i;
}
for(register int i=;i<=n;i++) {
f[w[i]]=t.query(w[i])+;
t.modify(w[i],f[w[i]]);
}
t.reset();
for(register int i=n;i;i--) {
g[w[i]]=t.query(n-w[i]+)+;
t.modify(n-w[i]+,g[w[i]]);
}
int ans=;
for(register int i=;i<=n;i++) {
ans=std::max(ans,f[w[i]]+g[w[i]]-);
}
printf("%d\n",ans);
for(register int i=;i<=n;i++) {
if(f[w[i]]+g[w[i]]-==ans) {
if(!~h[f[w[i]]]) continue;
if(!h[f[w[i]]]) {
h[f[w[i]]]=;
} else {
h[f[w[i]]]=-;
}
}
}
for(register int i=;i<=n;i++) {
if(f[w[i]]+g[w[i]]-==ans&&h[f[w[i]]]==) {
printf("%d ",i);
}
}
return ;
}
[CF340D]Bubble Sort Graph/[JZOJ3485]独立集的更多相关文章
- cf340D Bubble Sort Graph
link:http://codeforces.com/problemset/problem/340/D 感觉很好的一道题目. 认真思考,发现,逆序的数字对一定有边相连.所以,题目要求没有边相连的最大的 ...
- 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 ...
- Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列
Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间 ...
- 【Codeforces 340D】Bubble Sort Graph
[链接] 我是链接,点我呀:) [题意] 让你根据冒泡排序的规则 建立一张图 问你这张图的最大独立子集的大小 [题解] 考虑a[i]会和哪些点连边? 必然是在a[i]左边且比它大的数字以及在a[i]右 ...
- 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 ...
- Bubble Sort [ASM-MIPS]
# Program: Bubble sort # Language: MIPS Assembly (32-bit) # Arguments: 5 unordered numbers stored in ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
随机推荐
- 无缝衔接demo
如题. <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" con ...
- jq 浏览器窗口大小发生变化时
当调整浏览器窗口的大小时,发生 resize 事件: $(selector).resize(); 实例 对浏览器窗口调整大小进行计数: $(window).resize(function() { $( ...
- Linux中查看进程占用内存的情况【转】
转自:http://hutaow.com/blog/2014/08/28/display-process-memory-in-linux/ Linux中查看某个进程占用内存的情况,执行如下命令即可,将 ...
- 2012年Elsevier旗下Computer Science期刊最新SCI影响因子排名
Latest Impact Factor figures from Elsevier's Computer Science Journals Medical Image Analysis Impact ...
- 如何更新远程主机上的 Linux 内核
如何更新远程主机上的 Linux 内核 http://blog.csdn.net/robertsong2004/article/details/47277121 转载至:http://www.tiny ...
- PBFT算法的相关问题
PBFT(99.02年发了两篇论文)-从开始的口头算法(指数级)到多项式级 要求 n>3f why: 个人简单理解:注意主节点是可以拜占庭的,从节点对于(n,v,m)的投票最开始也是基于主节点给 ...
- mapper.xml中的<sql>标签
原文链接:http://blog.csdn.net/a281246240/article/details/53445547 sql片段标签<sql>:通过该标签可定义能复用的sql语句片段 ...
- tornado 模版
tornado 模版语法 取消转义 : 取消项目转义 :autoescape = None 取消模版转义:{% autoescape None %} 取消行转义 :{% raw bd %} 强制转 ...
- Linux 基础——开山篇
为什么要开始学习Linux命令? 首先当然是因为工作需要了,现在的工作是负责银行调度的系统的源系统接入的工作,经常要到生产部署版本.所以……买了一本<Linux命令行与shell脚本编程大全&g ...
- PHP生成随机字符串与唯一字符串
代码如下: <?php /* * 生成随机字符串 * @param int $length 生成随机字符串的长度 * @param string $char 组成随机字符串的字符串 * @ret ...