Skip to content

251211 : [BOJ 17779] 게리맨더링 2 - #2246

Merged
sunha20 merged 1 commit into
mainfrom
sunha/2243/2
Jan 13, 2026
Merged

sunha20 merged 1 commit into
mainfrom
sunha/2243/2

Conversation

@sunha20

@sunha20 sunha20 commented Dec 11, 2025

Copy link
Copy Markdown
Contributor

🚀 이슈 번호

Resolve: {#2243}

🧩 문제 해결

스스로 해결: ✅

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지 완전탐색
  • 🔹 어떤 방식으로 접근했는지

변수 x, y, d1, d2를 문제에서 주어지는 범위를 벗어나지 않는 한에서 전부 전부 탐색
만일 각 변수가 1~20(N)까지 가능하다고 해도 160000 경우의 수고, 거기에 모든 구역을 값을 탐색하는 것도 최대 400이니까, 무지성으로 완전탐색 돌려도 됨

5구역을 제외하는 조건문을 추가하는 부분에서 조금 헤멨음. 배열관점보다 직선의 방정식 느낌으로 경계선 식을 구하고 구역이 그 경계보다 위인지 아래인지를 체크하면됨.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(N^6)
  • 이유:

각 4개의 변수들에 대한 모든 조합 -> N^4
각 조합마다 구역 전부 탐색 -> N^2

💻 구현 코드

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        int[][] city;
        int[] population;
        int N, diff = Integer.MAX_VALUE;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        N = Integer.parseInt(br.readLine());
        city = new int[N+1][N+1];

        for (int i=1; i<=N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j=1; j<=N; j++) {
                city[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        for (int x=1; x<=N; x++) {
            for (int y=1; y<=N; y++) {
                for (int d2=1; d2<=N-y; d2++) {
                    for (int d1=1; d1<=y; d1++) {

                        population = new int[5];
                        for (int r=1; r<=N; r++) {
                            for (int c=1; c <=N; c++) {
                                int now = city[r][c];

                                // 각 구역 판별
                                if (r < x+d1 && c <= y && (x+y) > (r+c)) population[0] += now;
                                else if (r <= x+d2 && y < c && (x-y) > (r-c)) population[1] += now;
                                else if (x+d1<=r && c < y-d1+d2 && (x-y+(2*d1) < (r-c))) population[2] += now;
                                else if (x+d2<r && y-d1+d2 <= c && (x+y+(2*d2) < (r+c))) population[3] += now;
                                else population[4] += now;
                            }
                        }
                        int max_p = 0, min_p = Integer.MAX_VALUE;
                        for (int i=0; i<5; i++) {
                            if (population[i] > max_p) max_p = population[i];
                            if (population[i] < min_p) min_p = population[i];
                        }
                        if ((max_p - min_p) < diff) {
                            diff = max_p - min_p;
                        }
                    }
                }
            }
        }
        System.out.println(diff);
    }
}

@sunha20 sunha20 self-assigned this Dec 11, 2025
@sunha20 sunha20 linked an issue Dec 11, 2025 that may be closed by this pull request
@sunha20
sunha20 merged commit d0039a1 into main Jan 13, 2026
1 check passed
@sunha20
sunha20 deleted the sunha/2243/2 branch January 13, 2026 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

251211 : 코딩테스트

1 participant