Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- BFS
- 프로그래머스
- 자바
- 코테
- binary search
- 문자열
- Java
- MySQL
- SWEA
- DP
- 백준
- COS PRO
- 문제풀이
- 코딩테스트
- SQL
- 시뮬레이션
- db
- 이분탐색
- BOJ
- priorityqueue
- 투포인터
- 문제해결
- dfs
- greedy
- 러닝일지
- 건강
- math
- oracle
- 운동기록
- 알고리즘
Archives
- Today
- Total
슈콩
[BOJ] 백준 3151 합이 0 본문

[문제]
https://www.acmicpc.net/problem/3151
[소스 코드]
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
long cnt = 0;
for(int i=0;i<n;i++) {
int start = i+1;
int end = n-1;
while(start<end) {
int val = arr[i] + arr[start] + arr[end];
if(val==0) {
if(arr[start]==arr[end]) {
int len = end - start + 1;
cnt += len * (len-1) / 2 ;
break;
}
int startCnt = 1;
int endCnt = 1;
while(start+1<end && arr[start]==arr[start+1]) {
start++;
startCnt++;
}
while(end-1>start && arr[end]==arr[end-1]) {
end--;
endCnt++;
}
cnt += startCnt * endCnt;
start++;
end--;
}
else if(val>0) end--;
else if(val<0) start++;
}
}
System.out.println(cnt);
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 백준 2003 수들의 합 2 (0) | 2025.11.11 |
|---|---|
| [BOJ] 백준 21921 블로그 (0) | 2025.11.11 |
| [BOJ] 백준 2805 나무 자르기 (0) | 2025.10.06 |
| [BOJ] 백준 9935 문자열 폭발 (0) | 2025.10.04 |
| [BOJ] 백준 28215 대피소 (0) | 2025.10.04 |