(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;
}
相关推荐
Star在努力几秒前
15-C语言:第15~16天笔记
c语言·笔记·算法
CoovallyAIHub6 分钟前
工业质检新突破!YOLO-pdd多尺度PCB缺陷检测算法实现99%高精度
深度学习·算法·计算机视觉
gb42152876 分钟前
负载均衡算法中的加权随机算法
windows·算法·负载均衡
xdlka1 小时前
C++初学者4——标准数据类型
开发语言·c++·算法
go54631584651 小时前
大规模矩阵构建与高级算法应用
线性代数·算法·矩阵
向左转, 向右走ˉ2 小时前
为什么分类任务偏爱交叉熵?MSE 为何折戟?
人工智能·深度学习·算法·机器学习·分类·数据挖掘
霜绛3 小时前
机器学习笔记(四)——聚类算法KNN、Kmeans、Dbscan
笔记·算法·机器学习·kmeans·聚类
晨非辰4 小时前
#C语言——学习攻略:深挖指针路线(三)--数组与指针的结合、冒泡排序
c语言·开发语言·数据结构·学习·算法·排序算法·visual studio
zzywxc7874 小时前
编程算法在金融、医疗、教育、制造业等领域的落地案例
人工智能·算法·金融·自动化·copilot·ai编程
zzywxc7874 小时前
编程算法在金融、医疗、教育、制造业的落地应用。
人工智能·深度学习·算法·机器学习·金融·架构·开源