Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I can't tell you what it really is,
- // I can only tell you what it feels like.
- #include "bits/stdc++.h"
- using namespace std;
- #define int long long
- #define F first
- #define S second
- #define rep(i,a,n) for (int i = a; i <= n; ++i)
- #define fo(i, n) for (int i = 1; i <= n; ++i)
- #define all(v) v.begin(), v.end()
- #define pb push_back
- #define P pair < int, int >
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
- #define reno(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
- void err(istream_iterator<string> it) {}
- template<typename T, typename... Args>
- void err(istream_iterator<string> it, T a, Args... args) {
- cout << *it << " = " << a << ' ';
- err(++it, args...);
- }
- const int N = 1e3 + 5;
- int v[N], pre[N];
- vector < int > let;
- void fun(int x) {
- if(x == -1)
- return;
- fun(pre[x]);
- cout << v[x] << ' ';
- }
- signed main(){
- fast;
- int n;
- cin >> n;
- fo(i, n)
- cin >> v[i];
- fo(i, n) {
- if(let.empty()) {
- let.pb(i);
- pre[i] = -1;
- }
- else if(v[i] < v[let[0]]){
- let[0] = i;
- pre[i] = -1;
- }
- else if(v[i] > v[let.back()]) {
- pre[i] = let.back();
- let.pb(i);
- }
- else {
- int lo = 0, hi = let.size(), mid;
- while(lo <= hi) {
- mid = lo + hi >> 1;
- if(v[let[mid]] >= v[i])
- hi = mid - 1;
- else
- lo = mid + 1;
- }
- let[hi + 1] = i;
- if(hi + 1)
- pre[let[hi+1]] = let[hi];
- }
- }
- fun(let.back());
- return 0;
- }
Add Comment
Please, Sign In to add comment