LA 2678 Subsequence

利用单调性求最短连续子序列的和大于等于s;

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define maxn 100009
 5 using namespace std;
 6 
 7 int sum[maxn];
 8 int main()
 9 {
10     int n,s,x;
11     while(scanf("%d%d",&n,&s)!=EOF)
12     {
13         memset(sum,0,sizeof sum);
14         int ans=n+1;
15         for(int i=1;i<=n;i++)
16         {
17             scanf("%d",&x);
18             sum[i]=sum[i-1]+x;
19         }
20         int j=1;
21         for(int i=1;i<=n;i++)
22         {
23             if(sum[j-1]>sum[i]-s)continue;
24             while(sum[j]<=sum[i]-s)j++;
25             ans=min(ans,i-j+1);
26         }
27         printf("%d\n",ans==n+1?0:ans);
28     }
29     return 0;
30 }
View Code

 

posted @ 2013-11-14 17:28  Yours1103  阅读(158)  评论(0编辑  收藏  举报