Codeforces 340D Bubble Sort Graph 规律+LIS

http://codeforces.com/problemset/problem/340/D

题意:给出一个n个数构成的排列,类似冒泡排序,若a[i]>a[i+1] a[i]-a[i+1]连边 并swap(a[i],a[i+1])
反复操作 直到排序结束,n<=1e5,问该图构成的最大独立集大小为?

最大独立集:集合中任意两点无边相连.

直接建图显然不行,找规律:a[i]和后面比它大的都不会连边.
a[i],x,y下标递增,冒泡排序,a[i]<x<y x和y不可能交换
x>y 则x一定会被交换到y之后 每次又是两个相邻元素交换 则x-y一定有边
所以 本题独立集的元素单调递增 O(nlogn)求LIS即可

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=1e9;
const int N=2e5+20;
int n,a[N],f[N];
int L[N];//L[i],LIS3¤?è?aiμ?×?D??á?2a[j]
int main()
{
	while(cin>>n)
	{
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]),L[i]=inf;
		L[0]=-inf;
		for(int i=1;i<=n;i++)
		{
		 	int l=0,r=n;
			while(l<=r)
			{
				int mid=(l+r)>>1;
				if(a[i]>L[mid])
					l=mid+1;
				else
					r=mid-1;
			}
			f[i]=l;
			L[l]=a[i];//L[l]>a[i]
		//a[i]>L[l-1] ?üD?L[l]oó L[l]>L[l-1] LDòáDòàè?μ¥μ÷μY??
		}
		int ans=0;
		for(int i=1;i<=n;i++)
			ans=max(ans,f[i]);
		cout<<ans<<endl;
	}

	return 0;
}

  

时间: 2024-08-25 21:47:15

Codeforces 340D Bubble Sort Graph 规律+LIS的相关文章

codeforces 340D Bubble Sort Graph(dp,LIS)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Bubble Sort Graph Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple al

codeforces340D - Bubble Sort Graph dp + 最长上升子序列

题意:给你长为n的序列 ,每个节点都和在它前面且值比他大的点产生一条边,问你一个最大 两两点没有边的集合的 集合元素有多少 解题思路:想了半天才发现是最长上升子序列.. 解题代码: 1 // File Name: 340d.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月03日 星期日 12时31分24秒 4 5 #include<vector> 6 #include<list> 7 #include<map>

codeforces 340D D. Bubble Sort Graph(dp+线段树)

题目链接: codeforces 340D 题目大意: 给出一个程序,就是冒泡排序,每次如果发现相邻两个数前一个大于后一个,交换位置后建边,问最后得到的这个图中的最大独立集,这个最大独立集定义为所有的点都不相邻的最大点的集合的规模. 题目分析: 首先我们可以知道对于a[i],它只会且一定会与后面的比它小的建边,所以我们只需要固定第一个点,然后找最长上升子序列即可.(这样能够保证没有相邻的点) 定义状态dp[i]为以i项结尾的最长上升子序列的长度. 转移方程如下: dp[i]=max{dp[j]+

POJ 3761:Bubble Sort——组合数学

题目大意:众所周知冒泡排序算法多数情况下不能只扫描一遍就结束排序,而是要扫描好几遍.现在你的任务是求1~N的排列中,需要扫描K遍才能排好序的数列的个数模20100713.注意,不同于真正的冒泡排序算法,只要数列有序就立刻停止,而不用再检验一遍. 估计多数人都是找规律吧,先看出递推,然后求出通项……这个题只有找出通项公式才能通过,所以首先公布答案: K!((K + 1) ^ (N - K) - K ^ (N - K)) 好吧,现在让我们来证明一下. 首先定义函数d(x),对于1~N的一个排列,d(

hdu 5775 Bubble Sort (树状数组)

Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 591    Accepted Submission(s): 359 Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is t

Codeforces 459E Pashmak and Graph(dp+贪心)

题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边按照权值排序,每次将相同权值的边同时加入,维护每个点作为终止点的最大长度即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 3

经典排序算法 - 冒泡排序Bubble sort

 原文出自于 http://www.cnblogs.com/kkun/archive/2011/11/23/bubble_sort.html 经典排序算法 - 冒泡排序Bubble sort 原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头开始进行两两比较交换,直到倒数第二位时结束,其余类似看例子 例子为从小到大排序, 原始待排序数组| 6 | 2 | 4 | 1 | 5 | 9 | 第一趟排序(外循环) 第

HDU 5775:Bubble Sort(树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is the code of Bubble Sort in C++. for(int i=1;i<=N;++i) for(int j=N,t;j>i;—j) if(P[j-1] > P

hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 636    Accepted Submission(s): 378 Problem Description P is a permutation of the