题意:
在一个长度为n的序列中,找到最短的长度序列,使其和大于等于s;
思路:
two pointer ,水题;
Ac代码:
#include <bits/stdc++.h> /* #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> */ using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar(' '); } const LL mod=1e9+7; const double PI=acos(-1.0); const LL inf=1e18; const int N=2e5+10; const int maxn=1005; const double eps=1e-10; int a[N],sum[N]; int main() { int n,s; while(cin>>n>>s) { sum[0]=0; For(i,1,n)read(a[i]),sum[i]=sum[i-1]+a[i]; int l=1,r=1; int ans=2*n; For(i,1,n) { l=i-1; while(sum[r]-sum[l]<s&&r<=n)r++; if(r<=n)ans=min(ans,r-l); } if(ans<=n)cout<<ans<<" "; else cout<<"0"<<" "; } return 0; }