(AC)Playlist

题目描述

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;
}
相关推荐
晨曦54321014 分钟前
图(Graph):关系网络的数学抽象
开发语言·算法·php
cwplh2 小时前
Manacher(马拉车算法)详解
算法
快去睡觉~8 小时前
力扣73:矩阵置零
算法·leetcode·矩阵
小欣加油9 小时前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode
猿究院--王升11 小时前
jvm三色标记
java·jvm·算法
一车小面包12 小时前
逻辑回归 从0到1
算法·机器学习·逻辑回归
tt55555555555513 小时前
字符串与算法题详解:最长回文子串、IP 地址转换、字符串排序、蛇形矩阵与字符串加密
c++·算法·矩阵
元亓亓亓14 小时前
LeetCode热题100--101. 对称二叉树--简单
算法·leetcode·职场和发展
不会学习?14 小时前
算法03 归并分治
算法
NuyoahC15 小时前
笔试——Day43
c++·算法·笔试