20250725 baekjoon

This commit is contained in:
songyc macbook 2025-07-25 22:02:47 +09:00
parent 9de4b06813
commit 6db33b085a
3 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
long long cutTree(const int* tree, int treeNum, int cuttingHeight);
int main(){
int N, M;
scanf("%d %d",&N, &M);
int* tree = (int*)malloc(sizeof(int)*N);
int maxHeight = 0;
for(int i=0; i<N; i++){
scanf("%d",&tree[i]);
if(maxHeight<tree[i]) maxHeight = tree[i];
}
int start = 0, end = maxHeight;
int optimalHeight = 0;
while(start <= end){
int midHeight = start + (end - start) / 2; // (start + end) / 2 의 오버플로우 방지 형태
long long cutAmount = cutTree(tree,N,midHeight);
if(cutAmount<M) end = midHeight - 1;
else {
optimalHeight = midHeight;
start = midHeight + 1;
}
}
printf("%d\n",optimalHeight);
free(tree);
return 0;
}
long long cutTree(const int* tree, int treeNum, int cuttingHeight) {
long long totalCut = 0;
for(int i = 0; i < treeNum; i++){
if (tree[i] > cuttingHeight) {
totalCut += tree[i] - cuttingHeight;
}
}
return totalCut;
}

View File

@ -0,0 +1,20 @@
import java.util.*;
public class _11279{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
for(int i=0; i<N; i++){
int input = sc.nextInt();
if(input==0){
if(maxHeap.isEmpty()) System.out.println(0);
else System.out.println(maxHeap.poll());
}
else {
maxHeap.offer(input);
}
}
sc.close();
}
}

View File

@ -0,0 +1,32 @@
def DaC(paper) :
n = len(paper)
firstColor = paper[0][0]
isMixed = False
for i in range(n):
for j in range(n):
if paper[i][j] != firstColor :
isMixed = True
break
if isMixed :
break
if not isMixed :
if firstColor == 0 :
return (1,0)
else :
return (0,1)
half = n//2
area1 = [row[0:half] for row in paper[0:half]]
area2 = [row[half:n] for row in paper[0:half]]
area3 = [row[0:half] for row in paper[half:n]]
area4 = [row[half:n] for row in paper[half:n]]
w1, b1 = DaC(area1)
w2, b2 = DaC(area2)
w3, b3 = DaC(area3)
w4, b4 = DaC(area4)
return (w1+w2+w3+w4, b1+b2+b3+b4)
paper = [list(map(int, input().split())) for _ in range(int(input()))]
print('\n'.join(map(str,DaC(paper))))