문제
Did you try to ride a telepherique (cable car)? It is a lot of fun. Our company is trying to build a new telepherique line between two high mountains and you will be invited for a free ride. The trick to get your free ride is to help the company in building the telepherique line.
The company wants to build two platforms, one on each mountain. The line will extend between these two platforms. The suitable points for holding a platform in each mountain were determined, and the altitudes of these points were reported.
One of the talented engineers suggested that the company can save a lot of energy if the two stations have the same altitude or at least have altitudes which are as close to each other as possible. Your job is to select two points, one at each mountain, from those suitable points, so that the altitude difference between the two points is as little as possible.
입력
Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases (1 ≤ T ≤ 100). Follows 2T lines containing the test cases, each on a pair of lines.
Each of the two lines in a case describe the altitudes of suitable points to build a platform on one mountain. Each line starts with an integer N, the number of reported altitudes (1 ≤ N ≤ 1, 000), followed by N integers, which represent the altitudes reported from this mountain. Any altitude value will be between 1 and 1, 000, 000, inclusive.
출력
For each test case, output, on a single line, a single number representing the minimum altitude difference between two suitable platform points, one at each mountain.
풀이
간단하게, 두 줄을 입력 받은 뒤 그 두 줄에서 한 원소를 뽑았을때의 차이의 최솟값을 구하는 문제이다.
첫 줄에는 test_case를 입력 받고, 이후 두 줄씩 원소의 개수와 원소를 입력 받는다.
어차피 두 번째 줄은 입력 받아서 저장을 따로 하지 않아도 되기 때문에 하나를 입력받은 뒤 첫 번째 줄의 원소와 하나씩 비교하며 최솟값을 찾았다.
코드
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int num;
cin >> num;
vector<int> first;
for (int j = 0; j < num; j++) {
int input;
cin >> input;
first.push_back(input);
}
cin >> num;
int min = 9999999;
for (int j = 0; j < num; j++) {
int input;
cin >> input;
for (int k = 0; k < first.size(); k++) {
if (abs(input - first[k]) < min)
min = abs(input - first[k]);
}
}
printf("%d\n", min);
}
}
'Old > 백준' 카테고리의 다른 글
백준 6318번 Box of Bricks // C++ (0) | 2020.02.18 |
---|---|
백준 11403번 경로 찾기 // C++ (0) | 2020.02.18 |
백준 9557번 Arabic And English // C++ (0) | 2020.02.18 |
백준 14888번 연산자 끼워넣기 // C++ (0) | 2020.02.18 |
백준 14889번 스타트와 링크 // C++ (0) | 2020.02.18 |