Algorithm/Baekjoon
[Algorithm]Baekjoon 2461번 :: 대표 선수
MaxLevel
2025. 3. 17. 00:25
https://www.acmicpc.net/problem/2461
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
#include <functional>
#include <sstream>
#include <memory.h>
#include <deque>
#include <set>
#include <unordered_set>
using namespace std;
struct Node
{
int classNum;
int ability;
};
bool cmp(const Node& a, const Node& b)
{
return a.ability < b.ability;
}
int n, m, num;
vector<Node> students;
unordered_map<int, int> check;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> num;
students.push_back({ i,num });
}
}
sort(students.begin(), students.end(), cmp);
// 윈도우사이즈는 n. n개만큼 유지되어있어야함.
int answer = 0x3f3f3f3f;
int count = 0;
int start = 0;
int end = 0;
while (end < students.size())
{
auto iter = check.find(students[end].classNum);
if (iter == check.end()) // 현재 구간에 없으면
{
check.insert({ students[end].classNum, 1 });
}
else // 있으면
{
++iter->second;
}
while (check.size() == n)
{
answer = min(answer, students[end].ability - students[start].ability);
auto iter = check.find(students[start].classNum);
if (iter->second > 1)
{
--iter->second;
}
else
{
check.erase(iter);
}
++start;
}
++end;
}
cout << answer;
}
|
cs |
이전 눈사람문제를 풀었었어서 접근은 정확하게 했다.
다만, 배열로 체크하는게 아직 헷갈리다보니까 오히려 꼬여서, map으로 먼저 풀고 그다음에 배열로 체크해서 시간을 줄이던가 해야할 것 같다.