Study/Algorithm

백준 1940번

_WooHyun_ 2022. 2. 28. 04:24

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

 

1940번: 주몽

첫째 줄에는 재료의 개수 N(1 ≤ N ≤ 15,000)이 주어진다. 그리고 두 번째 줄에는 갑옷을 만드는데 필요한 수 M(1 ≤ M ≤ 10,000,000) 주어진다. 그리고 마지막으로 셋째 줄에는 N개의 재료들이 가진 고

www.acmicpc.net

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	int N, M;
	vector<int> arr;

	cin >> N >> M;
	arr.resize(N);
	for (int i = 0; i < N; i++) {
		cin >> arr[i];
	}

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

	int start = 0, end = 1, cnt = 0;

	while (start <= end && end < N) {
		if (arr[start] + arr[end] == M)
			cnt++;

		if (end == N - 1) {
			start++;
			end = start + 1;
		}
		else
			end++;
	}
	cout << cnt;
}