Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: C. Game on Permutation
- // Contest: Codeforces - Educational Codeforces Round 153 (Rated for Div. 2)
- // URL: https://codeforces.com/problemset/problem/1860/C
- // Memory Limit: 256 MB
- // Time Limit: 2000 ms
- //
- // Powered by CP Editor (https://cpeditor.org)
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #ifndef __DEBUG__
- #define dbg(...) 42
- #endif
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using vl = vector<ll>;
- using vi = vector<int>;
- void solve1()
- {
- ll n, ans = 0;
- cin >> n;
- vl p(n), cnt(n);
- for (auto &x : p)
- cin >> x;
- map<ll, ll> m;
- m[1e9 + 3] = 0;
- for (ll i = 0; i < n; ++i) {
- auto it = m.upper_bound(p[i]);
- if (it == m.begin()) {
- m[p[i]] = 0;
- } else {
- it = prev(it);
- m[p[i]] = it->second + 1;
- if (it->second == 0)
- ans++;
- }
- }
- cout << ans << endl;
- }
- void solve2()
- {
- ll n, ans = 0;
- cin >> n;
- vl p(n), cnt(n);
- for (auto &x : p)
- cin >> x;
- map<ll, ll> m;
- m[1e9 + 3] = 0;
- stack<ll> st;
- st.push(1e9 + 3);
- for (ll i = 0; i < n; ++i) {
- auto it = m.upper_bound(p[i]);
- if (it == m.begin()) {
- m[p[i]] = 0;
- } else {
- it = prev(it);
- m[p[i]] = it->second + 1;
- if (it->second == 0 && st.top() > p[i]) {
- ans++;
- st.push(p[i]);
- }
- }
- }
- cout << ans << endl;
- }
- void solve_editorial()
- {
- int n;
- cin >> n;
- int ans = 0;
- int mn = n + 1, mnWin = n + 1;
- while (n--) {
- int x;
- cin >> x;
- if (mn < x && x < mnWin) {
- ans += 1;
- mnWin = x;
- }
- mn = min(mn, x);
- }
- cout << ans << '\n';
- }
- void solve()
- {
- ll n, ans = 0;
- cin >> n;
- vl p(n), possible;
- for (auto &x : p)
- cin >> x;
- map<ll, ll> m;
- m[1e9 + 3] = 0;
- for (ll i = 0; i < n; ++i) {
- auto it = m.upper_bound(p[i]);
- if (it == m.begin()) {
- m[p[i]] = 0;
- } else {
- it = prev(it);
- m[p[i]] = it->second + 1;
- if (it->second == 0)
- possible.push_back(i);
- }
- }
- set<ll> s;
- for (ll i = n - 1, j = possible.size() - 1; i >= 0; --i) {
- while (j >= 0 && possible[j] >= i) {
- ll v = p[possible[j]];
- if (s.upper_bound(v) != s.end())
- ++ans;
- --j;
- }
- s.insert(p[i]);
- }
- cout << ans << endl;
- }
- int main(int argc, char **argv)
- {
- ll t;
- cin >> t;
- while (t--) {
- solve2();
- // solve_editorial();
- }
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement