Hướng dẫn giải của Thu Bài Thi
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
#include <bits/stdc++.h> #define int long long #define ll long long #define endl '\n' #define fi first #define se second using namespace std; const int maxn = 1e5 + 7; int a[maxn], n; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; vector<stack<int>> piles; vector<int> bottom; int first = 0, last = 0, k = n; for (int i = 0; i < n; i++) { int x = a[i]; if (x < last) { k = i; break; } int j = upper_bound(bottom.begin() + first, bottom.end(), x) - bottom.begin(); if (j == (int)piles.size()) { piles.emplace_back(); piles.back().push(x); bottom.push_back(x); continue; } if (piles[j].top() < x) { for (; first < j; first++) { while (!piles[first].empty()) { last = piles[first].top(); piles[first].pop(); } } while (piles[j].top() < x) { last = piles[j].top(); piles[j].pop(); } } piles[j].push(x); } cout << k << endl; return 0; }
Đang tải...