博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3320 Jessica's Reading Problem (尺取法)
阅读量:4318 次
发布时间:2019-06-06

本文共 2262 字,大约阅读时间需要 7 分钟。

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

51 8 8 8 1

Sample Output

2

题意:

一本书有p页 每一页都有一个知识点ai 存在不同两页上的知识点相同的情况

求最少读连续的多少页书 能把所有的知识点全部覆盖到

尺取法:

反复的推进区间的开头和结尾 来求满足条件的最小区间的方法被称为尺取法

尺取法的复杂度为O(n).

 题解:

初始化 start  end  sum =0;

然后推进end 直到恰好满足条件退出 如果区间已经到了结尾 仍不满足 则退出

然后更新答案 ans = min(ans,end-start);

然后start推进一位,要处理这时对sum的影响

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 using namespace std;14 #define lowbit(x) (x&(-x))15 #define max(x,y) (x>y?x:y)16 #define min(x,y) (x
mp;36 int num=0;37 for(int i=0;i

转载于:https://www.cnblogs.com/shixinzei/p/7272913.html

你可能感兴趣的文章
Spring Framework tutorial
查看>>
【VS开发】win7下让程序默认以管理员身份运行
查看>>
【机器学习】Learning to Rank 简介
查看>>
Unity 使用实体类
查看>>
MySQL常见注意事项及优化
查看>>
流畅的Python (Fluent Python) —— 前言
查看>>
Jquery-menu-aim流畅的菜单滑动体验
查看>>
Jquery EasyUI修改行背景的两种方式
查看>>
嵌入式Linux学习笔记(0)基础命令。——Arvin
查看>>
二分图匹配
查看>>
c++ 模板template
查看>>
CString的成员函数详解
查看>>
Appium Studio 初体验(windows做ios自动化,录制appium脚本)
查看>>
Linux常用命令
查看>>
整体二分&cdq分治 ZOJ 2112 Dynamic Rankings
查看>>
【POJ2976】Dropping tests (01分数规划入门题)
查看>>
通过正则表达式获取url中参数
查看>>
cxx signal信号捕获
查看>>
《Android开发艺术探索》读书笔记——Cha3.2.3改变布局参数实现View的滑动
查看>>
python闭包与装饰器
查看>>