标签:closed iostream ble 技术 isp eof blank max efi
题意:
给出一个序列,要求找出一个长度最短的连续子区间,满足区间上所有数之和大于等于S,输出这个最短长度。
分析:
枚举每个点为左端点,用尺取法找到其右端点,取n次结果中的最小值就好了。
代码:
#include <stack> #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; #define ll long long #define ull unsigned long long #define cls(x) memset(x,0,sizeof(x)) #define clslow(x) memset(x,-1,sizeof(x)) const int maxn=1e5+100; int n,T,S; int a[maxn]; int main() { // freopen("in.txt","r",stdin); scanf("%d",&T); while(T--) { scanf("%d%d",&n,&S); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } int s=1,e=1; int ans=n+1,sum=0; while(true) { while(e<=n&&sum<S) sum+=a[e++]; if(sum<S) break; ans=min(ans,e-s); sum-=a[s++]; } if(ans==n+1) ans=0; printf("%d\n",ans); } return 0; }
标签:closed iostream ble 技术 isp eof blank max efi
原文地址:https://www.cnblogs.com/shutdown113/p/9384612.html