Cod sursa(job #585920)

Utilizator dcm9000Dinu Cristian Mircea - UPB dcm9000 Data 30 aprilie 2011 12:40:34
Problema Avioane Scor 40
Compilator cpp Status done
Runda Algoritmiada 2011, Runda Finală, Open Marime 1.58 kb
#include <iostream>
#include <cstdio>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <algorithm>

using namespace std;

int N;
int V[100001];

typedef long long _ll;

_ll xV[100001];

static inline _ll maxBizProfit(int nEco, _ll ecoCost) {
	_ll best = 0;
	/*	
	static _ll prevEcoCost = 0;
	_ll delta = ecoCost-prevEcoCost;
	prevEcoCost = ecoCost;
	*/
	for (int nBiz = 0; nBiz <= nEco; nBiz++) {
		_ll bizProfit = xV[nBiz] - ecoCost*nBiz;
		if (bizProfit > best) best = bizProfit;
	}

	return best;
}

static inline _ll maxBizProfitCheck(int nEco, _ll ecoCost) {
	_ll best = 0;

	for (int nBiz = nEco; nBiz >= 1; nBiz--) {
		_ll bizCost = V[nBiz-1];
		_ll bizProfit = (bizCost-ecoCost) * (_ll)nBiz;

		if (bizProfit > best) {
			best = bizProfit;
		}
	}

	return best;
}

int main(int argc, char** argv) {
	FILE* fIn = fopen("avioane.in", "rt");	
	ofstream fOut("avioane.out");

	fscanf(fIn, "%d", &N);
	for (int i=0; i<N; i++) fscanf(fIn, "%d", V+i);
//
/*
	srand(1234);
	N = 1000;
	for (int i=0; i<N; i++) V[i] = rand() % 1000;
*/
// 335162
	sort(V, V+N);
	for (int i=0; i<N/2; i++) {
		int temp = V[i];
		V[i] = V[N-1-i];
		V[N-1-i] = temp; 
	}

	xV[0] = 0;
	for (int i=1; i<=N; i++) {
		xV[i] = ((_ll)V[i-1]) * i;
	}

	_ll best = 0;
	for (int nEco = N; nEco >= 1; nEco--) {
		_ll ecoCost = V[nEco-1];
		_ll ecoProfit = ecoCost * nEco;

		_ll bizProfit = maxBizProfit(nEco, ecoCost);

		/*
		if (bizProfit != maxBizProfitCheck(nEco, ecoCost)) {
			cout << "ERROR\n";
			exit(1);
		}
		*/

		if (ecoProfit+bizProfit > best) best = ecoProfit+bizProfit;
	}

	fOut << best << "\n";
	
	fOut.close();	
}