A B2029 코끼리 물 마시기 - 로그
수학 문제로, 원주율 π를 100배한 정수값을 사용하여 부동소수점 오차를 방지합니다.
#include <iostream>
using namespace std;
void calculate() {
int height, radius;
cin >> height >> radius;
int cylinderVol = height * 314 * radius * radius;
int totalWater = 2000000; // 20L * 1000cm³/L * 100 (스케일)
if (totalWater % cylinderVol != 0) {
cout << totalWater / cylinderVol + 1 << endl;
} else {
cout << totalWater / cylinderVol << endl;
}
}
int main() {
calculate();
return 0;
}
B P1152 즐거운 점프 - 로그
인접 원소 차이의 절댓값을 배열로 기록하여 모든 1~(n-1) 값이 존재하는지 검증합니다. 차이값 범위 검사로 배열 접근 오류를 방지합니다.
#include <iostream>
#include <cmath>
using namespace std;
const int MAX = 1005;
int arr[MAX], diffFlag[MAX];
void checkJump() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 1; i < n; i++) {
int gap = abs(arr[i] - arr[i-1]);
if (gap < n) diffFlag[gap] = 1;
}
for (int i = 1; i < n; i++) {
if (!diffFlag[i]) {
cout << "Not jolly" << endl;
return;
}
}
cout << "Jolly" << endl;
}
int main() {
checkJump();
return 0;
}
C P1923 k번째 작은 수 찾기 - 로그
빠른 선택 알고리즘으로 O(n) 복잡도 해결이 가능하나, 여기서는 정렬과 고속 입출력으로 구현합니다.
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 5000005;
long dataArr[MAX];
void findKth() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> dataArr[i];
sort(dataArr, dataArr + n);
cout << dataArr[k] << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
findKth();
return 0;
}
D P1216 숫자 삼각형 - 로그
하향식 동적 계획법으로, 삼각형 아래층부터 최대 경로 합을 계산하여 최적해를 도출합니다.
#include <iostream>
#include <algorithm>
using namespace std;
int tri[1005][1005];
void maxTrianglePath() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cin >> tri[i][j];
}
}
for (int row = n - 1; row >= 1; row--) {
for (int col = 1; col <= row; col++) {
tri[row][col] += max(tri[row+1][col], tri[row+1][col+1]);
}
}
cout << tri[1][1] << endl;
}
int main() {
maxTrianglePath();
return 0;
}
E P3799 작은 Y의 나무 막대 - 로그
조합론을 활용해 동일 길이 막대 2개와 길이 합이 일치하는 막대 쌍의 조합 수를 계산합니다.
#include <iostream>
using namespace std;
const int MOD = 1e9 + 7;
int stickCount[5005];
void countTriangles() {
int n, val;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> val;
stickCount[val]++;
}
long result = 0;
for (int len = 1; len <= 5000; len++) {
long pairCount = (long)stickCount[len] * (stickCount[len] - 1) / 2 % MOD;
if (!pairCount) continue;
for (int j = 1; j < len - j; j++) {
result = (result + (long)stickCount[j] * stickCount[len - j] % MOD * pairCount) % MOD;
}
if (len % 2 == 0) {
int half = len / 2;
long halfPair = (long)stickCount[half] * (stickCount[half] - 1) / 2 % MOD;
result = (result + halfPair * pairCount) % MOD;
} else {
int mid = len / 2;
result = (result + (long)stickCount[mid] * stickCount[mid + 1] % MOD * pairCount) % MOD;
}
}
cout << result << endl;
}
int main() {
countTriangles();
return 0;
}
F P1928 외계인 암호 - 로그
재귀적 문자열 처리를 통해 내부 괄호부터 차례대로 압축을 해제합니다.
#include <iostream>
#include <string>
using namespace std;
string decompress(string s) {
size_t pos;
while ((pos = s.find(']')) != string::npos) {
size_t start = pos;
while (s[start] != '[') start--;
string inner = s.substr(start + 1, pos - start - 1);
int num = 0, idx = 0;
while (idx < inner.size() && isdigit(inner[idx])) {
num = num * 10 + (inner[idx++] - '0');
}
inner = inner.substr(idx);
string expanded;
for (int i = 0; i < num; i++) {
expanded += inner;
}
s.replace(start, pos - start + 1, expanded);
}
return s;
}
int main() {
string s;
cin >> s;
cout << decompress(s);
return 0;
}
G P1591 팩토리얼 숫자 - 로그
큰 정수 곱셈을 배열로 구현하여 팩토리얼 계산 후 특정 숫자의 출현 횟수를 셉니다.
#include <iostream>
#include <cstring>
using namespace std;
int bigNum[10000];
void countDigitInFactorial() {
int n, digit;
cin >> n >> digit;
memset(bigNum, 0, sizeof(bigNum));
bigNum[0] = 1;
int digits = 1, carry = 0;
for (int i = 2; i <= n; i++) {
for (int j = 0; j < digits || carry; j++) {
int product = bigNum[j] * i + carry;
bigNum[j] = product % 10;
carry = product / 10;
if (j >= digits) digits = j + 1;
}
}
int count = 0;
for (int i = 0; i < digits; i++) {
if (bigNum[i] == digit) count++;
}
cout << count << endl;
}
int main() {
countDigitInFactorial();
return 0;
}
H P2234 매출액 통계 - 로그
이진 탐색 트리(set)를 이용해 이전 값 중 가장 가까운 수를 효율적으로 찾습니다.
#include <iostream>
#include <set>
#include <climits>
using namespace std;
void minDifferenceSum() {
int n;
cin >> n;
set<long> values;
values.insert(INT_MIN);
values.insert(INT_MAX);
long total = 0, current;
for (int i = 0; i < n; i++) {
cin >> current;
if (i == 0) {
total += current;
} else {
auto it = values.lower_bound(current);
long diff1 = abs(*it - current);
it--;
long diff2 = abs(*it - current);
total += min(diff1, diff2);
}
values.insert(current);
}
cout << total << endl;
}
int main() {
minDifferenceSum();
return 0;
}
I P4447 그룹 나누기 - 로그
이진 탐색으로 각 그룹의 마지막 숫자를 추적하며 최소 크기 그룹을 유지합니다.
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 100005;
int arr[MAX], lastVal[MAX], groupSize[MAX];
void findMinGroup() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
lastVal[0] = arr[0] + 1;
groupSize[0] = 1;
int count = 1;
for (int i = 1; i < n; i++) {
int left = 0, right = count;
while (left < right) {
int mid = (left + right) / 2;
if (lastVal[mid] <= arr[i]) left = mid + 1;
else right = mid;
}
int pos = left - 1;
if (pos >= 0 && lastVal[pos] == arr[i]) {
lastVal[pos]++;
groupSize[pos]++;
} else {
lastVal[count] = arr[i] + 1;
groupSize[count] = 1;
count++;
}
}
int minSize = groupSize[0];
for (int i = 1; i < count; i++) {
if (groupSize[i] < minSize) minSize = groupSize[i];
}
cout << minSize << endl;
}
int main() {
findMinGroup();
return 0;
}
J P3853 도로 표지판 설정 - 로그
이진 탐색을 통해 가능한 최대 간격을 찾고, 주어진 표지판 개수 내 조건을 만족하는지 검사합니다.
#include <iostream>
using namespace std;
const int MAX = 100005;
int markers[MAX], L, N, K;
bool isValid(int gap) {
int added = 0;
for (int i = 1; i <= N; i++) {
int dist = markers[i] - markers[i-1];
if (dist > gap) {
added += (dist - 1) / gap;
if (added > K) return false;
}
}
return true;
}
void findMinGap() {
cin >> L >> N >> K;
markers[0] = 0;
for (int i = 1; i <= N; i++) cin >> markers[i];
markers[N+1] = L;
N++;
int low = 1, high = L;
while (low < high) {
int mid = (low + high) / 2;
if (isValid(mid)) high = mid;
else low = mid + 1;
}
cout << low << endl;
}
int main() {
findMinGap();
return 0;
}
K P2719 재미있는 월드컵 - 로그
동적 계획법으로 두 종류의 티켓이 남은 상황에서 최종 일치 확률을 계산합니다.
#include <iostream>
using namespace std;
double dp[1300][1300];
void calculateProbability() {
int n;
cin >> n;
n /= 2;
for (int i = 1; i <= n; i++) {
dp[i][0] = 1.0;
dp[0][i] = 1.0;
}
for (int a = 1; a <= n; a++) {
for (int b = 1; b <= n; b++) {
dp[a][b] = 0.5 * dp[a][b-1] + 0.5 * dp[a-1][b];
}
}
printf("%.4f", dp[n][n]);
}
int main() {
calculateProbability();
return 0;
}
L P1364 병원 위치 - 로그
그래프 너비 우선 탐색(BFS)을 사용해 각 노드를 기준으로 모든 노드까지의 거리 합을 계산합니다.
#include <iostream>
#include <queue>
#include <vector>
#include <climits>
using namespace std;
const int MAX = 105;
vector<int> graph[MAX];
int weights[MAX], n;
int bfs(int start) {
queue<pair<int, int>> q; // {node, distance}
vector<bool> visited(n+1, false);
q.push({start, 0});
visited[start] = true;
int total = 0;
while (!q.empty()) {
int node = q.front().first;
int dist = q.front().second;
q.pop();
total += weights[node] * dist;
for (int neighbor : graph[node]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push({neighbor, dist + 1});
}
}
}
return total;
}
void findOptimalHospital() {
cin >> n;
for (int i = 1; i <= n; i++) {
int left, right;
cin >> weights[i] >> left >> right;
if (left) {
graph[i].push_back(left);
graph[left].push_back(i);
}
if (right) {
graph[i].push_back(right);
graph[right].push_back(i);
}
}
int minDist = INT_MAX;
for (int i = 1; i <= n; i++) {
int distSum = bfs(i);
if (distSum < minDist) minDist = distSum;
}
cout << minDist << endl;
}
int main() {
findOptimalHospital();
return 0;
}