본문 바로가기

Coding Test

(163)
백준 16953 A → B 큐에 b보다 작은 연산결과들을 연산의 횟수와 같이 집어넣고 연산으로 b를 만들었다면 그 횟수를 출력하는 방식으로 구현하였다. #include #include #define INF 987654321 using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long a, b; cin >> a >> b; queue q; //first: 해당 숫자, second: 연산의 수에 1을 더한 것 q.push({a, 1}); while (!q.empty()) { long long cur = q.front().first; int cnt = q.front().second; q.pop(); if (cur ..
백준 19637 IF문 좀 대신 써줘 https://www.acmicpc.net/problem/19637 문제에서의 if문을 그대로 사용하게 되면 시간초과가 발생한다. 이를 개선하기 위해 binary search 알고리즘을 사용하여 해결했다. 입력된 power가 들어오면 이게 어느 경계 전투력에 포함되는지 확인한다. 그 후 mid의 칭호를 쓸지 다음의 칭호를 쓸지 확인한다. #include #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; vector grade(n); for (int i = 0; i > grade[i]..
백준 17352 여러분의 다리가 되어 드리겠습니다! https://www.acmicpc.net/problem/17352 전형적인 분리집합 문제이다. 주어진 edge의 vertex를 모두 union하게 되면 입력 조건에 의해 단 한개의 구간만 parent가 다르게 나온다. 이를 나머지 vertex 중 아무 것과 잇게되면 어떤 두 섬 사이든 왕복할 수 있게 된다. #include #include using namespace std; int parent[300001]; int findParent(int k){ if (parent[k] == k) { return k; } return parent[k] = findParent(parent[k]); } void merge(int a, int b){ if (findParent(a) == findParent(b)) { ..