Cod sursa(job #1067260)

Utilizator MarcvsHdrMihai Leonte MarcvsHdr Data 26 decembrie 2013 17:02:56
Problema Loto Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include <vector>

using namespace std;

vector<int> numbers;
int n, s, v[10];
int it = 0;
int maxno;
int sofar;

void write_sol_and_quit() {
  ofstream out("loto.out");
  for (int i = 1; i <= 6; ++i) {
    out << (i == 1 ? "" : " ") << v[i];
  }
  out << std::endl;
  out.close();
  exit(0);
}

void gen(int current_level, int start) {
  // If you've run out of space, test for solutions.
  if (current_level == 7) {
    if (sofar == s) {
      write_sol_and_quit();
    } else {
      return;
    }
  }

  for (int i = start; i <= numbers.size(); ++i) {
    // Will we underrun with the current choice?
    if (sofar + numbers[i] + (6 - current_level) * maxno < s) {
      continue;
    }

    // Will we overrun with the current choice? Stop and backtrack.
    if (sofar + (6 - current_level + 1) * numbers[i] > s) {
      return;
    }

    // Assume the current choice, and then give it up.
    v[current_level] = numbers[i];
    sofar += numbers[i];
    gen(current_level + 1, i);
    sofar -= numbers[i];
  }
}

int main()
{
  ifstream in("loto.in");

  in >> n >> s;
  for (int i = 0; i < n; ++i) {
    int x;
    in >> x;
    numbers.push_back(x);
  }

  std::sort(numbers.begin(), numbers.end());
  maxno = numbers[numbers.size() - 1];

  gen(1, 0);

  // If it returned, that's bad news.
  ofstream out("loto.out");
  out << "-1\n";
  out.close();
//  std::cerr << "Iterations: " << it << std::endl;

  return 0;
}