Cod sursa(job #1033358)

Utilizator harababurelPuscas Sergiu harababurel Data 16 noiembrie 2013 19:43:14
Problema Grupuri Scor 76
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <iostream>
#include <fstream>
#define nmax 100005
#define inf (1<<30)
using namespace std;

int n, k, v[nmax], sol = 0, sum = 0;

bool check(int x) {
	int levels = 0, onLevel = 0;

	for(int i=n; i>=1; i--) {
		if(v[i] >= x) levels++;
		else {
			onLevel += v[i];
			//levels += onLevel / x;
			//onLevel %= x;
		}
	}
	levels += onLevel / x;

	return (levels >= k);
}
			
			

int main() {
	ifstream f("grupuri.in");
	ofstream g("grupuri.out");
	
	f>>k>>n;
	for(int i=1; i<=n; i++) f>>v[i];


	int lo = -1, mid, hi = inf;
	while(hi - lo > 1) {
		mid = (lo + hi) >> 1;
		if(check(mid)) lo = mid;
		else hi = mid;
	}


//	for(int i=1; i<=10; i++) cout<<"incerc cu "<<i<<" grupuri: "<<check(i)<<".\n";

	g<<lo<<"\n";

	return 0;
}