본문 바로가기
PS/BOJ

[백준] 3273. 두 수의 합(c++)

by backend 개발자 지망생 2025. 1. 16.

https://www.acmicpc.net/problem/3273

정렬해서 풀려다가, 풀리지 않을까 해서 했는데 어김없이 시간초과

가지치기를 해줘야하는 문제였다.

그와 별개로 옵시디언이라는 툴을 사용하기 시작했는데, 가독성이 좋은 것 같다.


 

#include <iostream>
#include <cstdio>
#include <algorithm> 
#include <vector>
#include <string>
#include <map>
#include <limits.h>
#include <queue>
#include <set>
#include <math.h>
#include <stack>

using namespace std;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n, x;

	cin >> n;

	vector<int> arr(n);

	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}

	cin >> x;

	sort(arr.begin(), arr.end());

	// 투 포인터 돌릴 것들
	int start = 0, end = n- 1;

	int ans = 0;

	while (start < end) {
		if (arr[start] + arr[end] < x) {
			start++;
		}
		else if (arr[start] + arr[end] > x) {
			end--;
		}
		else if (arr[start] + arr[end] == x) {
			start++;
			end--;
			ans++;
		}

		if (start == end)
			break;
	}

	cout << ans;

	return 0;
}

'PS > BOJ' 카테고리의 다른 글

[백준] 13300. 방 배정(c++)  (0) 2025.01.19
[백준] 10807. 개수 세기(c++)  (0) 2025.01.19
[백준] 1475. 방 번호(c++)  (0) 2025.01.15
[백준] 2577. 숫자의 개수(c++)  (0) 2025.01.15
[백준] 10808. 알파벳 개수(c++)  (0) 2025.01.13