• POJ 3320 Jessica's Reading Problem


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

    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

     
    //题意:找出最短的连续子串,子串包含所有母串出现过的数
     

    //不知道是今天状态不好、还是自己不行、今天老是犯些小错误。

    //快排+二分查找+队列

    // 先对原来序列排序 再离散化

    // 再用队列

    // 给所有数排序、用编号代表数的大小,再用二分查找该数

    //按照原来给的数循序加入队列,直到有的数都已经进入队列、然后,根据数在队列出现的次数

    ///判断是否把该数从队首删除,以达到队列包含所有数的同时长度最小。

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #define Y 1000003
    using namespace std;
    int a[Y],b[Y];
    int h[Y];
    int n;
    int bf(int x)
    {
        int l=0,r=n-1,m;
        while(l<=r)
        {
            m=(l+r)>>1;
            if(a[m]>x) r=m-1;
            else if(a[m]<x) l=m+1;
                else  return m;
        }
    }
    int main()
    {
        int P,i,index,t,l;
        while(scanf("%d",&P)!=EOF)
        {
            for(i=0;i<P;i++)
             scanf("%d",&a[i]),b[i]=a[i];
            queue<int>Q;
            sort(a,a+P);
            for(n=0,i=1;i<P;i++)
             if(a[i]!=a[n])
               a[++n]=a[i];
             n++;t=1;
            memset(h,0,n*sizeof(int));
            index=bf(b[0]);
            h[index]++;
            Q.push(b[0]);
            for(i=1;i<P;i++)
            {
                index=bf(b[i]);
                if(!h[index])  t++;
                 h[index]++;
                Q.push(b[i]);
                if(t==n) {l=Q.size(); break;}
            } // printf("%d \n",l);
                index=bf(Q.front());
                while(h[index]>1)
                {
                    h[index]--;
                    Q.pop();
                    index=bf(Q.front());
                }
                if(l>Q.size())
                 l=Q.size();
            for(i++;i<P;i++)
            {
                Q.push(b[i]);
                index=bf(b[i]);
                h[index]++;
                index=bf(Q.front());
                while(h[index]>1)
                {
                    h[index]--;
                    Q.pop();
                    index=bf(Q.front());
                }
                if(l>Q.size())
                 l=Q.size();
            }
            printf("%d\n",l);
        }
        return 0;
    }

  • 相关阅读:
    单链表相关笔试题:单链表逆转,约瑟夫环等
    cURL安装和使用笔记
    WSDL中文版——详解
    [c++语法]类
    spark源码解析之基本概念
    apache-spark导入eclipse环境
    zookeeper应用实例
    spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
    Let's do our own full blown HTTP server with Netty--转载
    1号店11.11:从应用架构落地点谈高可用高并发高性能--转载
  • 原文地址:https://www.cnblogs.com/372465774y/p/2590734.html
Copyright © 2020-2023  润新知