fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
백준 14566 (C++) Dongjak N1
최초 업로드: 2025-10-21 23:56:47
최근 수정 시간: 2025-10-22 08:02:22
게시자: rlatjwls3333
카테고리: 백준
조회수: 8
# [Bronze I] Dongjak N1 [문제 링크](https://www.acmicpc.net/problem/14566) ## 문제 설명 <p>You are the president of the company that runs the Chung Ang University shuttle bus. Students were able to use the bus to easily move the classroom. One day, the school expanded the campus by purchasing all the buildings in Heukseok - dong with your tuition. Because of this, you, the president of the shuttle bus operating company, must create a new line of buses to operate within the newly expanded campus.</p> <p>Coincidentally, because of the president who wants to be tidy, the road that the newly built in campus are just two-line straight roads. There are no other roads. There are n stops on the campus roads.</p> <p>For bus route development, you have numbered each of the n stops with an integer of A<sub>1</sub>, A<sub>2</sub>, A<sub>3</sub>, ..., A<sub>n-1</sub>, A<sub>n</sub>.</p> <p>To move from one stop to another stop, you must go to the bus by all means. You want to design the bus route with less cost. However, if you do not leave a reasonable number of stops, students will be dissatisfied in your design. In this case, they will blame you, and you will be fired :). So, you decided to find the nearest stop among the n stops and merge them into one.</p> <p>You majored in computer science and have decided to use n integers to number the bus stops. We decided to solve this problem by creating a program that finds the distance between two stops closest to the distance and calculates the number of pairs of two stops with this distance.</p> ## 입력 <p>In the first line, the number of stops (2 ≤ n ≤ 5000) is given.</p> <p>In the second line, the coordinates A<sub>1</sub>, ..., A<sub>n</sub> (0 ≤ A<sub>i</sub> ≤ 32,800,000) of each stop are given. There is no duplicate of each number.</p> ## 출력 <p>Two integers are output, the first integer is the minimum distance and the second integer is the number of pairs of two stops with this distance.</p> ## 풀이 정류장 사이의 가장 가까운 거리와 그 수를 구하는 문제입니다. 정렬해서 자신과 자신 옆만 확인하면 됩니다. ``` c++ #include<bits/stdc++.h> using namespace std; int arr[5000]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for(int i=0;i<n;i++) cin >> arr[i]; sort(arr, arr+n); int dist=INT_MAX, cnt=0; for(int i=1;i<n;i++) { if(dist>arr[i]-arr[i-1]) { dist = arr[i]-arr[i-1]; cnt=1; } else if(dist==arr[i]-arr[i-1]) { cnt++; } } cout << dist << ' ' << cnt; } ```