-
Notifications
You must be signed in to change notification settings - Fork 5
YoonYn9915/ 4월 4주차/ 3문제 #202
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
Merged
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,27 @@ | ||
| import sys | ||
|
|
||
| N = int(sys.stdin.readline()) | ||
| arr = sorted(map(int, sys.stdin.readline().split())) | ||
|
|
||
| left = 0 | ||
| right = N - 1 | ||
|
|
||
| answer = (arr[left], arr[right], abs(arr[left] + arr[right])) | ||
|
|
||
| # 투 포인터 사용해서 절대값이 0에 가장 가까운 두 값 찾기 | ||
| while left < right: | ||
| total = arr[left] + arr[right] | ||
| abs_total = abs(total) | ||
|
|
||
| if abs_total < answer[2]: | ||
| answer = (arr[left], arr[right], abs_total) | ||
| if abs_total == 0: | ||
| break | ||
| # 두 용액의 값이 0보다 크면 right를 줄여서 합을 작게 만든다 | ||
| if total > 0: | ||
| right -= 1 | ||
| else: | ||
| # 두 용액의 값이 0보다 작으면 left를 늘려서 합을 크게 만든다 | ||
| left += 1 | ||
|
|
||
| print(f"{answer[0]} {answer[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,43 @@ | ||
| import sys | ||
|
|
||
| inp = sys.stdin.readline | ||
|
|
||
| N = int(inp()) | ||
| arr = list(map(int, inp().split())) | ||
| arr.sort() | ||
|
|
||
| min_value = int(1e10) | ||
| answer = [-1, -1, -1] | ||
|
|
||
| # 각 용액을 순회하며 | ||
| for i in range(N): | ||
| left = i + 1 | ||
| right = N - 1 | ||
|
|
||
| # i번 용액을 넣었을 때 값이 가장 0에 가까운 두 값 찾기 | ||
| while left < right: | ||
| if left == i: | ||
| left = i + 1 | ||
| continue | ||
| elif right == i: | ||
| right = i - 1 | ||
| continue | ||
YoonYn9915 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| total = arr[i] + arr[left] + arr[right] | ||
|
|
||
| # 세 용액의 합이 0에 가장 가까우면 업데이트 | ||
| if abs(total) < min_value: | ||
| min_value = abs(total) | ||
| answer = [arr[i], arr[left], arr[right]] | ||
|
|
||
| if total == 0: | ||
YoonYn9915 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| break | ||
| elif total < 0: | ||
| left += 1 | ||
| else: | ||
| right -= 1 | ||
|
|
||
| answer.sort() | ||
|
|
||
| for ans in answer: | ||
| print(ans, end=" ") | ||
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,34 @@ | ||
| from sys import stdin | ||
|
|
||
| T, W = map(int, stdin.readline().split()) | ||
|
|
||
|
|
||
| lst = [0] | ||
| dp = [[0]*(W+1) for _ in range(T+1)] | ||
|
|
||
| # 입력 받기 | ||
| for _ in range(T): | ||
| lst.append(int(stdin.readline())) | ||
|
|
||
| # DP 테이블 채우기 | ||
| for i in range(1, T+1): | ||
| if lst[i] == 1: | ||
| # 이동을 한 번도 안 했을 경우 (항상 1번 나무 아래 있음) | ||
| dp[i][0] = dp[i-1][0] + 1 # 자두 먹으면 +1 | ||
| else: | ||
| # 1번 나무 아래 있는데 2번 나무에서 자두 떨어지면 못 먹음 | ||
| dp[i][0] = dp[i-1][0] | ||
|
|
||
| # 이동 횟수 1회 이상부터 | ||
| for j in range(1, W+1): | ||
| if lst[i] == 2 and j % 2 == 1: | ||
| # 홀수번 이동했을 때는 2번 나무 아래 있음 → 2번 나무에서 자두 떨어지면 +1 | ||
| dp[i][j] = max(dp[i-1][j-1], dp[i-1][j]) + 1 | ||
| elif lst[i] == 1 and j % 2 == 0: | ||
| # 짝수번 이동했을 때는 1번 나무 아래 있음 → 1번 나무에서 자두 떨어지면 +1 | ||
| dp[i][j] = max(dp[i-1][j-1], dp[i-1][j]) + 1 | ||
| else: | ||
| # 자두 못 먹을 경우, 이동하거나 이동 안 하거나 둘 중 최대값만 가져오기 | ||
| dp[i][j] = max(dp[i-1][j-1], dp[i-1][j]) | ||
|
|
||
| print(max(dp[-1])) |
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.
Uh oh!
There was an error while loading. Please reload this page.