Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Useless for LIS

#include <bits/stdc++.h>
using namespace std;

void solve(vector<int> a) {
    int n = a.size();

    // dp_start[i] is the length of LIS ending at index i.
    // dp_end[i] is the length of LIS starting at index i.
    vector<int> dp_start(n, 1), dp_end(n, 1);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (a[i] > a[j]) {
                dp_end[i] = max(dp_end[i], dp_end[j] + 1);
            }
        }
    }

    for (int i = n - 1; i >= 0; i--) {
        for (int j = i + 1; j < n; j++) {
            if (a[i] < a[j]) {
                dp_start[i] = max(dp_start[i], dp_start[j] + 1);
            }
        }
    }

    int LIS = *max_element(dp_start.begin(), dp_start.end());
    vector<int> res;
    for (int i = 0; i < n; i++) {
        if (dp_start[i] + dp_end[i] - 1 == LIS) {
            res.push_back(i);
        }
    }

    cout << res.size() << "\n";
    for (int i = 0; i < (int)res.size(); i++) {
        cout << res[i] + 1 << " ";
    }
    cout << "\n";
}

void compress(vector<int> &a) {
    int n = a.size();

    map<int, int> rank;
    for (int i = 0; i < n; i++) {
        rank[a[i]] = 1;
    }

    int now = 1;
    for (auto &kv : rank) {
        rank[kv.first] = now++;
    }

    for (int i = 0; i < n; i++) {
        a[i] = rank[a[i]];
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int zz = 0; zz < t; zz++) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        compress(a);
        solve(a);
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

const int inf = 1e6;

void solve(vector<int> a) {
    int n = a.size();

    // dp_start[i] is the length of LIS ending at index i.
    // dp_end[i] is the length of LIS starting at index i.
    vector<int> dp_start(n, 1), dp_end(n, 1);
    vector<int> t(n + 1, -inf);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < a[i]; j++) {
            dp_end[i] = max(dp_end[i], t[j] + 1);
        }
        t[a[i]] = max(t[a[i]], dp_end[i]);
    }

    t.clear();
    t.resize(n + 1, -inf);
    for (int i = n - 1; i >= 0; i--) {
        for (int j = a[i] + 1; j <= n; j++) {
            dp_start[i] = max(dp_start[i], t[j] + 1);
        }
        t[a[i]] = max(t[a[i]], dp_start[i]);
    }

    int LIS = *max_element(dp_start.begin(), dp_start.end());
    vector<int> res;
    for (int i = 0; i < n; i++) {
        if (dp_start[i] + dp_end[i] - 1 == LIS) {
            res.push_back(i);
        }
    }

    cout << res.size() << "\n";
    for (int i = 0; i < (int)res.size(); i++) {
        cout << res[i] + 1 << " ";
    }
    cout << "\n";
}

void compress(vector<int> &a) {
    int n = a.size();

    map<int, int> rank;
    for (int i = 0; i < n; i++) {
        rank[a[i]] = 1;
    }

    int now = 1;
    for (auto &kv : rank) {
        rank[kv.first] = now++;
    }

    for (int i = 0; i < n; i++) {
        a[i] = rank[a[i]];
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int zz = 0; zz < t; zz++) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        compress(a);
        solve(a);
    }
    return 0;
}
#include <atcoder/segtree>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

const int inf = 1e6;

int op(int a, int b) { return max(a, b); }

int e() { return -inf; }

void solve(vector<int> a) {
    int n = a.size();

    // dp_start[i] is the length of LIS ending at index i.
    // dp_end[i] is the length of LIS starting at index i.
    vector<int> dp_start(n, 1), dp_end(n, 1);
    segtree<int, op, e> t(n + 5);
    for (int i = 0; i < n; i++) {
        dp_end[i] = max(dp_end[i], t.prod(0, a[i]) + 1);
        t.set(a[i], max(t.get(a[i]), dp_end[i]));
    }

    for (int i = 0; i <= n; i++) {
        t.set(i, -inf);
    }
    for (int i = n - 1; i >= 0; i--) {
        dp_start[i] = max(dp_start[i], t.prod(a[i] + 1, n + 1) + 1);
        t.set(a[i], max(t.get(a[i]), dp_start[i]));
    }

    int LIS = *max_element(dp_start.begin(), dp_start.end());
    vector<int> res;
    for (int i = 0; i < n; i++) {
        if (dp_start[i] + dp_end[i] - 1 == LIS) {
            res.push_back(i);
        }
    }

    cout << res.size() << "\n";
    for (int i = 0; i < (int)res.size(); i++) {
        cout << res[i] + 1 << " ";
    }
    cout << "\n";
}

void compress(vector<int> &a) {
    int n = a.size();

    map<int, int> rank;
    for (int i = 0; i < n; i++) {
        rank[a[i]] = 1;
    }

    int now = 1;
    for (auto &kv : rank) {
        rank[kv.first] = now++;
    }

    for (int i = 0; i < n; i++) {
        a[i] = rank[a[i]];
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int zz = 0; zz < t; zz++) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        compress(a);
        solve(a);
    }
    return 0;
}