yosupo06 / library-checker-problems

The problem data (Test case generator, judge's solution, task, ...) of Library Checker
https://judge.yosupo.jp/
Apache License 2.0
527 stars 120 forks source link

[Problem proposal] (Lexicographically smallest after removing at most `k` elements from list) #1227

Open guoyiz23 opened 2 months ago

guoyiz23 commented 2 months ago

Problem name: Lexicographically smallest post-removal

Problem

Given a list $a$ of integers, return the lexigraphically smallest list after removing at most $K$ elements.

Constraint

Solution / Reference

Based on https://atcoder.jp/contests/abc262/editorial/4531

template <typename T> 
vector<T> lexicographically_smallest_post_remove(const vector<T>& a, int num_to_remove) {
    vector<T> res;
    if (num_to_remove >= int(a.size())) return res;
    auto expected_sz{int(a.size()) - num_to_remove};
    for (auto i{0}; i < int(a.size()); i++) {
        while (num_to_remove > 0 && !res.empty() && res.back() > a[i]) {
            res.pop_back();
            num_to_remove--;
        }
        if (int(res.size()) < expected_sz) {
            res.push_back(a[i]);
        }
    }

    return res;
};

Note

There may already be an item that tests this but it is not straightforward to find. Thus, I thought that on the site, one could add keywords for each of the items in the "Problem List" and a corresponding search feature. A keyword for this item would be "lexicographic".

maspypy commented 2 months ago

Thank you for suggesting the problem. Regarding the problem title, I think something like "lexicographically smallest subsequence" would sound more natural.

As for the search tags, we currently have no plans to implement them.