๐ ๋ฐฑ์ค 1920 ๋ฌธ์ https://www.acmicpc.net/problem/1920
๋ฌธ์
N๊ฐ์ ์ ์ A[1], A[2], โฆ, A[N]์ด ์ฃผ์ด์ ธ ์์ ๋, ์ด ์์ X๋ผ๋ ์ ์๊ฐ ์กด์ฌํ๋์ง ์์๋ด๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ ์์ฐ์ N(1 โค N โค 100,000)์ด ์ฃผ์ด์ง๋ค. ๋ค์ ์ค์๋ N๊ฐ์ ์ ์ A[1], A[2], โฆ, A[N]์ด ์ฃผ์ด์ง๋ค. ๋ค์ ์ค์๋ M(1 โค M โค 100,000)์ด ์ฃผ์ด์ง๋ค. ๋ค์ ์ค์๋ M๊ฐ์ ์๋ค์ด ์ฃผ์ด์ง๋๋ฐ, ์ด ์๋ค์ด A์์ ์กด์ฌํ๋์ง ์์๋ด๋ฉด ๋๋ค. ๋ชจ๋ ์ ์์ ๋ฒ์๋ -231 ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ 231๋ณด๋ค ์๋ค.
์ถ๋ ฅ
M๊ฐ์ ์ค์ ๋ต์ ์ถ๋ ฅํ๋ค. ์กด์ฌํ๋ฉด 1์, ์กด์ฌํ์ง ์์ผ๋ฉด 0์ ์ถ๋ ฅํ๋ค.
ํ์ด 1
๋ฌธ์ ๋ฅผ ๋ดค์๋ ์ดํดํ๋๋ฐ์ ์๊ฐ์ด ๊ฑธ๋ ธ์ง๋ง ์ดํดํ๊ณ ๋๋ ๊ต์ฅํ ์ฝ๊ฒ ๋๊ปด์ก๋ค.
๋ด๊ฐ ์ดํดํ ๋ฌธ์ ๋ ์ด๋ ๋ค.
N๊ฐ์ ์ ์ ์ค M๊ฐ์ ์ ์๊ฐ ํฌํจ๋๋์ง ํ์ธํ๋ค.
M๊ฐ์ ๊ฐ ์ ์๊ฐ ํฌํจ๋๋ฉด 1, ํฌํจ๋์ง ์์ผ๋ฉด 0์ ์ถ๋ ฅ์ํจ๋ค.
ํฌํจ๋๋์ง ํ์ธํ๋ค๋ ๋ถ๋ถ์์ contains() ๋ฅผ ํ์ฉํ๋ฉด ์ข๊ฒ ๋ค๊ณ ๋ ์ฌ๋ ธ๊ณ ๊ทธ๋ฅผ ํ ๋๋ก ์ฝ๋ ๋ก์ง์ ๊ตฌํํ์์ง๋ง ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ค.
๋ฌธ์ ๋ฅผ ์์ธํ ๋ณด๋ ์๊ณ ๋ฆฌ์ฆ ๋ถ๋ฅ์ ์ด์ง ํ์์ด ์๋ ๊ฒ์ ๋ฐ๊ฒฌํ๋ค.
๊ทธ๋ฆฌ๊ณ ์ ํ ์๊ฐ์ด 1์ด์ธ ๊ฒ๋ ๋ค๋ฆ๊ฒ ๋ฐ๊ฒฌํ๋ค..
์๊ฐ ์ด๊ณผ ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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));
int n = Integer.parseInt(br.readLine());
List<String> nList = new ArrayList<>();
StringTokenizer st = new StringTokenizer(br.readLine());
while (n-- > 0) {
nList.add(st.nextToken());
}
int m = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
while (m-- > 0) {
System.out.println(nList.contains(st.nextToken()) ? 1 : 0);
}
}
}
ํ์ด 2
์๊ฐ ์ ํ๊ณผ ์ด์ง ํ์์ ํ์ธํ๊ณ java.util.Collections ํด๋์ค์ Collections.binarySearch() ๋ฉ์๋๊ฐ ์๋ค๋ ๊ฒ์ ์๊ฒ๋์๊ณ ์ด๋ฅผ ํ์ฉํ์ฌ ์ฝ๋ ๋ก์ง์ ๊ตฌํํ์๋ค.
ํต๊ณผ๋ ํ์์ง๋ง ์๊ฐ์ด ๋๋ฌด ๊ธธ๊ฒ ๋์ ๊ณ ๋ฏผํ๋ค ๋ณด๋ StringBuilder๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฌธ์์ด๋ก ํฉ์ณ์ค ๋ค์ ์ถ๋ ฅ์ ์์ผ์ฃผ๋ 1704ms โ 776ms ๋ก ํ ์ค์๋ค.
๊ทธ๋ฆฌ๊ณ Collections.binarySearch()๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ง ์๋ค๋ ๊ธ์ ๋ณธ๊ฒ๊ฐ์ ์ง์ ์ด์งํ์์ ๊ตฌํํ์ฌ ๋ก์ง๋ ๊ตฌํํด๋ณด์๋ค.
ArrayList 1
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
import java.io.*;
import java.util.*;
// list + Collections.binarySearch
// ๋ฉ๋ชจ๋ฆฌ : 52960KB, ์๊ฐ : 1704ms
// list + Collections.binarySearch + StringBuilder
// ๋ฉ๋ชจ๋ฆฌ : 45168KB, ์๊ฐ : 776ms
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
List<String> nList = new ArrayList<>();
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
while (n-- > 0) nList.add(st.nextToken());
Collections.sort(nList);
int m = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine(), " ");
while (m-- > 0) {
sb.append(Collections.binarySearch(nList, st.nextToken()) >= 0 ? 1 : 0).append("\n");
}
System.out.print(sb);
}
}
ArrayList 2
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
import java.io.*;
import java.util.*;
// list + binarySearch
// ๋ฉ๋ชจ๋ฆฌ : 54424KB, ์๊ฐ : 1336ms
// list + binarySearch + StringBuilder
// ๋ฉ๋ชจ๋ฆฌ : 46912KB, ์๊ฐ : 760ms
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
List<Integer> nList = new ArrayList<>();
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
while (n-- > 0) nList.add(Integer.parseInt(st.nextToken()));
Collections.sort(nList);
int m = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine(), " ");
while (m-- > 0) {
sb.append(binarySearch(nList, Integer.parseInt(st.nextToken())) >= 0 ? 1 : 0).append("\n");
}
System.out.print(sb);
}
private static int binarySearch(List<Integer> nList, int key) {
int low = 0;
int high = nList.size() - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (key < nList.get(mid)) high = --mid;
else if (key > nList.get(mid)) low = ++mid;
else return mid;
}
return -1;
}
}
ํ์ด 3
์๊ฐ์ ๋ ์ค์ฌ๋ณด๊ณ ์ถ์ ๋ง์๊ณผ Array์ List์ ์๊ฐ ์ฐจ์ด๋ฅผ ๋น๊ตํด๋ณด๊ณ ์ถ์ด์ก๊ณ Array๋ฅผ ํ์ฉํ์ฌ ๋ก์ง์ ๊ตฌํํด๋ณด์๋ค.
ํ์ด 2 ์ ๊ฑฐ์ ๋น์ทํ๋ค.
Array 1
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
import java.io.*;
import java.util.*;
// Array + Arrays.binarySearch
// ๋ฉ๋ชจ๋ฆฌ : 52652KB, ์๊ฐ : 1380ms
// Array + Arrays.binarySearch + StringBuilder
// ๋ฉ๋ชจ๋ฆฌ : 45660KB, ์๊ฐ : 604ms
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
int[] nArr = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < n; i++) nArr[i] = Integer.parseInt(st.nextToken());
Arrays.sort(nArr);
int m = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine(), " ");
while (m-- > 0) {
sb.append(Arrays.binarySearch(nArr,Integer.parseInt(st.nextToken()))>=0?1:0).append("\n");
}
System.out.print(sb);
}
}
Array 2
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
import java.io.*;
import java.util.*;
// Array + binarySearch
// ๋ฉ๋ชจ๋ฆฌ : 54776KB, ์๊ฐ : 1228ms
// Array + binarySearch + StringBuilder
// ๋ฉ๋ชจ๋ฆฌ : 44864KB, ์๊ฐ : 612ms
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
int[] nArr = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < n; i++) nArr[i] = Integer.parseInt(st.nextToken());
Arrays.sort(nArr);
int m = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine(), " ");
while (m-- > 0) {
sb.append(binarySearch(nArr, Integer.parseInt(st.nextToken())) >= 0 ? 1 : 0).append("\n");
}
System.out.print(sb);
}
private static int binarySearch(int[] nArr, int key) {
int low = 0;
int high = nArr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (key < nArr[mid]) high = --mid;
else if (key > nArr[mid]) low = ++mid;
else return mid;
}
return -1;
}
}
ํ์ด 3
์๊ณ ๋ฆฌ์ฆ ๋ถ๋ฅ ๋ถ๋ถ์ ์ด์งํ์ ๋ง๊ณ ๋ ์๋ฃ๊ตฌ์กฐ ๊ฐ ์ ํ์์๊ณ ์๋ฃ๊ตฌ์กฐ๋ฅผ ํตํด์๋ ์๊ฐ์ ํ ์์ ๊ตฌํ์ด ๊ฐ๋ฅํ์ง ๊ถ๊ธํด์ ธ ๊ณ ๋ฏผ์ ํด ๋ณด๋ HashSet์ ํ์ฉํ๋ฉด ์ ํฉํ ๊ฑฐ๊ฐ๋ค๋ ์๊ฐ์ด ๋ค์๋ค.
HashSet์ ์ค๋ณต์ ์ ๊ฑฐํจ๊ณผ ๋์์ ์์๋ฅผ ๋ณด์ฅํ์ง ์๋๋ค.
๊ทธ๋ฆฌ๊ณ ์์๊ฐ ๋ณด์ฅ๋์ง ์๊ธฐ ๋๋ฌธ์ contains() ๋ฅผ ํตํด ๊ฐ์ ๋น๊ตํ๊ฒ ๋๋ฉด list, array๋ณด๋ค ์๋ฑํ ๋น ๋ฅธ ์๋๋ก ์กฐํ๊ฐ ๊ฐ๋ฅํ๋ค.
HashSet 1
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
import java.io.*;
import java.util.*;
// HashSet + StringBuilder
// ๋ฉ๋ชจ๋ฆฌ : 44952KB, ์๊ฐ : 564ms
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
HashSet<String> hashSet = new HashSet<>(); // ์ค๋ณต์ ๊ฑฐ
while (n-- > 0) hashSet.add(st.nextToken());
int m = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
while (m-- > 0) sb.append(hashSet.contains(st.nextToken()) ? "1\n" : "0\n");
System.out.println(sb);
}
}
HashSet 1.5
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
import java.io.*;
import java.util.*;
// HashSet + StringBuilder
// ๋ฉ๋ชจ๋ฆฌ : 44952KB, ์๊ฐ : 564ms
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
br.readLine();
String[] temp = br.readLine().split(" ");
HashSet<String> hashSet = new HashSet<>(Arrays.asList(temp)); // ์ค๋ณต์ ๊ฑฐ
br.readLine();
temp = br.readLine().split(" ");
for (String s : temp) sb.append(hashSet.contains(s) ? "1\n" : "0\n");
System.out.println(sb);
}
}