51nod-1376 最长递增子序列的数量

基准时间限制:1 秒 空间限制:131072 KB 分值: 160  难度:6级算法题
 收藏
 关注
数组A包含N个整数(可能包含相同的值)。设S为A的子序列且S中的元素是递增的,则S为A的递增子序列。如果S的长度是所有递增子序列中最长的,则称S为A的最长递增子序列(LIS)。A的LIS可能有很多个。例如A为:{1 3 2 0 4},1 3 4,1 2 4均为A的LIS。给出数组A,求A的LIS有多少个。由于数量很大,输出Mod 1000000007的结果即可。相同的数字在不同的位置,算作不同的,例如 {1 1 2} 答案为2。
Input
第1行:1个数N,表示数组的长度。(1 <= N <= 50000)
第2 - N + 1行:每行1个数A[i],表示数组的元素(0 <= A[i] <= 10^9)
Output
输出最长递增子序列的数量Mod 1000000007。
Input示例
5
1
3
2
0
4
Output示例
2

题解:我是看了别人的想法后才知道咋做的,太弱了。。。

本题使用线段树,先离散,对于a[i],只需要求[1 ,a[i]-1]里最长长度以及数量即可。

故线段树有两个值,一个是最长长度,一个是最长长度的数量。

每次更新单点更新,查询是区间查询

AC代码

#include <stdio.h>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cmath>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
 
using namespace std;

const int maxn = 5e4 + 10, mod = 1000000007;
struct node{
	int Max, sum;
}tree[maxn << 2];

struct node1{
	int val, id;
}a[maxn]; 

bool cmp1(node1 a1, node1 a2){
	return a1.val < a2.val;
}

bool cmp2(node1 a1, node1 a2){
	return a1.id < a2.id;
}

void push_up(int rt){
	if(tree[rt].Max < max(tree[rt << 1].Max, tree[rt << 1 | 1].Max)){
		tree[rt].Max = max(tree[rt << 1].Max, tree[rt << 1 | 1].Max);
		tree[rt].sum = 0;
		if(tree[rt].Max == tree[rt << 1].Max)
			tree[rt].sum = tree[rt << 1].sum;
		if(tree[rt].Max == tree[rt << 1 | 1].Max)
			tree[rt].sum = (tree[rt << 1 | 1].sum + tree[rt].sum) % mod;
	}
	else{
		tree[rt].sum = 0;
		if(tree[rt].Max == tree[rt << 1].Max)
			tree[rt].sum = (tree[rt << 1].sum + tree[rt].sum) % mod;
		if(tree[rt].Max == tree[rt << 1 | 1].Max)
			tree[rt].sum = (tree[rt << 1 | 1].sum + tree[rt].sum) % mod;
	}
}

void update(int p, int M, int num, int l, int r, int rt){
	if(l == r){
		if(tree[rt].Max == M)
			tree[rt].sum = (tree[rt].sum + num) % mod;
		else if(tree[rt].Max < M){
			tree[rt].Max = M;
			tree[rt].sum = num;
		}
		return ;
	}
	int m = (l + r) >> 1;
	if(p <= m)
		update(p, M, num, lson);
	else
		update(p, M, num, rson);
	push_up(rt);
}

node query(int L, int R,int l, int r, int rt){
	if(L <= l && r <= R){
		return tree[rt];
	}
	node ret, ret1, ret2;
	ret.Max = ret.sum = 0;
	ret1 = ret;
	ret2 = ret;
	int m = (l + r) >> 1;
	if(m >= L)
		ret1 = query(L, R, lson);
	if(m < R)
		ret2 = query(L, R, rson);
	ret.Max = max(ret1.Max, ret2.Max);
	if(ret.Max == ret1.Max)
		ret.sum = ret1.sum;
	if(ret.Max == ret2.Max)
		ret.sum = (ret.sum + ret2.sum) % mod;
	return ret;
}


int main(){
	memset(tree, 0, sizeof(tree));
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i].val);
		a[i].id = i;
	}
	sort(a + 1, a + 1 + n, cmp1);
	int now = -1, num = 0;
	for(int i = 1; i <= n; i++){
		if(now == a[i].val)
			a[i].val = num;
		else{
			num++;
			now = a[i].val;
			a[i].val = num;
		}
	}
	sort(a + 1, a + 1 + n, cmp2);
	for(int i = 1; i <= n; i++){
		node c;
		c.sum = c.Max = 0;
		if(a[i].val - 1 >= 1)
			c = query(1, a[i].val - 1, 1, n, 1);
		update(a[i].val, c.Max + 1, max(c.sum, 1), 1, n, 1);
	}
	node c = query(1, num, 1, n, 1);
	printf("%d\n", c.sum);
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值