POJ 3320(尺取法)

Jessica's Reading Problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15707   Accepted: 5429

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

Source


扫描二维码关注公众号,回复: 2207730 查看本文章

    啊这样算是水题吧,像这种求最短的符合条件的连续子序列,而且整个序列拥有某种随着长度变化的规律的话,都可以考虑使用尺取法进行解题。也就是说,当已知当前序列的时候,可以判断当前序列是否符合条件,而且能够判断下一个序列的变化趋势的时候可以使用尺取法。

    对本题来说,整个序列包含的知识点数量从左到右呈递增趋势,通过map容器可以判断这一页上的知识点是否出现过。当已知当前序列的时候,通过已包含知识点的数量和需要包含知识点数量的比较可以判断下一个序列是否需要再次缩短。


#include <iostream>
#include <set>
#include <map>
#include <cstdio>  //再次……poj不支持万能头
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1000010;
int input[MAXN];
map<int, int> hashTable; //用于保存已出现的知识点以及其出现的次数
set<int> temp;

int main()
{

    int n;
    scanf("%d",&n);
    for(int i = 1;i <= n;++i){
        scanf("%d",&input[i]);
        temp.insert(input[i]); 
    }
    int len = temp.size();  //set容器自动有序并去重,可以利用这一性质来得到不同的知识点总数量
    temp.clear();
    int left = 1, right = 1, num = 0, ans = INF;

    while(1){
        while(right <= n && num < len){  //此循环仅执行一次,用于判断子序列右端点的最小位置
            if(hashTable[input[right++]]++ == 0) //
                ++num;
        }
        if(num < len)// 此分支有两个用处,一是若上面那个循环完成后知识点总数不足要求,就退出循环(这个题不需要)。二是求最大左端点
            break;
        ans = min(ans, right - left); //【left,right) 区间左闭右开
        if(--hashTable[input[left++]] == 0) //如果去掉最右边元素,不同的
            --num;
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/aldo101/article/details/80647011