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 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
题意:
一本书有p页 每一页都有一个知识点ai 存在不同两页上的知识点相同的情况
求最少读连续的多少页书 能把所有的知识点全部覆盖到
尺取法:
反复的推进区间的开头和结尾 来求满足条件的最小区间的方法被称为尺取法
尺取法的复杂度为O(n).
题解:
初始化 start end sum =0;
然后推进end 直到恰好满足条件退出 如果区间已经到了结尾 仍不满足 则退出
然后更新答案 ans = min(ans,end-start);
然后start推进一位,要处理这时对sum的影响
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
const int N=1e6+;
const int mod=1e9+;
int a[N];
int main()
{
int n;
while(~scanf("%d",&n)){
map<int,int>mp;
int num=;
for(int i=;i<n;i++){
scanf("%d",a+i);
if(!mp[a[i]]) num++;
mp[a[i]]++;
}
int st=,t=,sum=,ans=n;
mp.clear();
while(){
while(t<n&&sum<num){
if(!mp[a[t++]]++)
sum++;
}
if(sum<num) break;
ans = min(ans,t-st);
if(--mp[a[st++]]==)
sum--;
}
printf("%d\n",ans);
}
return ;
}
POJ 3320 Jessica's Reading Problem (尺取法)的更多相关文章
- POJ 3320 Jessica's Reading Problem 尺取法/map
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7467 Accept ...
- 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 ...
- poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用
jessica's Reading PJroblem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9134 Accep ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- POJ 3320 Jessica's Reading Problem
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6001 Accept ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- 题解报告: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 ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
随机推荐
- 前端 HTML body标签相关内容 常用标签 段落标签 p标签
段落标签 <p>,paragraph的简写.定义段落,默认段落之间有间隔的 浏览器展示特点: 跟普通文本一样,但我们可以通过css来设置当前段落的样式 是否又独占一行呢? 答案是的 块级元 ...
- SQL Server 安装好后 Always On群组配置
需要对SQL Server必要的端口打开Windows防火墙的入站规则,比如TCP- 1433端口等. 特别注意:由于AG默认需要用到TCP-5022端口,所以该端口务必保证在Windows防火墙中开 ...
- .NET Core 指南 官方
https://docs.microsoft.com/zh-cn/dotnet/core/index
- 小程 序swiper自适应宽高
https://blog.csdn.net/qq_31604363/article/details/73715944 小程 序swiper自适应宽高 小程 序swiper自适应宽高
- [django]django corepython核心编程
model meta排序 class BlogPost(models.Model): title = models.CharField(max_length=150) body = models.Te ...
- haproxy json 自定义格式日志 并用rsyslog 收集
haproxy的配置 日志投送: global log 127.0.0.1 local1 info 默认端口是udp514 默认日志: defaults log global log-format \ ...
- Linux 网络编程之 Select
/*server*/ #include <stdio.h> #include <string.h> #include <unistd.h> #include < ...
- [LeetCode] 490. The Maze_Medium tag: BFS/DFS
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- js图的数据结构处理----邻链表,广度优先搜索,最小路径,深度优先搜索,探索时间拓扑
//邻居连表 //先加入各顶点,然后加入边 //队列 var Queue = (function(){ var item = new WeakMap(); class Queue{ construct ...
- numpy中np.c_和np.r_
np.r_:按列连接两个矩阵,就是把两矩阵上下相加,要求列数相等,类似于pandas中的concat() np.c_:按行连接两个矩阵,就是把两矩阵左右相加,要求行数相等,类似于pandas中的mer ...