【尺取法】poj 3061 Subsequence

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

2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5

Sample Output

2
3

题意:给定长度为n的数列(都为正整数) , 求出总和不超过S的连续子序列的长度的最小值,如果不存在则输出0 ; 

写法:先递推出所有项的和, 确定出子序列的起点s,就可以二分查找到末端;

PS:这个题输入量巨大,就算用二分也会导致擦边而过,跑了860MS,当然还可以优化的,但是用了输入挂,跑了16MS。对于巨量的数据这个还是很好用 的。

AC代码:

#include 
#include 
#include 
#include 
#include 
using namespace std ;
int a[100000000+5] , sum[100000000+5];
inline bool scan_d(int &num)
{
        char in;bool IsN=false;
        in=getchar();
        if(in==EOF) return false;
        while(in!='-'&&(in<'0'||in>'9')) in=getchar();
        if(in=='-'){ IsN=true;num=0;}
        else num=in-'0';
        while(in=getchar(),in>='0'&&in<='9'){
                num*=10,num+=in-'0';
        }
        if(IsN) num=-num;
        return true;
}
int main()
{
    int t ;
    cin>>t;
    int n , S ;
    while(t--)
    {
        scan_d(n);
         scan_d(S);
        for(int i = 0 ; i

尺取法:

#include 
#include 
#include 
#include 
#include 
using namespace std;
int a[100000000] , S,  n ;
int main()
{
    int m ;
    cin>>m;
    while(m--)
    {
        cin>>n>>S;
        for(int i = 0 ; i  < n ; i++)
        {
            cin>>a[i];
        }
        int res = n+1 ;
        int s , ans , t ;
         s = ans = t = 0 ;   //*ans代表和, s代表前节点,t代表后节点,res=t-s就是区间长度;
        for(;;)
        {
            while(t n) printf("0\n");
        else printf("%d\n",res);
    }
    return 0 ;
}


你可能感兴趣的:(技巧类,POJ)