Cod sursa(job #254786)

Utilizator monica_133Monica Marinescu monica_133 Data 7 februarie 2009 15:59:01
Problema Caramizi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include<cstdio>
#include<cstdlib>
#include<fstream.h>
#include<iostream.h>
#include<time.h>
using namespace std;
ifstream f("caramizi.in");
ofstream g("caramizi.out");

int C[200000];

inline void quick(int s, int r)
{
	int k=s,h=r,val=C[rand()%(h-k+1)+k];
	do
	{
		while(C[k]>val) ++k;
		while(C[h]<val) --h;
		if(k<=h)
		{
			int aux=C[k];
			C[k]=C[h];
			C[h]=aux;
			++k;
			--h;
		}
	}while(k<=h);
	if(k<r) quick(k,r);
	if(h>s) quick(s,h);
}


int main()
{
	srand(time(0));
	int N,M,i;
	long L,t,p;
	f>>N>>M;	
	for(i=1;i<=N;i++)
		f>>C[i];
	quick(1,N);
	while(M && f>>L)
	{
		i=1;t=0;p=0;//t cate caramizi, p cate caramizi sunt pe inaltimea curenta
		while(C[i]>=L && i<=N)
		{
			++t;
			++i;
		}
		while(i<=N)
		{
			p+=C[i];
			if(p>=L)
			{
				++t;
				p-=L;
			}
			++i;
		}
		--M;
		cout<<t<<" "<<L<<endl;
		p=C[1]-C[1]%N+N;
		if(L>p)
			g<<p*(N+1-p/N);
		else
		{
			p=(L-L%N)*(N+1-L/N);
			t*=L;
			if(t<p)
				g<<p;
			else
				g<<t;
		}
		g<<endl;
	}
	f.close();g.close();
	return 0;
	
}