๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Summer/Winder Coding ๊ธฐ์ง€๊ตญ ์„ค์น˜ level3

by Jouureee 2021. 5. 6.

๋ฌธ์ œ :

programmers.co.kr/learn/courses/30/lessons/12979

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ธฐ์ง€๊ตญ ์„ค์น˜

N๊ฐœ์˜ ์•„ํŒŒํŠธ๊ฐ€ ์ผ๋ ฌ๋กœ ์ญ‰ ๋Š˜์–ด์„œ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ค‘์—์„œ ์ผ๋ถ€ ์•„ํŒŒํŠธ ์˜ฅ์ƒ์—๋Š” 4g ๊ธฐ์ง€๊ตญ์ด ์„ค์น˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์ˆ ์ด ๋ฐœ์ „ํ•ด 5g ์ˆ˜์š”๊ฐ€ ๋†’์•„์ ธ 4g ๊ธฐ์ง€๊ตญ์„ 5g ๊ธฐ์ง€๊ตญ์œผ๋กœ ๋ฐ”๊พธ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ 5

programmers.co.kr

 

๋ถ„์„ :

 

์‹œ๊ฐ„์ดˆ๊ณผ + ์—๋Ÿฌ๋‚œ c++ ์ฝ”๋“œ :

//
//  [SW] 2018_BaseStation.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/05/06.
//

#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <math.h>

using namespace std;

//vector<int> stations = {4, 11};
vector<int> stations = {9};
int w = 2;
int n = 16;


int main(){
    int arr[n+1];
    memset(arr, 0, sizeof(arr));


    for(int i = 0; i < stations.size(); i++){
        int start = stations.at(i) - w;
        int end = stations.at(i) + w;
        while(start <= end){
            arr[start] = 1;
            start++;
        }
    }
    
    
    vector<pair<int, int>> segment;

    int s = 0, d = 0;
    int j = 1;
    while(j <n){
        if(arr[j] == 0){
            if(s == 0){
                s = j;
            }else{
                d = j;
            }
        }else{
            if(s != 0){
                d = j;
                segment.push_back({s, d});
                s = 0; d = 0;
            }
        }
        j++;
    }
    //0์œผ๋กœ ๋๋‚ฌ์„ ๋•Œ
    if(s != 0){
        d = j;
        segment.push_back({s, d});
    }

    int antenaCount = 0;


    for(int i =0; i <segment.size(); i++){
        float segLength = (float)segment.at(i).second - (float)segment.at(i).first;
        antenaCount += ceil(segLength / (1+ 2*w));
    }
    cout << antenaCount << '\n';
    
    return 0;
}

 

๊ธฐ์ง€๊ตญ์„ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๊ธฐ์ง€๊ตญ์ด ์„ค์น˜๋œ ๊ณณ์—๋Š” 1์„ ๋„ฃ์–ด 0์ธ ๊ตฌ๊ฐ„์„ segment ๋ฒกํ„ฐ์— pair ์‹์œผ๋กœ ๋‹ด์•„ ๊ทธ ๊ตฌ๊ฐ„์˜ ๊ธธ์ด์— ๋Œ€ํ•ด ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ์•ˆํ…Œ๋‚˜๋ฅผ ๊ตฌํ•ด ๋”ํ•˜๋Š” ์‹์œผ๋กœ ์ž‘์„ฑํ–ˆ๋‹ค.

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ๋Œ€ํ•ด 2๊ฐœ๊ฐ€ ํ†ต๊ณผ๊ฐ€ ์•ˆ๋๊ณ  (1๋ฒˆ, 15๋ฒˆ) ๊ทธ๋ฆฌ๊ณ  ํšจ์œจ์„ฑ ์ œ๋กœ์˜€๋‹ค ํ•˜ํ•˜ ๐Ÿ˜๐Ÿ˜

 

๊ทธ๋ž˜์„œ ์งˆ๋ฌธ ํ’€์ด๋ฅผ ๋ณด๊ณ  ๋ฐฐ์—ด ์—†์ด ํ‘ธ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค ํ•˜์—ฌ ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์—ฌ ํ’€์—ˆ๋‹ค.

์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค๋‹ˆ ..! ์‹ ๊ธฐํ•˜๋‹ค 

 

//
//  [SW] 2018_BaseStation.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/05/06.
//

#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <math.h>

using namespace std;

//vector<int> stations = {4, 11};
vector<int> stations = {9};
int w = 2;
int n = 16;


int main(){
    int begin = 0; int answer = 0;
           
       for(int i = 0; i < stations.size(); i++){
           int start = stations.at(i) - w - 1;
           int nextStart = stations.at(i) + w - 1;
           if(begin >= start && begin <= nextStart){
               //station ๊ฒน์น˜๋Š” ๋ถ€๋ถ„
               begin = nextStart + 1;
               continue;
           }
           answer += ceil((float)(start - begin)/ (float)(1 + 2*w));
           begin = nextStart + 1;
       }
       
       if(begin < n){
           answer += ceil((float)(n - begin)/ (float)(1 + 2*w));
       }
       cout <<answer << '\n';
    return 0;
}

 

์ฝ”๋“œ๋„ ํ›จ์”ฌ ์งง๊ณ  ์ง๊ด€์ ์ธ๊ฑฐ ๊ฐ™๋‹ค .. ๐Ÿ‘

๋Œ“๊ธ€