Cod sursa(job #1801153)

Utilizator Y0da1NUME JMECHER Y0da1 Data 8 noiembrie 2016 18:29:09
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int N;
long long S, v[101];
struct secv{
    int a, b, c, s;

    bool operator < ( secv & str)
    {
        return s < str.s;
    }
};
secv L[1000001];
int cautbin(int l,int r,int x)
{
    int mid;
    while(l<=r)
    {
        mid = (l + r)/2;
        if(L[mid].s == x)
            return mid;
        if(L[mid].s < x)
            l = mid + 1;
        else
            r = mid - 1;
    }
    return 0;
}
int main ()
{
   // 100 de kile, ruleaza pa rotile, si din spate mi-i rau
    ifstream in ("loto.in");
    ofstream out ("loto.out");

    int i, j, k, c, caut;
    in>>N>>S;
    for(i=1;i<=N;++i)
        in>>v[i];

    for(int i = 1; i <= N; ++i)
        for(int j = i; j <= N; ++j)
            for(int k = j; k <= N; ++k)
            {
                L[c].s = v[i] + v[j] + v[k];
                L[c].a = v[i];
                L[c].b = v[j];
                L[c].c = v[k];
                ++c;
            }
    sort(L + 1, L + c + 1);

    for (i=1;i<=c;++i)
    {
        caut=cautbin(1,c,S - L[i].s);
        if(caut)
        {
            out<<L[i].a<<" "<<L[i].b<<" "<<L[i].c<<" "<<L[caut].a<<" "<<L[caut].b<<" "<<L[caut].c;
            return 0;
        }
    }
    out<<"-1";
    return 0;

}