3649 - 로봇 프로젝트
2025. 2. 18. 11:39ㆍPS/백준
문제
https://www.acmicpc.net/problem/3649
느낀점
얼마나 들어올 지 모르는 입력을 받는 게 꽤 어려웠던 듯. 알고리즘 자체는 어렵지 않은 응용 투포인터였다.
풀이
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define pii pair<int, int>
int main()
{
int x;
while (cin >> x)
{
x *= 10e6;
int n;
cin >> n;
vector<int> lList(n);
for (int i = 0; i < n; i++)
{
cin >> lList[i];
}
if (n < 2)
{
cout << "danger" << "\n";
continue;
}
sort(lList.begin(), lList.end());
vector<pii> answer;
int left = 0, right = n - 1;
while (left < right)
{
int sum = lList[left] + lList[right];
if (sum == x)
{
answer.push_back(make_pair(lList[left], lList[right]));
left++;
right--;
}
else if (sum > x)
{
right--;
}
else
{
left++;
}
}
sort(answer.begin(), answer.end(), [](const pii &a, const pii &b)
{ return abs(a.second - a.first) > abs(b.second - b.first); });
if (answer.empty())
{
cout << "danger" << "\n";
}
else
{
cout << "yes" << " " << answer[0].first << " " << answer[0].second << "\n";
}
}
return 0;
}
'PS > 백준' 카테고리의 다른 글
9024 - 두 수의 합 (0) | 2025.02.18 |
---|---|
19583 - 싸이버개강총회 (0) | 2025.02.18 |
2467 - 용액 (0) | 2025.02.18 |
2230 - 수 고르기 (0) | 2025.02.17 |
2118 - 두 개의 탑 (0) | 2025.02.17 |