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. mongodb(一) NoSQL简介

    NoSQL简介   写在前面,本文就是学习的记录笔记,大部分内容都属于参考,分享给大家 关系与非关系数据库      那么应该了解下影响关系数据库性能的主要原因: 在关系型数据库中,导致性能欠佳的最主 ...

  2. 喜迎2015年新年:坦克大战(Robocode)游戏编程比赛图文总结

    2015春节前,葡萄城的软件工程师以特有的方式来迎接新年——2015新年编程邀请赛. 邀请赛的初衷,是和大家一起,寻找编程最初的单纯的快乐.       在代码的世界里,添加动力,继续远航.      ...

  3. webApp 阅读器项目实践

    这是一个webApp 阅读器的项目,是慕课网的老师讲授的一个实战,先给出项目源码在GitHub的地址:https://github.com/yulifromchina/MobileWebReader. ...

  4. [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论

    前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...

  5. windows下安装mysql压缩包版[转]

    版本:5.6.17 1.将解压后的文件夹放到某个目录下,比如c:\software; 2.在环境变量中新建MYSQL_HOME=C:\software\mysql-5.6.17-winx64,然后在系 ...

  6. paip.提升性能---mysql 性能 测试以及 参数调整.txt

    paip.提升性能---mysql 性能 测试以及 参数调整.txt 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://b ...

  7. Leetcode 179 Largest Number 贪心

    此题主要是讲给你一组数,如何将数连在一起能得到最大值(最小值反之),注意局部最优,就是说如果 123 234两个连在一起怎么样最大,显然是234123比123234大,对于3个数我们可以找到类似的性质 ...

  8. PHP中常用的正则表达式由哪些元素构成?

    在程序开发中,我们常常要用到正则表达式,对于新手来说,很多时候知道正则表达式是怎么回事,但当真正需要使用的时候,却不知该用什么函数,具体的修饰符也比较混乱.下面小编就为大家整理了一些php正则表达式中 ...

  9. solr多core的处理

    有2中配置方式,一是从Solr Admin进行multi core的配置. 在Solr Admin控制台里面选择:Core Admin 选择Add Core 然后把你准备好的路径写到里面去. name ...

  10. js Array 交集 并集 差集 去重

    最劲项目需要用到js数组去重和交集的一些运算,我的数组元素个数可能到达1000以上,网上的实现方式都是2次循环,性能不适合我的需求,1000*1000那循环次数太多了,所以我这里采用对象object来 ...