๋ฌธ์ :
programmers.co.kr/learn/courses/30/lessons/12979
๋ถ์ :
์๊ฐ์ด๊ณผ + ์๋ฌ๋ 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;
}
์ฝ๋๋ ํจ์ฌ ์งง๊ณ ์ง๊ด์ ์ธ๊ฑฐ ๊ฐ๋ค .. ๐
๋๊ธ