1141 - Playlist

Click to show code.


using namespace std;
using vi = vector<int>;
using mii = map<int, int>;
int main(void)
{
    int n;
    cin >> n;
    vi k(n);
    mii freq;
    for (auto &ki : k)
        cin >> ki;
    int ans = 0;
    for (int i = 0, j = 0; i < n; ++i)
    {
        while (j < n and freq[k[j]] == 0)
        {
            ++freq[k[j]];
            ++j;
        }
        ans = max(j - i, ans);
        freq[k[i]]--;
    }
    cout << ans << endl;
    return 0;
}