[ACM] poj 3061 subsequence (ruler acquisition method)

Source: Internet
Author: User

Subsequence
Time limit:1000 ms   Memory limit:65536 K
Total submissions:8403   Accepted:3264

Description

A sequence of n positive integers (10 <n <100 000), each of them less than or equal 10000, and a positive integer S (S <100 000 000) 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. for each test case the program has to read the numbers N and S, separated by an interval, from the first line. the numbers of the sequence are given in the second line of the test case, separated by intervals. the input will finish with the end of file.

Output

For each the case the program has 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


Given an integer series with a length of N and an integer S, obtain the minimum value of the length of the continuous subsequence whose sum is not less than S. If the solution does not exist, 0 is output.

Method 1:

First, obtain the sum [I], range from 1st to the number I, and fix the start point sum [I] for each query. then, the sum [I] + s position is found by using the binary search. The Interval Length is (the last position-(the start position-1). The radians are used to save the minimum value of the interval during the process. Time complexity 0 (nlogn)

Code:

# Include <iostream> # include <stdio. h> # include <algorithm> # include <string. h> using namespace STD; const int maxn = 100010; int num [maxn]; int sum [maxn]; int N, S; int main () {int T; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & S); sum [0] = 0; for (INT I = 1; I <= N; I ++) {scanf ("% d", & num [I]); sum [I] = sum [I-1] + num [I];} If (sum [N] <s) {cout <0 <Endl; continue ;} int ans = maxn; For (int s = 0; sum [s] + S <= sum [N]; s ++) // start from sum [S + 1]. S is the first position of the number to start searching. {int T = lower_bound (sum + S + 1, sum + n + 1, sum [s] + S)-(sum + S); // sum + S is the previous address from the sum + S + 1 address, therefore, the obtained address minus this address is the range length ans = min (ANS, T);} printf ("% d \ n", ANS);} return 0 ;}

Method 2: ruler acquisition

The method that repeatedly advances the beginning and end of a range to find the minimum interval that meets the conditions is called the ruler acquisition method.

The main idea is: When A1, A2, A3 meet and> = s, get an interval length of 3, then remove the starting A1, then A2, A3, and determine whether it is satisfied> = s, if yes, the interval length is updated. If not, the tail is extended to determine whether A2, A3, and A4 meet the conditions. Repeat this operation.

My understanding of the ruler acquisition method:

When an interval meets the conditions, remove the first number at the beginning of the interval to obtain the inter-zone, and determine whether the inter-zone meets the conditions. If no


If the conditions are met, the end of the interval is extended until the conditions are met. In this way, many intervals that meet the conditions are obtained.


You can select among these intervals, for example, the longest interval and the shortest interval. Run it again, time


The complexity is O (n ).

Code:

#include <iostream>#include <algorithm>#include <stdio.h>using namespace std;const int maxn=100010;int num[maxn];int n,S;int main(){    int t;scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&S);        for(int i=1;i<=n;i++)            scanf("%d",&num[i]);        int sum=0,s=1,e=1;        int ans=n+1;        for(;;)        {            while(e<=n&&sum<S)                sum+=num[e++];            if(sum<S)                break;            ans=min(ans,e-s);            sum-=num[s++];        }        if(ans==n+1)            cout<<0<<endl;        else            cout<<ans<<endl;    }    return 0;}

The second method is to calculate the Interval Length (last position + 1-start position)




[ACM] poj 3061 subsequence (ruler acquisition method)

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.