Pagini recente » Cod sursa (job #1163223) | Cod sursa (job #2211250) | Cod sursa (job #2556207) | Cod sursa (job #307577) | Cod sursa (job #1153816)
#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;
}