(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;
}
相关推荐
塔中妖44 分钟前
【华为OD】分割数组的最大差值
数据结构·算法·华为od
weixin_307779131 小时前
最小曲面问题的欧拉-拉格朗日方程 / 曲面极值问题的变分法推导
算法
RTC老炮1 小时前
webrtc弱网-AlrDetector类源码分析与算法原理
服务器·网络·算法·php·webrtc
孤廖1 小时前
【算法磨剑:用 C++ 思考的艺术・Dijkstra 实战】弱化版 vs 标准版模板,洛谷 P3371/P4779 双题精讲
java·开发语言·c++·程序人生·算法·贪心算法·启发式算法
sali-tec1 小时前
C# 基于halcon的视觉工作流-章33-矩状测量
开发语言·人工智能·算法·计算机视觉·c#
songx_992 小时前
leetcode29( 有效的括号)
java·数据结构·算法·leetcode
于樱花森上飞舞2 小时前
【java】常见排序算法详解
java·算法·排序算法
GawynKing2 小时前
图论3 图的遍历
算法·深度优先
东方芷兰4 小时前
Leetcode 刷题记录 21 —— 技巧
java·算法·leetcode·职场和发展·github·idea
kyle~4 小时前
排序---选择排序(Selection Sort)
java·算法·排序算法