CodeForces 340D - Bubble Sort Graph

11 篇文章 0 订阅
3 篇文章 0 订阅

Iahub recently has learned Bubble Sort, an algorithm that is used tosort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so heinvents his own graph. The graph (let's call it G) initially has nvertices and 0 edges. During Bubble Sort execution, edges appear asdescribed in the following algorithm (pseudocode).


procedure bubbleSortGraph()
    build a graph G with n vertices and 0edges
    repeat
        swapped = false
        for i = 1 to n - 1 inclusive do:
            if a[i] > a[i + 1] then
                add an undirected edge inG 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 asswapped value is true. */
end procedure

For a graph, an independent set is a set of vertices in a graph, no twoof which are adjacent (so there are no edges between vertices of an independentset). A maximum independent set is an independent set which has maximumcardinality. Given the permutation, find the size of the maximum independentset of graph G, if weuse 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 — theanswer to the problem.

Sample test(s)

input

3
3 1 2

output

2

Note

Consider the first example. Bubble sort swaps elements 3 and 1. We addedge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].

 

思路:

一开始理解错题意了,以为是求逆序数。。看样例也看不出来。。

赛后才知道这是图论的知识,求最大的?集(忘了),也就是集合里面的所有元素间没有直接边相连

题目中说如果a[i]>a[i+1]就在ai与ai+1间连一条边,结合排序,不难想到所有逆序对之间必有边相连,也就是说最大的集合,一定是LIS,数据量较大,要用dp+二分


程序:
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;

#define L(u) (u<<1)
#define R(u) (u<<1|1)
#define lowbit(x) (x&-x)
#define rep(i,x,y) for (i=x;i<=y;i++)
#define ll __int64
#define max(x,y) ((x>y)?(x):(y))
#define min(x,y) ((x<y)?(x):(y))
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define slld(x) scanf("%lld",&x)

const int N = 100005;

struct node
{
	int x, y;
};

int a[N];
int b[N];

int bin(int len, int x)
{
	int mid, l, r;

	l = 1;
	r = len;
	while (l <= r)
	{
		mid = (l + r) >> 1;
		if (x>b[mid])
			l = mid + 1;
		else
			if (x<b[mid])
				r = mid - 1;
			else
				return mid;
	}
	return l;
}

int LIS(int n)
{
	int i, j;

	int len = 1;
	b[1] = a[1];
	rep(i, 2, n)
	{
		j = bin(len, a[i]);
		b[j] = a[i];
		len = max(len, j);
	}

	return len;
}

int main()
{
	int n;
	sd(n);

	int i;
	rep(i, 1, n)
		sd(a[i]);
	printf("%d\n", LIS(n));

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值