Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 투 포인터
- 수학
- 우선순위 큐
- XR Interaction Toolkit
- 다익스트라
- 다이나믹 프로그래밍
- Team Fortress 2
- 알고리즘
- 그리디 알고리즘
- VR
- DFS
- Unreal Engine 5
- c++
- 스택
- 자료구조
- 그래프
- 백준
- 브루트포스
- 정렬
- 유니티
- 문자열
- 유니온 파인드
- 재귀
- 구현
- 시뮬레이션
- 백트래킹
- 트리
- ue5
- 누적 합
- BFS
Archives
- Today
- Total
1일1알
백준 2470번 두 용액 C++ 본문
left는 맨 왼쪽부터, right는 맨 오른쪽부터 시작해서
v[left] + v[right]의 값이 0보다 작으면 left를 증가시키고, 0보다 크면 right를 줄이는 식으로 해서 문제를 해결하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
sort(v.begin(), v.end());
int MIN = 2000000001;
int left = 0;
int right = n - 1;
int ans1, ans2;
while (left < right) {
int sum = v[left] + v[right];
if (abs(sum) < MIN) {
ans1 = v[left];
ans2 = v[right];
MIN = abs(sum);
if (sum == 0) break;
}
if (sum < 0) {
left++;
}
else if (sum > 0) {
right--;
}
}
cout << ans1 << " " << ans2;
};
'알고리즘' 카테고리의 다른 글
백준 11559번 Puyo Puyo C++ (0) | 2022.05.19 |
---|---|
백준 15918번 랭퍼든 수열쟁이야!! C++ (0) | 2022.05.18 |
백준 24954번 물약 구매 C++ (0) | 2022.05.16 |
백준 17359번 전구 길만 걷자 C++ (0) | 2022.05.14 |
백준 11728번 배열 합치기 C++ (0) | 2022.05.13 |