题目链接:http://codeforces.com/problemset/problem/279/B
题目大意:有n本书,阅读第i本书所需要的时间是a[i] 现在你有 t 时间 ,你最多能阅读几本书 (注意读书必须连续读)
连续区间的问题我感觉基本上尺取法都可以解决 ,这里用的也就是尺取法
AC代码:
1 #include <cstdio> 2 #include <string> 3 #include <iostream> 4 #include <algorithm> 5 #include <cstdbool> 6 #include <string.h> 7 #include <math.h> 8 9 using namespace std; 10 11 int main() 12 { 13 int n,t; 14 cin >> n >> t; 15 int a[n]; 16 for (int i=0;i<n;i++) 17 { 18 cin >> a[i]; 19 } 20 int i = 0,j = 0; 21 int sum = 0,cnt = 0; 22 while (j < n) 23 { 24 sum += a[j]; 25 j++; 26 while (sum > t) 27 { 28 sum -= a[i]; 29 i++; 30 } 31 cnt = max(cnt,j-i); 32 } 33 printf("%d\n",cnt); 34 return 0; 35 }