HackerRank - "The Longest Increasing Subsequence"

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;
}
时间: 2024-11-13 08:07:36

HackerRank - "The Longest Increasing Subsequence"的相关文章

中南OJ1551: Longest Increasing Subsequence Again(分块+离散化线段树)

1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 29  Solved: 15 [Submit][Status][Web Board] Description Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the

300. Longest Increasing Subsequence

Problem statement: Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there

SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19929 Description Given a sequence of N pairs of integers, find the length of the longest incre

Dynamic Programming | Set 3 (Longest Increasing Subsequence)

在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Optimal Substructure Property) 中我们已经讨论了重叠子问题和最优子结构性质,现在我们来看一个可以使用动态规划来解决的问题:最长上升子序列(Longest Increasing Subsequence(LIS)). 最长上升子序列问题,致力于在一个给定的序列中找到一个最长的子序列

LintCode Longest Increasing Subsequence

Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Clarification What's the definition of longest increasing subsequence? The longest increasing subsequence problem is to find a

Longest Increasing Subsequence

Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&&a[j]<a[i]}+1 直接两个for [二分查找优化]:O(n^2) g(i):d值为i的最小的a  每次更新然后lower_bound即可 [大于等于] lower_boundReturn iterator to lower bound Returns an iterator pointing

[LeetCode][JavaScript]Longest Increasing Subsequence

Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Not

LeetCode 300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Description Submission Solutions Add to List Total Accepted: 64115 Total Submissions: 170859 Difficulty: Medium Contributors: Admin Given an unsorted array of integers, find the length of longest increasing subsequ

【LeetCode从零单刷】Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more