제출 #491776

#제출 시각아이디문제언어결과실행 시간메모리
491776VodkaInTheJarMoney (IZhO17_money)C++14
100 / 100
1040 ms69772 KiB
#include <bits/stdc++.h>
#define endl '\n'

using namespace std;

const int maxn = 1e6 + 3;

int n;
int a[maxn];
void read()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
}

int dp[maxn];
int r[maxn];
int up[maxn];
void solve()
{
    r[n] = n;
    for (int i = n-1; i >= 1; i--)
    {
        if (a[i] <= a[i+1])
            r[i] = r[i+1];

        else
            r[i] = i;
    }

    set <int> s;
    for (int i = 1; i <= n; i++)
    {
        auto it = s.upper_bound(a[i]);
        if (it == s.end())
            up[i] = INT_MAX;

        else
            up[i] = *it;

        s.insert(a[i]);
    }

    dp[n+1] = 0;
    for (int i = n; i >= 1; i--)
    {
        int low = i, high = r[i];
        while (low < high)
        {
            int mid = (low + high + 1) / 2;
            if (a[mid] > up[i])
                high = mid-1;

            else
                low = mid;
        }

        dp[i] = dp[low + 1] + 1;
    }

    cout << dp[1] << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    read();
    solve();
}


/*
6
3 6 4 5 1 2
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...