一、题目内容
POJ 3320 原题地址
二、题意解释
一本书有 P 页,每页都有个知识点a
[i],知识点可能重复,求包含所有知识点的最少的页数。
三、代码及注释
#include<cstdio>
#include<algorithm>
#include<set>
#include<map>
using namespace std
;
int p
;
int a
[1000005];
void solve()
{
set
<int> all
;
for(int i
=0; i
<p
; i
++)
{
all
.insert(a
[i
]);
}
int n
=all
.size();
int s
=0,t
=0,num
=0;
int res
=p
;
map
<int,int> count
;
for(;;)
{
while(t
<p
&&num
<n
)
{
if(count
[a
[t
]]==0)
{
count
[a
[t
]]++;
t
++;
num
++;
}
else
{
count
[a
[t
]]++;
t
++;
}
}
if(num
<n
) break;
res
=min(res
,t
-s
);
--count
[a
[s
]];
if(count
[a
[s
]]==0)
{
num
--;
}
s
++;
}
printf("%d\n",res
);
}
int main()
{
scanf("%d",&p
);
for(int i
=0; i
<p
; i
++)
{
scanf("%d",&a
[i
]);
}
solve();
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-6154.html