(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;
}
相关推荐
ulias21218 分钟前
leetcode热题 - 5
数据结构·算法·leetcode
Funny_AI_LAB29 分钟前
Naval最新播客谈“氛围编码”:Vibe Coding 开启“一人独角兽”时代
人工智能·算法·语言模型·agi
如何原谅奋力过但无声29 分钟前
【灵神高频面试题合集04-05】二分查找
数据结构·python·算法·leetcode
我不是懒洋洋33 分钟前
【数据结构】排序算法(直接插入排序、希尔排序、选择排序、堆排序、冒泡排序、快速排序、归并排序、计数排序)
c语言·数据结构·c++·经验分享·算法·排序算法
MediaTea33 分钟前
ML:逻辑回归的基本原理与实现
人工智能·算法·机器学习·数据挖掘·逻辑回归
超级码力6669 小时前
【Latex文件架构】Latex文件架构模板
算法·数学建模·信息可视化
穿条秋裤到处跑9 小时前
每日一道leetcode(2026.04.29):二维网格图中探测环
算法·leetcode·职场和发展
Merlos_wind10 小时前
HashMap详解
算法·哈希算法·散列表
汉克老师10 小时前
GESP2025年3月认证C++五级( 第三部分编程题(1、平均分配))
c++·算法·贪心算法·排序·gesp5级·gesp五级
Yzzz-F13 小时前
Problem - 2205D - Codeforces
算法