poj 3320 Jessica's Reading Problem

题意:求最少复习天数 && 可以复习完书本全部知识点;

思路: 用俩个下标st ed 去扫描;中间用map 去维护区间 [st, ed] 的知识点涵盖情况;

时间复杂度: O(nlogn)  map 的复杂度;

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<map>
#include<set>
using namespace std;
const int N=1000010;
int n, allkind ;
int a[ N ];
int b[ N ];
map<int,int>cnt;
void deal_init(){
    for(int i = 0 ; i < n ; i ++){
        scanf("%d", &a[ i ] ) ;
        b[ i ] = a[ i ];
    }
    sort(b, b+n);
    allkind=unique(b , b + n) - b ;
//    cout << allkind << endl;
}

void work(){
    cnt.clear();

    int st=0,ed=0;
    int num = 0, ans=N+100;
    while( st < n ){
        while( ed < n && num < allkind ){
            int tp = cnt[ a[ed++] ]++;
            if( tp == 0 ) num ++ ;
        }
        if( num < allkind ) break;

        ans = min( ans , ed-st);
        int tp = cnt [ a[st++] ]--;
        if(tp == 1 ) num -- ;
    }
    cout << ans << endl;
}
int main()
{

//	freopen("in.in","r",stdin);
	while(~scanf("%d",&n)){
        deal_init();
        work();
	}


	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值