题目描述
给定一个长度为 nn 的序列 a1,a2,⋯ ,an和一个常数 S。
对于一个连续区间如果它的区间和大于或等于S,则称它为美丽的区间。
对于一个美丽的区间,如果其区间长度越短,它就越美丽。
请你从序列中找出最美丽的区间。
输入描述
第一行包含两个整数 n,S,其含义如题所述。
接下来一行包含 nn 个整数,分别表示 a1,a2,⋯ ,an。
10≤N≤10^5,1×ai≤10 ^4,1≤S≤10 ^8。
输出描述
输出共一行,包含一个整数,表示最美丽的区间的长度。
若不存在任何美丽的区间,则输出 0。
输入输出样例
输入
5 6
1 2 3 4 5
输出
2
import java.io.*;
public class Main {
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static int n,t;
static int N=100010;
static int[] a=new int[N];
static int[] g=new int[N];
public static void main(String[] args) throws IOException {
String[] s=br.readLine().split(" ");
n=Integer.parseInt(s[0]);
t=Integer.parseInt(s[1]);
s=br.readLine().split(" ");
for (int i=1;i<=n;i++){
a[i]=Integer.parseInt(s[i-1]);
g[i]=g[i-1]+a[i];
}
int ans=N;
for (int i=1;i<=n;i++){
int l=i,r=n;
while (l<r){
int mid=(l+r)>>1;
if (g[mid]-g[i-1]>=t){
r=mid;
}else {
l=mid+1;
}
}
if (g[r]-g[i-1]>=t){
ans=Math.min(ans,r-i+1);
}
}
out.println(ans==N?0:ans);
out.flush();
}
}