์ƒˆ์†Œ์‹

Programming Problem Solving

[PPS] Electricity Poles (C++)

  • -
๋ฐ˜์‘ํ˜•

<Problem>

 

Electricity Poles

๐‘› Electricity poles are on a horizontal line transmitting electricity through an electric wire. Each pole is located at a unique point between 0 and 1,000,000,000, inclusively, on the horizontal line. The intervals between two adjacent points are the same. The Office of Energy wants to select ๐‘˜ poles among the total of ๐‘› poles to install electricity amplifiers for reliable energy transmission. Considering threats of electromagnetic wave interference, the ๐‘˜ poles must be selected such that the minimum distance between two selected poles must be as large

as possible. Write a program that finds the minimum distance between two poles of the ๐‘˜ electricity poles that the Office of Energy will select.

 

 

<Requirements>

Input

  • The first line from the standard input has two numbers ๐‘› and ๐‘˜.
    The first number, ๐‘› stands for the number of electricity poles where 2 ≤ ๐‘› ≤ 100,000. The other number, ๐‘˜ represents the number of poles to install amplifiers for 2 ≤ ๐‘˜ ≤ ๐‘› ≤ 100,000.
  • Thereafter, ๐‘› lines follow, each of which contains one number that represents ๐‘ฅ๐‘–, the location of an electricity pole for 1 ≤ ๐‘ฅ๐‘– ≤ 1,000,000,000. Note that these numbers are not sorted in any order.

Output

  • Print out one number to the standard output within 0.5 seconds.

 

 

 

<Solved>

 

Problem Analysis

  • ์ „๋ด‡๋Œ€ n๊ฐœ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์ฆํญ๊ธฐ๋ฅผ ์„ค์น˜ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. 
  • ์„ค์น˜ํ•˜๊ณ  ์‹ถ์€ ์ฆํญ๊ธฐ๋Š” k ( 2<= k <= 10000) ์ด๋ฉฐ, ์ „๋ด‡๋Œ€ ์‚ฌ์ด ์ตœ๋Œ€ ์‚ฌ์ด ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์„ค์น˜ํ•ด์•ผํ•œ๋‹ค.
    ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ฆํญ๊ธฐ๋Š” ๋ฐ˜๋“œ์‹œ ์–‘ ๋์— ์œ„์น˜ํ•œ ์ „๋ด‡๋Œ€์—๋Š” ๋ฐ˜๋“œ์‹œ ์„ค์น˜๋˜์–ด์•ผ ํ•œ๋‹ค.
  • ์„ค์น˜ํ•œ ์ „๋ด‡๋Œ€ ์‚ฌ์ด ์ตœ๋Œ€ ์‚ฌ์ด ๊ฑฐ๋ฆฌ ์ค‘ ๊ฐ€์žฅ ์ตœ์†Œ์ธ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ผ 
  • ์ฆํญ๊ธฐ๋ฅผ ๋‘˜ ์ˆ˜ ์žˆ๋Š” ์ž๋ฆฌ๋Š” ๋งจ ์•ž๊ณผ ๋งจ ๋’ค, ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์‚ฌ์ด์— ์ตœ์†Œ๊ฑฐ๋ฆฌ๊ฐ€ ์„ฑ๋ฆฝ๋œ ๊ฑฐ๋ฆฌ์— ์œ„์น˜ํ•œ ์ „๋ด‡๋Œ€์ด๋‹ค. 

 

Solution

  1. ์ „๋ด‡๋Œ€ ๊ฐœ์ˆ˜ n ๊ณผ ์„ค์น˜ํ•˜๊ณ  ์‹ถ์€ ์ฆํญ๊ธฐ k๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. 
  2. n์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ „๋ด‡๋Œ€์˜ ์œ„์น˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ์ด๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์†”ํŒ…ํ•œ๋‹ค. 
  3. min_length์™€ max_length๋ฅผ ๊ตฌํ•œ๋‹ค. ์ฒซ min_length๋Š” ์ตœ์†Œ๊ธธ์ด๊ฐ€ 1์ด๋ฏ€๋กœ 1๋กœ ์„ค์ •ํ•˜๊ณ  max_length๋Š” ์–‘ ๋ ์ „๋ด‡๋Œ€ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋กœ ์„ค์ • ํ•œ๋‹ค.
  4. min_length๊ฐ€ max_length๋ณด๋‹ค ์ปค์งˆ๋•Œ๊นŒ์ง€ ์•„๋ž˜๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค
    1. ์ฒดํฌ ์ˆซ์ž๊ฐ€ k๋ณด๋‹ค ํฌ๋‹ค๋ฉด avg_length๋งŒํผ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์ตœ์†Œ๊ฑฐ๋ฆฌ๋กœ ๋งŒ์กฑํ–ˆ์œผ๋ฏ€๋กœ min_length๋ฅผ avg_length+1๋กœ ์„ค์ •ํ•˜์—ฌ avg_length๊ฐ’์„ ์žฌ์„ค์ • ํ•œ๋‹ค.
    2. ์ฒดํฌ ์ˆซ์ž๊ฐ€ k๋ณด๋‹ค ํฌ๋‹ค๋ฉด avg_length๋งŒํผ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์ตœ์†Œ๊ฑฐ๋ฆฌ๋กœ ๋ถˆ๋งŒ์กฑํ–ˆ์œผ๋ฏ€๋กœ max_length๋ฅผ avg_length-1๋กœ ์„ค์ •ํ•˜์—ฌ avg_length๊ฐ’์„ ์žฌ์„ค์ • ํ•œ๋‹ค.
  5. max_length๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค

 

Time Complexity

  • n (์ž…๋ ฅ๋ฐ›์€ ์ „๋ด‡๋Œ€ ๊ฐœ์ˆ˜)
  • O(n*logn)

 

<Code>

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int electricityPoles(int n, int k, vector<int> &poles)
{
    int left = 1, right = poles[n - 1] - poles[0];

    while (1)
    {
        if (left > right)
        {
            break;
        }

        int mid = (left + right) / 2;

        int cnt = 1;
        int start = 0;
        for (int i = 1; i < n; i++)
        {
            if (poles[i] - poles[start] >= mid)
            {
                cnt++;
                start = i;
            }
        }

        if (cnt >= k)
        {
            left = mid + 1;
        }
        else
        {
            right = mid - 1;
        }
    }

    return right;
}

int main()
{
    int n, k;
    vector<int> poles;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
    {
        int pole;
        cin >> pole;
        poles.push_back(pole);
    }

    sort(poles.begin(), poles.end());

    int ans = electricityPoles(n, k, poles);

    cout << ans << endl;
}

 

 

๋ฐ˜์‘ํ˜•

'Programming Problem Solving' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[PPS] Task Force (C++)  (0) 2023.09.20
Contents
-

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.