您的位置:首页 > 产品设计 > UI/UE

计蒜客 2017icpc南宁赛区 The Heaviest Non-decreasing Subsequence Problem 最长不递减子序列

2017-09-26 21:39 375 查看
https://nanti.jisuanke.com/t/17319

题意:对于每个数x,若x<0,则价值为0,x大于10000,价值为5且数值变为x-10000,反之价值为1。现在求不递减的序列使得价值最大。

题解:负数没有贡献不考虑,大于10000的存五次,相当于价值为1,反之存一次。然后求一次最长不递减子序列即可,答案就是子序列长度,因为价值都是1,这样处理之后,没有差别了。

本来应该是道水题,网络赛的时候,最后没时间自己脑残想了个二维的dp。还有补题的时候,数据保证长度不超过2*10e5,但实际上还要大。不知道是不是放题库的时候加入了数据?

代码:

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<string>
#include<bitset>

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>

#include<iomanip>
#include<iostream>

#define debug cout<<"aaa"<<endl
#define d(a) cout<<a<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64
#define MIN_LL (-9223372036854775807i64-1)
using namespace std;

const int N = 1000000;
const int mod = 1000000000 + 7;
const double eps = 1e-8;
int dp
,a
;
int n,len,pos,x;
//dp[i]:保存的是最长递增子序列长度为i的结尾的最小值
//可以当作往里面一个一个加数字,更新最小那个
int search(int l,int r,int x){
while(l<=r){
int mid=(l+r)>>1;
if(dp[mid]>x){
r=mid-1;
}
else{
l=mid+1;
}
}
return l;
}

void LIS(){
mem(dp,0),len=1;
dp[1]=a[1];
for(int i=2;i<=n;i++){
if(a[i]>=dp[len]){
dp[++len]=a[i];
}
else{
pos=search(1,len,a[i]);
dp[pos]=a[i];
}
}
}

int main(){
n=0;
while(~scanf("%d",&x)){
if(x<0) continue;
if(x>=10000){
for(int i=1;i<=5;i++){
a[++n]=x-10000;
}
}
else{
a[++n]=x;
}
}
LIS();
printf("%d\n",len);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐