Mai intai trebuie sa te autentifici.

Cod sursa(job #1760468)

Utilizator CalarisPredut Denis Stefanita Calaris Data 20 septembrie 2016 20:44:03
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <fstream>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

vector<int> values;

int main()
{
    fstream f("loto.in",ios::in);
    ofstream g("loto.out");
    vector<int> vect;
    int x,N,S,M,i,j,k,y,m,res;
    bool ok1 = 0, ok2 = 0;

    f>>N>>S;
    M= N;
    do
        {
            f>>x;
            vect.push_back(x);
        }while(--M);

    for(i=0;i<N;++i)
        for(j=i;j<N;++j)
          for(k=j;k<N;++k)
                values.push_back(vect[i] + vect[j] + vect[k]);


    sort(values.begin(),values.end());

    res = values.size();

    for(i=0;i<res;++i)
        {
        x = i;
        y = res-1;
        m = (x+y)/2;
        while(x!=y && (values[i]+values[m])!=S)
         {
             if(values[m]+values[i]<S) x = m+1;
             else if(values[m]+values[i]>S) y = m-1;

             m = (x+y)/2;
         }

         if((values[i]+values[m])==S)
            {
            for(x=0;x<N;++x)
              for(j=x;j<N;++j)
                for(k=j;k<N;++k)
                    {

                    if(ok1 == 1 && ok2 == 1) return 0;
                    if(!ok1)
                        if(vect[x]+vect[j]+vect[k] == values[i])
                        {
                        g<<vect[x]<<" "<<vect[j]<<" "<<vect[k]<<" ";
                        ok1 = 1;
                        }
                     if(!ok2)
                        if(vect[x]+vect[j]+vect[k] == values[m])
                        {
                        g<<vect[x]<<" "<<vect[j]<<" "<<vect[k]<<" ";
                        ok2 = 1;
                        }
                    }
            }

        }
    g<<"-1";

    return 0;
}