Cod sursă (job #810306)

Utilizator avatar bogdan_ Goian Bogdan bogdan_ IP ascuns
Problemă Domino Compilator cpp-32 | 1,46 kb
Rundă gogdan_percic_battle Status evaluat
Dată 21 feb. 2025 02:26:58 Scor 84
#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, bestCost = 2;
        pair<int, int> bestPair = {0, 0};

        for (int j = c; j < last; j++) {
            int a = p[j].first, b = p[j].second;
            pair<int, int> candidate;
            int cost;
            
            if (k1 > 0 && b > a) {
                candidate = {b, a};
                cost = 1;
            } else {
                candidate = {a, b};
                cost = 0;
            }
            
            if (candidate > bestPair || (candidate == bestPair && cost < bestCost)) {
                bestPair = candidate;
                bestIndex = j;
                bestCost = cost;
            }
        }

        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;
}