[백준 25377] 빵 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
여러 가게 중에서 빵을 살 수 있는 가장 빠른 시간을 구하는 문제입니다.
접근법
각 가게까지 이동 시간이 빵 도착 시간 이하면 빵을 살 수 있습니다.
모든 가게를 순회하며 조건을 만족하는 경우 빵 도착 시간의 최소값을 갱신합니다.
끝까지 갱신되지 않으면 -1을 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
class Program {
static void Main() {
var n = int.Parse(Console.ReadLine()!);
var best = 1001;
for (var i = 0; i < n; i++) {
var line = Console.ReadLine()!.Split();
var a = int.Parse(line[0]);
var b = int.Parse(line[1]);
if (a <= b) best = Math.Min(best, b);
}
if (best == 1001) best = -1;
Console.WriteLine(best);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if (!(cin >> n)) return 0;
int best = 1001;
for (int i = 0; i < n; i++) {
int a, b; cin >> a >> b;
if (a <= b) best = min(best, b);
}
if (best == 1001) best = -1;
cout << best << "\n";
return 0;
}