Cod sursa(job #1562203)

Utilizator retrogradLucian Bicsi retrograd Data 4 ianuarie 2016 21:34:18
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <cstdio>
#include <algorithm>
#include <iostream>

using namespace std;

int X[1000001], V[101], nodes;

int main() {
    int n, s;

//    freopen("loto.in", "r", stdin);
//    freopen("loto.out", "w", stdout);

    cin >> n >> s;
    for(int i=1; i<=n; i++)
        cin >> V[i];

    for(int i=1; i<=n; i++)
    for(int j=1; j<=n; j++)
    for(int k=1; k<=n; k++) {
        X[++nodes] = V[i] + V[j] + V[k];
    }

    sort(X + 1, X + nodes + 1);

    int j = nodes;
    for(int i = 1; i <= nodes; i += 1) {
        while(X[i] + X[j] > s)
            if(--j == 0)
                break;

        if(j == 0) break;

        if(X[i] + X[j] == s) {

            int a = X[i], b = X[j];

            bool ta = 0, tb = 0;

            for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            for(int k=1; k<=n; k++) {
                if(!ta && V[i] + V[j] + V[k] == a) {cout << i << " " << j << " " << k << " "; ta = 1;}
                if(!tb && V[i] + V[j] + V[k] == b) {cout << i << " " << j << " " << k << " "; tb = 1;}
            }

            return 0;
        }
    }

    cout << -1;
    return 0;
}