尺取法很好用啊,附上AC代码。
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
in.nextToken();
int t = (int)in.nval;
for(int i = 0; i<t; i++){
in.nextToken();
int n = (int)in.nval;
in.nextToken();
int s = (int)in.nval;
int[] dp = new int[n];
for(int j = 0; j<n; j++){
in.nextToken();
dp[j] = (int)in.nval;
if(j == n-1) continue;
}
int res = n+1;
int s1 = 0;
int s2 = 0;
int sum = 0;
for(;;){
while(s1<n && sum<s)
sum += dp[s1++];
if(sum < s) break;
res = Math.min(s1-s2, res);
sum -= dp[s2++];
}
if(res > n)
res = 0;
out.println(res);
out.flush();
}
}
}