Code Submission Evaluation System Login

CSES - HIIT Open 2016

HIIT Open 2016

Contest start:2016-05-28 11:00:00
Contest end:2016-05-28 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2016-05-28 11:24:25
Task:Approximate
Sender:Spitfire
Submission time:2016-05-28 11:24:25
Status:READY
Result:TIME LIMIT EXCEEDED

Show test data

Code

#include <iostream>

float handle_query(unsigned int numbers[], unsigned int a, unsigned int b) {
	float avg = 0;
	for (size_t i = a; i <= b; i++) {
		avg += numbers[i];
	}
	avg /= (b-a + 1);

	float err = 0;
	for (size_t i = a; i <= b; i++) {
		err += (numbers[i] - avg) * (numbers[i] - avg);
	}
	err /= (b-a + 1);
	return err;	
}

int main()
{
	std::ios_base::sync_with_stdio(0);
	std::cin.tie(0);

	unsigned int n, q;
	std::cin >> n >> q;

	unsigned int numbers[100000];

	unsigned int nb;
	for (size_t i = 0; i < n; i++) {
		std::cin >> nb;
		numbers[i] = nb;
	}

	unsigned int a,b;
	for (size_t i = 0; i < q; i++) {
		std::cin >> a >> b;
		std::cout <<std::fixed <<  handle_query(numbers, a-1, b-1) << std::endl;
	}

	return 0;
}