这个代码为什么不超时
查看原帖
这个代码为什么不超时
1318895
HPJG楼主2024/3/27 20:56
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

int main()
{
    vector<int> h;
    for (int x = 0; cin >> x;)
    {
        h.push_back(x);
    }

    vector<int> ml(*max_element(h.begin(), h.end()) + 1, 0);

    for (auto &&i : h)
    {
        ml[i] = *max_element(ml.begin() + i, ml.end()) + 1;
    }

    int ans1 = *max_element(ml.begin(), ml.end());

    set<int> b;
    for (auto &&i : h)
    {
        auto cur = b.lower_bound(i);
        if (cur == b.end())
        {
            b.insert(i);
        }
        else
        {
            b.erase(cur);
            b.insert(i);
        }
    }

    int ans2 = b.size();

    cout << ans1 << ' ' << ans2 << endl;

    return 0;
}

这个代码是nlogn的还是n2的,如果是n2的为什么能过?

2024/3/27 20:56
加载中...