알고리즘

백준 C# - 예산(실버 2)

미역제자 2024. 11. 1. 23:08

 

최초 생각:

최댓값을 찾은 다음, 그 값에서 1씩 빼가면서 계산하도록 진행

결과 -> 시간 초과

using System;
using System.Collections.Generic;

namespace Baekjoon
{
    class Program
    {
        static void Main()
        {
            string N_string = Console.ReadLine();
            int N = int.Parse(N_string);
            string N_Wanted = Console.ReadLine();
            int[] Wanted = new int[N];
            string[] Wanted_string = N_Wanted.Split();
            int firstTotal = 0;
            int MaxValue = 0;
            for (int i = 0; i < N; i++) {
                Wanted[i] = int.Parse( Wanted_string[i]);
                firstTotal += Wanted[i];
                if(MaxValue < Wanted[i])
                {
                    MaxValue = Wanted[i];
                }
            }
            string MaxString= Console.ReadLine();
            int Max = int.Parse(MaxString);

            while (firstTotal > Max)
            {
                MaxValue = 0;
                int maxint = 0;
                for (int i = 0; i < N; i++)
                {
                    if (MaxValue < Wanted[i])
                    {
                        MaxValue = Wanted[i];
                        maxint = i;
                    }
                }
                firstTotal = 0;
                for (int i = 0; i < N; i++)
                {
                    if(i == maxint)
                    {
                        Wanted[i]--;
                    }
                    firstTotal += Wanted[i];
                    
                }
                
            }
            Console.WriteLine(MaxValue.ToString());


        }

    }
}


문제의 분류를 다시 보니 2진탐색이였다.

문제를 2진탐색으로 다시 풀이

결과 -> 정답.

using System;

namespace Baekjoon
{
    class Program
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            string[] Wanted_string = Console.ReadLine().Split();
            int[] Wanted = new int[N];

            int MaxValue = 0;
            for (int i = 0; i < N; i++)
            {
                Wanted[i] = int.Parse(Wanted_string[i]);
                if (MaxValue < Wanted[i])
                {
                    MaxValue = Wanted[i];
                }
            }
            int Max = int.Parse(Console.ReadLine());

            int left = 0, right = MaxValue, result = 0;
            while (left <= right)
            {
                int mid = (left + right) / 2;
                int sum = 0;

                foreach (int want in Wanted)
                {
                    sum += Math.Min(want, mid);
                }

                if (sum <= Max)
                {
                    result = mid;
                    left = mid + 1;
                }
                else
                {
                    right = mid - 1;
                }
            }

            Console.WriteLine(result);
        }
    }
}

'알고리즘' 카테고리의 다른 글

백준 1049번 C#  (0) 2024.08.02
정렬 알고리즘  (0) 2024.07.03
Big O 표기법  (0) 2024.07.01