728x90
https://www.acmicpc.net/problem/2805
2805번: 나무 자르기
첫째 줄에 나무의 수 N과 상근이가 집으로 가져가려고 하는 나무의 길이 M이 주어진다. (1 ≤ N ≤ 1,000,000, 1 ≤ M ≤ 2,000,000,000) 둘째 줄에는 나무의 높이가 주어진다. 나무의 높이의 합은 항상 M보
www.acmicpc.net
[나의 코드]
let fs = require("fs");
let input = fs.readFileSync("index.txt").toString().split("\n");
let [n,m] = input[0].split(" ").map(Number)
let arr =input[1].split(" ").map(Number)
let start=1
let end = arr.reduce((a,b)=>Math.max(a,b))
let result=0;
while(start<=end){
let mid= parseInt((start+end)/2)
let total = 0
for(x of arr){
total= total+Math.max(x-mid,0)
}
if(total>=m){
result=mid;
start=mid+1
}else{
end=mid-1
}
}
console.log(result)
https://www.acmicpc.net/problem/1654
1654번: 랜선 자르기
첫째 줄에는 오영식이 이미 가지고 있는 랜선의 개수 K, 그리고 필요한 랜선의 개수 N이 입력된다. K는 1이상 10,000이하의 정수이고, N은 1이상 1,000,000이하의 정수이다. 그리고 항상 K ≦ N 이다. 그
www.acmicpc.net
[나의 코드]
let fs = require("fs");
let input = fs.readFileSync("index.txt").toString().split("\n");
let [k,n] = input[0].split(" ").map(Number)
let arr =[]
for(i=1;i<k+1;i++){
arr.push(Number(input[i]))
}
let start = 1
let end = arr.reduce((a,b)=>Math.max(a,b))
let result =0
while(start<=end){
let mid= parseInt((start+end)/2)
let total = 0;
for(x of arr){
total=total+parseInt(x/mid)
}
if(total>=n){
result=mid
start=mid+1
}else{
end=mid-1
}
}
console.log(result)
728x90
'알고리즘' 카테고리의 다른 글
투포인터 슬라이더 문제 풀이(1) (0) | 2023.05.07 |
---|---|
투 포인터 알고리즘 (0) | 2023.05.07 |
3. 파라메트릭 서치 이해하기 (0) | 2023.05.01 |
2. 정렬된 배열에서 특정 원소의 개수 구하기 (0) | 2023.04.30 |
1. 이진 탐색 알고리즘 이해하기 (0) | 2023.04.30 |