File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ public class Main {
4+
5+ // 이진 탐색 소스코드 구현(반복문)
6+ public static int binarySearch (int [] arr , int target , int start , int end ) {
7+ while (start <= end ) {
8+ int mid = (start + end ) / 2 ;
9+ // 찾은 경우 중간점 인덱스 반환
10+ if (arr [mid ] == target ) return mid ;
11+ // 중간점의 값보다 찾고자 하는 값이 작은 경우 왼쪽 확인
12+ else if (arr [mid ] > target ) end = mid - 1 ;
13+ // 중간점의 값보다 찾고자 하는 값이 큰 경우 오른쪽 확인
14+ else start = mid + 1 ;
15+ }
16+ return -1 ;
17+ }
18+
19+ public static void main (String [] args ) {
20+ Scanner sc = new Scanner (System .in );
21+
22+ // N(가게의 부품 개수)
23+ int n = sc .nextInt ();
24+ int [] arr = new int [n ];
25+ for (int i = 0 ; i < n ; i ++) {
26+ arr [i ] = sc .nextInt ();
27+ }
28+
29+ // 이진 탐색을 수행하기 위해 사전에 정렬 수행
30+ Arrays .sort (arr );
31+
32+ // M(손님이 확인 요청한 부품 개수)
33+ int m = sc .nextInt ();
34+ int [] targets = new int [n ];
35+ for (int i = 0 ; i < m ; i ++) {
36+ targets [i ] = sc .nextInt ();
37+ }
38+
39+ // 손님이 확인 요청한 부품 번호를 하나씩 확인
40+ for (int i = 0 ; i < m ; i ++) {
41+ // 해당 부품이 존재하는지 확인
42+ int result = binarySearch (arr , targets [i ], 0 , n - 1 );
43+ if (result != -1 ) {
44+ System .out .print ("yes " );
45+ }
46+ else {
47+ System .out .print ("no " );
48+ }
49+ }
50+ }
51+
52+ }
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ public class Main {
4+
5+ public static void main (String [] args ) {
6+ Scanner sc = new Scanner (System .in );
7+
8+ // N(가게의 부품 개수)
9+ int n = sc .nextInt ();
10+ int [] arr = new int [1000001 ];
11+ for (int i = 0 ; i < n ; i ++) {
12+ int x = sc .nextInt ();
13+ arr [x ] = 1 ;
14+ }
15+
16+ // M(손님이 확인 요청한 부품 개수)
17+ int m = sc .nextInt ();
18+ int [] targets = new int [n ];
19+ for (int i = 0 ; i < m ; i ++) {
20+ targets [i ] = sc .nextInt ();
21+ }
22+
23+ // 손님이 확인 요청한 부품 번호를 하나씩 확인
24+ for (int i = 0 ; i < m ; i ++) {
25+ // 해당 부품이 존재하는지 확인
26+ if (arr [targets [i ]] == 1 ) {
27+ System .out .print ("yes " );
28+ }
29+ else {
30+ System .out .print ("no " );
31+ }
32+ }
33+ }
34+
35+ }
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ public class Main {
4+
5+ public static void main (String [] args ) {
6+ Scanner sc = new Scanner (System .in );
7+
8+ // N(가게의 부품 개수)
9+ int n = sc .nextInt ();
10+ // 집합(Set) 정보를 처리하기 위한 HashSet 라이브러리
11+ HashSet <Integer > s = new HashSet <>();
12+ for (int i = 0 ; i < n ; i ++) {
13+ int x = sc .nextInt ();
14+ s .add (x );
15+ }
16+
17+ // M(손님이 확인 요청한 부품 개수)
18+ int m = sc .nextInt ();
19+ int [] targets = new int [n ];
20+ for (int i = 0 ; i < m ; i ++) {
21+ targets [i ] = sc .nextInt ();
22+ }
23+
24+ // 손님이 확인 요청한 부품 번호를 하나씩 확인
25+ for (int i = 0 ; i < m ; i ++) {
26+ // 해당 부품이 존재하는지 확인
27+ if (s .contains (targets [i ])) {
28+ System .out .print ("yes " );
29+ }
30+ else {
31+ System .out .print ("no " );
32+ }
33+ }
34+ }
35+
36+ }
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ public class Main {
4+
5+ public static void main (String [] args ) {
6+ Scanner sc = new Scanner (System .in );
7+
8+ // 떡의 개수(N)와 요청한 떡의 길이(M)
9+ int n = sc .nextInt ();
10+ int m = sc .nextInt ();
11+
12+ // 각 떡의 개별 높이 정보
13+ int [] arr = new int [n ];
14+ for (int i = 0 ; i < n ; i ++) {
15+ arr [i ] = sc .nextInt ();
16+ }
17+
18+ // 이진 탐색을 위한 시작점과 끝점 설정
19+ int start = 0 ;
20+ int end = (int ) 1e9 ;
21+ // 이진 탐색 수행 (반복적)
22+ int result = 0 ;
23+ while (start <= end ) {
24+ long total = 0 ;
25+ int mid = (start + end ) / 2 ;
26+ for (int i = 0 ; i < n ; i ++) {
27+ // 잘랐을 때의 떡의 양 계산
28+ if (arr [i ] > mid ) total += arr [i ] - mid ;
29+ }
30+ if (total < m ) { // 떡의 양이 부족한 경우 더 많이 자르기(왼쪽 부분 탐색)
31+ end = mid - 1 ;
32+ }
33+ else { // 떡의 양이 충분한 경우 덜 자르기(오른쪽 부분 탐색)
34+ result = mid ; // 최대한 덜 잘랐을 때가 정답이므로, 여기에서 result에 기록
35+ start = mid + 1 ;
36+ }
37+ }
38+
39+ System .out .println (result );
40+ }
41+
42+ }
You can’t perform that action at this time.
0 commit comments