본문 바로가기

프로그래밍/백준

백준 acm 7453 - 합이 0인 네 정수

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


- 브루트 포스

- 이분탐색


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
 
        int n = sc.nextInt();
        int[][] get = new int[4][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 4; j++) {
                get[j][i] = sc.nextInt();
            }
        }
 
        int[] plus1 = new int[n * n];
        int[] plus2 = new int[n * n];
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                plus1[count] = get[0][i] + get[1][j];
                plus2[count++= get[2][i] + get[3][j];
            }
        }
        Arrays.sort(plus1);
        Arrays.sort(plus2);
 
        long zero = 0;
        int min = 0, max = n*n-1;
        long count1, count2;
        int temp1, temp2;
 
        while(min < n*&& max >= 0){
            if(plus1[min] + plus2[max] < 0) min++;
            else if(plus1[min]+plus2[max] == 0){
                count1 = 0;
                count2 = 0;
                temp1 = plus1[min];
                temp2 = plus2[max];
                for(; min < n*n; min++){
                    if(plus1[min] == temp1)count1++;
                    else break;
                }
                for(; max >= 0; max--){
                    if(plus2[max] == temp2) count2++;
                    else break;
                }
                zero += count1*count2;
            }else max--;
        }
        System.out.print(zero);
 
    }
}
cs


'프로그래밍 > 백준' 카테고리의 다른 글

2577 숫자의 개수  (0) 2017.10.14
1152 단어의 개수  (0) 2017.10.07
백준 acm 2448 - 별찍기 (11)  (0) 2017.10.06
백준 acm 4673 - 셀프 넘버  (0) 2017.10.01
백준 acm 1915 가장 큰 정사각형  (0) 2017.07.19