Total Submissions: 20809 | Accepted: 7115 |
Description
Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that text book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.
A very hard-working boy had manually indexed for her each page of Jessica's text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.
Input
The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica's text-book. The second line contains P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit integer type.
Output
Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.
Sample Input
5
1 8 8 8 1
Sample Output
2
题意大致是给你一个数列,k种不同的数,问需要最少多长区间才可以涵盖所有。
思路:问题肯定是有解的。用尺取法来做,然后标记不同的数的话用map吧,开bool 数组RE。
AC Code:
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<bitset>
#include<cctype>
#define LL long long
#define maxn (LL)1e7
#define INF 0x3f3f3f3f
#define inf 0x7fffffff
#define PI acos(-1.0)
#define pb push_back
#define re register LL
const double eps = 0.0000001;
using namespace std;
typedef pair<LL,LL> pii;
inline int sgn(double x) {
return (x > eps) - (x < -eps);
}
static char buf[100000],*pa=buf,*pd=buf;
#define gc pa==pd&&(pd=(pa=buf)+fread(buf,1,100000,stdin),pa==pd)?EOF:*pa++
inline int read()
{
register LL x(0),f(1);register char c(gc);
while(c>'9'||c<'0')f=c=='-'?-1:1,c=gc;
while(c>='0'&&c<='9')x=x*10+c-48,c=gc;
return f*x;
}
LL n,m;
LL ans[maxn+5];
map<LL,LL> vis;
set<LL> st;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
LL T;
cin>>T;
for(re i =1;i<=T;++i)
{
ans[i] = read();
if(st.count(ans[i])==0){
st.insert(ans[i]);
}
}
LL L = st.size();//数列种类
LL _s = 1,_e = 1,res = inf;
LL sum = 0;
while(1)
{
while(_e<=T&&sum<L) if(!vis[ans[_e]]) {//如果没有出现过
vis[ans[_e]]++;
_e++;
sum += 1;
}
else vis[ans[_e]]++,_e++;
if(sum<L) break;//剩余不够L,跳出
res = min(res,_e - _s);//更新区间
--vis[ans[_s]];//移动左端点
if(vis[ans[_s]]==0) sum--;
_s++;
}
cout<<res<<endl;
}