线段树 求一个序列的非递增子序列的权值和的最大值

题目链接:https://nanti.jisuanke.com/t/17319  

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define lson step<<1
#define rson step<<1|1
#define getMID (l+r)>>1
#define maxn 100005
#define LL long long

using namespace std;
int ans;
int tree[maxn<<2];
void pushup(int step)
{
	tree[step]=max(tree[lson],tree[rson]);
}
void update(int l,int r,int pos,int value,int step)
{
	if(l==r)
	{
		tree[step]=max(tree[step],value);
		return ;
	}
	int mid=getMID;
	if(pos<=mid)
		update(l,mid,pos,value,lson);
	else
		update(mid+1,r,pos,value,rson);
	pushup(step);
}
void query(int l,int r,int left,int right,int step)
{
	if(l==left&&r==right)
	{
		ans=max(ans,tree[step]);
		return ;
	}
	int mid=getMID;
	if(right<=mid)
		query(l,mid,left,right,lson);
	else if(left>mid)
		query(mid+1,r,left,right,rson);
	else
	{
		query(l,mid,left,mid,lson);
		query(mid+1,r,mid+1,right,rson);
	}
}
int main()
{
	int a,w;
	int res=0;
	while(scanf("%d",&a)!=EOF)
	{
		if(a<0)
			continue;
		if(a>=10000)
		{
			a-=10000;
			w=5;
		}
		else
			w=1;
		ans=0;
		query(0,maxn,0,a,1);//先查找,找出比a小的数的区间非递增子序列的权值的最大值 
		update(0,maxn,a,ans+w,1);//更新操作,如果包含上这个数看看能否更新最优解 
		res=max(res,ans+w);//不断取最优解 
	}
	printf("%d\n",res);
	return 0;	
} 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值