Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- #include <assert.h>
- using namespace std;
- const long double log_2 = 1.584962500721156;
- vector<int> power[4];
- vector<int> ans, a, parent;
- bool check(int pos, int cnt)
- {
- bool f = true;
- for (int i = pos - cnt + 1; i < pos; i++)
- if (a[i] < a[i + 1])f = false;
- if (f) return true;
- f = true;
- for (int i = pos - cnt + 1; i < pos; i++)
- if (a[i] > a[i + 1])f = false;
- return f;
- }
- bool comp(int i, int j)
- {
- int x1 = power[2][i], x2 = power[2][i - j];
- int y1 = power[3][i], y2 = power[3][i - j];
- if (j == 2) x2++;
- else y2++;
- if (x1 < x2 && y1 < y2)
- return true;
- if (x1 >= x2 && y1 >= y2)
- return false;
- int t = min(x1, x2);
- x1 -= t; x2 -= t;
- t = min(y1, y2);
- y1 -= t; y2 -= t;
- if (x1 == 0) return y1 * log_2 < x2;
- else return x1 < log_2 * y2;
- }
- int main()
- {
- //freopen("monotone.in", "r", stdin);
- //freopen("monotone.out", "w", stdout);
- int n;
- cin >> n;
- assert( 1 <= n && n <= 0.5e5);
- a.resize(n + 1);
- for (int i = 1; i <= n; i++)
- {
- cin >> a[i];
- assert(0 <= a[i] && a[i] <= 1e9);
- }
- parent.resize(n + 1);
- power[2].resize(n + 1);
- power[3].resize(n + 1);
- power[2][2] = 1;
- for (int i = 3; i <= n; i++)
- {
- power[3][i] = power[3][i - 1]; power[2][i] = power[2][i - 1];
- parent[i] = i - 1;
- for (int j = 2; j < 4; j++)
- if (check(i, j))
- if (comp(i, j))
- {
- power[5 - j][i] = power[5 - j][i - j];
- power[j][i] = power[j][i - j] + 1;
- parent[i] = i - j;
- }
- }
- int t = n;
- while (t > 0)
- {
- ans.push_back(t);
- t = parent[t];
- }
- reverse(ans.begin(), ans.end());
- cout << ans.size() << "\n";
- for (int i = 0; i < ans.size(); i++)
- cout << ans[i] << ((i == ans.size() - 1) ? "\n" : " ");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement