Cod sursa(job #2255006)

Utilizator dey44andIoja Andrei-Iosif dey44and Data 6 octombrie 2018 12:19:21
Problema Cuburi2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 kb
// Cuburi2 - INFOARENA.cpp : Defines the entry point for the console application.
//

#include <fstream>
#include <vector>

#define NMAX 250005

using namespace std;

ifstream in("cuburi2.in");
ofstream out("cuburi2.out");

int h_towers[NMAX], sum_towers[NMAX], number_of_towers, queries;

int max(int a, int b)
{
	if (a > b)
		return a;
	return b;
}

void Read_Data()
{
	int aux;
	in >> number_of_towers >> queries;
	for (int i = 1; i <= number_of_towers; i++)
	{
		in >> h_towers[i];
		sum_towers[i] = sum_towers[i - 1] + h_towers[i];
	}
}

long long Solve_Interval(int left_1, int right_1, int &corect_point)
{
	int posible_solution = corect_point;
	long long Sum = 300000000000;
	for (int i = corect_point; i <= right_1; i++)
	{
		long long local_sum = (long long)sum_towers[i - 1] - sum_towers[left_1 - 1] + sum_towers[right_1] - sum_towers[i];
		if (local_sum < Sum)
		{
			posible_solution = i, Sum = local_sum;
		}
		//out << i << " " << local_sum << "\n";
	}
	long long Final_S = 0;
	for (int i = left_1; i <= right_1; i++)
	{
		if (i != posible_solution)
		{
			Final_S += h_towers[i] * max(i - posible_solution, posible_solution - i);
		}
	}
	corect_point = posible_solution;
	//out << "\n";
	return Final_S;
}

void Solve_Query()
{
	for (int query = 1; query <= queries; query++)
	{
		int a, b, point;
		in >> a >> b;
		point = a;
		long long sumum = Solve_Interval(a, b, point);
		out << point << " " << sumum << "\n";
	}
}


int main()
{
	Read_Data();
	Solve_Query();
	return 0;
}