https://acmicpc.net/problem/3273
먼저 두 수의 합이 점차적으로 증가 혹은 감소해야 하기 때문에 배열을 정렬한다.
정렬한 배열의 포인터를 인덱스 0, n-1에 두어 만약 합이 x보다 크면 e를 감소시키고,
x라면 s를 증가시키고 e를 감소시켜 다음 값을 탐색할 수 있게 했다.
코드는 다음과 같다.
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int arr[100001];
int x;
int cnt=0;
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n;
for(int i=0;i<n;i++){
cin >> arr[i];
}
cin >> x;
sort(arr,arr+n);
if(n!=1){
int s=0;
int e=n-1;
while(s<e){
if(arr[s]+arr[e]>=x){
if(arr[s]+arr[e]==x){
s++;
cnt++;
}
e--;
}
else{
s++;
}
}
}
cout << cnt;
}