poj 3320 Jessica's Reading Problem(尺取法)

  1. 题意:为了准备考试,Jessica开始读一本很厚的课本。要想通过考试,必须把课本中所有的知识点都掌握。这本书总共有P页,第i页恰好有一个知识点ai(每个知识点都有一个整数编号)。全书中同一个知识点可能会被多次提到,所以她希望通过阅读其中连续的一些页把所有的知识点都覆盖到。给定每页写到的知识点,请求出要阅读的最少页数。
  2. problem link:http://poj.org/problem?id=3320
  3. 尺取法应用题目

AC code:

#include<iostream>
#include<set>
#include<map>
using namespace std;
const int N=1e6+6;
int n,a[N];
void solve(){
    set<int>s;
    for(int i=0;i<n;i++)s.insert(a[i]);
    int cnt=s.size();
    int x=0,y=0,num=0,res=n;
    map<int,int>mp;
    while(1){
        while(num<cnt&&y<n)if(mp[a[y++]]++==0)num++;
        if(num<cnt)break;
        res=min(res,y-x);
        if(--mp[a[x++]]==0)num--;
    }
    cout<<res<<endl;
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>n;for(int i=0;i<n;i++)cin>>a[i];
    solve();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值