CodeForces 340D - Bubble Sort Graph
2017-07-24 23:52
477 查看
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 withn elements
a1,a2, ...,
an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call itG) initially has
n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode).
For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent
set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graphG, if we use such permutation as the premutationa
in procedure bubbleSortGraph.
Input
The first line of the input contains an integern (2 ≤ n ≤ 105).
The next line containsn distinct integers
a1,
a2, ...,an(1 ≤ ai ≤ n).
Output
Output a single integer — the answer to the problem.
Examples
Input
Output
Note
Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3).
Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
看懂题意很重要: 所以好好看题。
题意是:找一个最长 的序列,使他们之间没有直接相连的边
找规律发现就是 求LIS最长非递减子序列。
然后o(n*n)的做法会超时,所以要dp+二分解决
代码:
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 withn elements
a1,a2, ...,
an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call itG) initially has
n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode).
procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G 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 as swapped value is true. */ end procedure
For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent
set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graphG, if we use such permutation as the premutationa
in procedure bubbleSortGraph.
Input
The first line of the input contains an integern (2 ≤ n ≤ 105).
The next line containsn distinct integers
a1,
a2, ...,an(1 ≤ ai ≤ n).
Output
Output a single integer — the answer to the problem.
Examples
Input
3 3 1 2
Output
2
Note
Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3).
Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
看懂题意很重要: 所以好好看题。
题意是:找一个最长 的序列,使他们之间没有直接相连的边
找规律发现就是 求LIS最长非递减子序列。
然后o(n*n)的做法会超时,所以要dp+二分解决
代码:
#include #include #include #include #define maxn 100100 using namespace std; int dp[maxn],A[maxn]; int n,cnt; int LS() { for(int i=1;i<=n;i++) { if(A[i]>=dp[cnt]) dp[++cnt]=A[i]; else { int tt=upper_bound(dp,dp+cnt+1,A[i])-dp; //二分操作,即返回第一个大于A[i]的下标 dp[tt]=A[i]; } } } int main() { scanf("%d",&n); dp[0]=0; for(int i=1;i<=n;i++) { scanf("%d",&A[i]); dp[i]=0; } cnt=0; LS(); printf("%d\n",cnt); return 0; }
相关文章推荐
- CodeForces 340D - Bubble Sort Graph
- codeforces 340D D. Bubble Sort Graph(dp+线段树)
- Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)
- [CF340D]Bubble Sort Graph/[JZOJ3485]独立集
- Bubble Sort Graph
- Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)
- 【codeforces 724D】【贪心】 Dense Subsequence 【一个字符串,按照一定的区间要求从中选出一些字符,使得这串字符的sort后字典序最小】
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort(暴力)
- Codeforces 652B z-sort【水题】
- Codeforces 366D Dima and Trap Graph 【并查集】
- Bubble Sort MIPS
- 2014.2.25 改进型冒泡排序 improved bubble sort algorithm
- Codeforces 362C. Insertion Sort
- Codeforces 466 E. Information Graph
- Codeforces 459E. Pashmak and Graph (DP)
- Bubble_sort
- Codeforces Div. 2 #258-B. Sort the Array
- CodeForces 295B - Greg and Graph Floyd的巧妙应用
- codeforces 686B Little Robber Girl's Zoo (sort)
- Codeforces 505B - Mr. Kitayuta's Colorful Graph (DFS)