[백준] 18513번 샘터 (C++)

2025. 12. 25. 21:58·Algorithm
728x90

https://www.acmicpc.net/problem/18513

 

 

N개의 샘터와 M개의 집을 세우려고 할때 불행도의 합을 최소로 해야 한다.

 

불행도는 세우려는 집의 위치를 기준으로 가장 가까운 샘터의 위치의 차이의 합이다.

 

따라서 이를 해결하기 위해서 샘터 개수(N) 만큼 순회하며 샘터 위치를 기준으로 좌우로 배치하며 크기를 최소화 하는 방법을 생각하여

 

bfs + set을 사용하였다.

 

 

정답코드

 

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define endl "\n"
#define MAX 1e9

struct coordinate {
    int x;
    int y;
    int r;
};

struct halloween {
    int cnt;
    int score;
};

// int dx[] = {-1, 1, 0, 0, 1, -1, -1, 1};
// int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
// int dx[] = {0, 0, 1, -1};
// int dy[] = {1, -1, 0, 0};
int dx[] = {-1, 1};
int N, K;
ll answer;
queue<pii> q;
set<int> st;

void bfs() {
    while(!q.empty()) {
        int x = q.front().first;
        int bad = q.front().second;
        q.pop();

        for(int i = 0; i < 2; i++) {
            int nx = x + dx[i];

            if(st.count(nx) == 0) {
                q.push({nx, bad+1});
                answer += bad + 1;
                K--;
                st.insert(nx);
            }

            if(!K) {
                cout << answer;
                return;
            }
        }
    }
}

void solve() {
    bfs();
}

void input() {
    cin >> N >> K;

    for(int i = 0; i < N; i++) {
        int a;
        cin >> a;
        q.push({a, 0});
        st.insert(a);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    input();
    solve();
}

 

728x90
저작자표시 (새창열림)

'Algorithm' 카테고리의 다른 글

[백준] 20366번 같이 눈사람 만들래? (C++)  (1) 2025.12.27
[백준] 14722번 우유 도시 (C++)  (0) 2025.12.26
[백준] 1520번 내리막 길 (C++)  (0) 2025.12.24
[백준] 14595번 동방 프로젝트 (Large) (C++)  (0) 2025.12.23
[백준] 14925번 목장 건설하기 (C++)  (0) 2025.12.22
'Algorithm' 카테고리의 다른 글
  • [백준] 20366번 같이 눈사람 만들래? (C++)
  • [백준] 14722번 우유 도시 (C++)
  • [백준] 1520번 내리막 길 (C++)
  • [백준] 14595번 동방 프로젝트 (Large) (C++)
쿨쿨.
쿨쿨.
  • 쿨쿨.
    All of the life
    쿨쿨.
  • 전체
    오늘
    어제
    • 분류 전체보기 (279) N
      • Programming (47)
        • C, C++ (18)
        • Python (6)
        • Java (1)
        • HTML,CSS,JS (3)
        • SQL(DB) (13)
        • SpringBoot (3)
        • Android (2)
        • CI,CD (1)
      • Algorithm (173)
        • Review (4)
      • Security (14)
        • WebHacking (3)
        • Websecurity (11)
      • OS (19)
        • Linux (12)
        • Mac os (2)
      • 머신러닝 (1)
      • CS(Computer Science) (12)
        • 컴퓨터 네트워크 (3)
        • 컴퓨터 구조 (1)
        • 인공지능 (8)
      • Docker (1)
      • Dev Book Review (3)
        • Clean Code (1)
        • Effective Java (0)
        • Real MySQL (2)
      • SWM (1)
      • Review (6) N
      • AWS (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    코딩
    트리
    Leviathan
    Bandit
    구현
    WebSecurity
    다익스트라
    우선순위큐
    이분탐색
    백준
    그래프 이론
    BFS
    DP
    wargame
    깊이우선탐색
    재귀
    다이나믹 프로그래밍
    우선순위 큐
    누적합
    에라토스테네스의 체
    그리디
    정렬
    시뮬레이션
    백트래킹
    linux
    브루트포스
    c언어
    투포인터
    DFS
    비트마스킹
  • 최근 댓글

  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.6
쿨쿨.
[백준] 18513번 샘터 (C++)
상단으로

티스토리툴바