[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 ...
随机推荐
- hdu 1272 小希的迷宫(并查集+最小生成树+队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) ...
- linux系统引导流程
1.固件:硬件和软件结合.加电自检是我们按下电源会检测硬件像cpu.内存.网卡等信息.(从硬件层次检测硬件是否可用) 固件设置实例:设置时间 软件时间和硬件时间: [root@VM_0_12_cent ...
- JavaWeb使用Session防止表单重复提交
在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 1.什么是表单 ...
- 通用套接字选项和TCP套接字选项
1. 套接字选项函数原型: #include <sys/socket.h> int getsockopt(int sockfd, int level, int optname, void ...
- python基础===【爬虫】爬虫糗事百科首页图片代码
import requests import re import urllib.request def getHtml(url): page = requests.get(url) html = pa ...
- python基础===一道小学奥数题的解法
今早在博客园和大家分享了一道昨晚微博中看到的小学奥数题,后来有朋友给出了答案.然后我尝试用python解答它. 原题是这样的: 数学题:好事好 + 要做好 = 要做好事,求 “好.事.做.要”的值分别 ...
- 64_m2
mimetic-devel-0.9.8-7.fc26.i686.rpm 12-Feb-2017 05:40 288474 mimetic-devel-0.9.8-7.fc26.x86_64.rpm 1 ...
- twemproxy 简介、安装配置
twemproxy 简介.安装配置 http://www.xuchanggang.cn/archives/993.html
- 2017多校第7场 HDU 6128 Inverse of sum 推公式或者二次剩余
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6128 题意:给你n个数,问你有多少对i,j,满足i<j,并且1/(ai+aj)=1/ai+1/a ...
- [Deep dig] ViewController初始化过程调查
代码:https://github.com/xufeng79x/ViewControllerLife 1.简介: 介绍xib方式.storyborad方式以及code方式下ViewController ...