728x90
분류: 정렬, 맵
문제: www.acmicpc.net/problem/2108
2108번: 통계학
첫째 줄에 수의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 그 다음 N개의 줄에는 정수들이 주어진다. 입력되는 정수의 절댓값은 4,000을 넘지 않는다.
www.acmicpc.net
- 최빈값 처리를 위해 맵에 숫자/빈도수를 저장해주고 pair 벡터로 옮겨서 빈도수로 정렬해주었다.
- 이 때, 같은 빈도수가 있다면 두 번째로 작은 수를 출력하도록 했다.
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <climits>
#include <cstring>
#include <iomanip>
#include <bitset>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#define ll long long
#define INF 1e9
using namespace std;
//https://www.acmicpc.net/problem/2108 통계학
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
vector<int> v;
int sum = 0;
int many_num = 0;
map<int, int> m;
cin >> n;
for(int i=0; i<n; i++) {
int a;
cin >> a;
v.push_back(a);
sum += a;
m[a]++;
}
//최빈값을 찾기 위해 맵의 밸류로 정렬
vector<pair<int, int> > vm(m.begin(), m.end());
sort(vm.begin(), vm.end(), [](pair<int, int> a, pair<int, int> b) {
return a.second > b.second;
});
many_num = vm[0].first;
if(vm[0].second == vm[1].second) many_num = vm[1].first;
sort(v.begin(), v.end());
cout << round((double)sum / n) << '\n';
cout << v[v.size()/2] << '\n';
cout << many_num << '\n';
cout << v.back() - v.front();
return 0;
}
728x90
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준 / BOJ] 15650 N과 M(2) (0) | 2021.03.15 |
---|---|
[백준 / BOJ] 2630 색종이 만들기 (0) | 2021.03.15 |
[백준 / BOJ] 1260 DFS와 BFS (0) | 2021.03.15 |
[백준 / BOJ] 5052 전화번호 목록 (0) | 2021.03.15 |
[백준 / BOJ] 14003 가장 긴 증가하는 부분 수열 5 (0) | 2021.03.14 |