Jessica's reading problem

85 篇文章 0 订阅
36 篇文章 0 订阅

问题描述如下:

Jessiac 读一本P页的书, 第i页恰好记录了知识点ai(每个知识点都有一个整数标号), 书中同一个知识点可能会出现多次。 Jessica希望通过阅读书中的连续的页数, 并且能够覆盖掉所有的知识点。 试着确定Jessica要阅读的最少页数。

限制条件: 1<= P <= 1000000

程序如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <map>

using namespace std;

const int MAX_P = 1e6 + 1000; // 基本语法, 程序员留一定的
                         // 留个余量;业务最大用到1e6,
                         //但程序员多分配1000,留一定余量
//const int INF = 0x3fffffff;

int P, a[MAX_P], res;
set<int> all;

map<int, int> Count;

int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int testCase = 0;

    scanf("%d", &testCase);

    for(int testID = 1; testID <= testCase; testID++) {
        all.clear(); // every time, you need to delete all the elements,  and restart
        Count.clear(); // every time,  you need to delete all the elements, every time you need to a fresh start
        scanf("%d", &P); 
        res = P; // we can also use P instead of INF
        for(int i = 0; i < P; ++i) {
            scanf("%d", &a[i]);
            all.insert(a[i]);
        }

        int n = all.size();

        //cout << n << endl;
        int s = 0, t = 0;
        int num = 0;
        while(1) {
            while(t < P && num < n) {               // exit this loop only if t=P(means end time) or num > n(means candidate is found )
                if(Count[a[t++]]++ == 0) { // Count[a[t]] == 0? t++, find a new point, Count[a[t]]++, doing statistics
                    num++;
                }
            }

            if(num < n) break; // if num < n, means we exit because t = P, end time 
            res = min(res, t - s); // update res
            if(--Count[a[s++]] == 0) { // 
                num--;
            }
        }


        printf(" case #%d: %d\n",testID, res);
    }

}

输入文件input.txt:

格式为:  the number of testCases

                  case1's total  pages

                  points covered from page 1 all the way to the last page

                  case2's total pages ...................................................................

                  ........................................................................................................  

<pre name="code" class="plain">14
6
1 1 1 1 1 1
5
1 8 8 8 1
6
1 2 3 4 5 6
5
1 2 3 4 4
8
1 2 3 3 3 4 5 6
10
1 2 2 2 4 2 5 2 2 2
18
1 2 3 3 3 3 2 1 1 1 1 2 2 2 2 2 3 3
5
1 2 2 3 1
5
1 2 2 2 1
5
1 2 2 2 2
6
1 2 3 2 3 1
9
1 2 3 4 5 4 3 2 1
10
1 2 7 9 10 7 10 3 2 1
11
1 1 2 6 8 5 6 6 6 6 6

 

运行结果output.txt:

 case #1: 1
 case #2: 2
 case #3: 6
 case #4: 4
 case #5: 8
 case #6: 7
 case #7: 3
 case #8: 3
 case #9: 2
 case #10: 2
 case #11: 3
 case #12: 5
 case #13: 7
 case #14: 5



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值