2017 ACM-ICPC 亚洲区(南宁赛区)网络赛:The Heaviest Non-decreasing Subsequence Problem

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

题目翻译:给出一个数列,该数列的数字,如果是负数,其权值为0,如果是小于10000

的权值为1,如果是大于等于10000的,那么数值减去10000,然后其对应的权值为5.

解题思路:用线段树维护区间最大值,这个题由于负数权值是0,而且要找的还是非

递减的序列中权值和最大的,所以负数不必考虑,当输入一个数num的时候,我们

在0~num区间内找个权值和最大的,然后加上其自身权值插入到线段树对应位置,

从而更新整棵线段树。


AC代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#define lchild left,mid,root<<1
#define rchild mid+1,right,root<<1|1
using namespace std;

const int maxn = 22000;
int Max[maxn<<2];
void push_up(int root)
{
    Max[root] = max(Max[root<<1] ,Max[root<<1|1]);
}
void init()
{
    memset(Max,0,sizeof(Max));
}
void insertElem(int pos,int value,int left,int right,int root)
{
    if(left == right)
    {
        Max[root] = value;
        return;
    }
    int mid = (left+right)>>1;
    if(pos <= mid) insertElem(pos,value,lchild);
    else insertElem(pos,value,rchild);
    push_up(root);
}
///查询
int query(int L,int R,int left,int right,int root)
{
    if(L<=left && right<=R)
    {
        return Max[root];
    }
    int mid = (left+right)>>1;
    int ans = 0;
    if(L <= mid) ans = max(ans,query(L,R,lchild));
    if(R > mid) ans = max(ans,query(L,R,rchild));
    return ans;
}
int main()
{
    init();
    int num,value;
    while(~scanf("%d",&num))
    {
        if(num < 0) continue;
        else if(num < 10000) value = 1;
        else
        {
            num = num-10000;
            value = 5;
        }
        int t = query(0,num,1,maxn,1);
        //printf("&&&&&t = %d\n",t);
        insertElem(num,t+value,1,maxn,1);
        //printf("@@@@@ %d\n",Max[1]);
    }
    printf("%d\n",Max[1]);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值