码迷,mamicode.com
首页 > 其他好文 > 详细

HackerRank - "The Longest Increasing Subsequence"

时间:2015-05-10 14:08:02      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

It requires O(nlgn) solution. And actually I think the passing code is for "non-decreasing"..

#include <cmath>
#include <cstdio>
#include <vector>
#include <unordered_set>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int t; cin >> t;
    vector<int> arr(t);
    for (int i = 0; i < t; i++)
        cin >> arr[i];

    vector<int> dp(t, 1);

    //    c[i]: Smallest LAST elem of a LIS seq with lenghth i
    vector<int> c(1, arr[0]);

    int ret = 1;
    for (int i = 1; i < t; i++)
    {
        if (arr[i] <= c[0])
        {
            c[0] = arr[i];
            dp[i] = 1;
        }
        else if (arr[i] >= c.back())
        {
            c.push_back(arr[i]);
            dp[i] = c.size();
        }
        else
        {
            int k = std::lower_bound(c.begin(), c.end(), arr[i]) - c.begin();
            c[k] = arr[i];
            dp[i] = k + 1;
        }
        ret = std::max(ret, dp[i]);
    }
    cout << ret << endl;
    return 0;
}

HackerRank - "The Longest Increasing Subsequence"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4492072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!