ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

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

 

7453๋ฒˆ: ํ•ฉ์ด 0์ธ ๋„ค ์ •์ˆ˜

์ฒซ์งธ ์ค„์— ๋ฐฐ์—ด์˜ ํฌ๊ธฐ n (1 ≤ n ≤ 4000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ n๊ฐœ ์ค„์—๋Š” A, B, C, D์— ํฌํ•จ๋˜๋Š” ์ •์ˆ˜๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด์ ธ์„œ ์ฃผ์–ด์ง„๋‹ค. ๋ฐฐ์—ด์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜์˜ ์ ˆ๋Œ“๊ฐ’์€ ์ตœ๋Œ€ 228์ด๋‹ค.

www.acmicpc.net

[๋ฌธ์ œ]

 

[ํ’€์ด]

4๊ฐœ ์กฐํ•ฉ์„ ์ƒ์„ฑํ•˜์—ฌ ๊ณ„์‚ฐ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์™€์„œ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ์•„๋ž˜์˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ’€์ดํ•˜์˜€์Šต๋‹ˆ๋‹ค.

https://skdltm117.tistory.com/49

 

[๋ฐฑ์ค€] 7453๋ฒˆ - ํ•ฉ์ด 0์ธ ๋„ค ์ •์ˆ˜ (java)

Baekjoon 7453 - ํ•ฉ์ด 0์ธ ๋„ค ์ •์ˆ˜ (ํด๋ฆญ ์‹œ ์ด๋™) ๋ฌธ์ œ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์€ ๋ฐฐ์—ด A, B, C, D๊ฐ€ ์žˆ๋‹ค. A[a], B[b], C[c], D[d]์˜ ํ•ฉ์ด 0์ธ (a, b, c, d) ์Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์‹œ๊ฐ„..

skdltm117.tistory.com

A์™€ B, C์™€ D๋ฅผ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•˜์—ฌ preSum[][] ์ €์žฅํ•˜์—ฌ ํˆฌํฌ์ธํ„ฐ ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

  • data[][]์— A,B,C,D์˜ ์ˆซ์ž๋“ค์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. 
  • data[0] X data[1] , data[2] X data[3] ์„ preSum[0], preSum[1]์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  • ํˆฌํฌ์ธํ„ฐ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด preSum[0]์™€ preSum[1]์„ Arrays.sort()๋ฅผ ํ†ตํ•ด ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • preSum[0]์€ ์™ผ์ชฝ๋ถ€ํ„ฐ,  preSum[1]์€ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉฐ ํ•ฉ์ด 0์ด ๋˜๋Š” ๊ฐฏ์ˆ˜๋ฅผ ์„ธ์–ด๊ฐ‘๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ ๊ฐ™์œผ ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด, ๊ทธ ์ˆซ์ž์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ์„œ ๊ณฑํ•ด์ฃผ๋Š” ๊ณผ์ •์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

[์ฝ”๋“œ]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    static int N;
    static long answer = 0;
    static int[][] data;
    static int preSum[][];
    static StringTokenizer st;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        N = Integer.parseInt(br.readLine());
        data = new int[N][4];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            data[i][0] = Integer.parseInt(st.nextToken());
            data[i][1] = Integer.parseInt(st.nextToken());
            data[i][2] = Integer.parseInt(st.nextToken());
            data[i][3] = Integer.parseInt(st.nextToken());
        }

        preSum = new int[2][N * N];
        int count = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                preSum[0][count] = data[i][0] + data[j][1];
                preSum[1][count++] = data[i][2] + data[j][3];
            }
        }
        Arrays.sort(preSum[0]);
        Arrays.sort(preSum[1]);
        int first = 0;
        int second = preSum[0].length - 1;

        int end = N * N;
        while (first < end && 0 <= second) {
            int sum =  preSum[0][first] + preSum[1][second];
            int firstCnt =1;
            int secondCnt = 1;
            if (sum == 0) {
                while (first <= end - 2 && preSum[0][first] == preSum[0][first + 1]) {
                    firstCnt++;
                    first++;
                }
                while (0 < second && preSum[1][second] == preSum[1][second - 1]) {
                    secondCnt++;
                    second--;
                }
                answer += (long) firstCnt * secondCnt;
            }

            if (sum < 0) {
                first++;
            } else{
                second--;
            }
        }
        System.out.println(answer);
    }
}