[C#] 랜선 자르기

소슬잎·2023년 11월 23일

백준 문제

hhttps://www.acmicpc.net/problem/1654ttps://school.programmers.co.kr/learn/courses/30/lessons/131701

풀이 후기

1. 분석

이분 탐색. 잘못 구현하면 예외가 좀 많다.

2. 실행 결과

3. 코드

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 백준{
    public class Solution{
        public static void Main()
        {
            var read = Console.ReadLine()!.Split(" ").Select(int.Parse).ToArray();
            var lines = Enumerable.Range(0, read[0]).Select(_ => int.Parse(Console.ReadLine()!)).ToArray();

            var target = read[1];
            var lineEa = 0.0;
            var start = 1.0;
            var end = (double)lines.Max();
            var mid = 0.0;

            while (start <= end)
            {
                mid = (int)((start + end) / 2);
                lineEa = lines.Select(l => (int)(l / mid)).Sum(Convert.ToDouble);

                if (lineEa < target)
                {
                    end = mid - 1;
                }
                else
                {
                    start = mid + 1;
                }
            }

            Console.WriteLine(end);
        }
    }
}

4. 깨달은 점

  1. 최적 크기 무시하고 크게 잡는게 편하다.
  2. 이분 탐색의 조건과 부등호에 대해 잘 이해해야 한다.
  3. 12시 넘으면 잠이나 자자.

https://www.acmicpc.net/board/view/124982
nebobyeoli님 감사합니다.

profile
그냥 바보

0개의 댓글