Cod sursa(job #1467489)

Utilizator OpportunityVlad Negura Opportunity Data 3 august 2015 15:01:48
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <fstream>
#include <algorithm>
#include <vector>
#define pivot 2234
using namespace std;

ifstream fi("loto.in");
ofstream fo("loto.out");

struct cell {
 long nr1, nr2, sum;
}c = {0};

long n,s,sn,a[101];
cell h[1000001];

bool comp(cell c1, cell c2) {
 return c1.sum < c2.sum;
}

cell exists(long nr, long l, long r) {
 if (l==r) {
  if (h[l].sum == nr) {
   return h[l];
  } else {
   return c;
  }
 }
 if (nr > h[(l+r)/2].sum) {
  return exists(nr, (l+r)/2+1, r);
 } else {
  return exists(nr, l, (l+r)/2);
 }
}


int main() {

 fi >> n >> s;
 for (int i = 0; i<n; i++) {
  fi >> a[i];
 }

 for (int i = 0; i<n; i++) {
  for (int j = i; j<n; j++) {
   for (int k = j; k<n; k++) {
    cell aux;
    aux.sum = a[i]+a[j]+a[k];
    aux.nr1 = a[i];
    aux.nr2 = a[j];
    h[sn++] = aux;
   }
  }
 }

    sort(h,h+sn,comp);

 for (long i = 0; i<sn; i++) {
  if (h[i].sum > s) continue;
  cell x = exists(s-h[i].sum,0,sn-1);
  if (x.sum) {
   fo << h[i].nr1 << ' ' << h[i].nr2 << ' ' << (h[i].sum-h[i].nr1-h[i].nr2) << ' ' << x.nr1 << ' ' << x.nr2 << ' ' << (x.sum-x.nr1-x.nr2);
   return 0;
  }
 }
 
 fo << -1;

 return 0;
}