zoukankan      html  css  js  c++  java
  • poj3320 Jessica's Reading Problem

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <set>
    #include <map>
    using namespace std;
    const int MAX_P = 1e6 + 10; 
    
    int P;
    int a[MAX_P];
    
    void solve()
    {
    	// 计算全部知识点的总数
    	 set<int>all;
    	 for (int i = 0; i < P; i++)
    	 all.insert(a[i]);
    	 
    	 int n = all.size();
    	 
    	 // 利用尺取法来求解
    	 int s = 0, t = 0, num = 0;
    	 map<int, int> count; // 知识点到出现次数的映射
    	 int res = P;
    	 for ( ; ; )
    	 {
    	 	while (t < P && num < n)
    	 	{
    	 		if (count[a[t++]]++ == 0) // 出现新的知识点...注意,两个自增符号,无论==是否满足,都会执行 
    	 		{
    	 			num++;
    			}
    		}
    		if (num < n) break;
    		res = min(res, t - s);
    		if (--count[a[s++]] == 0) num--; //去掉首页,对应的,首页上的知识点出现次数-1,如果原来就只出现一次,则出现的知识点种类数-1 
    	 }
    	 cout << res << endl;	  
    }
    int main()
    {
    	scanf("%d", &P);
    	for (int i = 0; i < P; i++) scanf("%d", a + i);
    	solve();
    	return 0;
    }

  • 相关阅读:
    xt
    UVA 10200 Prime Time (打表)
    CodeForces 540B School Marks
    CodeForces 540C Ice Cave (BFS)
    poj 3250 Bad Hair Day(栈的运用)
    hdu A Magic Lamp
    hdu 4325 Flowers(区间离散化)
    hdu 5500 Reorder the Books
    V2X之标准
    V2X的前生今世
  • 原文地址:https://www.cnblogs.com/mofushaohua/p/7789516.html
Copyright © 2011-2022 走看看