From e0ea0f2c7dcaa3d2b94c559bbe98de99895b5697 Mon Sep 17 00:00:00 2001 From: kangrae Date: Tue, 31 Dec 2024 17:56:24 +0900 Subject: [PATCH] =?UTF-8?q?2024-12-31=20=EB=91=90=20=EC=9A=A9=EC=95=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kangrae-jo/README.md | 1 + kangrae-jo/Two Pointer/14-kangrae-jo.cpp | 40 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 kangrae-jo/Two Pointer/14-kangrae-jo.cpp diff --git a/kangrae-jo/README.md b/kangrae-jo/README.md index ecbf638..2fa012c 100644 --- a/kangrae-jo/README.md +++ b/kangrae-jo/README.md @@ -12,5 +12,6 @@ | 8차시 | 2024.11.12 | HEAP | [더 맵게](https://school.programmers.co.kr/learn/courses/30/lessons/42626)|[#27](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/27)| | 9차시 | 2024.11.23 | DP | [정수 삼각형](https://school.programmers.co.kr/learn/courses/30/lessons/43105)|[#30](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/30)| | 10차시 | 2024.11.30 | 누적합 | [구간 합 구하기 4](https://www.acmicpc.net/problem/11659)|[#18](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/37)| +| 14차시 | 2024.12.31 | Two Pointer | [두 용액](https://www.acmicpc.net/problem/2470)|[#48](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/48)| --- \ No newline at end of file diff --git a/kangrae-jo/Two Pointer/14-kangrae-jo.cpp b/kangrae-jo/Two Pointer/14-kangrae-jo.cpp new file mode 100644 index 0000000..34ebffb --- /dev/null +++ b/kangrae-jo/Two Pointer/14-kangrae-jo.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +using namespace std; + +int main() { + int N; + cin >> N; + + vector have(N); + for (int i = 0; i < N; i++) { + cin >> have[i]; + } + sort(have.begin(), have.end()); + + int left = 0, right = N - 1; + long int mix = have[left] + have[right]; + + int pLeft = left, pRight = right; + long int pMix = mix; + + while (left < right) { + long int mix = have[left] + have[right]; + if (mix == 0) { + cout << have[left] << " " << have[right]; + return 0; + } + else if (abs(mix) < abs(pMix)) { + pMix = mix; + pLeft = left; + pRight = right; + } + if (mix > 0) right--; + else left++; + } + cout << have[pLeft] << " " << have[pRight]; + + return 0; +}