Jessica‘s Reading Problem(尺取法)

这篇博客介绍了一个利用尺取法解决全出现区间问题的算法。给定一本包含多个不同思想的厚书,Jessica需要找到最短的连续部分以便阅读所有思想。通过建立索引并使用尺取法降低时间复杂度,博主提供了AC代码实现,该代码使用了set和map数据结构来跟踪和更新每个思想的出现情况,最终找到最短的区间长度。
部署运行你感兴趣的模型镜像

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

题意: 给你一个n,下面有n个数。求一个最短的区间,使这n个数都在这个区间出现过至少一次。

思路: 数据太大,所以需要用尺取法降低时间复杂度,尺取法就是定义l,r左边界和右边界,r++直到区间满足题意,然后开始缩区间,即l++,遍历一遍后,得答案,因为这题数据太大,数组会爆掉,所以用到了set和map。

AC代码:

个人感觉这个代码更好理解一些

#include<stdio.h>
#include<string.h>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
int a[1100000];
int main()
{
	int t,n,s,i,j=0,k,sum,c;
	set<int>set;
	scanf("%d",&n);
	for(i=0; i<n; i++)
	{
		scanf("%d",&a[i]);
		set.insert(a[i]);//插入,set本身就有去重功能
	}
	j=set.size();//集合中元素的数目,即有几个不同的数
	map<int,int>map;//标记
	int l=0,r=0,ans=0x3f3f3f;
	sum=0;
	k=0;
	while(l<=r&&r<=n)
	{
		if(sum==j)//如果区间满足题意
		{
			ans=min(ans,r-l);//取区间最小值
			//	printf("%d  %d  %d----\n",ans,l,r);
			map[a[l]]--;//l要右移,减去一次
			if(map[a[l]]==0)//如果为0,说明l++后的区间中没有a[l],个数减少
			sum--;
			l++;
		//	printf("%d****\n",sum);
		}
		else
		{
			if(map[a[r]]==0)//如果之前没有出现过
			sum++;//记录目前有几个不同的数
			map[a[r]]++;//printf("%d++++\n",a[r]);	//记录这个数出现了几次
		//	printf("%d>>>>>\n",sum);
			r++;
		}
	}
	printf("%d\n",ans);
	return 0;
}

您可能感兴趣的与本文相关的镜像

Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值