POJ 3061 subsequence (ruler)

Source: Internet
Author: User

Portal



subsequence
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 11284
Accepted: 4694

Description

a sequence of n positive integers (< n <), each of the them less than or equal 10000, and A positive inte Ger S (S <) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is Greater than or equal to S.

Input

The first line is the number of test cases. The program have to read the numbers N and S, separated by a interval, from the first line. The numbers of the sequence is given in the second line of the "test case", separated by intervals. The input would finish with the end of file.

Output

The the program have to print the result on separate line of the output File.if no answer, print 0.

Sample Input

210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5

Sample Output

23

Source

Southeastern Europe 2006

Main topic:

Given the T-group data, each set of data has a number n, which indicates that there is n number, and then given a number of s, so that you can find the sum of the numbers of >=s the minimum value of the length of the series. (Data range N<1e5,s<1e8,a[i]<=1e4)


Problem Solving Ideas:

(1) let's start with an algorithm that has an O (N*log (n)) complexity.

Because all the elements are >0, we can think of the assumption that the sequence AI ai+1 ai+2 ... at-1 and >=s, then we can ask for the first I of these series and, now define Sum[i] for A0 A1 A2 ... ai-1, then AI ai+1 Ai+2 at-1 can be written sum[t]-sum[i], because Sum[t]-sum[i] >= T, then we ask for the minimum value of these lengths, so that is the minimum value of t-i, then we can first preprocess the value of Sum[i], and then can be done in two To find the minimum value of t-i by the algorithm


My Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 100000+5;int a[maxn],sum[maxn];int n;void get_sum () {    sum[0] = 0;    for (int i=0; i<n; i++)        sum[i+1] = Sum[i] + a[i];} int main () {    int t,s;    scanf ("%d", &t);    while (t--)    {        scanf ("%d%d", &n,&s);        for (int i=0; i<n; i++)            scanf ("%d", &a[i]);        Get_sum ();        if (Sum[n] < S)            puts ("0");        else        {            int ans = n + +;            for (int i=0; sum[i]+s<=sum[n]; i++)            {                int tmp = Lower_bound (sum+i,sum+n,s+sum[i])-sum;                ans = min (ans,tmp-i);            }            cout<<ans<<endl;        }    }    return 0;}

(2)an O (n) algorithm for complex degrees

Now you have to say the ruler, in fact, the key to the ruler is two pointers are from the beginning, two pointers S, t,if (sum<s) so sum+=a[t],t++; otherwise, sum = a[s],s++; seize the key. Do not forget to update Ans,ans is always the minimum value of s-t when you do not meet the criteria ...

My Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int MAXN = 100000+5;int A[maxn];int main () {    int t,s,n;    scanf ("%d", &t);    while (t--)    {        scanf ("%d%d", &n,&s);        for (int i=0; i<n; i++)            scanf ("%d", &a[i]);        int t=0, s=0, sum=0;        int ans = n + +;        while (1)        {            while (t<n && sum<s)            {                sum + = a[t];                t++;            }            if (sum < S) break                ;            ans = min (ans, t-s);            Sum-= A[s];            s++;        }        if (ans = = n+100)            puts ("0");        else        {            cout<<ans<<endl;        }    }    return 0;}


POJ 3061 subsequence (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.