POJ 3320 Jessica ' s Reading problem (ruler)

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=3320

Test instructions

A book has P page, each page has a knowledge point AI, there are different two pages on the same knowledge points,

Ask for a minimum number of consecutive pages of the book, can be all the knowledge points covered.


Ruler extraction:

The method of finding the minimum interval for satisfying a condition is called a ruler by repeatedly pushing the beginning and ending of the interval.

The degree of complexity of the ruler is O (n).


Initialize, start, end, sum = 0;

Then push end until the condition exits, if the interval has reached the end and is still not satisfied, exit

Then update the answer ans = min (ans,end-start);

Then start advances one, to deal with the effect of this time on sum.


The code is as follows:

#include <iostream> #include <cstring> #include <cstdio> #include <map>using namespace std; const int MAXN = 1e6+10;int A[maxn];int main () {    int n;    while (~SCANF ("%d", &n)) {        map<int, int >mp;        int num = 0;        for (int i=0;i<n;i++) {            scanf ("%d", a+i);            if (!mp[a[i]]) num++;            mp[a[i]]++;        }        int st=0,t=0,sum=0,ans = n;        Mp.clear ();        while (1) {            while (t<n&&sum<num) {                if (!mp[a[t++]]++)                    sum++;            }            if (sum<num) break;            ans = min (ans,t-st);            if (--mp[a[st++]]==0)                sum--;        }        printf ("%d\n", ans);    }    return 0;}

and a similar subject, POJ 3061.

Subsequence

Test instructions: Given a sequence, the minimum value of the length of successive sub-sequences with a sum not less than S is calculated

There are two ways to do this problem.

Method One:

Pretreatment, sum[i]=a[0]+....+a[i-1];

Then, in the interval [i,n] dichotomy, look for the subscript that is greater than or equal to the minimum value of Sum[i]+s,

Ans=min (ans,pos-i);

The code is as follows:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int MAXN = 1e5+10;int A[maxn],sum[maxn];int main () {    int t,s,n;    scanf ("%d", &t);    while (t--) {        scanf ("%d%d", &n,&s);        sum[0]=0;        for (int i=0;i<n;i++) {            scanf ("%d", a+i);            Sum[i+1]=sum[i]+a[i];        }        if (sum[n]<s) {            printf ("0\n");            Continue;        }        int ans = n;        for (int i=0;sum[i]+s<=sum[n];i++) {            int pos=lower_bound (sum+i,sum+n,sum[i]+s)-sum;            ans = min (ans,pos-i);        }        printf ("%d\n", ans);    }    return 0;}

Method Two,

Ruler extraction

The code is as follows:

#include <iostream> #include <cstring> #include <cstdio>using namespace std;const int maxn = 1e5+10;int A[maxn];int Main () {    int t,n,s;    scanf ("%d", &t);    while (t--) {        scanf ("%d%d", &n,&s);        for (int i=0;i<n;i++)            scanf ("%d", &a[i]);        int st=0,t=0,sum=0,ans= n+1;        while (1) {            while (t<n&&sum<s)                sum+=a[t++];            if (sum<s) break;            Ans=min (ans,t-st);            sum-=a[st++];        }        if (ans>n) {            printf ("0\n");            Continue;        }        else            printf ("%d\n", ans);    }    return 0;}



POJ 3320 Jessica ' s Reading problem (ruler)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.