CodeForces 340D
来源:互联网 发布:魔兽世界源码 编辑:程序博客网 时间:2024/06/06 10:54
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation withn elementsa1,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 hasn 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.
The first line of the input contains an integern (2 ≤ n ≤ 105). The next line containsn distinct integersa1, a2, ...,an(1 ≤ ai ≤ n).
Output a single integer — the answer to the problem.
33 1 2
2
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 100100using 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
- codeforces 340D D. Bubble Sort Graph(dp+线段树)
- Codeforces 617D Polyline (Round #340 (Div. 2) D题)
- CodeForces 340D - Bubble Sort Graph
- Codeforces Round #340D - Polyline-水
- Codeforces Round #340 (Div. 2) D. Polyline
- Codeforces Round #340 (Div. 2)D. Polyline
- Codeforces Round #340 (Div. 2)-D. Polyline
- CodeForces 617D CodeForces 617D
- CodeForces 101D
- CodeForces 103D
- CodeForces 222D Olympiad
- codeforces 242d
- CodeForces 111D
- 【codeforces】3D
- CodeForces 127D Password
- codeforces 257D. Sum
- Codeforces 292D
- IoC控制反转
- Oracle SQL:经典查询练手第三篇
- C语言小结--指针和变量传参
- 【bzoj3829】 [Poi2014]FarmCraft
- 涉猎大数据--序
- CodeForces 340D
- 前端开发工作必备
- 题目1021:统计字符
- Oracle SQL:经典查询练手第四篇
- 概率统计与机器学习:极大后验概率以及正则化项
- eclipse中安卓机器人图标不见的恢复方法
- Invoke和begininvoke的使用方法、区别
- Oracle SQL:经典查询练手第五篇
- 搞IT的到底怎么了