您的位置:首页 > 其它

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).


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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: