# 생각
더해서 m이 되는 이진 탐색이나
투포인터를 사용하면 해결 가능하다
# 전체 코드
#include <bits/stdc++.h>
using namespace std;
void fastIO() { ios::sync_with_stdio(false); cin.tie(nullptr); }
int n, m, ans;
vector<int> v;
int main()
{
cin >> n;
cin >> m;
for (int i = 0; i < n; i++)
{
int tmp;
cin >> tmp;
v.push_back(tmp);
}
sort(v.begin(), v.end());
int st = 0, en = n - 1;
while (st < en)
{
if (v[st] + v[en] == m)
{
st++;
en--;
ans++;
}
if (v[st] + v[en] > m)
{
en--;
}
if (v[st] + v[en] < m)
{
st++;
}
}
cout << ans;
}
https://www.acmicpc.net/problem/1940
'Algorithm > BOJ' 카테고리의 다른 글
[BOJ] 1629_곱셈.cpp (0) | 2022.05.30 |
---|---|
[BOJ] 3986_좋은 단어.cpp (0) | 2022.05.30 |
[BOJ] 1213_팰린드롬 만들기.cpp (0) | 2022.05.30 |
[BOJ] 9375_패션왕 신해빈.cpp (0) | 2022.05.30 |
[BOJ] 1620_나는야 포켓몬 마스터 이다솜.cpp (0) | 2022.05.29 |