标签:
Description
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
Source
1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 #include <string.h> 5 using namespace std; 6 7 const int INF = 2147483647; 8 int a[100010]; 9 int main() 10 { 11 int S, t, n; 12 int T, mi; 13 int l, r, mid; 14 while(cin >> T) 15 { 16 while(T--) 17 { 18 cin >> n >> S; 19 for(int i = 1; i <= n; i++) 20 { 21 scanf("%d", &t); 22 if(i == 1) 23 a[i] = t; 24 else 25 a[i] = a[i-1] + t; 26 } 27 28 a[0] = 0; 29 mi = INF; 30 for(int i = 1; i <= n; i++) 31 { 32 l = i; 33 r = n; 34 while(l <= r) 35 { 36 mid = (l + r)/2; 37 if( a[mid] - a[i-1] >= S) 38 { 39 if( mi > mid-i+1 ) 40 mi = mid - i + 1; 41 r = mid - 1; 42 } 43 else 44 { 45 l = mid + 1; 46 } 47 } 48 } 49 if(mi == INF) 50 printf("0\n"); 51 else printf("%d\n", mi); 52 53 54 55 } 56 } 57 return 0; 58 }
1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 #include <string.h> 5 using namespace std; 6 7 int a[100010]; 8 const int INF = 2147483647; 9 int main() 10 { 11 int T, n, S, sum; 12 int l, r, mi; 13 14 while(cin >> T) 15 { 16 while(T--) 17 { 18 cin >> n >> S; 19 for(int i = 0; i < n; i++) 20 { 21 scanf("%d", a + i); 22 } 23 24 l = r = sum = 0; 25 mi = INF; 26 for(;;) 27 { 28 while(r < n && sum < S) 29 { 30 sum += a[r++]; 31 } 32 if( sum < S) 33 break; 34 else 35 { 36 mi = min(mi, r - l); 37 sum -= a[l++]; 38 } 39 } 40 41 42 if(mi == INF) 43 printf("0\n"); 44 else printf("%d\n", mi); 45 } 46 } 47 return 0; 48 }
标签:
原文地址:http://www.cnblogs.com/Yumesenya/p/5357482.html