제출 #491773

#제출 시각아이디문제언어결과실행 시간메모리
491773VodkaInTheJarMoney (IZhO17_money)C++14
45 / 100
1528 ms55828 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]; 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...