-
Notifications
You must be signed in to change notification settings - Fork 5
HONGJOO / 4월 4주차 /3개 #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HONGJOO / 4월 4주차 /3개 #203
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """ | ||
| https://www.acmicpc.net/problem/28069 | ||
|
|
||
| - 0 ~ N-1 번 계단 옆에 목표 김밥집 | ||
| - 2가지 중 택 1을 K번 반복하여 김밥집(=N) 에 도달하자 | ||
|
|
||
| (1) nex_i = 계단 +1 | ||
| (2) nex_i = i + i//2 | ||
| -> goal)K번 행동으로 0 -> N 까지 도달 여부 확인하기 | ||
| (1<=n<=1000000) | ||
| # 유형 : 그래프 탐색 - DFS/BFS | ||
| - 목적지 "K" 까지 도달 여부 확인 | ||
|
|
||
| # 출력 | ||
| 김밥 도달 = minigimbob | ||
| 물 = water | ||
|
|
||
| # 풀이 renewal : BFS는 시간 초과난다고 하고, 횟수 count 를 어떻게 해야할지 모르겠다 | ||
| # 보편적 풀이인 DP 로 간다 | ||
| - 점화식 | ||
|
|
||
|
|
||
| """ | ||
| import sys | ||
| from collections import deque | ||
| #1. 입력 변수 | ||
| N , K = map(int, sys.stdin.readline().split()) | ||
| # DP | ||
| INF = 1e9 | ||
| dp = [INF] * (N+1) | ||
|
|
||
| dp[0] = 0 | ||
| dp[1] = 1 # 1 = 0+1 1가지 밖에 없음 | ||
|
|
||
| """ | ||
| dp[i] : 현재 i 도달하는데 최소 횟수 | ||
| dp[i+1] = min(dp[i+1] , dp[i]+1) # 유지 , 업데이트 | ||
| dp[i + i//2] = min(dp[i+i//2] , dp[i]+1) | ||
| """ | ||
|
|
||
| for i in range(1,N+1): | ||
| if i+1 <= N : | ||
| dp[i+1] = min(dp[i+1] , dp[i]+1) | ||
| if i+i//2 <= N : #순간이동 가능한 경우 | ||
| dp[i + i//2] = min(dp[i+i//2] , dp[i]+1) | ||
|
|
||
| if dp[N] <= K : | ||
| print("minigimbob") | ||
| else : | ||
| print("water") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """ | ||
| https://www.acmicpc.net/problem/2470 | ||
|
|
||
| # 음 - 염 / 양 - 산성 | ||
| goal) 2개 혼합 -> 0에 가장 가까운 두 용액 찾기 | ||
| # 출력 | ||
| - 2개 오름차순 출력 | ||
| - 경우가 2개 이상이면 둘중에 1개 아무거나 | ||
| # flow | ||
| 1. 오름차순 정렬 | ||
| 2. 2개가 +- 조합인 경우 => 양끝에 투 포인터..? | ||
| => [-99,-2,-1,4,98] | ||
| start end | ||
| #2-1. 둘 간 합이 기존 min보다 작으면 -> 업데이트 | ||
| # start와 end의 절대값 크기 차이를 기준으로 포인터가 이동함 | ||
| if abs(arr[start]) > abs(arr[end]) => start += 1 이동 , end 유지 | ||
| elif # end -= 1 이동 | ||
| else : #같으면 | ||
| break #끝 | ||
|
|
||
|
|
||
| # until : start = end 가 같은 idx를 가르키면 (start >= end) | ||
|
|
||
|
|
||
|
|
||
| 2. 2개가 ++ 조합 => + 중 최소값 2개의 합 | ||
| 3. 2개가 -- 조합 => -중 최대값 2개의 합 | ||
| => 3개 비교 후 가장 0에 가까운 값 찾기 | ||
| """ | ||
|
|
||
| import sys | ||
| N = int(sys.stdin.readline()) | ||
| liqs = sorted(list(map(int, sys.stdin.readline().split()))) | ||
|
|
||
| # print(liqs) | ||
| # 1. +- 의 조합 | ||
| start = 0 ; end = len(liqs)-1 | ||
| closed_z = [start,end,abs(liqs[start] + liqs[end])] | ||
| while start < end : | ||
| c = liqs[start] + liqs[end] | ||
| if closed_z[-1] > abs(c) : | ||
| closed_z = [start,end, abs(c)] | ||
| if c == 0 : | ||
| break | ||
| # print(f"##") | ||
| # print(f"# {c} : {closed_z} = {liqs[start]} / {liqs[end]}") | ||
|
|
||
| if abs(liqs[start]) > abs(liqs[end]) : | ||
| start+= 1 | ||
| # elif abs(liqs[start]) < abs(liqs[end]) : | ||
| else : | ||
| end -=1 | ||
|
|
||
|
|
||
| print(liqs[closed_z[0]] , liqs[closed_z[1]]) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| """ | ||
| https://www.acmicpc.net/problem/2473 | ||
| # 두용액 -> 3용액 | ||
| # 포인트 | ||
| #flow | ||
| x<= y<= z 일떄 | ||
| (1) (x,y) 의 모든 조합 | ||
| (2)z 는 y+1 ~ N 번째 중 x+y+z -> 0 인 숫자 구하기 | ||
| #ME | ||
| (1) Start idx = 0 , end _iex =-1 | ||
| (2) middle : for문으로 start+1 : end 내 값 중 -(start+end)과 가장 가까운 값 ? | ||
| (3) start + end + miidle 합과 기존 최소값 비교하기 | ||
|
|
||
| 4 | ||
| 1 2 3 4 | ||
|
|
||
| 4 | ||
| -1 -2 -3 -4 | ||
|
|
||
| 4 | ||
| -2 -1 1 2 | ||
|
|
||
| # 0 가능 | ||
| 6 | ||
| -10 0 2 3 4 8 | ||
| => -10 2 8 | ||
|
|
||
| """ | ||
|
|
||
|
|
||
| import sys | ||
|
|
||
| INF = 1e12 | ||
| N = int(sys.stdin.readline()) | ||
| arr = sorted(list(map(int, sys.stdin.readline().split()))) | ||
| #1. 오름차순 정렬 과 포인트 초기화 | ||
|
|
||
| total_min = INF | ||
| answer = [] | ||
| # print(f"arr {arr}") | ||
| # 2. 투 포인터 | ||
| # x < y<z 일때 - X 는 fix , y,z는 투 포인터 | ||
| for i in range(N-2): | ||
| x = arr[i] | ||
| yp = i+1 | ||
| zp =N-1 | ||
| while yp < zp : | ||
| xyz_sum =x + arr[yp] + arr[zp] | ||
| #결과값 업데이트 | ||
| if abs(xyz_sum) < total_min : # 업데이트 | ||
| answer = [x,arr[yp],arr[zp]] | ||
| total_min = abs(xyz_sum) | ||
| # 포인터 이동 | ||
| if xyz_sum <0 : | ||
| yp+=1 | ||
| elif xyz_sum >0 : | ||
| zp-=1 | ||
| else : # xyz_sum == 0 | ||
| print(" ".join(map(str,answer))) | ||
| sys.exit() | ||
|
Comment on lines
+58
to
+60
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 홍주님처럼 합이 0이면 바로 출력하고 종료하는 것도 효율적이겠네요!! |
||
|
|
||
|
|
||
|
|
||
| str_answer = " ".join(map(str,answer)) | ||
| print(str_answer) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 두 용액을 더한 값을 따로 변수로 두었는데 이렇게 하나의 리스트에 한 번에 관리해도 좋겠군요!