Cod sursa(job #2405948)

Utilizator divianegoescuDivia Negoescu divianegoescu Data 15 aprilie 2019 10:50:10
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("loto.in");
ofstream fout("loto.out");
int n,i,j,k,s,v[101],y,st,dr,mid,val;
struct s3{
    int s,a,b,c;
}t[1000001];
int cmp(const s3 &x,const s3 &y){
    return x.s<y.s;
}///punand const si & nu se mai fac copii ale variab,e mai rapid,dar nu se pot schimba val x,y
int main(){
    fin>>n>>s;
    for(i=1;i<=n;i++)
        fin>>v[i];
    for(i=1;i<=n;i++)
        for(j=i;j<=n;j++)
            for(k=j;k<=n;k++){
                t[++y].a=v[i];
                t[y].b=v[j];
                t[y].c=v[k];
                t[y].s=v[i]+v[j]+v[k];
            }
    sort(t+1,t+y+1,cmp);

    ///in t am sume de cate 3; parcurg t si calc daca s-t[i] se afla in t
    for(i=1;i<=y;i++){
        st=1;
        dr=y;
        val=s-t[i].s;
        while(st<=dr){
            mid=(st+dr)/2;
            if(t[mid].s==val){
                fout<<t[mid].a<<" "<<t[mid].b<<" "<<t[mid].c<<" "<<t[i].a<<" "<<t[i].b<<" "<<t[i].c;
                return 0;
            }
            if(val<t[mid].s)
                dr=mid-1;
            else st=mid+1;
        }
    }
    fout<<-1;
    return 0;
}