题目链接: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;
}