반응형

https://www.acmicpc.net/problem/2108

 

2108번: 통계학

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

www.acmicpc.net

#include <bits/stdc++.h>
using namespace std;

struct Count {
	int x, cnt;

	bool operator<(const Count& b) const {
		if (cnt == b.cnt) {
			return x < b.x;
		}
		return cnt > b.cnt;
	}
};

int arr[9000];

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

	int n;
	vector<int> v;

	cin >> n;
	int sum = 0;
	for (int i = 0; i < n; i++) {
		int tmp;
		cin >> tmp;
		sum += tmp;

		arr[tmp + 4000]++;
		v.push_back(tmp);
	}

	sort(v.begin(), v.end());

	vector<Count> freq;

	for (int i = 0; i < 9000; i++) {
		if (arr[i] > 0) {
			freq.push_back({ i,arr[i] });
		}
	}

	sort(freq.begin(), freq.end());

	int freq_ans = freq[0].x - 4000;
	if (freq.size() >= 2 && freq[0].cnt == freq[1].cnt) {
		freq_ans = freq[1].x - 4000;
	}
	

	double mean = sum / (double)n;

	int mm;
	if (mean >= 0) {
		mm = int(mean + 0.5);
	}
	else {
		mm = int(mean - 0.5);
	}

	cout << mm << '\n';
	cout << v[n / 2] << '\n';

	cout << freq_ans << '\n';

	cout << v[v.size() - 1] - v[0] << '\n';
	
	return 0;
}

백준 2108번

반응형

+ Recent posts