Subsequence——POJ3061

题目:http://poj.org/problem?id=3061

尺取法解题

import java.util.Scanner;;

public class Main {

    public static void main(String[] args) {
        Scanner reader=new Scanner(System.in);
        int test=reader.nextInt();
        while(test>0){
            
            int N=reader.nextInt();
            int S=reader.nextInt();
            long array[]=new long[N];
            for(int i=0;i<N;i++){
                array[i]=reader.nextLong();
            }
            long sum=0;
            int start=0;
            int end=0;
            long step=0;
            long result=99999999999999L;
            
            while(true){
                while(end<N && sum<S){
                    sum=sum+array[end];
                    end++;
                }
                if(sum<S){
                    break;
                }
                step=end-start;
                result=Math.min(result, step);
                sum=sum-array[start];
                start++;
            }
            if(result==99999999999999L){
                System.out.println("0");
            }else{
                System.out.println(result);
            }
            test--;
        }
    }

}

20:19:15

2019-02-26

猜你喜欢

转载自www.cnblogs.com/chiweiming/p/10439836.html