Cod sursa(job #1290112)

Utilizator sulzandreiandrei sulzandrei Data 10 decembrie 2014 20:44:44
Problema Loto Scor 5
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.03 kb
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
struct asd
{
   long a,b,c,sum;
};
bool comp(asd a,asd b)
{
    return a.sum<b.sum;
}
int main()
{

    asd  s[101*101];
    ifstream fin("loto.in");
    int sum,n,v[200],nr=0;
    fin>>n>>sum;
    for(int i=1;i<=n;++i)
        fin>>v[i];
    for(int i=1;i<=n;++i)
        for(int j=i;j<=n;++j)
            for(int k=j;k<=n;++k)
            {
                s[++nr].sum=v[i]+v[j]+v[k];
                s[nr].a=v[i],s[nr].b=v[j],s[nr].c=v[k];
            }
    fin.close();
    sort(s+1,s+nr,comp);
    int high=nr,low=1;
    while(low<=high)
    {
        if((s[low].sum+s[high].sum)>sum)
            high--;
        else if(s[low].sum+s[high].sum<sum)
            low++;
        else
            break;
    }
    ofstream fout("loto.out");
    if(low>high)
        fout<<-1;
    else
        fout<<s[low].a<<" "<<s[low].b<<" "<<s[low].c<<" "<<s[high].a<<" "<<s[high].b<<" "<<s[high].c;
    fout.close();
    return 0;
}