-
[3649] 로봇 프로젝트BOJ 2022. 3. 30. 02:09
https://www.acmicpc.net/problem/3649
3649번: 로봇 프로젝트
각 테스트 케이스마다 한 줄에 하나씩, 구멍을 완벽하게 막을 수 있는 두 조각이 없다면 'danger'를 출력한다. 막을 수 있는 경우에는 'yes ℓ1 ℓ2'를 출력한다. (ℓ1 ≤ ℓ2) 정답이 여러 개인 경우에
www.acmicpc.net
s를 front()에, e를 back()에 놓고 s<e일동안 투포인터로 찾는데, cur=v[s] + v[e];일때 cur>x이면 e--, 그 외에는 s++이다.
도중에 v[s] + v[e] == x인 순간이 한번이라도 발생하면, 해당 s, e는 abs(e - s)가 최대라는 조건을 자연스레 만족한다.
물론 그러한 경우가 한번도 존재하지 않았다면, danger를 출력하면 된다.
#include <bits/stdc++.h>using namespace std;#ifdef ONLINE_JUDGEconstexpr bool local = false;#elseconstexpr bool local = true;#endifusing ll = long long;using pi = pair<int, int>;int x, n;pi ans;int main(void) {if (!local) ios_base::sync_with_stdio(0), cin.tie(0);while (cin >> x) {x *= 10'000'000;ans = {-1, -1};cin >> n;vector<int> v(n);int i;for (i = 0; i < n; i++) cin >> v[i];sort(v.begin(), v.end());int s = 0, e = n - 1;while (s < e) {int cur = v[s] + v[e];if (cur == x) {cout << "yes " << v[s] << ' ' << v[e] << '\n';goto nxt_tc;}if (cur > x)e--;elses++;}cout << "danger\n";nxt_tc:;}return 0;}
'BOJ' 카테고리의 다른 글
[1302] 베스트셀러 (0) 2022.04.01 [1940] 주몽 (0) 2022.03.30 [2458] 키 순서 (0) 2022.03.29 [9370] 미확인 도착지 (0) 2022.03.29 [1719] 택배 (0) 2022.03.29