码迷,mamicode.com
首页 > 其他好文 > 详细

Jessica's reading problem

时间:2015-03-07 11:41:14      阅读:170      评论:0      收藏: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

技术分享


Jessica's reading problem

标签:

原文地址:http://blog.csdn.net/a130737/article/details/44114205

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!