Cod sursă (job #810307)

Utilizator avatar bogdan_ Goian Bogdan bogdan_ IP ascuns
Problemă Domino Compilator cpp-32 | 1,27 kb
Rundă gogdan_percic_battle Status evaluat
Dată 21 feb. 2025 02:30:15 Scor 88
#include <bits/stdc++.h>
using namespace std;
ifstream fin("domino.in"); 
ofstream fout("domino.out");

int main() {
    int k, k1, k2;
    fin >> k >> k1 >> k2;
    
    vector<pair<int, int>> p(k);
    for (int i = 0; i < k; i++) {
        fin >> p[i].first >> p[i].second;
    }
    
    int m = k - k2, c = 0;
    string result = "";

    for (int pos = 0; pos < m; pos++) {
        int last = k - (m - pos - 1), bestIndex = -1;
        pair<int, int> bestPair = {-1, -1};

        for (int j = c; j < last; j++) {
            pair<int, int> candidate = p[j];

            if (k1 > 0 && p[j].second > p[j].first) {
                candidate = {p[j].second, p[j].first};
            }

            if (candidate > bestPair) {
                bestPair = candidate;
                bestIndex = j;
            }
        }

        if (bestIndex == -1) continue; // Sa evitam accesul invalid

        if (k1 > 0 && p[bestIndex].second > p[bestIndex].first) {
            result += to_string(p[bestIndex].second) + to_string(p[bestIndex].first);
            k1--;
        } else {
            result += to_string(p[bestIndex].first) + to_string(p[bestIndex].second);
        }

        c = bestIndex + 1;
    }

    fout << result << "\n";
    return 0;
}