题目描述
You are given a playlist of a radio station since its establishment. The playlist has a total of n songs.
What is the longest sequence of successive songs where each song is unique?
输入
The first input line contains an integer n(1 ≤ n ≤ 2*105): the number of songs.
The next line has n integers k1,k2,...,kn(1 ≤ ki ≤ 109): the id number of each song.
输出
Print the length of the longest sequence of unique songs.
样例输入 Copy
8 1 2 1 3 2 7 4 2
样例输出 Copy
5
代码
cpp
#include<bits/stdc++.h>
using namespace std;
int n,k[200009];
int main(){
cin>>n;
for(int i=0;i<n;++i)cin>>k[i];
map<int,int>book;
int ans=0,l=0,r=-1;
for(int i=0;i<n;++i){
if(book[k[i]]){
ans=ans>(r-l+1)?ans:r-l+1;
while(book[k[i]]&&l<=r){
book[k[l]]--;
l++;
}
}
r++;
book[k[i]]++;
}
ans=ans>(r-l+1)?ans:r-l+1;
cout<<ans;
return 0;
}