Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)
2013-08-31 23:51
351 查看
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 this so simple algorithm, so he invents his own graph. The graph (let's call it G) 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 graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph.
Input
The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n).
Output
Output a single integer — the answer to the problem.
Sample test(s)
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].
思路:
问题等价于找一个最长非降子序列。
代码:
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 this so simple algorithm, so he invents his own graph. The graph (let's call it G) 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 graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph.
Input
The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n).
Output
Output a single integer — the answer to the problem.
Sample test(s)
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].
思路:
问题等价于找一个最长非降子序列。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define maxn 100005 using namespace std; int n,m,ans,cnt; int a[maxn],dp[maxn]; void solve() { int i,j,pos; dp[0]=0; cnt=0; for(i=1; i<=n; i++) { if(a[i]>=dp[cnt]) dp[++cnt]=a[i]; else { pos=upper_bound(dp,dp+cnt+1,a[i])-dp; // 找到>a[i]的第一次出现的位置 printf("i:%d pos:%d\n",i,pos); dp[pos]=a[i];low } } } int main() { int i,j; while(~scanf("%d",&n)) { for(i=1; i<=n; i++) { scanf("%d",&a[i]); } solve(); printf("%d\n",cnt); // 长度即为cnt 但序列不是dp保存的序列 要输出序列的话应在更新ant时记录序列 } return 0; } /* 7 2 3 3 5 3 2 4 */
相关文章推荐
- Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)
- Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)
- codeforces 340D D. Bubble Sort Graph(dp+线段树)
- [模型转化 最长下降子序列] BZOJ 2924 [Poi1998]Flat broken lines
- CodeForces 340D - Bubble Sort Graph
- Bubble Sort Graph
- hunnu 11313 无重复元素序列的最长公共子序列转化成最长递增子序列 求法及证明
- 【Codeforces Round 354 (Div 2)C】【前缀和二分or双指针】Vasya and String ab序列最多改变k位置的最长同字符子串长度
- hunnu 11313 无重复元素序列的最长公共子序列转化成最长递增子序列 求法及证明
- (字符串的映射与匹配7.3.13)UVA 10152 ShellSort(将初始序列转化成目标序列所需的最少次数,前提条件是:每一个元素只能移动栈顶)
- 【Codeforces Round 271 (Div 2)E】【离散化线段树】Pillars 最长连续序列使得序列相邻的数差值至少为k
- Codeforces Round #FF (Div. 2)446A - DZY Loves Sequences(最长上升子序列变形)
- CodeForces 340D - Bubble Sort Graph
- CF209 Div2 (D) 找最长的连续子序列拥有相同的最大公约数,并且最大公约数是这个序列中的一个数-------左右延伸的方法
- Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift(序列)
- Codeforces Round #323 (Div. 1) B. Once Again... (最长不下降序列_DP)
- 【Codeforces Round 333 (Div 2)B】【贪心 多指针】Approximating a Constant Range 给定数组 相邻元素波动为1 求差值不超1的最长序连续子序列
- [CF340D]Bubble Sort Graph/[JZOJ3485]独立集
- Codeforces Round #261(Div 2) E Pashmak and Graph(图中严格递增的最长路径、思维)