2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 L.The Heaviest Non-decreasing Subsequence Problem 权值最大的最长带权非递减子序列

题目链接:The Heaviest Non-decreasing Subsequence Problem
思路:因为是找非递减子序列,所以如果数b的权值是a,可以当成a个数b连续排列在数组中。然后找最长非递减子序列的长度即可,下列代码LNDS()函数也可以作为最长非递减子序列的模板使用,时间复杂度nlog2(n)。

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
const double eps = 1e-6;
const double Pi = acos(-1.0);
const int INF=0x3f3f3f3f;

const int maxn = 1e6+10;
int arr[maxn],dp[maxn];
int n,ans;

int Find(int tag){
    int l = 1, r = ans, mid;
    while(l <= r){
        mid = (l + r) / 2;
        if(tag >= dp[mid]) l = mid + 1;
        else r = mid - 1;
    }
    return l;
}

void LNDS(){
    int p;
    mem(dp,INF);
    ans = 0;
    dp[0] = 0;
    for(int i = 1; i < n; i++){
        p = Find(arr[i]);
        if(p > ans) ans++;
        dp[p] = Min(dp[p],arr[i]);
    }
}

int main(){
    int x;
    n = 1;
    mem(arr,0);
    while(scanf("%d",&x) != EOF){
        if(x >= 0){
            if(x >= 10000){
                arr[n++] = x - 10000;
                arr[n++] = x - 10000;
                arr[n++] = x - 10000;
                arr[n++] = x - 10000;
                arr[n++] = x - 10000;
            }else{
                arr[n++] = x;
            }
        }
    }
    LNDS();
    printf("%d\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值