标签:ide log return output else less gif map write
Input
Output
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<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pi acos(-1) #define ll long long #define mod 1000000007 using namespace std; const int N=100000+5,maxn=100+5,inf=0x3f3f3f3f; int main() { ios::sync_with_stdio(false); cin.tie(0); int t,n,s,a[N],sum[N]; cin>>t; while(t--){ cin>>n>>s; for(int i=0;i<n;i++)cin>>a[i]; int sum=0,st=0,en=0,ans=n+2; while(en<=n){ while(sum<s&&en<=n){ sum+=a[en++]; } // cout<<sum<<" "<<st<<" "<<en<<endl; if(sum>=s)ans=min(ans,en-st); sum-=a[st++]; } if(ans==n+2)cout<<0<<endl; else cout<<ans<<endl; } return 0;
非尺取:
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pi acos(-1) #define ll long long #define mod 1000000007 using namespace std; const int N=100000+5,maxn=100+5,inf=0x3f3f3f3f; int main() { ios::sync_with_stdio(false); cin.tie(0); int t,n,s,a[N],sum[N]; cin>>t; while(t--){ cin>>n>>s; for(int i=0;i<n;i++) { cin>>a[i]; if(i==0)sum[i]=a[i]; else sum[i]=sum[i-1]+a[i]; } int res=n+2; for(int i=0;sum[i]+s<=sum[n-1];i++) { int j=lower_bound(sum+i,sum+n,sum[i]+s)-sum; // cout<<j<<endl; res=min(res,j-i); } if(sum[n-1]>=s)cout<<res<<endl; else cout<<0<<endl; } return 0; }
标签:ide log return output else less gif map write
原文地址:http://www.cnblogs.com/acjiumeng/p/6752452.html