Codeforces 340D Bubble Sort Graph【LIS变形】

探讨一种基于冒泡排序构建的特殊图——冒泡排序图,并介绍如何利用最长上升子序列算法来高效求解该图的最大独立集规模。

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

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.

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


题目大意:

我们都学过冒泡排序,对于一个具有N个元素的序列来讲,每一次遍历数组,比较相邻两个数字的大小,如果a【i】>a【i+1】,将两个元素调换,直到最终排序成功为止。

现在如果存在了a【i】>a【i+1】.那么现在在一个具有N个点的图中建出无向边,连接a【i】和a【i+1】两个点。

问最大独立集的size是多大。


思路:


最大独立集:一个集合中所有点之间都没有连边。


我们通过枚举和思考可以得知:对于原序列来讲,若存在a【i】>a【j】的话(j>i),那么一定存在建边:Add(a【i】,a【j】).

那么反之,若存在a【i】<a【j】的话,那么两点之间一定没有建边。


那么我们能够递推出一个关系,如果a【i】和a【j】之间不能建边,而且a【j】和a【k】之间不能建边,那么a【i】和a【k】之间也一定不能建边。(i<j<k)

那么问题其实就是在求原序列的最长上升子序列的长度。


n范围比较大,采用二分思维nlogn去做即可。


#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 444444
using namespace std;

int f[N];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int c=0;
        for(int i=1;i<=n;i++)
        {
            int t;
            scanf("%d",&t);
            if(i==1) f[++c]=t;
            else
            {
                if(t>f[c]) f[++c]=t;//最长非递减就变成等号即可。
                else
                {
                    int pos=lower_bound(f+1,f+c,t)-f;//二分找到数组中比t大的第一个元素的的地址。
                    f[pos]=t;
                }
            }
        }
        printf("%d\n",c);
    }
}









下载前可以先看下教程 https://pan.quark.cn/s/16a53f4bd595 小天才电话手表刷机教程 — 基础篇 我们将为您简单的介绍小天才电话手表新机型的简单刷机以及玩法,如adb工具的使用,magisk的刷入等等。 我们会确保您看完此教程后能够对Android系统有一个最基本的认识,以及能够成功通过magisk root您的手表,并安装您需要的第三方软件。 ADB Android Debug Bridge,简称,在android developer的adb文档中是这么描述它的: 是一种多功能命令行工具,可让您与设备进行通信。 该命令有助于各种设备操作,例如安装和调试应用程序。 提供对 Unix shell 的访问,您可以使用它在设备上运行各种命令。 它是一个客户端-服务器程序。 这听起来有些难以理解,因为您也没有必要去理解它,如果您对本文中的任何关键名词产生疑惑或兴趣,您都可以在搜索引擎中去搜索它,当然,我们会对其进行简单的解释:是一款在命令行中运行的,用于对Android设备进行调试的工具,并拥有比一般用户以及程序更高的权限,所以,我们可以使用它对Android设备进行最基本的调试操作。 而在小天才电话手表上启用它,您只需要这么做: - 打开拨号盘; - 输入; - 点按打开adb调试选项。 其次是电脑上的Android SDK Platform-Tools的安装,此工具是 Android SDK 的组件。 它包括与 Android 平台交互的工具,主要由和构成,如果您接触过Android开发,必然会使用到它,因为它包含在Android Studio等IDE中,当然,您可以独立下载,在下方选择对应的版本即可: - Download SDK Platform...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值