No Rules Rules

통계학 (feat. 백준, 2108번) 본문

생활/코테

통계학 (feat. 백준, 2108번)

개발하는 완두콩 2022. 8. 2. 15:19
728x90
반응형

통계학
https://www.acmicpc.net/problem/2108

 

2108번: 통계학

첫째 줄에 수의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 단, N은 홀수이다. 그 다음 N개의 줄에는 정수들이 주어진다. 입력되는 정수의 절댓값은 4,000을 넘지 않는다.

www.acmicpc.net

반응형
// woohyeon.kim
// https://www.acmicpc.net/problem/2108
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
	ios::sync_with_stdio(false), cin.tie(NULL);
	register int i, N, avg, count, ans, t = 0;
	register short arr[500000] = { 0 };
	map<int, int> tmpl;
	cin >> N;
	for (i = 0, avg = 0; i < N; ++i)
		cin >> arr[i], avg += arr[i];
	avg = round(avg / (double)N);
	sort(arr, arr + N);
	for (i = 0, count = 0; i < N; ++i)
		++tmpl[arr[i]], count = max(count, tmpl[arr[i]]);
	for (auto& item : tmpl) {
		if (item.second == count) {
			ans = item.first;
			if (++t == 2)
				break;
		}
	}
	cout << avg << "\n" << arr[N / 2] << "\n" << ans << "\n" << arr[N - 1] - arr[0] << "\n";
	return 0;
}
// *&)*@*

문제에서 주어진 최빈값은 여러 개 있을 때에는 최빈값 중 두 번째로 작은 값을 출력한다. 라는 조건이 있습니다.

728x90
반응형
Comments