49552023-04-07 20:38:48Valaki2Hadjáratcpp14Időlimit túllépés 64/100275ms8936 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 1e6;

int n;
vector<int> v, w;

int dp[1 + maxn];

// segtree
int tree[1 + 4 * maxn];

/*
void update(int i, int vl, int vr, int pos, int val) {
    if(vl == vr) {
        tree[i] = val;
    } else {
        int mid = (vl + vr) / 2;
        if(pos <= mid) {
            update(2 * i, vl, mid, pos, val);
        } else {
            update(2 * i + 1, mid + 1, vr, pos, val);
        }
        if(dp[tree[2 * i]] > dp[tree[2 * i + 1]]) {
            tree[i] = tree[2 * i];
        } else {
            tree[i] = tree[2 * i + 1];
        }
    }
}

int query(int i, int vl, int vr, int ql, int qr) {
    if(vr < ql || vl > qr) {
        return 0;
    }
    if(vl == ql && vr == qr) {
        return tree[i];
    } else {
        int mid = (vl + vr) / 2;
        int x = query(2 * i, vl, mid, ql, min(qr, mid)), y = query(2 * i + 1, mid + 1, vr, max(ql, mid + 1), qr);
        if(dp[x] > dp[y]) {
            return x;
        } else {
            return y;
        }
    }
}
*/

void update(int pos, int val) {
    while(pos <= maxn) {
        if(dp[tree[pos]] < dp[val]) {
            tree[pos] = val;
        }
        pos += pos & (-pos);
    }
}

void clr(int pos) {
    while(pos <= maxn) {
        tree[pos] = 0;
        pos += pos & (-pos);
    }
}

int query(int pos) {
    int res = 0;
    while(pos > 0) {
        if(dp[res] < dp[tree[pos]]) {
            res = tree[pos];
        }
        pos -= pos & (-pos);
    }
    return res;
}

int from[1 + maxn];

bool strangesrt(int i, int j) {
    return v[i] < v[j];
}

void calc(int vl, int vr) {
    //cout << "here" << " " << vl << " " << vr << endl;
    int mid = (vl + vr) / 2;
    if(vl == vr) {
        if(dp[vl] == 0) {
            dp[vr] = 1;
            from[vl] = from[vr] = 0;
        }
        return;
    }
    calc(vl, mid);
    vector<int> a, b;
    for(int i = vl; i <= mid; i++) {
        a.pb(i);
    }
    for(int i = mid + 1; i <= vr; i++) {
        b.pb(i);
    }
    sort(a.begin(), a.end(), strangesrt);
    sort(b.begin(), b.end(), strangesrt);
    int ptr = 0;
    vector<int> to_remove = a;
    for(int idx : b) {
        while(ptr < a.size() && v[a[ptr]] < v[idx]) {
            update(w[a[ptr]], a[ptr]);
            ptr++;
        }
        int prv = query(w[idx] - 1);
        if(dp[idx] < dp[prv] + 1) {
            //cout << idx << " " << prv << "\n";
            dp[idx] = dp[prv] + 1;
            from[idx] = prv;
        }
        // to_remuuv :)
    }
    for(int idx : to_remove) {
        clr(w[idx]);
    }
    calc(mid + 1, vr);
}

void print(vector<int> &x) {
    for(int &y : x) {
        cout << y << " ";
    }
    cout << "\n";
}

void solve() {
    cin >> n;
    v.assign(1 + n, 0ll);
    w.assign(1 + n, 0ll);
    for(int i = 1; i <= n; i++) {
        cin >> v[i] >> w[i];
    }
    auto vcomp = v, wcomp = w;
    sort(vcomp.begin(), vcomp.end());
    sort(wcomp.begin(), wcomp.end());
    vcomp.resize(unique(vcomp.begin(), vcomp.end()) - vcomp.begin());
    wcomp.resize(unique(wcomp.begin(), wcomp.end()) - wcomp.begin());
    //print(vcomp);
    //print(wcomp);
    for(int i = 1; i <= n; i++) {
        v[i] = (lower_bound(vcomp.begin(), vcomp.end(), v[i]) - vcomp.begin());
        w[i] = (lower_bound(wcomp.begin(), wcomp.end(), w[i]) - wcomp.begin());
        //cout << v[i] << " " << w[i] << "\n";
    }
    //cout << "here" << endl;
    calc(1, n);
    /*for(int i = 1; i <= n; i++) {
        cout << dp[i] << " ";
    }
    cout << "\n";*/
    int best = 0;
    for(int i = 1; i <= n; i++) {
        if(best == 0 || dp[best] < dp[i]) {
            best = i;
        }
    }
    cout << dp[best] << "\n";
    stack<int> s;
    while(best != 0) {
        s.push(best);
        best = from[best];
    }
    while(!s.empty()) {
        cout << s.top() << " ";
        s.pop();
    }
    cout << "\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base64/100
1Elfogadva0/03ms2008 KiB
2Elfogadva0/0172ms6232 KiB
3Elfogadva4/43ms2540 KiB
4Elfogadva4/43ms2660 KiB
5Elfogadva4/43ms2880 KiB
6Elfogadva4/43ms3088 KiB
7Elfogadva4/43ms3120 KiB
8Elfogadva4/43ms3308 KiB
9Elfogadva4/43ms3488 KiB
10Elfogadva4/44ms3540 KiB
11Elfogadva4/414ms3840 KiB
12Elfogadva4/429ms4396 KiB
13Elfogadva6/629ms4592 KiB
14Elfogadva6/661ms5600 KiB
15Elfogadva6/697ms6556 KiB
16Elfogadva6/6172ms8156 KiB
17Időlimit túllépés0/6212ms8936 KiB
18Időlimit túllépés0/6252ms6600 KiB
19Időlimit túllépés0/6244ms6892 KiB
20Időlimit túllépés0/6275ms7228 KiB
21Időlimit túllépés0/6263ms7196 KiB
22Időlimit túllépés0/6228ms6808 KiB