Cod sursa(job #1284781)

Utilizator rangerChihai Mihai ranger Data 6 decembrie 2014 20:01:57
Problema Loto Scor 30
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.21 kb
#include<fstream>
#include<algorithm>
using namespace std;

ifstream cin("loto.in");
ofstream cout("loto.out");

const  int nmax=101;

struct lol{
 int x,y,z,sum;};
lol a[nmax*nmax*nmax];

int n,s,i,j,k, b[nmax],l;

bool cmp(lol a, lol b)
{
    return a.sum < b.sum;
}

int main()
{
    cin >> n >> s;

    for (i=1;i<=n;i++) cin >> b[i];

    for (i=1;i<=n;i++)
    for (j=1;j<=n;j++)
    for (k=1;k<=n;k++){
        l++;
        a[l].x=b[i]; a[l].y=b[j]; a[l].z=b[k];
        a[l].sum=b[i]+b[j]+b[k];
    }

    sort(a+1,a+l+1,cmp);
    int gasit=0;
    for (i=1;i<=n && !gasit;i++)
    for (j=1;j<=n && !gasit;j++)
    for (k=1;k<=n && !gasit;k++){
        int s1 = s - b[i] - b[j] - b[k];
        if (s1<0) continue;
        int beg=1, en=l,sol=-1;
        while (beg<=en && sol==-1){
            int  mij=(beg+en)/2;
            if (a[mij].sum==s1) sol=mij;
              else if (a[mij].sum<s1) beg=mij+1;
                else en=mij-1;
        }
        if (sol!=-1){
            gasit=1;
            cout << a[sol].x << " " << a[sol].y << " " << a[sol].z << " ";
            cout << b[i] << " " << b[j] << " " << b[k];
        }
    }
    if (!gasit) cout << -1;
    return 0;
}