求较小区间差并且区间和大于某个值的问题
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
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; int sum[100005]; int main() { int n,N,S; cin>>n; while(n--) { cin>>N>>S; for(int t=0;t<N;t++) { int k; scanf("%d",&k); sum[t]=sum[t-1]+k; } if(sum[N-1]<S) { printf("0\n"); } else { int flag; int a=N-1; for(int t=0;sum[t-1]+S<=sum[N-1];t++) { int index=lower_bound(sum+t,sum+N-1,sum[t-1]+S)-sum; a=min(a,index-t+1); } printf("%d\n",a); } } return 0; }
作者:李斌
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!

浙公网安备 33010602011771号