-
백준 2512번: 예산이분 탐색(Binary Search) 2019. 1. 20. 15:48반응형
https://www.acmicpc.net/problem/2512
1. 문제
2. 알고리즘
키워드 - 이분탐색, 구현
3. 코드
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859#include <iostream>#include <sstream>#include <string>#include <algorithm>#include <functional>#include <vector>#include <list>#include <queue>#include <deque>#include <map>#include <set>#include <stack>#include <cstring>using namespace std;#define MAX_SIZE 100#define INF 0x7fffffff#define CENDL "\n"#define ll long longint main() {std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);int n; cin >> n;vector<int> arr(n);int max_num = 0;for(int i=0; i<n; i++) {cin >> arr[i];max_num = max(arr[i], max_num);}int sum_num; cin >> sum_num;int start = 0;int end = max_num;int sum = 0;while (start <= end) {int middle = (start + end) / 2;sum = 0;for (int i=0; i<n; i++) {sum += min(arr[i], middle);}if(sum > sum_num) {end = middle - 1;} else {start = middle + 1;}}cout << end << CENDL;return 0;}cs 반응형'이분 탐색(Binary Search)' 카테고리의 다른 글
백준 10815번: 숫자 카드 (0) 2019.01.13 백준 1205번: 등수 구하기 (0) 2018.08.04 백준 2805번: 나무 자르기 (0) 2018.07.06 백준 1920번: 수 찾기 (0) 2018.06.30