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.
InputThe 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 one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.
5 1 8 8 8 1
2
#include <cstring>
#include <map>
#include <set>
#include <algorithm>
#define MAX 1000000+10
using namespace std;
int P, n;
int a[MAX];
{
int t,s,num;
int ans=P;
map<int, int> count;
t=s=num=0;
for(;;)
{
while(t<P&&num<n)
{
if(count[a[t++]]++==0)
num++;
}
if(num<n) break;
ans=min(ans,t-s);
if(--count[a[s++]]==0)
num--;
}
printf("%d",ans);
}
{
while(~scanf("%d",&P))
{
set<int> all;
for(int i=0;i<P;i++)
{
scanf("%d",&a[i]);
all.insert(a[i]);
}
n=all.size();
solve();
}
return 0;
}
本文介绍了一种解决最短覆盖子集问题的算法,即如何从给定的一系列页面中找到包含所有不同思想的最短连续部分。通过使用尺取法并结合C++中的set和map数据结构实现解决方案。
393

被折叠的 条评论
为什么被折叠?



