概述
Let SSS be a sequence of integers s1s_{1}s1,s2s_{2}s2,.........,sns_{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 sis_{i}si is si−10000s_{i}-10000. For example, if sis_{i}si is 1010110101, then is is reset to 101101and its weight is 55.
(3) Otherwise, its weight is 11.
A non-decreasing subsequence of SSS is a subsequence si1s_{i1},si2s_{i2},.........,siks_{ik}, with i1<i2 ... <iki_{1}<i_{2} ... <i_{k}i1<i2 ... <ik, such that, for all 1≤j<k1 leq j<k, we have sij<sij+1 s_{ij}<s_{ij+1<sij+1.
A heaviest non-decreasing subsequence of SSS 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:
8080757737393937373737310101101019797−1-1−1-1114114−1-11011310113118118
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 141414 in this example.
We guarantee that the length of the sequence does not exceed2∗1052*10^{5}。
Input Format
A list of integers separated by blanks:s1s_{1}s1,s2s_{2}s2,.........,sns_{n}
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
//题意:给定一个数组,现规定,里面的数值为负的话,它的weight是0,若大于等于0且小于10000的话,其weight为1,若大于等于10000,它的weight为5,且它的值是原来的值减10000。求这个数组的子串中weight和的最大值。
//思路:开一个新数组,遍历给定的数组,值为负的直接舍去,若值是小于10000的非负数,就依次存到一个新数组,若值大于10000,将这个值减去10000后,放入新数组中,放5次,因为它的weught=5,然后就得到一个新数组,求这个新数组的非递减最长子序列的长度即可。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX=2e5+100;
int num[MAX];
int a[MAX];
int dp[MAX];
int cnt;
int bsearch(int len,int x)
{
int l=0,r=len-1;
while(l<=r)
{
int mid=(l+r)/2;
if(x>=dp[mid-1]&&x<dp[mid])return mid;
else if(x<dp[mid-1])r=mid-1;
else l=mid+1;
}
return l;
}
//最长非递减子序列
int LIS()
{
dp[0]=a[0];
int len=1;
int j;
for(int i=1; i<cnt; i++)
{
if(a[i]<dp[0])j=0;
else if(a[i]>=dp[len-1])j=len++;
else j=bsearch(len,a[i]);
dp[j]=a[i];
}
return len;
}
int main()
{
memset(num,0,sizeof(num));
memset(dp,0,sizeof(dp));
memset(a,0,sizeof(a));
int nn=0;
while(scanf("%d",&num[nn])!=EOF)
{
nn++;
}
cnt=0;
for(int i=0;i<nn;i++)
{
if(num[i]<0)
continue;
if(num[i]>=10000)
{
num[i]-=10000;
for(int j=0;j<5;j++)
{
a[cnt++]=num[i];
}
}
else
{
a[cnt++]=num[i];
}
}
int ans=LIS();
printf("%dn",ans);
return 0;
}
最后
以上就是受伤绿茶为你收集整理的The Heaviest Non-decreasing Subsequence Problem(2017南宁网络赛)的全部内容,希望文章能够帮你解决The Heaviest Non-decreasing Subsequence Problem(2017南宁网络赛)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复