Cod sursa(job #2612430)

Utilizator Horia14Horia Banciu Horia14 Data 8 mai 2020 23:31:41
Problema Loto Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.68 kb
#include<cstdio>
#include<unordered_map>
#include<vector>
#include<cctype>
#define BUF_SIZE 1 << 9
#define MAX_N 100
using namespace std;

struct elem {
    int v1, v2, v3;
};

unordered_map<int, pair<bool, elem>> umap;
int v[MAX_N];
int n, S, pos = BUF_SIZE;
char buf[BUF_SIZE];

inline char getChar(FILE* fin) {
    if(pos == BUF_SIZE) {
        fread(buf, 1, BUF_SIZE, fin);
        pos = 0;
    }
    return buf[pos++];
}

inline int read(FILE* fin) {
    int res = 0;
    char ch;
    do {
        ch = getChar(fin);
    }while(!isdigit(ch));
    do {
        res = 10*res + ch - '0';
        ch = getChar(fin);
    }while(isdigit(ch));
    return res;
}

int main() {
    FILE* fin, *fout;
    fin = fopen("loto.in", "r");
    fout = fopen("loto.out", "w");
    elem e;
    int x;
    n = read(fin);
    S = read(fin);
    for(int i = 0; i < n; ++i)
        v[i] = read(fin);

    for(int i = 0; i < n; ++i)
        for(int j = i; j < n; ++j)
            for(int k = j; k < n; ++k) {
                e.v1 = v[i];
                e.v2 = v[j];
                e.v3 = v[k];
                umap[v[i] + v[j] + v[k]] = make_pair(true, e);
            }

    for(int i = 0; i < n; ++i)
        for(int j = i; j < n; ++j)
            for(int k = j; k < n; ++k) {
                x = S - (v[i] + v[j] + v[k]);
                if(umap[x].first) {
                    fprintf(fout,"%d %d %d ", v[i], v[j], v[k]);
                    e = umap[x].second;
                    fprintf(fout,"%d %d %d\n", e.v1, e.v2, e.v3);
                    fclose(fin);
                    fclose(fout);
                    return 0;
                }
            }

    fprintf(fout,"-1\n");
    fclose(fin);
    fclose(fout);
    return 0;
}