Computer Science/C++
백준 C++ | #26 BOJ2358 평행선 C++ 문제 풀이
토마토.
2022. 9. 1. 20:10
2358번: 평행선
첫째 줄에 n(1 ≤ n ≤ 100,000)이 주어진다. 다음 n개의 줄에는 각 점의 좌표가 주어진다. 만약 입력에 서로 같은 두 점이 주어지면, 그 두 점을 이용하여 직선을 만들 수 있다. 좌표는 절댓값이 231보
www.acmicpc.net
문제는 너무 간단해서 딱 그냥 for문으로 풀면 시간초과가 나겠다는 생각이 들었다.
STL map을 이용하면 해결되는 문제
Code by horang :: c++ std::map 사용법 총 정리1 (생성, 반복자, 크기, 값 확인 등) (tistory.com)
#include <iostream>
#include <map>
#include <utility>
int main() {
// n 개의 좌표
int n;
std::cin >> n;
std::map<int, int> x_value;
std::map<int, int> y_value;
for (int i = 0; i < n; i++) {
int x, y;
std::cin >> x >> y;
x_value[x] += 1;
y_value[y] += 1;
}
int answer = 0;
for (std::map<int, int>::iterator it = x_value.begin(); it != x_value.end(); it++) {
if (it->second >= 2) {
answer++;
}
}
for (std::map<int, int>::iterator it = y_value.begin(); it != y_value.end(); it++) {
if (it->second >= 2) {
answer++;
}
}
std::cout << answer << std::endl;
return 0;
}