Jessica‘s Reading Problem(尺取法)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 由于没有具体的要求,以下是一个简单的示例,包含了3000个json对象的列表: [ { "id": 1, "name": "John", "age": 25 }, { "id": 2, "name": "Sarah", "age": 31 }, { "id": 3, "name": "Mark", "age": 42 }, { "id": 4, "name": "Emily", "age": 19 }, { "id": 5, "name": "David", "age": 56 }, { "id": 6, "name": "Jessica", "age": 28 }, { "id": 7, "name": "Thomas", "age": 39 }, { "id": 8, "name": "Anna", "age": 22 }, { "id": 9, "name": "Michael", "age": 47 }, { "id": 10, "name": "Grace", "age": 33 }, ..., { "id": 2999, "name": "Alex", "age": 27 }, { "id": 3000, "name": "Olivia", "age": 38 } ] ### 回答2: 要将3000条JSON数据取合集,首先需要理解合集的概念。合集指的是将多个集合中的元素进行合并去重,最终得到一个新的集合,其中不包含重复的元素。 在处理JSON数据时,我们可以通过以下步骤来实现合集的操作: 1. 创建一个空集合,用于存储最终的合集结果。 2. 遍历每个JSON数据,可以通过循环来实现。在每次循环中,我们需要取出当前的JSON数据。 3. 检查当前的JSON数据是否为有效的JSON格式,可以使用现有的JSON解析库进行解析和校验。如果不是有效的JSON格式,则跳过当前JSON数据。 4. 将当前JSON数据解析为对象或字典,依据具体情况选择。这一步可以使用JSON解析库来实现。 5. 将解析得到的对象或字典中的元素添加到合集中,可以通过遍历解析结果的键值对,并逐个添加到合集中。 6. 重复步骤2到步骤5,直到遍历完所有的JSON数据。 7. 最后,合集中将只保留不重复的元素,即得到了3000条JSON数据的合集。 需要注意的是,在合集操作中需要对每个JSON数据进行有效性检查和解析,以确保数据的正确性和完整性。同时,在处理大量JSON数据时,要注意内存的使用,避免出现内存溢出的情况。 总结起来,要将3000条JSON数据取合集,可以通过遍历每个JSON数据,将其解析为对象或字典,并将解析结果添加到合集中。在合集操作中需要注意数据的有效性检查、解析和内存的使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值