poj 3320 Jessica's Reading Problem

Jessica面临期末考试,需掌握一本含有重复知识点的厚书。通过快速排序和哈希等技术手段,寻找包含全部知识点的最短连续页数。
Jessica's Reading Problem
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6463 Accepted: 1985

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 author of that text book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.

Jessica是个非常可爱的女孩子被很多男孩子追。最近她遇到了一个麻烦,期末考试又要来了啦,不过一点都没学咋整啊。。如果她不想挂树上,那么久不得不学会那一厚练习册里面的所有知识点。这本练习册的作者跟其他作者一样蛋疼,里面经常有很多重复知识点。Jessica想如果她可以把所有的知识点看那么一遍,这次就应该能侥幸过关。他决定阅读连续的几页,然后里面能够包含所有的知识点。当然,这个连续的几页越薄越好。

A very hard-working boy had manually indexed for her each page of Jessica's text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.

一个非常勤劳的男孩已经帮Jessica标记好了每一页对应的知识点,然后你需要做的事情就是,给你了这个男孩做好的索引,之后你去找到最少的连续的几页中能够包含所有知识点。每个知识点都有一个ID标记,是一个正整数。

Input

The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica's text-book. The second line contains P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit integer type.

第一行是有多少页P (1 ≤ P ≤ 1000000),然后第二行是每一页对应的知识点的ID,你可以认为知识点的ID不超过Int

Output

Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.

输出就一行,能够包含所有知识点的最少的连续页数

Sample Input

5
1 8 8 8 1

Sample Output

2

Source


做题甚少,这种题第一次做。。。根据学习,一般思路是快排加哈希,然后用尺取法,尺取法就是创造一个区间,左边一个指针,右边一个指针,如果右边指针指向的内容第一次出现,则右移,如果是第二次出现,并且内容与左边的指针相同,则左边的指针右移,不然就说明从左到右的区间内有重负内容,则取此长度与当前最小长度比较取最小。
然后还有各种deque的方法,不太懂,还有用堆的,那方面没研究。。。
最后采用的是二分加快排,因为方法比较意外。。。主要是尺取法本身不同。。。首先对于快排后将数据整理成每个idea只出现一次的升序序列,然后在这个序列里面二分查找,后记录的是每一次idea出现所处在的最右面的位置,这究竟是怎么想的。。。。然后判断最右面的指针和左边的指针内容如果不同,则继续记录,如果相同,则从左边扫描到右边,然后将左边的指针移动到,标记的最右面位置最小的那一个,这样就保证了不会丢失idea。。。。。。
还是看程序吧。。。基本参照那个人的方法写的。。。所以这次比较水。。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#define LEN 1000001

void q_sort(int p, int r);
int partition_q(int p, int r);
int findnext(int i, int left, int num);
int Binsearch(int x, int l, int r);

int page[LEN], s[LEN], index[LEN];

int main()
{
    int p, i, j;
    int num;
    int left;
    int minlen;
    int sum;

    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    while(scanf("%d", &p) != EOF){
        for (i = 0;i < p;++ i){
            scanf("%d", &page[i]);
            s[i] = page[i];
        }
        q_sort(0, p - 1);

        num = 1;
        j = 1;

        for (i = 1;i < p;++ i){
            if (s[i] != s[i-1]){
                num ++;
                s[j] = s[i];
                j++;
            }
        }
//        for (i = 0;i < p;++ i){
//            printf("%d ", s[i]);
//        }
//        printf(" num = %d", num);

        for (i = 0;i < LEN;++ i){
            index[i] = -1;
        }

        minlen = LEN;
        left = 0;
        sum = 0;
        for (i = 0;i < p;++ i){
            j = Binsearch(page[i], 0, num - 1);
            if (index[j] == -1){
                sum ++;
            }else if(page[i] == page[left]){
                left = findnext(i, left, num);
            }
            index[j] = i;
            if (sum == num){
                    if ((i - left + 1) < minlen)
                        minlen = i - left + 1;
            }
        }

        printf("%d\n", minlen);
    }

    return 0;
}

void q_sort(int p, int r){
    int q;
    if (p < r){
        q = partition_q(p, r);
        q_sort(p, q-1);
        q_sort(q+1, r);
    }
}

int partition_q(int p, int r){
    int i = p - 1, j;
    int temp;

    for (j = p;j < r;++ j){
        if (s[j] <= s[r]){
            ++i;
            temp = s[i]; s[i] = s[j]; s[j] = temp;
        }
    }
    temp = s[i + 1]; s[i + 1] = s[r]; s[r] = temp;

    return i + 1;
}

int Binsearch(int x, int l, int r){
    int mid;
    if (s[l] == x)
        return l;
    if (s[r] == x)
        return r;
    while(l <= r){
        mid = l + ((r - l) >> 1);
        if (s[mid] == x){
            return mid;
        }else if (s[mid] > x){
            r = mid - 1;
        }else{
            l = mid + 1;
        }
    }
    return -1;
}

int findnext(int i, int left, int num){
    int j, k;
    int rs = i;
    for (j = left + 1;j < i;++ j){
        k = Binsearch(page[j], 0, num-1);
        if (index[k] < rs){
            rs = index[k];
        }
    }
    return rs;
}


内容概要:本文围绕六自由度机械臂的人工神经网络(ANN)设计展开,重点研究了正向与逆向运动学求解、正向动力学控制以及基于拉格朗日-欧拉法推导逆向动力学方程,并通过Matlab代码实现相关算法。文章结合理论推导与仿真实践,利用人工神经网络对复杂的非线性关系进行建模与逼近,提升机械臂运动控制的精度与效率。同时涵盖了路径规划中的RRT算法与B样条优化方法,形成从运动学到动力学再到轨迹优化的完整技术链条。; 适合人群:具备一定机器人学、自动控制理论基础,熟悉Matlab编程,从事智能控制、机器人控制、运动学六自由度机械臂ANN人工神经网络设计:正向逆向运动学求解、正向动力学控制、拉格朗日-欧拉法推导逆向动力学方程(Matlab代码实现)建模等相关方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握机械臂正/逆运动学的数学建模与ANN求解方法;②理解拉格朗日-欧拉法在动力学建模中的应用;③实现基于神经网络的动力学补偿与高精度轨迹跟踪控制;④结合RRT与B样条完成平滑路径规划与优化。; 阅读建议:建议读者结合Matlab代码动手实践,先从运动学建模入手,逐步深入动力学分析与神经网络训练,注重理论推导与仿真实验的结合,以充分理解机械臂控制系统的设计流程与优化策略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值