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









### Codeforces Problem 1014D 解答与解释 当前问题并未提供关于 **Codeforces Problem 1014D** 的具体描述或相关背景信息。然而,基于常见的竞赛编程问题模式以及可能涉及的主题领域(如数据结构、算法优化等),可以推测该问题可能属于以下类别之一: #### 可能的解法方向 如果假设此问题是典型的计算几何或者图论类题目,则通常会涉及到如下知识点: - 图遍历(DFS 或 BFS) - 贪心策略的应用 - 动态规划的状态转移方程设计 由于未给出具体的输入输出样例和约束条件,这里无法直接针对Problem 1014D 提供精确解答。但是可以根据一般性的解决思路来探讨潜在的方法。 对于类似的复杂度较高的题目,在实现过程中需要注意边界情况处理得当,并且要充分考虑时间效率的要求[^5]。 以下是伪代码框架的一个简单例子用于说明如何构建解决方案逻辑流程: ```python def solve_problem(input_data): n, m = map(int, input().split()) # 初始化必要的变量或数组 graph = [[] for _ in range(n)] # 构建邻接表或其他形式的数据表示方法 for i in range(m): u, v = map(int, input().split()) graph[u].append(v) result = [] # 执行核心算法部分 (比如 DFS/BFS 遍历) visited = [False]*n def dfs(node): if not visited[node]: visited[node] = True for neighbor in graph[node]: dfs(neighbor) result.append(node) for node in range(n): dfs(node) return reversed(result) ``` 上述代码仅为示意用途,实际应用需依据具体题目调整细节参数设置及其功能模块定义[^6]。 #### 关键点总结 - 明确理解题意至关重要,尤其是关注特殊测试用例的设计意图。 - 对于大规模数据集操作时应优先选用高效的时间空间性能表现良好的技术手段。 - 结合实例验证理论推导过程中的每一步骤是否合理有效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值