[백준 16255] Martian Volleyball (C#, C++) - soo:bak
작성일 :
문제 링크
설명
한 팀이 점수 k 이상이면서 상대보다 2점 이상 앞서면 경기가 끝납니다. 현재 점수가 x:y일 때 경기 종료까지 필요한 최소 공의 개수를 구하는 문제입니다.
접근법
큰 점수 a, 작은 점수 b로 두고 a가 앞서 있다고 보면, a가 그대로 이기는 경우가 최소 공입니다.
- 아직 a가 k에 못 미치면 k까지 k−a공이 필요합니다. k에 도달한 뒤에도 리드가 2점이 안 되면 부족한 만큼만 더 점수를 얻으면 됩니다.
- 이미 a ≥ k라면 점수 차가 2점이 될 때까지 a만 연속 득점시키면 끝입니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
class Program {
static void Main() {
int t = int.Parse(Console.ReadLine()!);
for (int _ = 0; _ < t; _++) {
var p = Console.ReadLine()!.Split();
int k = int.Parse(p[0]);
int x = int.Parse(p[1]);
int y = int.Parse(p[2]);
int a = Math.Max(x, y);
int b = Math.Min(x, y);
int diff = a - b;
int ans;
if (a < k) {
int toK = k - a;
int lead = diff + toK;
int extra = Math.Max(0, 2 - lead);
ans = toK + extra;
} else {
ans = 2 - diff;
}
Console.WriteLine(ans);
}
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--) {
int k, x, y;
cin >> k >> x >> y;
int a = max(x, y);
int b = min(x, y);
int diff = a - b;
int ans;
if (a < k) {
int toK = k - a;
int lead = diff + toK;
int extra = max(0, 2 - lead);
ans = toK + extra;
} else {
ans = 2 - diff;
}
cout << ans << "\n";
}
return 0;
}