이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
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;
}
map <int, int> mp;
for (int i = 1; i <= n; i++)
mp[a[i]]++;
set <int> s;
for (int j = 1; j <= n; j++)
s.insert(a[j]);
dp[n+1] = 0;
for (int i = n; i >= 1; i--)
{
mp[a[i]]--;
if (mp[a[i]] == 0)
s.erase(a[i]);
auto it = s.upper_bound(a[i]);
if (it == s.end())
{
dp[i] = dp[r[i] + 1] + 1;
continue;
}
int low = i, high = r[i];
while (low < high)
{
int mid = (low + high + 1) / 2;
if (a[mid] > *it)
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |