【CodeForces - 340D】Bubble Sort Graph (思维,nlogn最长上升子序列类问题)
題干:
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 edgesrepeatswapped = falsefor i = 1 to n - 1 inclusive do:if a[i] > a[i + 1] thenadd an undirected edge in G between a[i] and a[i + 1]swap( a[i], a[i + 1] )swapped = trueend ifend foruntil not swapped /* repeat the algorithm as long as swapped value is true. */ end procedureFor 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.
Examples
Input
3 3 1 2Output
2Note
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].
?
題目大意:
冒泡排序,每交換一次就在圖中建一條邊,最終要求圖中沒有直接邊相連的點(diǎn)集個數(shù)最大值,如果i<j && a[i]>a[j]就要交換。問你最大不相連的點(diǎn)有多少個。
解題報(bào)告:
? ? 類似于逆序?qū)Φ母拍?#xff0c;逆序?qū)χg一定有邊 所以這題就轉(zhuǎn)化成類似于求非逆序?qū)€數(shù)了,不一樣的地方在于這題不是求個數(shù),而是求最多的這樣的點(diǎn)數(shù),很明顯轉(zhuǎn)化成lis問題、、
? ?話說訓(xùn)練的時(shí)候沒想到這么多人會nlogn的lis。。。2333
? ?雖然這是我做到過的第五個吧,,需要用到nlogn的、、這里總結(jié)一下分別是:
? ? ZOJ 2319,51nod1134,HDU1025,codeforce270D
AC代碼:
?
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; int a[MAX],b[MAX]; int n; int DP() {int len = 0;b[++len] = a[1];for(int i = 2; i<=n; i++) {if(a[i] >= b[len]) b[++len] = a[i];else {int pos = lower_bound(b+1,b+len+1,a[i]) - b;b[pos] = a[i];}} // for(int i = 1; i<=len; i++) printf("%d ",b[i]);return len; } int main() {cin>>n;for(int i = 1; i<=n; i++) {scanf("%d",a+i);}printf("%d\n",DP());return 0 ;} 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 340D】Bubble Sort Graph (思维,nlogn最长上升子序列类问题)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我国高分十二号03星成功发射:卫星已顺利
- 下一篇: 【POJ - 3037】Skiing (