(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;
}
相关推荐
IronMurphy12 分钟前
【算法五十六】84. 柱状图中最大的矩形
算法
fie888921 分钟前
matlab打靶法求解两点边值优化问题
开发语言·算法·matlab
不做无法实现的梦~37 分钟前
常见工程分析软件
stm32·嵌入式硬件·算法
hetao173383742 分钟前
2026-05-28~06-02 hetao1733837 的刷题记录
c++·算法
ZhengEnCi42 分钟前
O08-单写线程与单读线程冲突分析
算法
仍然.1 小时前
算法题目---优先级队列
算法
一个爱编程的人1 小时前
图的相关概念
c++·算法·图论
迈巴赫车主1 小时前
贪心算法
算法·贪心算法
星马梦缘1 小时前
死锁与进程资源分配问题的解法
算法·操作系统·深度优先·死锁
爱炼丹的James1 小时前
第四章 数学知识
算法