[백준 30394] 회전하지 않는 캘리퍼스 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
y축에 평행하게 세운 캘리퍼스로 점 집합을 측정할 때, 날이 처음 닿는 거리를 구하는 문제입니다. 캘리퍼스가 y축 방향으로 서 있으므로 측정되는 것은 y좌표의 범위입니다.
접근법
캘리퍼스가 y축에 평행하게 고정되어 있으므로 측정되는 거리는 y좌표의 범위입니다. 모든 점의 y좌표를 저장한 뒤 정렬하면 최솟값과 최댓값을 쉽게 얻을 수 있습니다.
정렬된 배열의 마지막 원소에서 첫 원소를 빼면 답입니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Linq;
class Program {
static void Main() {
var n = int.Parse(Console.ReadLine()!);
var yCoords = new int[n];
for (var i = 0; i < n; i++) {
var parts = Console.ReadLine()!.Split();
yCoords[i] = int.Parse(parts[1]);
}
Array.Sort(yCoords);
Console.WriteLine(yCoords[n - 1] - yCoords[0]);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vi yCoords(n);
for (int i = 0; i < n; i++) {
int x, y; cin >> x >> y;
yCoords[i] = y;
}
sort(yCoords.begin(), yCoords.end());
cout << yCoords.back() - yCoords.front() << "\n";
return 0;
}