动态规划——Bubble Sort Graph

来源:互联网 发布:死飞自行车多少钱淘宝 编辑:程序博客网 时间:2024/06/05 15:33

Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1a2, ..., 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 a1a2, ..., an (1 ≤ ai ≤ n).

Output

Output a single integer — the answer to the problem.

Example
Input
33 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].



题意:看了之后才知道是求最长非递减子序列。。。

思路:看数据范围知道是用dp+二分


#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;int a[100010];int dp[100010];int n;int wtf(int l, int x)   //二分搜索求dp数组中<=x的位置{    int left=1,right=l,mid=(left+right)>>1;    while(left<=right)    {        if(dp[mid]>x)            right=mid-1;        else            if(dp[mid]<x)                left=mid+1;            else                return mid;        mid=(left+right)>>1;    }    return left;}int DP(){    int l=1;    dp[1]=a[0];    for(int i=1; i<n; i++)    {        int k=wtf(l, a[i]);        dp[k]=a[i];        l=max(l, k);    }    return l;}int main(){    while(~scanf("%d", &n))    {        memset(dp, 0, sizeof(dp));        memset(a, 0, sizeof(a));        for(int i=0; i<n; i++)            scanf("%d", &a[i]);        int ans=DP();        printf("%d\n", ans);    }    return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 学习学不进去怎么办 小孩停不下来怎么办 小孩老爱玩不爱学习怎么办 小孩子不喜欢吃菜怎么办 孩子初中不爱学习怎么办 大学不爱学家长怎么办 小孩子不爱读书不听话怎么办 小孩不爱读书写字怎么办 一年级学生不爱学习怎么办 孩子练字怕累怎么办 孩子不愿意学英语怎么办 生了儿子 不喜欢 怎么办 养两个儿子的怎么办 看诗词记不住怎么办 经常读书记不住怎么办 孩子不爱记数字怎么办 考研学不进去怎么办 读了职高后悔怎么办 不喜欢看书的人怎么办 生的儿子不喜欢怎么办 孩子上网不回家怎么办 儿子不想读书了怎么办 中考体育考不好怎么办 小孩突然没礼貌怎么办 小孩读书记忆差怎么办 这几天不爱吃饭怎么办 读书读到不懂的怎么办 读书很多词不懂怎么办 看书看不进去怎么办 一年级小孩不喜欢读书怎么办 考研还不想学习怎么办 怎么吃还是瘦怎么办 要想读的书该怎么办 大人瘦不爱吃饭怎么办 大人不爱吃青菜怎么办 如果不想写作业怎么办 一年级不爱写作业怎么办 生气总打孩子怎么办 人爱喝酒怎么办才不改 俩岁半宝宝不吃饭怎么办 孩子高中不爱学习怎么办