Skip to content

Commit d2ac94e

Browse files
committed
2 parents ff3f8c0 + dd045b7 commit d2ac94e

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

17298번 - 오큰수/Main.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
/* ************************************************************************** */
3+
/* */
4+
/* ::: ::: ::: */
5+
/* Problem Number: 17298 :+: :+: :+: */
6+
/* +:+ +:+ +:+ */
7+
/* By: thxogh1 <boj.kr/u/thxogh1> +#+ +#+ +#+ */
8+
/* +#+ +#+ +#+ */
9+
/* https://boj.kr/17298 #+# #+# #+# */
10+
/* Solved: 2025/04/28 19:07:58 by thxogh1 ### ### ##.kr */
11+
/* */
12+
/* ************************************************************************** */
13+
import java.io.BufferedReader;
14+
import java.io.InputStreamReader;
15+
import java.util.Stack;
16+
import java.util.StringTokenizer;
17+
18+
public class Main {
19+
20+
public static void main(String[] args) throws Exception {
21+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
22+
StringTokenizer st;
23+
StringBuilder sb = new StringBuilder();
24+
int n = Integer.parseInt(br.readLine());
25+
26+
st = new StringTokenizer(br.readLine());
27+
int[] arr = new int[n];
28+
for (int i = 0; i < n; i++) {
29+
arr[i] = Integer.parseInt(st.nextToken());
30+
}
31+
int[] ans = new int[n];
32+
Stack<Integer> stack = new Stack<>();
33+
stack.push(0);
34+
for (int i = 1; i < n; i++) {
35+
while (!stack.isEmpty() && arr[stack.peek()] < arr[i]) {
36+
ans[stack.pop()] = arr[i];
37+
}
38+
stack.push(i);
39+
}
40+
while (!stack.isEmpty()) {
41+
ans[stack.pop()] = -1;
42+
}
43+
for (int num : ans) {
44+
sb.append(num).append(" ");
45+
}
46+
System.out.println(sb);
47+
}
48+
}

17298번 - 오큰수/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 17298번: 오큰수 - <img src="https://static.solved.ac/tier_small/12.svg" style="height:20px" /> Gold IV
2+
3+
<!-- performance -->
4+
5+
<!-- 문제 제출 후 깃허브에 푸시를 했을 때 제출한 코드의 성능이 입력될 공간입니다.-->
6+
7+
<!-- end -->
8+
9+
## 문제
10+
11+
[문제 링크](https://boj.kr/17298)
12+
13+
<p>크기가 N인 수열 A = A<sub>1</sub>, A<sub>2</sub>, ..., A<sub>N</sub>이 있다. 수열의 각 원소 A<sub>i</sub>에 대해서 오큰수 NGE(i)를 구하려고 한다. A<sub>i</sub>의 오큰수는 오른쪽에 있으면서 A<sub>i</sub>보다 큰 수 중에서 가장 왼쪽에 있는 수를 의미한다. 그러한 수가 없는 경우에 오큰수는 -1이다.</p>
14+
15+
<p>예를 들어, A = [3, 5, 2, 7]인 경우 NGE(1) = 5, NGE(2) = 7, NGE(3) = 7, NGE(4) = -1이다. A = [9, 5, 4, 8]인 경우에는 NGE(1) = -1, NGE(2) = 8, NGE(3)&nbsp;= 8, NGE(4) = -1이다.</p>
16+
17+
## 입력
18+
19+
<p>첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에 수열 A의 원소 A<sub>1</sub>, A<sub>2</sub>, ..., A<sub>N</sub>&nbsp;(1 ≤ A<sub>i</sub> ≤ 1,000,000)이&nbsp;주어진다.</p>
20+
21+
## 출력
22+
23+
<p>총 N개의 수 NGE(1), NGE(2), ..., NGE(N)을 공백으로 구분해 출력한다.</p>
24+
25+
## 소스코드
26+
27+
[소스코드 보기](Main.java)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: ::: ::: */
4+
/* Problem Number: 7453 :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: thxogh1 <boj.kr/u/thxogh1> +#+ +#+ +#+ */
7+
/* +#+ +#+ +#+ */
8+
/* https://boj.kr/7453 #+# #+# #+# */
9+
/* Solved: 2025/04/25 18:44:34 by thxogh1 ### ### ##.kr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
import java.io.BufferedReader;
14+
import java.io.InputStreamReader;
15+
import java.util.Arrays;
16+
import java.util.StringTokenizer;
17+
18+
public class Main {
19+
20+
public static void main(String[] args) throws Exception {
21+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
22+
StringTokenizer st;
23+
24+
int n = Integer.parseInt(br.readLine());
25+
int[] a = new int[n];
26+
int[] b = new int[n];
27+
int[] c = new int[n];
28+
int[] d = new int[n];
29+
int[] ab = new int[n * n];
30+
int[] cd = new int[n * n];
31+
for (int i = 0; i < n; i++) {
32+
st = new StringTokenizer(br.readLine());
33+
a[i] = Integer.parseInt(st.nextToken());
34+
b[i] = Integer.parseInt(st.nextToken());
35+
c[i] = Integer.parseInt(st.nextToken());
36+
d[i] = Integer.parseInt(st.nextToken());
37+
}
38+
int idx = 0;
39+
for (int i = 0; i < n; i++) {
40+
for (int j = 0; j < n; j++) {
41+
ab[idx] = a[i] + b[j];
42+
cd[idx] = c[i] + d[j];
43+
idx++;
44+
}
45+
}
46+
Arrays.sort(ab);
47+
Arrays.sort(cd);
48+
49+
int s = 0;
50+
int e = n * n - 1;
51+
long ans = 0;
52+
53+
while (s < n * n && e >= 0) {
54+
if (ab[s] + cd[e] < 0) {
55+
s++;
56+
} else if (ab[s] + cd[e] > 0) {
57+
e--;
58+
} else if (ab[s] + cd[e] == 0) {
59+
long sc = 1;
60+
long ec = 1;
61+
while (s + 1 < n * n && ab[s] == ab[s + 1]) {
62+
sc++;
63+
s++;
64+
}
65+
while (e - 1 >= 0 && cd[e] == cd[e - 1]) {
66+
ec++;
67+
e--;
68+
}
69+
ans += sc * ec;
70+
s++;
71+
}
72+
}
73+
System.out.println(ans);
74+
}
75+
}
76+
77+
// 1000 000 000 000
78+
// n^4 = 4조 => 브루트포스 다메
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 7453번: 합이 0인 네 정수 - <img src="https://static.solved.ac/tier_small/14.svg" style="height:20px" /> Gold II
2+
3+
<!-- performance -->
4+
5+
<!-- 문제 제출 후 깃허브에 푸시를 했을 때 제출한 코드의 성능이 입력될 공간입니다.-->
6+
7+
<!-- end -->
8+
9+
## 문제
10+
11+
[문제 링크](https://boj.kr/7453)
12+
13+
<p>정수로 이루어진 크기가 같은 배열 A, B, C, D가 있다.</p>
14+
15+
<p>A[a], B[b], C[c], D[d]의&nbsp;합이 0인&nbsp;(a, b, c, d) 쌍의 개수를 구하는 프로그램을 작성하시오.</p>
16+
17+
## 입력
18+
19+
<p>첫째 줄에 배열의 크기 n (1 ≤ n ≤ 4000)이 주어진다. 다음 n개 줄에는 A, B, C, D에 포함되는 정수가 공백으로 구분되어져서 주어진다. 배열에 들어있는 정수의 절댓값은 최대 2<sup>28</sup>이다.</p>
20+
21+
## 출력
22+
23+
<p>합이 0이 되는 쌍의 개수를 출력한다.</p>
24+
25+
## 소스코드
26+
27+
[소스코드 보기](Main.java)

0 commit comments

Comments
 (0)