Jessica's Reading Problem(尺取法)

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

//尺取法,先得出出现的不同数字的个数len,然后从头开始先选出等于len的子序列,然后减去该子序列左边的值,减去后如果还是等于len,继续减去左边的值,如果小于len了,则向右加,直到再次达到len.

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
using namespace std;
map<int,int>a;
int P, p[1000010];


int main()
{
    while(scanf("%d",&P)!=EOF)
    {
        a.clear();
        for(int i=0; i<P; i++)
        {
            scanf("%d",&p[i]);
            a[p[i]] = 1;
        }
        int len = a.size();//一共多少种不同元素
        a.clear();
        int num=0, s=0, t=0, ans=P;
        while(1)
        {
            while(t<P && num<len)
            {
                if(a[p[t]]==0)
                    num++;
                a[p[t]]++;//前len种不同元素出现的次数
                t++;
            }
            if(num < len)
                break;//没有连续的子书包含所有内容
            ans = min(ans,t-s);//尽量取小一点的子书
            a[p[s]]--;
            if(a[p[s]]==0)
                num--;
            s++;
        }
        printf("%d\n",ans);
    }
}






  • 1
    点赞
  • 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、付费专栏及课程。

余额充值