Bubble Sort Graph
来源:互联网 发布:python加注释快捷键 编辑:程序博客网 时间:2024/06/05 20:55
D. Bubble Sort Graph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 thisso simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n verticesand 0 edges. During Bubble Sort execution, edges appear as described in thefollowing algorithm (pseudocode).
procedure bubbleSortGraph()
build a graph G with n vertices and 0edges
repeat
swapped = false
for i = 1 to n - 1 inclusive do:
if a[i] > a[i + 1] then
add an undirected edge inG between a[i] and a[i + 1]
swap( a[i], a[i + 1] )
swapped = true
end if
end for
until not swapped
/* repeat the algorithm as long asswapped value is true. */
end procedure
For a graph, an independent set is a setof vertices in a graph, no two of which are adjacent (so there are no edgesbetween vertices of an independent set). A maximum independent set is anindependent set which has maximum cardinality. Given the permutation, find thesize of the maximum independent set of graph G, if we use such permutation as thepremutation a in procedure bubbleSortGraph.
Input
The first line of the input contains aninteger n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n).
Output
Output a single integer — the answer tothe problem.
Sample test(s)
input
3
3 1 2
output
2
Note
Consider the first example. Bubble sortswaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Thenbubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is nowsorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Itsmaximal independent set is [1, 2].
题解
这是一道寻找最长非递减子序列的题,如果按照最原始的dp做法是会超时的,所以不超时的可以用二分查找加dp。
我们必须建立以个数组c[k],k表示长度为k的子序列,它的最后一个元素的最小值(如果一开始的c[k],中的值大于后来的值v,而v大于c[k-1],则修改c[k] = v),通过二分法+dp,时间复杂度为O(lgn),比原来的的O(n)快。
源代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
int a[100500];
int c[100500],n;
int binary_search(int c[],int len,int v)
{
int
left=1,right=len,mid=left+(right-left)/2;
while(left<=right)
{
if(v>= c[mid])
left=mid+1;
else
right=mid-1;
mid=left+(right-left)/2;
}
return left;
}
int LIS()
{
int i,j,len=1; //j指的是i之前的最长递增子序列len指的是当前的最长递增子序列
c[0]=-1,c[1]=a[0];
for(i=1;i<n;i++)
{
j=binary_search(c,len,a[i]);
c[j]=a[i]; //c[j]=a[i]指的是长度为j的子序列中的最大的元素的值
if(j>len)
len=j;
}
return len;
}
int main()
{
while(cin>> n)
{
for(inti = 0;i < n;i++)
scanf("%d",&a[i]);
intmx = LIS();
cout<< mx << endl;
}
}
- Bubble Sort Graph
- CodeForces 340D - Bubble Sort Graph
- 动态规划——Bubble Sort Graph
- codeforces 340D D. Bubble Sort Graph(dp+线段树)
- Codeforces 340D Bubble Sort Graph【LIS变形】
- codeforces 340D Bubble Sort Graph(dp+二分)
- Bubble sort
- Bubble Sort
- bubble sort
- Bubble Sort
- Bubble Sort
- bubble sort
- Bubble Sort
- Bubble Sort
- Bubble Sort
- Bubble Sort
- Bubble Sort
- Bubble Sort
- Illegal mix of collations (gb2312_chinese_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT)错误的解决方案
- Hadoop学习笔记(四):Hadoop中的streaming
- 【Android】创建Popwindow弹出菜单的两种方式
- CCLayer 指定显示区域
- php连接access
- Bubble Sort Graph
- 利用独立ip在百度知道留链接方式揭秘
- 在wordpress中实现分类目录下文章只显示标题
- 如何让程序忙时保持界面响应
- 北大ACM 1002题终于AC了 贴出代码 写的不好的地方望指教
- 小明的调查作业
- Lightoj 1051 DP
- iBATIS实战-读者须知
- Linux系统GDB调试