GitHubSeob

C++ / 백준 / 22942 / 데이터 체커 본문

Baekjoon/Gold

C++ / 백준 / 22942 / 데이터 체커

GitHubSeob 2022. 3. 14.

문제

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

 

22942번: 데이터 체커

데이터가 조건에 맞는다면 YES, 조건에 만족하지 않는다면 NO를 출력한다.

www.acmicpc.net

문제풀이

원이 모두 x축 위에 있으므로 모두 y=0이다.

vector<pair<int, int>> 를 이용하여 x좌표와 반지름을 입력받을 때 first에는 x-r, second에는 x+r값을 저장한다.

(first = 원이 x축에 닿는 점 중 왼쪽 점, second 오른쪽 점)

먼저 왼쪽 점 좌표를 기준으로 오름차순으로 정렬한다.

이중 for문을 이용해 경우의 수를 따진다.

 

- 첫 번째 if문

첫 번째 원의 first값 == 두 번째 원의 first값일 때

두 원이 겹치면 continue

그렇지 않으면 if문 조건에 걸려 NO출력 후 종료

 

 

- 두 번째 if문

첫 번 재원의 오른쪽 좌표보다 두 번 재원의 왼쪽 좌표가 더 크면 break를 하여 원을 바꿔준다.

 

- 세 번째 if문

두 번째 원이 첫 번째 원안에 들어가는 경우 continue를 한다.

 

이 이외는 모두 NO 출력 후 종료한다.

 

 

 

코드

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


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

	int N(0), idx(0), idx2(0);
	cin >> N;

	vector<pair<int, int>>data(N, { 0,0 });
	int x(0), r(0);

	for (idx = 0; idx < N; ++idx) {
		cin >> x >> r;
		data[idx].first = x - r;
		data[idx].second = x + r;
	}
	sort(data.begin(), data.end());

	for (idx = 0; idx + 1 < N; ++idx) {
		for (idx2 = idx + 1; idx2 < N; ++idx2) {
			if (data[idx].first == data[idx2].first) {
				if (data[idx].second != data[idx2].second) {
					cout << "NO";
					return 0;
				}
				else continue;
			}
			else if (data[idx].second < data[idx2].first) {
				break;
			}
			else if (data[idx2].second < data[idx].second) {
				continue;
			}
			else {
				cout << "NO";
				return 0;
			}
		}
	}
	cout << "YES";
}

 

'Baekjoon > Gold' 카테고리의 다른 글

C++ / 백준 / 2493 / 탑  (0) 2022.03.18
C++ / 백준 / 2800 / 괄호 제거  (0) 2022.03.14
C++ / 백준 / 1039 / 교환  (0) 2022.01.30
C++ / 백준 / 16397 / 탈출  (0) 2022.01.24
C++ / 백준 / 3055 / 탈출  (0) 2022.01.23