Submission details
Task:Lista
Sender:zli0122
Submission time:2026-01-17 20:59:38 +0200
Language:C++ (C++11)
Status:READY
Result:9
Feedback
groupverdictscore
#10
#2ACCEPTED9
#30
#40
#50
#60
Test results
testverdicttimegroup
#10.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
#60.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
#110.00 s1, 4, 5, 6details
#12ACCEPTED0.00 s1, 4, 5, 6details
#130.00 s1, 4, 5, 6details
#14ACCEPTED0.05 s2, 6details
#15ACCEPTED0.06 s2, 6details
#16ACCEPTED0.06 s2, 6details
#17ACCEPTED0.07 s2, 6details
#18ACCEPTED0.04 s2, 6details
#19ACCEPTED0.06 s2, 6details
#20ACCEPTED0.00 s1, 3, 4, 5, 6details
#210.05 s3, 6details
#220.05 s3, 6details
#230.06 s3, 6details
#240.07 s3, 6details
#25ACCEPTED0.04 s3, 6details
#26ACCEPTED0.06 s3, 6details
#270.00 s4, 6details
#280.01 s4, 6details
#290.01 s4, 6details
#30ACCEPTED0.00 s2, 4, 6details
#310.01 s4, 6details
#32ACCEPTED0.01 s4, 6details
#330.04 s5, 6details
#340.04 s5, 6details
#350.06 s5, 6details
#36ACCEPTED0.03 s2, 5, 6details
#37ACCEPTED0.05 s5, 6details
#380.09 s6details
#390.08 s6details
#400.11 s6details
#41ACCEPTED0.03 s2, 5, 6details
#42ACCEPTED0.06 s5, 6details
#430.00 s1, 3, 4, 5, 6details
#44ACCEPTED0.00 s1, 2, 4, 5, 6details
#450.00 s1, 4, 5, 6details

Code

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

/*
  Problem:
    Choose exactly k indices and permute the values among those indices arbitrarily.
    Output the lexicographically smallest possible array.

  Key fact:
    For a chosen index set S, the best we can do is:
      - sort indices S
      - sort their values
      - assign smallest value to smallest index, etc.
    So we only need to find the optimal set S.
*/

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

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

    // ---- Step 1: compute suffix minimums ----
    const int INF = 1e9;
    vector<int> sufMin(n + 2, INF);
    for (int i = n; i >= 1; i--) {
        sufMin[i] = min(x[i], sufMin[i + 1]);
    }

    // ---- Step 2: find earliest index i0 that can be improved ----
    int i0 = -1;
    for (int i = 1; i <= n; i++) {
        if (sufMin[i + 1] < x[i]) {
            i0 = i;
            break;
        }
    }

    // If no improvement is possible, answer is the original array
    if (i0 == -1) {
        for (int i = 1; i <= n; i++) {
            cout << x[i] << (i == n ? '\n' : ' ');
        }
        return 0;
    }

    /*
      By the correctness argument:
        - any optimal solution must include i0
        - the remaining k-1 indices should be the smallest values from suffix (i0+1..n),
          tie-breaking by taking rightmost positions.
        - if suffix has fewer than k-1 indices, we fill from prefix (i0-1..1) right-to-left.
    */

    // ---- Step 3: bucket indices by value for the suffix ----
    // Because 1 <= x[i] <= n, we can do O(n) selection with buckets.
    vector<vector<int>> bucket(n + 1);
    for (int i = i0 + 1; i <= n; i++) {
        bucket[x[i]].push_back(i); // indices stored increasing
    }

    // We will pop from the back => rightmost occurrences first (best tie-break)
    vector<int> chosen;
    chosen.reserve(k);

    // Mandatory choice
    chosen.push_back(i0);

    int need = k - 1;

    // ---- Step 4: pick the (k-1) smallest values from the suffix ----
    for (int v = 1; v <= n && need > 0; v++) {
        auto &vec = bucket[v];
        while (!vec.empty() && need > 0) {
            int idx = vec.back(); // rightmost index with this value
            vec.pop_back();
            chosen.push_back(idx);
            need--;
        }
    }

    // ---- Step 5: if still need indices, fill from prefix right-to-left ----
    // These fillers do not spoil lexicographic optimality (see Lemma 5).
    for (int i = i0 - 1; i >= 1 && need > 0; i--) {
        chosen.push_back(i);
        need--;
    }

    // Now chosen.size() == k
    sort(chosen.begin(), chosen.end());

    // ---- Step 6: sort the values on chosen positions, assign back in index order ----
    vector<int> vals;
    vals.reserve(k);
    for (int idx : chosen) vals.push_back(x[idx]);
    sort(vals.begin(), vals.end());

    vector<int> y = x;
    for (int t = 0; t < k; t++) {
        y[chosen[t]] = vals[t];
    }

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

Test details

Test 1 (public)

Group: 1, 3, 4, 5, 6

Verdict:

input
6 3
6 5 1 4 1 3

correct output
1 5 1 4 3 6

user output
1 5 1 4 6 3

Feedback: Incorrect character on line 1 col 9: expected "3", got "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:

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 9 4 7 5 6 9 9 10 8

Feedback: Incorrect character on line 1 col 3: expected "6", got "9"

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 10 2 3 4 5 6 9 8 9

Feedback: Incorrect character on line 1 col 3: expected "2", 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:

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 9 2 3 4 5 6 7 8 10

Feedback: Incorrect character on line 1 col 3: expected "2", got "9"

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:

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 8 2 3 4 5 6 7 9

Feedback: Incorrect character on line 1 col 3: expected "2", got "8"

Test 14

Group: 2, 6

Verdict: ACCEPTED

input
200000 2
176369 57172 92603 196271 1967...

correct output
1155 57172 92603 196271 196768...

user output
1155 57172 92603 196271 196768...

Test 15

Group: 2, 6

Verdict: ACCEPTED

input
200000 2
188653 156245 40967 173336 185...

correct output
57 156245 40967 173336 185896 ...

user output
57 156245 40967 173336 185896 ...

Test 16

Group: 2, 6

Verdict: ACCEPTED

input
200000 2
170455 14692 60230 38375 31037...

correct output
20 14692 60230 38375 31037 395...

user output
20 14692 60230 38375 31037 395...

Test 17

Group: 2, 6

Verdict: ACCEPTED

input
200000 2
59289 119695 145821 16906 1149...

correct output
1 119695 145821 16906 114932 1...

user output
1 119695 145821 16906 114932 1...

Test 18

Group: 2, 6

Verdict: ACCEPTED

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
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 19

Group: 2, 6

Verdict: ACCEPTED

input
200000 2
200000 199999 199998 199997 19...

correct output
1 199999 199998 199997 199996 ...

user output
1 199999 199998 199997 199996 ...

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
390 7587 176209 27489 170275 3...

Feedback: Incorrect character on line 1 col 10: expected "66357", got "176209"

Test 22

Group: 3, 6

Verdict:

input
200000 3
93946 193045 25177 150263 1482...

correct output
205 93946 25177 150263 148229 ...

user output
205 193045 25177 150263 148229...

Feedback: Incorrect character on line 1 col 5: expected "93946", got "193045"

Test 23

Group: 3, 6

Verdict:

input
200000 3
81262 22620 25235 22620 10144 ...

correct output
6 22620 25235 22620 10144 2614...

user output
6 22620 25235 22620 10144 2614...

Feedback: Incorrect character on line 1 col 33: expected "81262", got "182252"

Test 24

Group: 3, 6

Verdict:

input
200000 3
62925 65929 74691 187894 13817...

correct output
1 62925 74691 187894 138170 15...

user output
1 65929 74691 187894 138170 15...

Feedback: Incorrect character on line 1 col 4: expected "62925", got "65929"

Test 25

Group: 3, 6

Verdict: ACCEPTED

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
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Test 26

Group: 3, 6

Verdict: ACCEPTED

input
200000 3
200000 199999 199998 199997 19...

correct output
1 199999 199998 199997 199996 ...

user output
1 199999 199998 199997 199996 ...

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
1 510 463 644 1429 1108 1531 8...

Feedback: Incorrect character on line 1 col 3: expected "2", got "510"

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
1 1024 2 1448 3 4 5 1866 1055 ...

Feedback: Incorrect character on line 1 col 3: expected "2", got "1024"

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
1 2 3 4 5 1930 6 7 8 9 10 11 1...

Feedback: Incorrect character on line 1 col 11: expected "6", got "1930"

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
1 1999 1998 1997 1996 1995 199...

Feedback: Incorrect character on line 1 col 3: expected "2", got "1999"

Test 32

Group: 4, 6

Verdict: ACCEPTED

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
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

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
1 4 2 6 7 2 9 2 10 9 4 1 1 3 1...

Feedback: Incorrect character on line 1 col 3: expected "1", got "4"

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
1 7 2 6 1 9 7 2 4 10 1 4 4 1 9...

Feedback: Incorrect character on line 1 col 3: expected "1", got "7"

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
1 1 1 1 1 1 10 10 1 10 1 1 1 1...

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

Test 36

Group: 2, 5, 6

Verdict: ACCEPTED

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
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

Test 37

Group: 5, 6

Verdict: ACCEPTED

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
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

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
1 41607 101924 180578 132057 1...

Feedback: Incorrect character on line 1 col 3: expected "2", got "41607"

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
1 90759 102500 164610 20009 34...

Feedback: Incorrect character on line 1 col 3: expected "2", got "90759"

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
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Feedback: Incorrect character on line 1 col 112: expected "41", got "192180"

Test 41

Group: 2, 5, 6

Verdict: ACCEPTED

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
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

Test 42

Group: 5, 6

Verdict: ACCEPTED

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
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

Test 43

Group: 1, 3, 4, 5, 6

Verdict:

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 10 10 10 6 8

Feedback: Incorrect character on line 1 col 11: expected "8", got "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:

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 5 2 7 2 2 4 2

Feedback: Incorrect character on line 1 col 7: expected "2", got "5"