Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define task "DROPTEST"
- #include <iostream>
- #include <cstdio>
- #include <vector>
- using namespace std;
- using ll = long long;
- using ld = long double;
- constexpr int N = 1e6 + 5;
- constexpr int Inf = 1e9 + 7;
- int n, f[N];
- int a[N], b[N];
- void Read()
- {
- cin >> n;
- for (int i = 1; i <= n; ++i)
- cin >> a[i] >> b[i];
- }
- void Solve()
- {
- vector<int> s;
- int ans(0);
- a[n + 1] = b[n + 1] = -Inf;
- for (int i = 1, j = 0; i <= n + 1; ++i)
- {
- int maxn = 1;
- while (!s.empty() && a[s.back()] <= a[i])
- {
- if (j < (int)s.size())
- maxn = max(maxn, f[s.back()] + i - s.back());
- s.pop_back();
- j = min(j, (int)s.size());
- }
- s.emplace_back(i);
- while (j < (int)s.size() && a[s[j]] > b[i])
- {
- ans = max(ans, f[s[j]] + i - s[j] - 1);
- ++j;
- }
- f[i] = maxn;
- }
- cout << ans;
- }
- int32_t main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement