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

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
题目大意:输入一串数字,球连续数字串的最短长度,使得数字串包含数字串中的所有字符。
解题方法:用哈希表统计每个数字出现的次数,然后求最短长度,关于这道题很多人用哈希表+二分,这样时间复杂度为O(n*logn),我采用的方法是用i,j两个下标直接遍历,时间复杂度为O(n)。
#include <stdio.h>
#include <iostream>
using namespace std; #define MAX_VAL 1000050 typedef struct
{
int x;
int nCount;
}Hash; Hash HashTable[];//哈希表,统计数字出现的次数
int Maxn = ;//统计总共有多少个不同的数字
int ans[];//ans[i]代表当出现的不同数字个数为i的时候的最短长度
int num[];//输入的数字 //插入哈希表
void InsertHT(int n)
{
int addr = n % MAX_VAL;
while(HashTable[addr].nCount != && HashTable[addr].x != n)
{
addr = (addr + ) % MAX_VAL;
}
HashTable[addr].nCount++;
HashTable[addr].x = n;
} //得到哈希表中元素的地址
int GetAddr(int n)
{
int addr = n % MAX_VAL;
while(HashTable[addr].nCount != && HashTable[addr].x != n)
{
addr = (addr + ) % MAX_VAL;
}
return addr;
} int main()
{
int n;
scanf("%d", &n);
if (n == )
{
printf("1\n");
return ;
}
for (int i = ; i <= n; i++)
{
ans[i] = ;
}
for (int i = ; i < n; i++)
{
scanf("%d", &num[i]);
}
int i = , j = ;
InsertHT(num[]);
while(j < n)
{
//如果某个数字的计数为0,则说明这是一个新数字,所以Maxn加1
if (HashTable[GetAddr(num[j])].nCount == )
{
Maxn++;
}
InsertHT(num[j]);//将数字插入到哈希表
//i从前向后遍历,如果某个数字的出现次数大于1,则i加1
while(HashTable[GetAddr(num[i])].nCount > )
{
HashTable[GetAddr(num[i])].nCount--;
i++;
}
//每次记录当前不同数字为Maxn的最短长度
ans[Maxn] = min(ans[Maxn] ,j - i + );
j++;//j加1,跳转到下一个数字
}
printf("%d\n", ans[Maxn]);//最后打印的结果即为所有数字都出现的最短连续子序列
return ;
}

POJ 3320 Jessica's Reading Problem的更多相关文章

  1. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

  2. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  3. POJ 3320 Jessica's Reading Problem 尺取法/map

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accept ...

  4. POJ 3320 Jessica's Reading Problem 尺取法

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  5. POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)

    http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...

  6. <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针

    地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展  若满足要求则从左缩减区域 代码如下  正确性调整了几次 然后 ...

  7. poj 3320 Jessica's Reading Problem(尺取法)

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  8. POJ 3320 Jessica's Reading Problem (尺取法)

    Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...

  9. 题解报告:poj 3320 Jessica's Reading Problem(尺取法)

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

随机推荐

  1. [安卓] 9、线程、VIEW、消息实现从TCP服务器获取数据动态加载显示

    一.前言: 一般情况下从TCP服务器读取数据是放在一个线程里读的,但是刷新界面又不得不放在线程外面,所以需要用消息传递把线程里从TCP里获得的数据传送出来,然后根据数据对页面进行相应的刷新. 二.业务 ...

  2. Mac上搭建直播服务器Nginx+rtmp

    简介 nginx是非常优秀的开源服务器,用它来做hls或者rtmp流媒体服务器是非常不错的选择,本人在网上整理了安装流程,分享给大家并且作备忘. 步骤安装 1.安装Homebrow Homebrew简 ...

  3. linux下ssh远程登录服务器入门操作

    使用用户名密码登录 在命令行中输入命令: ssh username@ip_address -p port 之后系统会提示输入密码,输入后即可登录 如果不添加-p选项,则默认是22端口 还可以使用-l选 ...

  4. Install wget for mac

    Download: http://ftp.gnu.org/gnu/wget/ Unpack: tar zxvf wget-1.16.tar Configuration: ./configure If ...

  5. 解决 "Windows 无法启动Mongo DB服务 错误:1067 进程意外终止"

    在启动MongoDB服务时,有时会报上图所示的错误,解决方案为: 1. MongoDB安装目录\data\将此文件夹下的mongod.lock删除 2. 查看官方文档或按照上一篇安装文章检查是否设置d ...

  6. shell切割日志脚本

    #!/bin/bash set -e source /etc/bashrc cd `dirname $` linenum=`wc -l userinfolist.txt | awk '{print $ ...

  7. IUnknown—COM和MFC

    http://www.vckbase.com/index.php/wv/60 问题: 我用MFC编写COM程序有一段时间了,知道如何使用宏和嵌套类,以及如何在嵌套类中处理IUnknown接口,但对IU ...

  8. Python:字符编码详解

    相关文章 Python中文编码问题:为何在控制台下输出中文会乱码及其原理 1. 字符编码简介 1.1. ASCII ASCII(American Standard Code for Informati ...

  9. Docker实践(6)—CentOS7上部署Kubernetes

    Kubernetes架构 Kubernetes的整体架构如下: Master为主控节点,上面运行apiserver,scheduler,controller-manager等组件.Minion相当于工 ...

  10. Java httpclient请求,解决乱码问题

    public class HttpPostRequestUtil { public HttpPostRequestUtil() { } public static String post(String ...