백준 20922번

2022. 2. 21. 03:20·Study/Algorithm

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

 

20922번: 겹치는 건 싫어

홍대병에 걸린 도현이는 겹치는 것을 매우 싫어한다. 특히 수열에서 같은 원소가 여러 개 들어 있는 수열을 싫어한다. 도현이를 위해 같은 원소가 $K$개 이하로 들어 있는 최장 연속 부분 수열

www.acmicpc.net

#include<iostream>
using namespace std;

int N, K;
int cnt[100005];
int arr[200005];
int main() {
    cin.tie(nullptr); ios::sync_with_stdio(false);

    cin >> N >> K;

    for (int i = 1; i <= N; ++i) 
        cin >> arr[i];

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

    while (end <= N && start <= end) {
        while (end <= N && cnt[arr[end]] <= K) {
            if (cnt[arr[end]] == K)
                break;
            cnt[arr[end]]++;
            ans = max(ans, end - start + 1);
            ++end;
        }
        while (start < end) {
            if (cnt[arr[start]] == K) {
                --cnt[arr[start++]];
                break;
            }
            --cnt[arr[start++]];
        }
    }
    cout << ans << '\n';
    return 0;
}

'Study > Algorithm' 카테고리의 다른 글

백준 2003번  (0) 2022.02.21
백준 21921번  (0) 2022.02.21
백준 15489번  (0) 2022.02.14
백준 16953번  (0) 2022.02.08
백준 14916번  (0) 2022.02.08
'Study/Algorithm' 카테고리의 다른 글
  • 백준 2003번
  • 백준 21921번
  • 백준 15489번
  • 백준 16953번
_WooHyun_
_WooHyun_
  • _WooHyun_
    Nerd
    _WooHyun_
  • 전체
    오늘
    어제
    • 분류 전체보기 (79)
      • Study (60)
        • Algorithm (24)
        • Unreal Engine (19)
        • C++ (1)
        • Maya (1)
        • GoLang (3)
        • Mysql (3)
        • Linux (7)
        • Server (2)
      • Projects (0)
        • Unreal Engine (0)
        • Server (0)
      • 개발일지 (8)
        • Unreal Engine (7)
        • Art (1)
        • Server (0)
      • 미래 (5)
      • 개발아이디어 (0)
      • 잡지식 (2)
  • 블로그 메뉴

    • 홈
    • 방명록
    • 글쓰기
    • 블로그설정
  • 링크

    • GitHub
  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
_WooHyun_
백준 20922번
상단으로

티스토리툴바