nowise (Codeforces Round 1018, Div. 1 + Div. 2)B — Wonderful Gloves logic by me and proof

Правка en1, от samra_seher_12, 2025-04-20 18:35:14

The adversary can force you to draw all of the larger supplies of left or right gloves for every color before you manage to form a single matching pair, so you first need to draw the total obtained by adding, for each color, whichever is larger between its left glove count and its right glove count. At that point you might still have zero matches. Drawing one more glove guarantees that you form your first matching pair. To collect k matching pairs of different colors, after securing the first pair you must likewise force the remaining k minus one colors; each such color requires drawing additional gloves equal to whichever is smaller between its left glove count and its right glove count. Therefore, the minimum number of gloves you must draw is the sum of all the larger counts, plus the sum of the k minus one largest smaller counts, plus one additional glove. solution :

include <bits/stdc++.h>

using namespace std; using ll = long long;

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

int T;
cin >> T;
while (T--) {
    int n, k;
    cin >> n >> k;
    vector<ll> l(n), r(n);
    for (int i = 0; i < n; i++) cin >> l[i];
    for (int i = 0; i < n; i++) cin >> r[i];


    vector<ll> m(n);
    ll sum_max = 0;
    for (int i = 0; i < n; i++) {
        m[i] = min(l[i], r[i]);
        sum_max += max(l[i], r[i]);
    }

    sort(m.begin(), m.end(), greater<ll>());
    ll sum_m = 0;
    for (int i = 0; i < k-1; i++) {
        sum_m += m[i];
    }

    cout << (sum_max + sum_m + 1) << "\n";
}

return 0;

}

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский samra_seher_12 2025-04-20 18:35:14 1758 This is my code and logic for Problem B; I’m only posting it because the system happened to remind me by coincidence this is from last round Neowise Labs Contest 1 (Codeforces Round 1018, Div. 1 + Div. 2) (published)