Submission #799574

#TimeUsernameProblemLanguageResultExecution timeMemory
799574vjudge1Financial Report (JOI21_financial)C++17
65 / 100
4037 ms10212 KiB
#include <bits/stdc++.h>

#ifndef LOCAL
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#endif

typedef unsigned short u16;
typedef short i16;
typedef unsigned int u32;
typedef int i32;
typedef unsigned long long u64;
typedef long long i64;
typedef float f32;
typedef double f64;
typedef long double f80;
typedef long double f128;

#ifdef LOCAL
#include "tools.hpp"
#define here(...)                                                                \
    std::cerr << __func__ << ':' << __LINE__ << " [" << #__VA_ARGS__ << "] = ["; \
    _debug(__VA_ARGS__)
#else
#define here(...)
#endif

namespace std
{
    template <typename T, typename U>
    struct hash<pair<T, U>>
    {
        size_t operator()(const pair<T, U>& x) const { return hash<T>()(x.first) ^ hash<U>()(x.second); }
    };
}  // namespace std

struct custom_hash
{
    static u64 splitmix64(u64 x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    std::size_t operator()(u64 x) const
    {
        static const u64 rand_time = static_cast<u64>(std::chrono::steady_clock::now().time_since_epoch().count());
        return splitmix64(x + rand_time);
    }

    template <typename T, typename U>
    std::size_t operator()(const std::pair<T, U>& x) const
    {
        return splitmix64((*this)(x.first) ^ (*this)(x.second));
    }
};

template <typename T>
using limits = std::numeric_limits<T>;

void solve()
{
    u64 n, d;
    std::cin >> n >> d;
    std::vector<u64> a(n);
    for (auto& i : a)
    {
        std::cin >> i;
    }
    if (d == 1)
    {
        u64 res = 0;
        std::stack<u64> st;
        for (u64 i = n - 1; i + 1 > 0; --i)
        {
            while (!st.empty() && st.top() <= a[i])
            {
                st.pop();
            }
            st.push(a[i]);
            res = std::max<u64>(res, st.size());
        }
        std::cout << res << '\n';
        return;
    }
    if (d == n)
    {
        std::vector<u64> lis(n, 0);
        u64 len = 1;
        lis[0] = a[0];
        for (auto i = a.begin() + 1; i != a.end(); i++)
        {
            auto st = a.begin(), ed = a.begin() + len;
            auto it = std::lower_bound(st, ed, *i);
            if (it == ed)
            {
                a[len++] = *i;
            }
            else
            {
                *it = *i;
            }
        }
        std::cout << len << '\n';
        return;
    }
    std::vector<u64> upper(n);
    for (u64 i = 0; i < n; ++i)
    {
        upper[i] = i;
        for (u64 j = i + 1; j < std::min(n, upper[i] + d + 1); ++j)
        {
            if (a[j] <= a[i])
            {
                upper[i] = j;
            }
        }
    }
    std::vector<u64> dp(n, 1);
    u64 res = 0;
    for (u64 i = 0; i < n; ++i)
    {
        for (u64 j = 0; j < i; ++j)
        {
            if (a[i] > a[j] && upper[j] + d >= i)
            {
                dp[i] = std::max(dp[i], dp[j] + 1);
            }
        }
        if (upper[i] == n - 1)
        {
            res = std::max(res, dp[i]);
        }
    }
    std::cout << res << '\n';
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
#ifdef LOCAL
    std::ifstream input("./in.txt");
    std::ofstream output("./log/bsol.txt");
    auto cin_buf = std::cin.rdbuf(input.rdbuf());
    auto cout_buf = std::cout.rdbuf(output.rdbuf());
#endif
    solve();
#ifdef LOCAL
    std::cin.rdbuf(cin_buf);
    std::cout.rdbuf(cout_buf);
    input.close();
    output.close();
#endif
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...