poj 3061 Subsequence(尺取法)

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 #define INF 0xffffff
 7 const int maxn = 100000 + 5;
 8 typedef long long LL;
 9 int a[maxn];
10 LL sum[maxn];
11 
12 int main(){
13     ios::sync_with_stdio(false);
14     int t;
15     cin >> t;
16     while (t--){
17         memset(a, 0, sizeof(a));
18         memset(sum, 0, sizeof(sum));
19         int n, s;
20         cin >> n >> s;
21         for (int i = 1; i <= n; i++){
22             cin >> a[i];
23             sum[i] = sum[i - 1] + a[i];
24         }
25         int ans = INF;
26         LL cnt = a[1];
27         int l = 1;
28         for (int i = 2; i <= n; i++){
29             cnt += a[i];
30             while(cnt >= s){
31                 ans = min(ans, i - l + 1);
32                 cnt -= a[l++];
33             }
34         }
35         if (ans == INF) ans = 0;
36         cout << ans << endl;
37     }
38     //system("pause");
39     return 0;
40 }

 

posted @ 2018-04-05 15:40  ouyang_wsgwz  阅读(124)  评论(0编辑  收藏  举报