-
Notifications
You must be signed in to change notification settings - Fork 0
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
14-kangrae-jo #48
14-kangrae-jo #48
Conversation
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.
ํฌํฌ์ธํฐ ๋ฌธ์ ๋ ์ฒ์ ํ์ด๋ณธ ๊ฒ ๊ฐ์๋ฐ ํ์ด๋ฅผ ๋ณด๋ ์ด๋์ ๊ฐ ํ๋ฒ ๋ณธ๋ฏํ ํํ๋ค์! ๋๋ถ์ ํฌํฌ์ธํฐ ๋ฌธ์ ์ ์ ๊ทผํ ์ ์์ด ์ข์์ต๋๋ค.
๋ฌผ๋ก ์ ๋ ํฌํฌ์ธํฐ๊ฐ ๋ญ์ง ๋ชฐ๋ผ์ ํด์์ ์ ํ ์ด์งํ์์ ํ์ฉํ์ต๋๋ค. ํ์คํ ์ฝ๋๊ฐ ๋ง์ด ๋ณต์กํ๋ค์. ๊ทธ๋ฆฌ๊ณ ์๊ฐ ๋ณต์ก๋๊ฐ ๋ง์ด ๋ธ๋ฆฌ๋ค์.
code
//
// Created by ๊น๊ท ํธ on 2025. 1. 12..
//
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int n;
vector<int> solution;
int bSearch(int target, int exclusive) {
int s = 0, e = n - 1;
// ๋ฐฐ์ ํด์ผํ๋ index๊ฐ 0์ด๋ฉด solution ๋ฐฐ์ด์ 1๋ถํฐ ์์ -> ํด๋น ์์น๋ฅผ ์ ํ์ํ๋ ๊ฒ
// ๋ฐฐ์ ํด์ผํ๋ index๊ฐ 0์ด ์๋ ๊ฒฝ์ฐ 0๋ถํฐ ์์
int closest = (exclusive == 0) ? solution[1] : solution[0];
while (s <= e) {
int mid = (s + e) / 2;
int midVal = solution[mid];
// ๊ทผ์ฌ์น ์
๋ฐ์ดํธ
if (exclusive != mid && abs(midVal - target) < abs(closest - target))
closest = midVal;
if (midVal == target) return target;
else if (midVal > target) e = mid - 1;
else s = mid + 1;
}
// ์ํ๋ ๊ฐ์ ๋ชป์ฐพ์ผ๋ฉด ๊ทผ์ฌ์น ๋ฐํ
return closest;
}
int main() {
cin >> n;
solution = vector<int>(n, 0);
for (int i = 0; i < n; i++)
cin >> solution[i];
sort(solution.begin(), solution.end());
int diff = INT_MAX;
int fir = 0, sec = 0;
for (int i = 0; i < n; i++) {
int sol = solution[i];
// ํ์ฌ ์ฉ์ก์ ๊ฐ๊ณผ ๋ถํธ๋ง ๋ค๋ฅธ ๊ฐ์ ์ํ๋ -๋ฅผ ๋ถ์
int closest = bSearch(-sol, i);
if (diff > abs(sol + closest)) {
diff = abs(sol + closest);
fir = sol;
sec = closest;
}
}
if (fir < sec) cout << fir << ' ' << sec;
else cout << sec << ' ' << fir;
return 0;
}
์ด ๋ฌธ์ ๊ฐ ๋ฒ์จ ์๋ ๊บผ์๋ค์.. ๊ณ ์ํ์ จ์ต๋๋ค
if (mix == 0) { | ||
cout << have[left] << " " << have[right]; | ||
return 0; | ||
} |
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.
๋ง์ง๋ง์ ์ธ๊ธํ๊ฒ ์ฌ๊ธฐ ๋ถ๋ถ์ breakํ์ง ์์ ์ด์ ์ธ๊ฐ์?
์ ์๊ฐ์ mix๊ฐ ๋ด๋ถ์์ ์๋ก ์ ์ธ๋๊ธด ํ์ง๋ง ๋ก์ง์๋ ๋ฌธ์ ๊ฐ ์์ด ๋ณด์
๋๋ค.. break๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ ์์๋ฅผ ๋ฐ๊ฟ if(abs(mix)<abs(pMix))
๋ค์์ if
๋ฌธ์ผ๋ก ๋ค์ด๊ฐ์ผํ ๊ฒ ๊ฐ์์! ๋ฐฉ๊ธ ํด๋ณด๋๊น ์ฑ๊ณต์ ์ผ๋ก ํต๊ณผํ์์ต๋๋ค :)
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.
๋ง์ง๋ง์ ์ธ๊ธํ๊ฒ ์ฌ๊ธฐ ๋ถ๋ถ์ breakํ์ง ์์ ์ด์ ์ธ๊ฐ์?
๋ค ๋ง์์
์ ์๊ฐ์ mix๊ฐ ๋ด๋ถ์์ ์๋ก ์ ์ธ๋๊ธด ํ์ง๋ง ๋ก์ง์๋ ๋ฌธ์ ๊ฐ ์์ด ๋ณด์ ๋๋ค.. break๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ ์์๋ฅผ ๋ฐ๊ฟ if(abs(mix)<abs(pMix)) ๋ค์์ if๋ฌธ์ผ๋ก ๋ค์ด๊ฐ์ผํ ๊ฒ ๊ฐ์์! ๋ฐฉ๊ธ ํด๋ณด๋๊น ์ฑ๊ณต์ ์ผ๋ก ํต๊ณผํ์์ต๋๋ค :)
์ค ๊ทธ๋ ๊ตฐ์ ์ ์ฝ๋๋ฅผ ์งค๋ if๋ฌธ์ ์์๋ฅผ ๋ฐ๊พผ๋ค๊ฑฐ๋ ํ๋ ๊ฒ์ ์๊ฐํ์ง ๋ชปํ๋ค์ ๊ฐ์ฌํฉ๋๋น๐
์ต๊ทผ์ ๊ต๋ณด๋ฌธ๊ณ ์์ ์๊ฐ ๋ผ์ฐ๋ฉด์ ์๊ณ ๋ฆฌ์ฆ ์ฑ
์ฝ์๋๋ฐ Java codeimport java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] products = new int[N];
for (int i = 0; i < N; i++) {
products[i] = sc.nextInt();
}
Arrays.sort(products);
int first = Integer.MAX_VALUE;
int a = 0;
int b = N - 1;
int resultA = 0;
int resultB = 0;
while (a < b) {
int sum = products[a] + products[b];
if (Math.abs(first) > Math.abs(sum)) {
first = sum;
resultA = products[a];
resultB = products[b];
}
if (sum < 0) {
a++;
} else {
b--;
}
}
System.out.println(resultA + " " + resultB);
}
} |
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.
์ด ๋ฌธ์ ์์๋ ํฉ์ด 0์ ๊ฐ์ฅ ๊ฐ๊น์ด ๋ ์๋ฅผ ์ฐพ๋๊ฒ์ ๋๋ค.
์ฆ ์ ๋๊ฐ์ด ๊ฐ์ฅ ์์๊ฐ์ ์ฐพ๋๊ฒ์ด ํต์ฌ์ ๋๋ค. ์๋๋ผ๋ฉด N๊ฐ์ ๊ฐ์ด ์ฃผ์ด์ง๋ฉด, ๋ชจ๋ ๋ฐฐ์ด์ ์์ ๋น๊ตํด์ผํ๊ธฐ ๋๋ฌธ์ N^2์ ์๊ฐ๋ณต์ก๋๊ฐ ํ์ํฉ๋๋ค.
๋ง์ฝ ํฌํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ๋ค๋ฉด, ๋จผ์ ์ ๋ ฌ์ ํ๊ณ ์์ชฝ ๋๊ฐ์ ๋น๊ตํ๋ฉฐ ์์ ๋๊ฐ๋ฅผ ๋ํ๋ค๋์ง, ๊ฐ์ ์ฐจ์ด๊ฐ ๋ง์ด๋๋ ๋ ์๋ฅผ ๋ํ๋ค๋์ง ํ๋ ๋ถํ์ํ ์์ ์ ๊ฑฐํ ์ ์๊ฒ ๋ฉ๋๋ค.
์์ ์์๊ฐ ์กด์ฌํ ๋, ๋ฐฐ์ด์ ์๋๊ฐ์ ๋ํ์๋ ์ ๋๊ฐ์ด ๊ฐ์ฅ ๋ฎ์ ํ๋ฅ ์ด ๋๊ธฐ ๋๋ฌธ์ ๋๋ค.
N = int(input())
values = list(map(int, input().split()))
INF = 987654321
def calculate(N, values):
values.sort()
left = 0
right = N - 1
smallest = INF
result = (0, 0)
while left < right:
current = values[left] + values[right]
if abs(current) < abs(smallest):
smallest = current
result = (values[left], values[right])
if current < 0:
left += 1
elif current > 0:
right -= 1
else:
break
return result
result = calculate(N, values)
print(result[0], result[1])
์ฌ๊ธฐ์ while๋ฌธ ์ฆ left < right๋ ๋ ํฌ์ธํฐ๊ฐ ์๋ก ๊ต์ฐจํ๋ฉด ํ์์ด ์ข
๋ฃ๋๋ค๋ ๋ป์
๋๋ค.
์ข
๋ฃ ์ ๊น์ง ๋ ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๋ ๊ฐ์ ํฉ์ ๊ณ์ฐํ๋ฉฐ 0์ ๊ฐ๊น์ธ์๋ก ๊ฒฐ๊ณผ๋ฅผ ๊ฐฑ์ ํฉ๋๋ค.
ํ์ฌ๊ฐ์ด ์์๋ผ๋ฉด leftํฌ์ธํฐ๋ฅผ ์ค๋ฅธ์ชฝ์ผ๋ก ํ๋ ์ด๋ํด ์์์ ๊ฐ์ ๋ฎ์ถฐ์ฃผ๊ณ , ๋ฐ๋์ ์ํฉ์ด๋ผ๋ฉด ์์์ ๊ฐ์ ํ๋ ๋ฎ์ถ์ด์ค๋๋ค.
์ด๋ ๊ฒ ํ์ธํ๊ฒ๋๋ฉด ์๊ฐ๋ณต์ก๋๊ฐ ๋จ์ํ ๋ชจ๋ ์์ ๋น๊ตํ๋ ๊ฒ๊ณผ๋ ๋ค๋ฅด๊ฒ ๊ฐ ํฌ์ธํฐ์ ์ต๋ ์ด๋ํ์ N๋ฒ์ ๋๋ค.
์ ๋ ฌ์์ NlogN, ํ์์์ N ์ฆ ๋์ ๋ํ๋ฉด NlogN์
๋๋ค.
์ ๋ ฌ์ ํ๊ณ ์ฐพ๋๋ฐ๋, ๋จ์ํ ๋ชจ๋ ์์ ๊ตฌํ๋ ์ด์ค ๋ฐ๋ณต๋ฌธ๋ณด๋ค ๋ ์ ์ ์๊ฐ๋ณต์ก๋๋ก ๊ฐ๋ฅํ๊ฒ๋ฉ๋๋ค.
- ๋ ๊ฐ ๋๋ ๊ตฌ๊ฐ์ ์ ํํด์ผํ๋ ๊ฒฝ์ฐ์์ ์ค๋ณตํ์์๊ฐ์ ์ค์ฌ์ผํ๋ ๊ฒฝ์ฐ๊ฐ ํน์ํ์ง๋ง, ๋ค์์ ์ด๋ฐ ๊ฒฝ์ฐ๊ฐ ๋์จ๋ค๋ฉด ๋ ์ฌ๋ ค์ ์ฌ์ฉํ ์ ์์๊ฒ ๊ฐ์ต๋๋ค!
๐ ๋ฌธ์ ๋งํฌ
[๋ ์ฉ์ก]
โ๏ธ ์์๋ ์๊ฐ
47m
โจ ์๋ ์ฝ๋
์๋ก์ด ์ ํ์ ๋ฌธ์ ๊ฐ ํ์ํ ๊ฒ ๊ฐ์์ ๊ฐ์ ธ์๋ดค์ต๋๋ค~
ํฌ ํฌ์ธํฐ
๋ฌธ์ ์ธ๋ฐ์,์ ๋ ฌ
๊ณผ์ด๋ถ ํ์
๋ฑ์์ ์ฌ์ฉํ๋ ๋ฐฉ์์ด๋ผ๊ณ ํฉ๋๋ค.๋ฌธ์ ๋ฅผ ์ฝ๊ณ ์์ฝํ๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๋์ ๋๋ค!
ํฌ ํฌ์ธํฐ ๋ฌธ์ ์ธ๊ฑธ ์๊ณ ๋ค์ด์์ ์์ด๋์ด๋ ๊ฝค ๊ธ๋ฐฉ ์๊ฐ๋ฌ๊ณ ๋น ๋ฅด๊ฒ ๊ตฌํ์ ํด๋๊ฐ์ต๋๋ค.
1๋ฒ์ ์ฝ๊ณ 2๋ฒ์ ๋ค์๊ณผ ๊ฐ์ ํต์ฌ๋ก์ง์ ํตํด ๊ตฌํํ ์ ์์ต๋๋ค.
์๋ํ๋ฉด ์ ๋ ฌ์ด ๋์ด์๊ธฐ ๋๋ฌธ์ด์ฃต
๊ทธ๋ฐ๋ฐ 1๋ฒ, 2๋ฒ ๊ณผ์ ์ค์์ ํด์ผํ๋ ํ๋์ด ์์ต๋๋ค.
๋ง์ฝ ํฉ์ณ์ 0์ด๋๋ ๋ ์ฉ์ก์ด ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ์๋
๊ฐ์ฅ 0์ ๊ฐ๊น์ด
๋ ์์ ๋ง๋๋ ๋ ๊ฐ์ ์ถ๋ ฅํด์ผํฉ๋๋ค.๋ฐ๋ผ์...
๊ณผ๊ฑฐ์ ํ์ฌ์
abs(mix)
๊ฐ์ ๋น๊ตํ๊ณ ๋ ์์ ๊ฐ์ ์์ฑํ๋ ๋ ๊ฐ์ ๊ฐฑ์ ํ๋ฉฐ ๊ธฐ๋กํด๋๋ ๋ก์ง์ด ํ์ํฉ๋๋ค!๊ทธ๋ ๊ฒ ๊ตฌํํ๋ฉด ์ด ๋ฌธ์ ๋ ์ ๋ต์ ๋๋ค~
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
ํน์ ์ฝ๋ ๋ณด์๊ณ ์ด์ํ ์ ๋์น์ฑ์ จ๋์?
์ ์ด๋ ๊ฒ ๊ตฌํํ์๊น์?
์ ๋ ๋ด๋ถ์์
return
ํ๋ ์ฑํฅ์ด ์๋๋ผbreak
ํด์ ์ธ๋ถ์์ ์ฃผ๋ก ์ถ๋ ฅํ๋๋ฐ์ ์ ์ด๋ฌ์๊น์?๊ทธ๊ฑด ์ ๊ฐ ์ค์๋ก
while
๋ด๋ถ์์long int mix = have[left] + have[right];
์ด๋ ๊ฒ... ํด๋ฒ๋ ธ๊ธฐ ๋๋ฌธ์ ๋๋ค.(๋ด๋ถ ๋ณ์๋ก ์ ์ธ๋์ด ๋ฐ์์ ์ฌ์ฉํ๋
mix
์ ๋ด๋ถmix
๋ ์๋ก ๋ค๋ฅธ ๋ณ์๊ฐ ๋์ด๋ฒ๋ ธ๋ ๊ฒ..)์ง๊ธ ๋ณด๋๊น ๋ณด์ด๋๋ฐ ๋ฌธ์ ํ์ด ๋น์์๋ ์ด๊ฒ ์๋ณด์ฌ์ 10๋ถ ์ ๋ ๊ณ ๋ฏผํ๋ค์...