|
| 1 | +package study.week13; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +// 두 배열의 합 |
| 13 | +// 부분합, 투포인터 |
| 14 | +/** |
| 15 | + * 시간복잡도 : O(N^2) |
| 16 | + * 요소에 음수가 있을 줄 몰라서 잘못 접근을 했다. |
| 17 | + * 해결의 키 포인트는, 각 배열에사 나올 수 있는 부분배열 값들을 구한 뒤 오름차순 정렬(중복값은 Map으로 카운팅) 하는 것 |
| 18 | + * 정렬된 중복없는 A배열을 0부터, B배열을 B.size()-1 부터 탐색하여 두 합이 T가 되는지를 확인한다. |
| 19 | + * 이 때, 각 배열의 Map 에서 해당 요소의 개수만큼 조합이 나오므로 aCount * bCount 결과를 result에 더한다. |
| 20 | + * 오름 차순 정렬이므로,현재 A값 B값의 조합은 유일하므로, 둘다 left++, right-- 로 넘어간다. |
| 21 | + * 만약 T보다 크다면 right를 줄여서 작은 수로 더하고, T보다 작다면 left 를 증가시켜 더 큰 수를 더한다. |
| 22 | + * 물론 A를 right 로 B를 left 로 두고 연산해도 문제 없음 |
| 23 | + */ |
| 24 | +public class BOJ_2143 { |
| 25 | + |
| 26 | + public static void main(String[] args) throws IOException { |
| 27 | + BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); |
| 28 | + |
| 29 | + int T = Integer.parseInt(br.readLine()); |
| 30 | + |
| 31 | + int AN = Integer.parseInt(br.readLine()); |
| 32 | + String []inputA = br.readLine().split(" "); |
| 33 | + int [] A = new int[AN]; |
| 34 | + for(int i=0; i<AN; i++){ |
| 35 | + A[i] = A[i] + Integer.parseInt(inputA[i]); |
| 36 | + } |
| 37 | + |
| 38 | + int BN = Integer.parseInt(br.readLine()); |
| 39 | + String []inputB = br.readLine().split(" "); |
| 40 | + int [] B = new int[BN]; |
| 41 | + for(int i=0; i<BN; i++){ |
| 42 | + B[i] = B[i] + Integer.parseInt(inputB[i]); |
| 43 | + } |
| 44 | + |
| 45 | + long result = solution(T, A, B); |
| 46 | + |
| 47 | + System.out.println(result); |
| 48 | + } |
| 49 | + |
| 50 | + private static long solution(int T,int [] A, int [] B) { |
| 51 | + Map<Integer, Integer> aCnt = new HashMap<>(); |
| 52 | + Map<Integer, Integer> bCnt = new HashMap<>(); |
| 53 | + |
| 54 | + for(int i=0; i<A.length; i++){ |
| 55 | + int sum = 0; |
| 56 | + for(int j=i; j<A.length; j++){ |
| 57 | + sum += A[j]; |
| 58 | + aCnt.put(sum, aCnt.getOrDefault(sum, 0) + 1); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + for(int i=0; i<B.length; i++){ |
| 63 | + int sum = 0; |
| 64 | + for(int j=i; j<B.length; j++){ |
| 65 | + sum += B[j] ; |
| 66 | + bCnt.put(sum, bCnt.getOrDefault(sum, 0) + 1); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + List<Integer> AList = new ArrayList<>(aCnt.keySet()); |
| 71 | + List<Integer> BList = new ArrayList<>(bCnt.keySet()); |
| 72 | + Collections.sort(AList); |
| 73 | + Collections.sort(BList); |
| 74 | + |
| 75 | + long result = 0; |
| 76 | + int left = 0; |
| 77 | + int right = BList.size() - 1; |
| 78 | + |
| 79 | + while(left < AList.size() && right >=0){ |
| 80 | + int curA = AList.get(left); |
| 81 | + int curB = BList.get(right); |
| 82 | + int sum =curA + curB; |
| 83 | + |
| 84 | + if(sum == T){ |
| 85 | + int aCount = aCnt.get(curA); |
| 86 | + int bCount = bCnt.get(curB); |
| 87 | + |
| 88 | + result += ((long) aCount * bCount); |
| 89 | + left++; |
| 90 | + right--; |
| 91 | + }else if(sum < T){ |
| 92 | + left++; |
| 93 | + }else{ |
| 94 | + right--; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return result; |
| 99 | + } |
| 100 | +} |
0 commit comments