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
题目是说找出一个值最大的序列,而这个序列的值的定义方式是对于负数,值为0。对于正数,如果小于10000,那么值就是1,否则为5。另外,如果大于10000,那么这个数要减去10000才是他真正在数列中的数(比如10101,在数列中也将其视为101)。然后将其排列出一个最长的序列,要求这个序列的值最大,且序列为不下降的序列。
处理的时候,对于负数,因为没有值,所以直接舍去不读入就可以了。对于正数,如果小于10000就正常读入,如果大于10000的话,先将这个数减去10000存入,因为这个数的值为5,所以再复制4次,存入这个数5次进去,这样就相当于处理了权值为5的问题,然后读入完之后求出最长不下降子序列就可以了。、
(PS:这个题输入是一组数据,比赛的时候我当成多组数据写了,然后写了很多getchar判断空格回车结束,结果TLE了两发改成一组数据才A了....)
下面AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1000005];
int b[1000005],c[1000005];
int fin(int *t,int len,int n)
{
int left=0,right=len,mid=(left+right)/2;
while(left<=right)
{
if(n>=t[mid])
left=mid+1;
else if(n<t[mid])
right=mid-1;
else
return mid;
mid=(left+right)/2;
}
return left;
}
int main()
{
int n;
int i,j;
int k;
int m;
int t;
int len;
char g;
i=0;
while(scanf("%d",&m)!=EOF)
{
a[i]=m;
if(a[i]<0)
{
i--;
}
else if(a[i]>10000)
{
a[i]-=10000;
k=a[i];
for(j=1;j<=4;j++)
{
i++;
a[i]=k;
}
}
i++;
}
n=i;
/*for(i=0;i<n;i++)
{
cout<<"a["<<i<<"] = "<<a[i]<<endl;
}*/
b[0]=1;
c[0]=-1;
c[1]=a[0];
len=1;
for(i=1;i<n;i++)
{
j=fin(c,len,a[i]);
c[j]=a[i];
if(j>len)
len=j;
}
cout<<len<<endl;
return 0;
}