Cod sursa(job #1060706)

Utilizator dorinmoldovanMoldovan Dorin dorinmoldovan Data 18 decembrie 2013 15:20:57
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <fstream>

using namespace std;

int main(void)
{
	int G; // number of generators
	int W; // quantity of energy necessary to start the central
	int EG[1000]; // quantity of energy produced by a generator
	int CG[1000]; // necessary cost for the production of energy
	double R[1000]; // an array containing the price for producing one unit of energy
	int sum; // the final cost

	// Read data from the input file 

	ifstream fin ("energii.in");
	fin >> G;
	fin >> W;
	for(int i = 0; i < G; i++)
	{
		fin >> EG[i];
		fin >> CG[i];
	}
	fin.close();

	for(int i = 0; i < G; i++)
		R[i] = (1.0 * CG[i]) / EG[i];

	// sort the R array in the increasing order. At the same time sort the arrays CG and EG.

	for(int i = 0; i < G - 1; i++)
		for(int j = i + 1; j < G; j++)
		{
			if(R[i] > R[j])
			{
				double a = R[i];
				R[i] = R[j];
				R[j] = a;

				int b = EG[i];
				EG[i] = EG[j];
				EG[j] = b;

				b = CG[i];
				CG[i] = CG[j];
				CG[j] = b;
			}
		}

	sum = 0;
	int numberOfGenerators = 0;

	while (numberOfGenerators < G)
	{
		if(sum < W)
			sum += CG[numberOfGenerators];
		numberOfGenerators++;
	}

	if(sum < W)
		sum = -1;

	FILE *fout;
	fout = fopen("energii.out", "w");
	fprintf(fout, "%d\n", sum);
	fclose(fout);

	return 0;
}