Cod sursa(job #2618128)

Utilizator DenisaCantuCantu Denisa DenisaCantu Data 23 mai 2020 18:44:45
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("loto.in");
ofstream g("loto.out");
struct suma
{
    int a, b, c, s;
} t[1000005];
bool com(suma x, suma y)
{
    return x.s < y.s;
}

int n,  v[101], S;
int main()
{
    f >> n >> S;
    for( int i = 1; i <= n; i++)
        f >> v[i];
    int nr = 0;
    for(int i = 1; i<= n; i++)
        for(int j = i; j <= n; j++)
            for(int k = j; k <= n; k++)
            {
                nr++;
                t[nr].a = v[i];
                t[nr].b = v[j];
                t[nr].c = v[k];
                t[nr].s = v[i] + v[j] + v[k];
            }
    sort(t+1, t+nr+1, com);
    int i = 1, j = nr;
    int ok = 0;
    while(i <= j)
    {
        if(t[i].s + t[j].s == S)
        {
            ok = 1;
            g << t[i].a << " " << t[i].b << " " << t[i].c << " " << t[j].a << " " << t[j].b << " " << t[j].c;
            break;
        }

        else if(t[i].s + t[j].s < S)
            i++;
        else
            j--;
    }
    if(ok == 0)
        g << -1;
    return 0;
}