Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 시간 복잡도
- 백준 5397
- Stack 이란
- AVL 시간 복잡도
- heap
- 버킷 정렬
- 백준
- 구현
- 원판 돌리기
- 별 찍기 10
- qorwns
- 1764
- 해시구현
- 자료구조
- 조세퍼스 순열
- 백준 1158
- 백준 1406
- 백준 17822
- ㅣ풀이
- 게리멘더링2
- 백준 17779
- C/C++ 구현
- 해시 구현
- 백준 17471
- 스택의 특징
- dfs
- 백준 2447
- 5397
- 풀이
- c#
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 11812] K진 트리 본문
분류 : dfs
요구사항
K진 트리에서 주어진 두 숫자 간의 거리 구하기
풀이
이진 트리에서 자식은 node*2, 부모는 node/2이다
3진 트리는
1
2 3 4
5 6 7
4진 트리는
1
2 3 4 5
6 7 8 9
5진 트리는
1
2 3 4 5 6
7 8 9 10 11
자식은 node*k+1까지가 자식이다
부모는 (node+(k-2))/k이다 (k-2)인 이유는 node*k+1까지가 자식이기 때문이다
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
ll n, k, q;
ll parent(ll x, ll y) {
return (x + y - 2) / y;
}
int main() {
scanf("%lld %lld%lld", &n, &k, &q);
for (int tc = 0; tc < q; tc++) {
ll low, high;
scanf("%lld%lld", &low, &high);
if (k == 1) {
printf("%lld\n", abs(low - high));
continue;
}
int cnt = 0;
while (low != high) {
while (low>high) {
cnt++;
low = parent(low, k);
}
while (high > low) {
cnt++;
high = parent(high, k);
}
}
printf("%d\n", cnt);
}
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 2012] 등수 매기기 (0) | 2020.03.30 |
---|---|
[백준 1063] 킹 (0) | 2020.03.30 |
[백준 1726] 로봇 (0) | 2020.03.29 |
[백준 6087] 레이저 통신 (0) | 2020.03.28 |
[백준 1305] 광고 (0) | 2020.03.28 |
Comments