Cod sursa(job #1288524)

Utilizator fhandreiAndrei Hareza fhandrei Data 8 decembrie 2014 21:21:45
Problema Loto Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 1.68 kb
//Include
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
 
//Definitii
#define pb push_back
 
//Structuri
struct mystr
{
    int value;
    int first, second, third;
}newElement, searched;
 
//Clase
class compare
{
    public:
    bool operator() (mystr a, mystr b)
    {   return a.value < b.value;    }
};
 
//Variabile
ifstream in("loto.in");
ofstream out("loto.out");
 
int eNum, sum;
int numbers[101];
 
vector<mystr> sums;
 
//Main
int main()
{
    in >> eNum >> sum;
    for(int i=1 ; i<=eNum ; ++i)
        in >> numbers[i];
     
    sums.reserve(eNum*eNum*eNum);
     
    for(int i=1 ; i<=eNum ; ++i)
        for(int j=i ; j<=eNum ; ++j)
            for(int k=j ; k<=eNum ; ++k)
            {
                newElement.value = (newElement.first = numbers[i]) + (newElement.second = numbers[j]) + (newElement.third = numbers[k]);
                sums.pb(newElement);
            }
     
    sort(sums.begin(), sums.end(), compare());
    //end = unique(sums.begin(), sums.end(), compare());
    vector<mystr>::iterator it1, it2, end = sums.end();
     
    for(it1=sums.begin() ; it1!=end ; ++it1)
    {
        searched.value = sum - it1->value;
        it2 = lower_bound(it1, end, searched, compare());
            if(it2->value == searched.value)
            {
                out << it1->first << ' ' << it1->second << ' ' << it1->third << ' ' << it2->first << ' ' << it2->second << ' ' << it2->third << '\n';
                in.close();
                out.close();
                return 0;
            }
    }
     
     
    out << -1 << '\n';
    in.close();
    out.close();
    return 0;
}