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

注意: 不要把 p付给 ans,把 inf付给 ans。

完整代码:

#include<cstdio>
#include <iostream>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f3f3f3f3f;
ll a[1000010];
set<ll>st;
map<ll,ll>mp;       //对应每个知识点出现的次数
int main()
{
    ll p;
    cin>>p;
    for(ll i=0;i<p;i++)
    {
        scanf("%lld",&a[i]);
        //cin>>a[i];//这里使用cin真的慢,贼慢 ,很慢
        st.insert(a[i]);
    }
    ll len=st.size();   //知识点的个数
    ll ans=inf;
    ll l=0,r=0;         //左右
    ll sum=0;
    while(1)
    {
        while(r<p&&sum<len)     //不停的向右
        {
            if(mp[a[r++]]++==0)
            {
                sum++;
            }
        }
        if(sum<len)         //sum<len r延申出去了
        {
            break;
        }
        ans=min(ans,r-l);
        if(--mp[a[l++]]==0)     //如果一个知识点不出现了(出现的次数变为0),就减少一个知识点
        {
            sum--;
        }
    }
    printf("%lld\n",ans);
    return 0;
}


不会容器(set和map)的同学可以看一下这个博客: https://blog.csdn.net/qq_45856289/article/details/104506676

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

余额充值