22857 - 가장 긴 짝수 연속한 부분 수열 (small)

2025. 2. 14. 17:51PS/백준

문제

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

느낀점

확실히 투 포인터 문제는 경계를 설정하는 것이 까다롭고, 포인터의 이동을 생각하는 게 어렵다.. 더 연습이 필요할 듯

풀이

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int N, K;
    cin >> N >> K;
    vector<int> arr(N);
    for (int i = 0; i < N; i++)
    {
        cin >> arr[i];
    }

    int start = 0, end = 0;
    int maxValue = 0;
    int sInd = 0, eInd = 0;
    while (true)
    {
        if (end >= N)
        {
            break;
        }

        if (arr[end] % 2 != 0)
        {
            end++;
        }
        else
        {
            start = end;
            int cnt = 0;
            int possibleDelete = K;
            while (true)
            {
                if (end >= N)
                {
                    break;
                }

                if (arr[end] % 2 != 0)
                {
                    if (possibleDelete <= 0)
                    {
                        end = start + 1;
                        break;
                    }
                    possibleDelete--;
                    end++;
                }
                else if (arr[end] % 2 == 0)
                {
                    end++;
                    cnt++;
                }
            }
            maxValue = max(maxValue, cnt);
        }
    }
    cout << maxValue;

    return 0;
}

'PS > 백준' 카테고리의 다른 글

15565 - 귀여운 라이언  (0) 2025.02.17
2531 - 회전 초밥  (0) 2025.02.15
2003 - 수들의 합  (0) 2025.02.14
14246 - K보다 큰 구간  (0) 2025.02.14
28353 - 고양이 카페  (2) 2025.02.13