안녕하세요. 오늘은 카페를 운영할 거예요.

문제

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

아이디어

큰것과 작은것이 세트로 있으면 좋습니다.
그래서 정렬을 한 뒤에 양쪽에서 오면서 가능한지 살펴보면 됩니다.

소스코드

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

int main(void)
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int N, K, i, x, cnt = 0;
    vector <int> nums;
    cin >> N >> K;

    for (i = 0; i < N; i++)
    {
        cin >> x;
        nums.push_back(x);
    }
    sort(nums.begin(), nums.end());

    int start = 0, end = N - 1;
    while (start < end)
    {
        if (nums[start] + nums[end] <= K)
        {
            cnt++;
            start++;
            end--;
        }
        else end--;
    }
    cout << cnt;
}


감사합니다.

0개의 댓글