Let S be a sequence of integers s1, s2, ..., sn Each integer is is associated with a weight by the following rules:
(1) If is is negative, then its weight is 0.
(2) If is is greater than or equal to 10000, then its weight is 5. Furthermore, the real integer value of si is si−10000 . For example, if si is 10101, then is is reset to 101 and its weight is 5.
(3) Otherwise, its weight is 1.
A non-decreasing subsequence of S is a subsequence si1, si2, ..., sik, with i1<i2 ... <ik, such that, for all 1≤j<k, we have sij<sij+1.
A heaviest non-decreasing subsequence of S is a non-decreasing subsequence with the maximum sum of weights.
Write a program that reads a sequence of integers, and outputs the weight of its
heaviest non-decreasing subsequence. For example, given the following sequence:
80 75 73 93 73 73 10101 97 −1 −1 114 −1 10113 118
The heaviest non-decreasing subsequence of the sequence is <73,73,73,101,113,118> with the total weight being 1+1+1+5+5+1=14. Therefore, your program should output 14 in this example.
We guarantee that the length of the sequence does not exceed 2∗105
Input Format
A list of integers separated by blanks:s1, s2,...,sn
Output Format
A positive integer that is the weight of the heaviest non-decreasing subsequence.
样例输入
80 75 73 93 73 73 10101 97 -1 -1 114 -1 10113 118
样例输出
14
题目来源
分析:人生的第一个树状数组优化dp题目,尴尬的是队友会dp,我会树状数组,很遗憾,没做出来。
这里介绍两种思路。
思路见代码 :
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int tree[300005];
struct Node {
int a[300005];
int val[300005];
}data;
int dp[300005];
void add(int x,int num)
{
for(int i=x;i<=300005;i+=i&-i)
if(num>tree[i])
tree[i]=num;
}
int read(int x)
{
int num=0;
for(int i=x;i>0;i-=i&-i)
if(num<tree[i])
num=tree[i];
return num;
}
int main()
{
int n=1;
while(~scanf("%d",&data.a[n]))
{
if(data.a[n]<0) //负数可以不存
continue;
else if(data.a[n]>=10000)
{
data.a[n]-=10000; //大于10000的数,real值是原数减10000
data.val[n]=5;
}
else
data.val[n]=1;
n++;
}
dp[1]=data.val[1];
add(data.a[1],dp[1]);
int ans=-1;
for(int i=2;i<n;i++)
{//为什么说是树状数组优化dp,因为这里本来是又一层循环,用来查找在满足data.a[j]<=data.a[i]的情况下,
//寻找最大的dp[j]; 所以咱们以 data.a[i]为区间坐标,求区间最值,从0~data.a[i]中找到的最大的dp[j],
//一定满足 data.a[j]<=data.a[i]
int j=read(data.a[i]);
dp[i]=max(dp[i],j+data.val[i]);
add(data.a[i],dp[i]);
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
return 0;
}
第二种:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int a[1000005];
int dp[1000005];
int main()
{
int n;
n=1;
while(~scanf("%d",&a[n]))
{//既然只有val==1,5的时候有用,而且求的是不下降的heavies序列。那么不妨将这些val=5的
//拆分成5个val=1的,然后直接求解最大不下降子序列。
if(a[n]<0)
continue;
else if(a[n]>=10000)
{
a[n]-=10000;
int tem=a[n];
n++;
for(int i=0;i<4;i++)
a[n++]=tem;
}
else
n++;
}
int len=1;
memset(dp,0,sizeof(dp));
dp[1]=a[1];
for(int i=2;i<n;i++)
{
if(a[i]>=dp[len])
dp[++len]=a[i];
else
{
int add = lower_bound(dp+1,dp+len+1,a[i]) - dp;
while(dp[add]<=a[i])
add++;
dp[add] = a[i];
}
}
if(n==1 && a[n]<0)//注意边界情况处理
printf("0\n");
else
printf("%d\n",len);
return 0;
}