2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 The Heaviest Non-decreasing Subsequence Problem 最长不下降序列

Let SS be a sequence of integers s_{1}s1s_{2}s2......s_{n}sn Each integer is is associated with a weight by the following rules:

(1) If is is negative, then its weight is 00.

(2) If is is greater than or equal to 1000010000, then its weight is 55. Furthermore, the real integer value of s_{i}si is s_{i}-10000si10000 . For example, if s_{i}si is 1010110101, then is is reset to 101101 and its weight is 55.

(3) Otherwise, its weight is 11.

A non-decreasing subsequence of SS is a subsequence s_{i1}si1s_{i2}si2......s_{ik}sik, with i_{1}<i_{2}\ ...\ <i_{k}i1<i2 ... <ik, such that, for all 1 \leq j<k1j<k, we have s_{ij}<s_{ij+1}sij<sij+1.

A heaviest non-decreasing subsequence of SS 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:

8080 7575 7373 9393 7373 7373 1010110101 9797 -11 -11 114114 -11 1011310113 118118

The heaviest non-decreasing subsequence of the sequence is <73, 73, 73, 101, 113, 118><73,73,73,101,113,118> with the total weight being 1+1+1+5+5+1 = 141+1+1+5+5+1=14. Therefore, your program should output 1414 in this example.

We guarantee that the length of the sequence does not exceed 2*10^{5}2105

Input Format

A list of integers separated by blanks:s_{1}s1s_{2}s2,......,s_{n}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;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值