Submission

Status:
[P][P][P][P][P][-SSSS]

Score: 85

User: Jokul

Problemset: ละลานตา

Language: c

Time: 0.007 second

Submitted On: 2025-04-09 18:40:34

#include <stdio.h>

#define MAX_COLOR 100000

int main() {
    int N, K;
    scanf("%d %d", &N, &K);
    int colors[N];
    for (int i = 0; i < N; i++) {
        scanf("%d", &colors[i]);
    }

    int left = 0, right = 0;
    int count = 0;
    int color_count[MAX_COLOR + 1] = {0};
    int unique_colors = 0;

    while (right < N) {
        // Add the current color to the count
        if (color_count[colors[right]] == 0) {
            unique_colors++;
        }
        color_count[colors[right]]++;
        right++;

        // While we have at least K unique colors
        while (unique_colors >= K) {
            // Count the number of valid subarrays
            count += (N - right + 1);

            // Remove the leftmost color from the count
            color_count[colors[left]]--;
            if (color_count[colors[left]] == 0) {
                unique_colors--;
            }
            left++;
        }
    }

    printf("%d\n", count);
    return 0;
}