문제) 백준 - 이분 탐색 - 게임
https://www.acmicpc.net/problem/1072
1072번: 게임
김형택은 지금 몰래 Spider Solitaire(스파이더 카드놀이)를 하고 있다. 형택이는 이 게임을 이길 때도 있었지만, 질 때도 있었다. 누군가의 시선이 느껴진 형택이는 게임을 중단하고 코딩을 하기 시
www.acmicpc.net
이분 탐색으로 승률이 오를 승수를 계산합니다. 승률이 증가했을 경우 ans 업데이트 후 범위를 줄이는 식으로 이분 탐색을 진행합니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
#define endl "\n" | |
#define MAX 1000000000 | |
#define INF 987654321 | |
#define MOD 1001 | |
#define ll long long | |
#define int ll | |
using namespace std; | |
typedef pair<int, int> p; | |
int winRate(int x, int y) { | |
return (y * 100) / x; | |
} | |
signed main() { | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); cout.tie(0); | |
int x, y; cin >> x >> y; | |
int now = winRate(x, y); | |
if (now >= 99) { | |
cout << -1; | |
return 0; | |
} | |
int ans = -1; | |
int s = 0, e = 1000000000; | |
while (s <= e) { | |
int mid = (s + e) / 2; | |
if (winRate(x + mid, y + mid) > now) { | |
ans = min(ans, mid); | |
e = mid - 1; | |
} | |
else { | |
s = mid + 1; | |
} | |
} | |
cout << ans; | |
return 0; | |
} |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 19699번 - 소-난다! (Python) 문제 및 풀이 (0) | 2022.01.21 |
---|---|
[백준] 10546번 - 배부른 마라토너 (C++) 문제 및 풀이 (0) | 2022.01.21 |
[백준] 14567번 - 선수과목 (Prerequisite) (C++) 문제 및 풀이 (0) | 2022.01.20 |
[백준] 1159번 - 농구 경기 (C++) 문제 및 풀이 (0) | 2022.01.20 |
[백준] 18427번 - 함께 블록 쌓기 (C++) 문제 및 풀이 (0) | 2022.01.19 |
댓글