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. [ACM_数学] Taxi Fare [新旧出租车费差 水 分段函数]

    Description Last September, Hangzhou raised the taxi fares. The original flag-down fare in Hangzhou ...

  2. C++ 关联容器

    <C++ Primer 4th>读书笔记 关联容器和顺序容器的本质差别在于:关联容器通过键(key)存储和读取元素,而顺序容器则通过元素在容器中的位置顺序存储和访问元素. 关联容器(Ass ...

  3. 01_JavaScript简介

    js用途 前端三层 结构层 HTML 从主义角度描述页面的结构 样式层 CSS 从审美的角度装饰页面 行为层 JS 从交互角度提升体验 HTML 里面的 b(加粗)/i(倾斜)/u(下划线)等标签由于 ...

  4. 优化TableView性能

    优化tableView性能(针对滑动时出现卡的现象) (2013-08-02 11:18:15) 转载▼ 标签: ios tableview it 分类: 技术文档 在iOS应用中,UITableVi ...

  5. 爬虫神器xpath的用法(一)

    1.如果你没有安装lxml,请运行pip install lxml或者easy_install lxml安装,如果在安装过程中失败的话, 是因为lxml需要依赖某些库文件,具体可以问下度娘,这里不再赘 ...

  6. 《软件性能测试与LoadRunner实战教程》新书上市

    作者前三本书<软件性能测试与LoadRunner实战>.<精通软件性能测试与LoadRunner实战>和<精通软件性能测试与LoadRunner最佳实战>面市后,受 ...

  7. Activiti 查看流程图

    package com.mycom.processDefinition; import java.io.File; import java.io.IOException; import java.io ...

  8. 今天踩过的坑——structs和phpmyadmin

    phpmyadmin 错误:缺少 mcrypt 扩展解决mv -i /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available/sudo php5enmo ...

  9. Lucene 4.X 倒排索引原理与实现: (2) 倒排表的格式设计

    1. 定长编码 最容易想到的方式就是常用的普通二进制编码,每个数值占用的长度相同,都占用最大的数值所占用的位数,如图所示. 这里有一个文档ID列表,254,507,756,1007,如果按照二进制定长 ...

  10. Revit2013工具栏工具无法显示BUG

    该BUG在Revit2013版中存在,主要症状就是当你激活某些工具的时候,上部工具栏中本应该显示的上下文工具显示不出来,比如当你选中模型中的风管的时候,正常情况下工具栏应该是这个样子. 但是在Revi ...