L i n k Link Link
D e s c r i p t i o n Description Description
农夫约翰的N只奶牛排成了一条直线,每只奶牛都有一个特定的标识序号,从左到右第i只奶牛的编号是Bi。
约翰认为他的奶牛队列中存在某段连续区间都是相同序号的,那他的队列就非常的美观。为了创造这样的一个连续区间,约翰决定将某个特定的序号的奶牛全部剔除队列。
问题描述:
请帮助约翰计算出通过选择除去某个特定序号的全部奶牛后,所剩下的队列中连续的有相同序号的奶牛的最大区间。
I n p u t Input Input
第一行一个正整数N,表示奶牛的数量。
第2行到第N+1行,每行一个非负整数,表示从左到右每只奶牛的序号。
O u t p u t Output Output
只有一行一个整数,表示连续的有相同序号的奶牛的最大区间中奶牛的数量。
S a m p l e Sample Sample I n p u t Input Input
9
2
7
3
7
7
3
7
5
7
S a m p l e Sample Sample O u t p u t Output Output
4
E x p l a i n Explain Explain
数据范围:1<=N<=1000,奶牛序号的范围是0到1000000。
说明:通过除去所有序号为3的奶牛,我们就得到了2777757这样一个序列,那么最长的连续的有相同序号的区间长度就是4。
T r a i n Train Train o f of of T h o u g h t Thought Thought
直接暴力枚举每一个数字被去掉的可能,求最大值就好了
C o d e Code Code
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
bool p[1000001];
int t,n,l,maxn=1,a[1001],b[1001],sum;
int main()
{
scanf("%d",&n);
for (int i=1; i<=n; ++i) {
scanf("%d",&a[i]);
if (!p[a[i]]) {
p[a[i]]=true;
b[++t]=a[i];
}
}
for (int i=1; i<=t; ++i)
{
for (int j=1; j<=n; ++j)
{
if (a[j]==b[i]) continue;//被去掉的数字
else if (a[j]==l) maxn=max(maxn,++sum);//求最大值
else sum=1,l=a[j];
}
}
printf("%d",maxn);
}