Cod sursă (job #810188)

Utilizator avatar x_____Robert___x Nani Robert x_____Robert___x IP ascuns
Problemă Domino Compilator cpp-32 | 2.08 kb
Rundă lasm_20_02_2025_clasa11 Status evaluat
Dată 20 feb. 2025 19:30:40 Scor 84
#include <fstream>
#include <vector>
#include <string>
using namespace std;

struct Domino {
    int a, b; 
};

int main(){
    ifstream fin("domino.in");
    ofstream fout("domino.out");

    int N, K1, K2;
    fin >> N >> K1 >> K2;
    
    
    vector<Domino> pieces(N);
    for (int i = 0; i < N; i++){
        fin >> pieces[i].a >> pieces[i].b;
    }
    
   
    int L = N - K2;
    int pos = 0;        
    int rotations = K1; 

   
    vector<pair<Domino, bool>> chosen;
    chosen.reserve(L);
    
    
    for (int count = 0; count < L; count++){
        
        int allowed_end = N - (L - count);
        int best_index = -1;
        int best_value = -1;   
        int best_cost = 2;     
        bool best_rotated = false;
        
        
        for (int i = pos; i <= allowed_end; i++){
            int cost = 0;
            int orig = pieces[i].a * 10 + pieces[i].b;
            int candidate = orig;
            bool useRot = false;
           
            if (rotations > 0) {
                int rotVal = pieces[i].b * 10 + pieces[i].a;
                if (rotVal > candidate) {
                    candidate = rotVal;
                    cost = 1;
                    useRot = true;
                }
            }
           
            if (candidate > best_value || (candidate == best_value && cost < best_cost)){
                best_value = candidate;
                best_cost = cost;
                best_index = i;
                best_rotated = useRot;
            }
        }
 
        chosen.push_back({pieces[best_index], best_rotated});
        rotations -= best_cost; 
        pos = best_index + 1;    
    }
    
    string result;
    result.reserve(chosen.size() * 2);
    for (auto &entry : chosen) {
        Domino d = entry.first;
        if (entry.second) {
            result.push_back('0' + d.b);
            result.push_back('0' + d.a);
        } else {
            result.push_back('0' + d.a);
            result.push_back('0' + d.b);
        }
    }
    
    fout << result << "\n";
    return 0;
}