Skip to content

Commit

Permalink
Merge pull request #45 from AlgoLeadMe/11-rivkms
Browse files Browse the repository at this point in the history
11-rivkms
  • Loading branch information
tgyuuAn authored Jun 27, 2024
2 parents eee8b46 + 67de9be commit 55b2fb1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions rivkms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
| 8μ°¨μ‹œ | 2024.03.01 | Greedy | [νšŒμ˜μ‹€ λ°°μ •](https://www.acmicpc.net/problem/1931) | [#8](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/28) |
| 9μ°¨μ‹œ | 2024.03.22 | two-pointer | [두 수의 ν•©](https://www.acmicpc.net/problem/3273) | [#9](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/38) |
| 10μ°¨μ‹œ | 2024.03.28 | queue | [μ΅œλŒ€νž™](https://www.acmicpc.net/problem/11279) | [#10](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/40) |
| 11μ°¨μ‹œ | 2024.04.02 | two-pointer | [두 μš©μ•‘](https://www.acmicpc.net/problem/2470) | [#11](https://github.com/AlgoLeadMe/AlgoLeadMe-7/pull/45) |
40 changes: 40 additions & 0 deletions rivkms/Two_pointer/2470.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
int n;
cin >> n;
vector<int> vec(n,0);
for(int i = 0; i<n; i++){
cin >> vec[i];
}
sort(vec.begin(), vec.end());
vector<int> answer(2);
int p1 = 0, p2 = n-1;
int min = INT32_MAX;
while(true){
if(p1 >= p2){
break;
}
int sum = vec[p1]+vec[p2];
if(abs(sum) < min){
min = abs(sum);
answer[0] = vec[p1];
answer[1] = vec[p2];
if(sum == 0){
break;
}
}
if(sum < 0){
p1++;
}
else{
p2--;
}
}
cout << answer[0] << " " << answer[1];
return 0;
}

0 comments on commit 55b2fb1

Please sign in to comment.