Cod sursa(job #1453344)

Utilizator alexmisto342Turdean Alexandru alexmisto342 Data 23 iunie 2015 12:47:54
Problema Transport Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <fstream>
#include <algorithm>
#include <vector>
#define x first
#define y second
using namespace std;
ifstream fin("loto.in");
ofstream fout("loto.out");
int i,n,v[200],j,r,s,q,w,nr[2000000][3],z[2000000];
vector < pair<int,int> > m;
bool cmp(pair <int,int> a,pair <int,int> b){

    if(a.x>b.x)
        return 1;
        return 0;
}
int main()
{
    fin>>n;
    fin>>s;
    for(i=1;i<=n;i++)
        fin>>v[i];
    w=0;
    m.push_back(make_pair(0,0));
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            for(q=1;q<=n;q++)
            {
                w++;
                m.push_back(make_pair(v[i]+v[j]+v[q],w));
                nr[w][0]=i;
                nr[w][1]=j;
                nr[w][2]=q;
            }
    sort(m.begin()+1,m.end());
    for(i=1;i<=w;i++)
    {int a,b;
        a=s-m[i].x;
        b=lower_bound(m.begin()+1,m.end(), a,cmp)-m.begin();
        if(m[b].x==a)
        {
            a=b;
            fout<<v[nr[m[i].y][0]]<<" "<<v[nr[m[i].y][1]]<<" "<<v[nr[m[i].y][2]]<<" "<<v[nr[m[a].y][0]]<<" "<<v[nr[m[a].y][1]]<<" "<<v[nr[m[a].y][2]];return 0;
        }
    }
    fout<<-1;

    return 0;
}