Cod sursa(job #856991)

Utilizator mazaandreiAndrei Mazareanu mazaandrei Data 17 ianuarie 2013 09:39:27
Problema Loto Scor 55
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include<fstream>
#include<algorithm>
using namespace std;
struct e{int s,a,b,c;}sol[700000];
int a[105],n,s,acum,nr;
ifstream in("loto.in"); ofstream out("loto.out");
bool cmp(e x, e y){
    if(x.s>y.s) return 0; return 1;
}
void afis(int x, int y){
    out<<sol[x].a<<' '<<sol[x].b<<' '<<sol[x].c<<' '<<sol[y].a<<' '<<sol[y].b<<' '<<sol[y].c<<'\n';
}
int main(){
    in>>n>>s;
    for(int i=1;i<=n;++i) in>>a[i];
    for(int i=1;i<=n;++i){
        for(int j=i;j<=n;++j){
            for(int k=j;k<=n;++k){
                acum=a[i]+a[j]+a[k];
                sol[++nr]=(e){acum,a[i],a[j],a[k]};
            }
        }
    }
    sort(sol+1,sol+nr+1,cmp);
    int high = nr, low = 1;
    while(low <= high){
        if((sol[low].s + sol[high].s) > s)
            high--;
        else if(sol[low].s + sol[high].s < s)
            low++;
        else
            break;
    }
    if(low > high)
        out << -1;
    else
        out << sol[low].a << " " << sol[low].b << " " << sol[low].c << " " << sol[high].a << " " << sol[high].b << " " << sol[high].c;
    out.close(); return 0;
}