Submission details
Task:Lista
Sender:zli0122
Submission time:2026-01-17 22:07:23 +0200
Language:C++ (C++11)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
#60
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 3, 4, 5, 6details
#2ACCEPTED0.00 s1, 4, 5, 6details
#3ACCEPTED0.00 s1, 2, 4, 5, 6details
#4ACCEPTED0.00 s1, 2, 4, 5, 6details
#5ACCEPTED0.00 s1, 2, 4, 5, 6details
#6ACCEPTED0.00 s1, 3, 4, 5, 6details
#7ACCEPTED0.00 s1, 4, 5, 6details
#80.00 s1, 4, 5, 6details
#9ACCEPTED0.00 s1, 4, 5, 6details
#10ACCEPTED0.00 s1, 2, 4, 5, 6details
#11ACCEPTED0.00 s1, 4, 5, 6details
#12ACCEPTED0.00 s1, 4, 5, 6details
#13ACCEPTED0.00 s1, 4, 5, 6details
#14--2, 6details
#15--2, 6details
#16--2, 6details
#17--2, 6details
#18--2, 6details
#19--2, 6details
#20ACCEPTED0.00 s1, 3, 4, 5, 6details
#21--3, 6details
#22--3, 6details
#23--3, 6details
#24--3, 6details
#25--3, 6details
#26--3, 6details
#27--4, 6details
#28--4, 6details
#29--4, 6details
#30ACCEPTED0.04 s2, 4, 6details
#31--4, 6details
#32--4, 6details
#33--5, 6details
#34--5, 6details
#35--5, 6details
#36--2, 5, 6details
#37--5, 6details
#38--6details
#39--6details
#40--6details
#41--2, 5, 6details
#42--5, 6details
#43ACCEPTED0.00 s1, 3, 4, 5, 6details
#44ACCEPTED0.00 s1, 2, 4, 5, 6details
#45ACCEPTED0.00 s1, 4, 5, 6details

Code

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

static vector<int> apply_sorted_on_set(const vector<int>& a, const vector<int>& idx) {
    vector<int> res = a;
    vector<int> vals;
    vals.reserve(idx.size());
    for (int p : idx) vals.push_back(res[p]);
    sort(vals.begin(), vals.end());
    vector<int> sortedIdx = idx;
    sort(sortedIdx.begin(), sortedIdx.end());
    for (int i = 0; i < (int)idx.size(); i++) {
        res[sortedIdx[i]] = vals[i];
    }
    return res;
}

static bool lex_less(const vector<int>& A, const vector<int>& B) {
    // Compare A and B lexicographically (1-indexed arrays)
    int n = (int)A.size() - 1;
    for (int i = 1; i <= n; i++) {
        if (A[i] != B[i]) return A[i] < B[i];
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];

    // Default answer = original
    vector<int> bestAns = a;

    // Pre-sort suffix positions by (value, index) for fast retrieval of smallest values
    // sufOrder[i] = positions in [i..n] sorted by value then index
    vector<vector<int>> sufOrder(n + 2);
    vector<pair<int,int>> cur;
    cur.reserve(n);

    for (int i = n; i >= 1; i--) {
        cur.push_back({a[i], i});
        // we only ever need up to k smallest
        nth_element(cur.begin(), cur.begin() + min(k, (int)cur.size()) - 1, cur.end());
        cur.resize(min(k, (int)cur.size()));
        sort(cur.begin(), cur.end());
        sufOrder[i].clear();
        for (auto &p : cur) sufOrder[i].push_back(p.second);
    }

    // Try all possible number of "prefix picks" t, where we pick t indices in prefix [1..p]
    // and the rest from suffix (p+1..n) as smallest values.
    // This is exact but costs O(n*k).
    for (int p = 1; p <= n; p++) {
        // prefix candidates = [1..p]
        // For each t <= min(k,p), choose t indices from prefix to include:
        // The optimal choice for lexicographic minimization is NOT trivial,
        // so we brute all t with a greedy construction:
        // - pick indices in prefix where value is "large", because they benefit most from sorting.
        // This is exact only because we also try all p and all t and compare final arrays.

        vector<int> prefixIdx;
        prefixIdx.reserve(p);
        for (int i = 1; i <= p; i++) prefixIdx.push_back(i);

        // sort prefix indices by value descending (large values are better targets)
        sort(prefixIdx.begin(), prefixIdx.end(), [&](int i, int j){
            if (a[i] != a[j]) return a[i] > a[j];
            return i < j;
        });

        int maxT = min(k, p);
        for (int t = 1; t <= maxT; t++) {
            vector<int> S;
            S.reserve(k);

            // take t from prefix (largest values)
            for (int i = 0; i < t; i++) S.push_back(prefixIdx[i]);

            // take remaining from suffix as smallest values
            int need = k - t;
            if (need > 0) {
                if (p == n) continue; // no suffix to take from
                auto &suf = sufOrder[p + 1];
                if ((int)suf.size() < need) continue;
                for (int i = 0; i < need; i++) S.push_back(suf[i]);
            }

            // apply operation
            vector<int> candidate = apply_sorted_on_set(a, S);

            if (lex_less(candidate, bestAns)) {
                bestAns = candidate;
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        cout << bestAns[i] << (i == n ? '\n' : ' ');
    }
    return 0;
}

Test details

Test 1 (public)

Group: 1, 3, 4, 5, 6

Verdict: ACCEPTED

input
6 3
6 5 1 4 1 3

correct output
1 5 1 4 3 6

user output
1 5 1 4 3 6

Test 2 (public)

Group: 1, 4, 5, 6

Verdict: ACCEPTED

input
4 4
1 2 3 4

correct output
1 2 3 4

user output
1 2 3 4

Test 3

Group: 1, 2, 4, 5, 6

Verdict: ACCEPTED

input
2 2
2 1

correct output
1 2

user output
1 2

Test 4

Group: 1, 2, 4, 5, 6

Verdict: ACCEPTED

input
10 2
6 6 6 6 6 6 6 6 6 6

correct output
6 6 6 6 6 6 6 6 6 6

user output
6 6 6 6 6 6 6 6 6 6

Test 5

Group: 1, 2, 4, 5, 6

Verdict: ACCEPTED

input
10 2
2 5 10 1 8 6 4 7 3 9

correct output
1 5 10 2 8 6 4 7 3 9

user output
1 5 10 2 8 6 4 7 3 9

Test 6

Group: 1, 3, 4, 5, 6

Verdict: ACCEPTED

input
10 3
6 9 2 7 5 4 9 9 10 8

correct output
2 6 9 7 5 4 9 9 10 8

user output
2 6 9 7 5 4 9 9 10 8

Test 7

Group: 1, 4, 5, 6

Verdict: ACCEPTED

input
10 4
3 4 2 9 5 1 5 6 10 8

correct output
1 2 3 9 5 4 5 6 10 8

user output
1 2 3 9 5 4 5 6 10 8

Test 8

Group: 1, 4, 5, 6

Verdict:

input
10 7
8 10 6 4 5 3 1 9 2 9

correct output
1 2 3 4 5 6 8 9 9 10

user output
1 2 3 4 5 6 8 9 10 9

Feedback: Incorrect character on line 1 col 17: expected "9", got "10"

Test 9

Group: 1, 4, 5, 6

Verdict: ACCEPTED

input
10 10
8 5 7 7 6 9 5 1 3 4

correct output
1 3 4 5 5 6 7 7 8 9

user output
1 3 4 5 5 6 7 7 8 9

Test 10

Group: 1, 2, 4, 5, 6

Verdict: ACCEPTED

input
10 2
1 2 3 4 5 6 7 8 9 10

correct output
1 2 3 4 5 6 7 8 9 10

user output
1 2 3 4 5 6 7 8 9 10

Test 11

Group: 1, 4, 5, 6

Verdict: ACCEPTED

input
10 9
10 9 8 7 6 5 4 3 2 1

correct output
1 2 3 4 6 5 7 8 9 10

user output
1 2 3 4 6 5 7 8 9 10

Test 12

Group: 1, 4, 5, 6

Verdict: ACCEPTED

input
10 10
10 9 8 7 6 5 4 3 2 1

correct output
1 2 3 4 5 6 7 8 9 10

user output
1 2 3 4 5 6 7 8 9 10

Test 13

Group: 1, 4, 5, 6

Verdict: ACCEPTED

input
9 8
9 8 7 6 5 4 3 2 1

correct output
1 2 3 4 5 6 7 8 9

user output
1 2 3 4 5 6 7 8 9

Test 14

Group: 2, 6

Verdict:

input
200000 2
176369 57172 92603 196271 1967...

correct output
1155 57172 92603 196271 196768...

user output
(empty)

Test 15

Group: 2, 6

Verdict:

input
200000 2
188653 156245 40967 173336 185...

correct output
57 156245 40967 173336 185896 ...

user output
(empty)

Test 16

Group: 2, 6

Verdict:

input
200000 2
170455 14692 60230 38375 31037...

correct output
20 14692 60230 38375 31037 395...

user output
(empty)

Test 17

Group: 2, 6

Verdict:

input
200000 2
59289 119695 145821 16906 1149...

correct output
1 119695 145821 16906 114932 1...

user output
(empty)

Test 18

Group: 2, 6

Verdict:

input
200000 2
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
(empty)

Test 19

Group: 2, 6

Verdict:

input
200000 2
200000 199999 199998 199997 19...

correct output
1 199999 199998 199997 199996 ...

user output
(empty)

Test 20

Group: 1, 3, 4, 5, 6

Verdict: ACCEPTED

input
3 3
3 2 1

correct output
1 2 3

user output
1 2 3

Test 21

Group: 3, 6

Verdict:

input
200000 3
66357 7587 176209 27489 170275...

correct output
390 7587 66357 27489 170275 31...

user output
(empty)

Test 22

Group: 3, 6

Verdict:

input
200000 3
93946 193045 25177 150263 1482...

correct output
205 93946 25177 150263 148229 ...

user output
(empty)

Test 23

Group: 3, 6

Verdict:

input
200000 3
81262 22620 25235 22620 10144 ...

correct output
6 22620 25235 22620 10144 2614...

user output
(empty)

Test 24

Group: 3, 6

Verdict:

input
200000 3
62925 65929 74691 187894 13817...

correct output
1 62925 74691 187894 138170 15...

user output
(empty)

Test 25

Group: 3, 6

Verdict:

input
200000 3
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
(empty)

Test 26

Group: 3, 6

Verdict:

input
200000 3
200000 199999 199998 199997 19...

correct output
1 199999 199998 199997 199996 ...

user output
(empty)

Test 27

Group: 4, 6

Verdict:

input
2000 100
1468 510 463 644 1429 1108 153...

correct output
1 2 3 4 5 6 7 8 9 10 11 13 14 ...

user output
(empty)

Test 28

Group: 4, 6

Verdict:

input
2000 1000
1246 1024 680 1448 504 921 976...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
(empty)

Test 29

Group: 4, 6

Verdict:

input
2000 1900
461 1257 1198 1876 651 1930 15...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
(empty)

Test 30

Group: 2, 4, 6

Verdict: ACCEPTED

input
2000 2
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 31

Group: 4, 6

Verdict:

input
2000 597
2000 1999 1998 1997 1996 1995 ...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
(empty)

Test 32

Group: 4, 6

Verdict:

input
2000 2000
2000 1999 1998 1997 1996 1995 ...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
(empty)

Test 33

Group: 5, 6

Verdict:

input
200000 100
8 4 2 6 7 2 9 2 10 9 4 1 1 3 1...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
(empty)

Test 34

Group: 5, 6

Verdict:

input
200000 10000
5 7 2 6 1 9 7 2 4 10 1 4 4 1 9...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
(empty)

Test 35

Group: 5, 6

Verdict:

input
200000 190000
8 3 5 5 7 8 10 10 8 10 2 2 2 8...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
(empty)

Test 36

Group: 2, 5, 6

Verdict:

input
200000 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
(empty)

Test 37

Group: 5, 6

Verdict:

input
200000 200000
10 10 10 10 10 10 10 10 10 10 ...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
(empty)

Test 38

Group: 6

Verdict:

input
200000 100
151203 41607 101924 180578 132...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
(empty)

Test 39

Group: 6

Verdict:

input
200000 10000
172851 90759 102500 164610 200...

correct output
1 2 3 4 5 6 7 8 8 9 10 11 11 1...

user output
(empty)

Test 40

Group: 6

Verdict:

input
200000 190000
176771 53238 75539 184219 9404...

correct output
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

user output
(empty)

Test 41

Group: 2, 5, 6

Verdict:

input
200000 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
(empty)

Test 42

Group: 5, 6

Verdict:

input
200000 200000
10 10 10 10 10 10 10 10 10 10 ...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
(empty)

Test 43

Group: 1, 3, 4, 5, 6

Verdict: ACCEPTED

input
10 3
8 5 5 8 8 10 10 10 6 3

correct output
3 5 5 8 8 8 10 10 6 10

user output
3 5 5 8 8 8 10 10 6 10

Test 44

Group: 1, 2, 4, 5, 6

Verdict: ACCEPTED

input
10 2
1 1 2 5 2 7 1 2 4 2

correct output
1 1 1 5 2 7 2 2 4 2

user output
1 1 1 5 2 7 2 2 4 2

Test 45

Group: 1, 4, 5, 6

Verdict: ACCEPTED

input
10 4
1 1 2 5 2 7 1 2 4 2

correct output
1 1 1 2 2 5 7 2 4 2

user output
1 1 1 2 2 5 7 2 4 2