[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 ...
随机推荐
- php伪协议
文件包含函数:include.require.include_once.require_once.highlight_file .show_source .readfile .file_get_con ...
- 【Windows使用笔记】Windows科研软件
1 Anaconda Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.主要是内置有jupyter notebook和jupyter ...
- libSVM笔记之(一)在matlab环境下安装配置libSVM
本文为原创作品,转载请注明出处 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing 台湾林智仁教 ...
- [Leetcode Week12]Unique Paths
Unique Paths 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths/description/ Description A ...
- 写个dump_stack【转】
转自:http://blog.chinaunix.net/uid-27714502-id-3434761.html 简单实现dump_stack 0.首先确保你能写个内核模块:打印"hell ...
- CF625D Finals in arithmetic-构造,贪心,细节
题目链接:http://codeforces.com/contest/625/problem/D 题意: 给你一个数字字符串s,长度1e6,算是一个大数吧,让你找到一个x,使得,x加上 逆转(x)= ...
- Python——format()/str.format()函数
格式化输出,除了类似于C语言的格式化输出外,还有str.format()方法,Python内建的format()函数,允许用户将待输出值以参数的形式,调用format()函数,在Python交互式sh ...
- elasticsearch使用Analyze API
curl -XGET 'localhost:9200/index_name/_analyze?pretty&field=type_name.field_name' -d 'Robots car ...
- http跟https的区别
http: Hypertext transform protocol 超文本传输协议 是一个为了传输超媒体文档(比如html)的应用层协议 是为了web的浏览器跟web的server端的交流而设计的, ...
- DataSet、DataTable、DataRow的数据复制方法
DataSet 对象是支持 ADO.NET的断开式.分布式数据方案的核心对象 ,用途非常广泛.我们很多时候需要使用其中的数据,比如取得一个DataTable的数据或者复制另一个DataTabe中的数据 ...