Cod sursa(job #1153816)

Utilizator MarcvsHdrMihai Leonte MarcvsHdr Data 25 martie 2014 19:19:05
Problema Grupuri Scor 8
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <iostream>
#include <fstream>
#include <queue>

bool is_possible(int target, int k, std::vector<int> v) {
  // Create a priority heap.
  std::priority_queue<int> pq(v.begin(), v.end());
  int reserve[k];
  for (int i = 1; i <= target; ++i) {
    if (pq.size() < k) {
      return false;
    }
    for (int j = 0; j < k; ++j) {
      reserve[j] = pq.top() - 1;
      pq.pop();
    }
    for (int j = 0; j < k; ++j) {
      if (reserve[j] > 0) {
        pq.push(reserve[j]);
      }
    }
  }
  return true;
}

int binsearch(int li, int lf, int k, std::vector<int>& v) {
  if (li == lf) {
    return li;
  }

  // We know that the lower end is always possible, but we're not sure about the
  // upper end. We also know that the upper end is stricly greater than the
  // lower end.
  int m = (li + lf) / 2;
  if (m == li) {
    m = li + 1;
  }

  if (is_possible(m, k, v)) {
    return binsearch(m, lf, k, v);
  } else {
    return binsearch(li, m - 1, k, v);
  }
}

int main()
{
  std::ifstream in("grupuri.in");
  int n, k, total = 0;
  std::vector<int> v;
  in >> k >> n;
  for (int i = 0; i < n; ++i) {
    int x;
    in >> x;
    v.push_back(x);
    total += x;
  }
  in.close();

  std::ofstream out("grupuri.out");
  out << binsearch(0, total / k, k, v) << std::endl;
  out.close();
  return 0;
}