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

时间:2022-09-23 21:07:27

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
解题思路:题意就是找出连续的最少页数包含所有知识点ID,典型的尺取法。用set统计知识点的总个数,然后尺取取出某个区间中含有所有知识点,如果有多个满足条件,则取最小的区间长度;注意:右指针向右移动,对应指向的知识点个数加1,如果出现新的知识点,对应种类数加1;左指针向右移动,对应知识点个数先减1,如果其个数减为0,则对应种类数减1。时间复杂度为O(PlogP)。
AC代码(407ms):
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<set>
#include<map>
using namespace std;
const int maxn=;
int P,cnt,bg,ed,res,num,a[maxn];set<int> st;map<int,int> mp;
int main(){
while(~scanf("%d",&P)){
st.clear();mp.clear();//清空
for(int i=;i<P;++i)scanf("%d",&a[i]),st.insert(a[i]);
cnt=st.size();bg=ed=num=;res=P;//res初始化为P,表示最多读P页
while(){
while(ed<P&&num<cnt){
if(mp[a[ed++]]++==)num++;//出现新的知识点
}
if(num<cnt)break;//如果尺取得到的区间中知识点个数小于总个数,不用继续循环了,直接退出
res=min(res,ed-bg);
if(--mp[a[bg++]]==)num--;//队首元素的次数如果减为0,则种数num减1
}
printf("%d\n",res);
}
return ;
}

题解报告:poj 3320 Jessica's Reading Problem(尺取法)的更多相关文章

  1. POJ 3320 Jessica&&num;39&semi;s Reading Problem 尺取法&sol;map

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

  2. POJ 3320 Jessica&&num;39&semi;s Reading Problem 尺取法

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

  3. poj 3320 jessica&&num;39&semi;s Reading PJroblem 尺取法 -map和set的使用

    jessica's Reading PJroblem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9134   Accep ...

  4. 尺取法 POJ 3320 Jessica&&num;39&semi;s Reading Problem

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

  5. POJ 3320 Jessica&&num;39&semi;s Reading Problem

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

  6. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map&plus;set&plus;尺取法

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

  7. POJ 3320&Tab;Jessica&&num;39&semi;s Reading Problem (尺取法)

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

  8. POJ 3320 Jessica&OpenCurlyQuote;s Reading Problem(哈希、尺取法)

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

  9. &lt&semi;挑战程序设计竞赛&gt&semi; poj 3320 Jessica&&num;39&semi;s Reading Problem 双指针

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

随机推荐

  1. Spark join 源码跟读记录

    PairRDDFunctions类提供了以下两个join接口,只提供一个参数,不指定分区函数时默认使用HashPartitioner;提供numPartitions参数时,其内部的分区函数是HashP ...

  2. jquery选择器 之 获取父级元素、同级元素、子元素

    jquery选择器 之 获取父级元素.同级元素.子元素 一.获取父级元素 1. parent([expr]): 获取指定元素的所有父级元素 <div id="par_div" ...

  3. 一些JS周边工具

    Visual Studio JS 辅助插件 JScript Editor Extensions 功能: 1.     代码块折叠 2.     方法参数智能提示 3.     代码块Outlining ...

  4. 使用 ZooKeeper 同步集群配置

    用 ZooKeeper 同步集群配置,当需要修改所有节点配置时,将配置更新到 ZooKeeper 的一个节点,引起这个节点数据发生变化, 其他所有需要同步配置的节点上的本地 Watcher 会立即发现 ...

  5. Redmine管理项目3-调整用户显示格式

    在 Redmine 中新建用户时是这样的: 必须指定姓氏.名字,然后 Redmine 默认是按“名字 姓氏”这种方式显示用户.比如“张三”,会显示成“三张”……看起来好别扭啊. 怎么调整呢,参看 Re ...

  6. scrapy 爬取当当网产品分类

    #spider部分import scrapy from Autopjt.items import AutopjtItem from scrapy.http import Request class A ...

  7. iOS绘制坐标图,折线图-Swift

    坐标图,经常会在各种各样的App中使用,最常用的一种坐标图就是折线图,根据给定的点绘制出对应的坐标图是最基本的需求.由于本人的项目需要使用折线图,第一反应就是搜索已经存在的解决方案,因为这种需求应该很 ...

  8. Image转Base64

    今天和一个朋友联调图片转Base64时发现一个问题 public static string ImageToBase64(Image img) { BinaryFormatter binFormatt ...

  9. laravel在控制器中动态创建数据表

    Schema::connection('usertable')->create('test', function ($table) { $table->increments('id'); ...

  10. hdu1385 最短路字典序

    http://blog.csdn.net/ice_crazy/article/details/7785111 http://blog.csdn.net/shuangde800/article/deta ...