POJ3320 Jessica's Reading Problem(尺取+map+set)

  POJ3320 Jessica‘s Reading Problem

  set用来统计所有不重复的知识点的数,map用来维护区间[s,t]上每个知识点出现的次数,此题很好的体现了map的灵活应用

  

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cmath>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;
const int MAX_P = 1000010;
int P;
int a[MAX_P];
int main()
{
    scanf("%d", &P);
    for (int i = 0; i < P; ++i)
    {
        scanf("%d", &a[i]);
    }
    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++;
            }
            count[a[t]]++;
            t++;
        }
        if (num < n) break;
        res = min(res, t-s);//更新最小区间长度
        count[a[s]]--;
        if (count[a[s]] == 0) //某个知识点出现次数为0
        {
            num--;
        }
        s++;
    }
    printf("%d\n", res );
    return 0;
}

POJ3320 Jessica's Reading Problem(尺取+map+set)

时间: 2024-10-17 04:02:13

POJ3320 Jessica's Reading Problem(尺取+map+set)的相关文章

poj3061 Subsequence&amp;&amp;poj3320 Jessica&#39;s Reading Problem(尺取法)

这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针(t)要么不动要么也往前走.满足这种特点的就可以考虑尺取法. poj3061 比较简单,也可以用二分做,时间复杂度O(n*logn).用尺取法可以O(n)解决. #include<iostream> #include<cstdio> #include<cstdlib> #i

poj3320(Jessica&#39;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 au

poj 3320 Jessica&#39;s Reading Problem(尺取法+map/hash)

题目:http://poj.org/problem?id=3320 题意:给定N个元素的数组,找出最短的一段区间使得区间里面的元素种类等于整个数组的元素种类. 分析:暴力枚举区间的起点x,然后找到最小的y,使得区间[x,y]满足条件,x向有移位后变成x',现在的y'肯定不至于在y的左边.存状态的话map和hash都可以. map代码: #include <iostream> #include <set> #include <map> #include <cstdi

POJ 3320 Jessica&#39;s Reading Problem 尺取法/map

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accepted: 2369 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 littl

poj3320 Jessica&#39;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 au

poj 3320 Jessica&#39;s Reading Problem (尺取法)

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Accepted: 2824 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 littl

Jessica&#39;s Reading Problem POJ 3320(尺取)

原题 题目链接 题目分析 题目要求一段连续的书页,里面包括了全部知识点,可以考虑尺取,由于知识点的编号无规律,可以考虑用set来记录全部知识点的种数,尺取的过程可以考虑用map来辅助判断区间[s,t]是否包括全部知识点,映射map<知识点编号,个数>,当每种知识点个数至少为1的时候表明[s,t]包含所有知识点. 代码 1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #inclu

(尺取法)POJ 3320 Jessica&#39;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

POJ - 3320 Jessica&#39;s Reading Problem(尺取法+STL)

题目链接:http://poj.org/problem?id=3320 题意:一本n页的书(有很多重复的页),要求连续最少页数把这本书每种页都包括进去,求出最少页数. 例如1 8 8 8 1 就1和8两种页,第一次1 8连续两页就把每种页都包括进去了. 尺取法:顾名思义就是一段一段的取.一开始一直往后取,符合条件,然后进行前面减少操作,不符合条件再往后取. 如此重复,得到最优的解.这道题目给出的页的标记好像有特别大的,直接用数组的话会爆炸. 试过用vector,但是对vector的操作太麻烦了.