Cod sursa(job #789783)

Utilizator toranagahVlad Badelita toranagah Data 19 septembrie 2012 11:21:30
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <fstream>
#include <iostream>
#include <cmath>
#include <stack>

std::ifstream fin;
std::ofstream fout;

const int N_MAX = 100001;

short cote[N_MAX];
int N, K, S;
long long L, minTime;

std::stack<int> heights;

void read_input();
void solve();
int calculate_distance(int c1, int c2);
void write_output();

int main(int argc, char const *argv[])
{
	read_input();
	solve();
	write_output();
	return 0;
}

void read_input()
{
	fin.open("telecab.in");
	fin >> N >> K >> S;
	fin >> cote[1];
	for( int i = 2; i <= N; ++i )
	{
		fin >> cote[i];
		if( cote[i] <= cote[i-1] )
		{
			heights.push(i - 1);
			//std::cout << heights.top() << "   " << cote[heights.top()] << '\n';
		}
		if( !heights.empty() )
			while( cote[i] > cote[heights.top()] )
			{
				// std::cout << i << ' ' << cote[i] << '-' << heights.top() << ' ' << cote[heights.top()] << '\n';
				for( int j = i - 1; j > heights.top(); --j )
					cote[j] = -1;
				heights.pop();
			}
	} 

	fin.close();
}

void solve()
{
	L = 0;
	int t1 = 0;
	for( int i = 2; i <= N; ++i )
	{
		if( cote[i-1] != -1 )
			t1 = i - 1;
		if( cote[i] == -1 )
			continue;
		L += calculate_distance(t1, i);
		// std::cout << cote[t1] << ' ' << cote[i] << '\n';
	} 
}

int calculate_distance(int c1, int c2)
{
	return (int) sqrt( pow(cote[c1] - cote[c2], 2) + pow( c1 - c2, 2) );
}



void write_output()
{
	fout.open("telecab.out");
	fout << L << '\n' << minTime << '\n'; 
	fout.close();
}