Codeforces 279 B Books

题意:给出n本书,总的时间t,每本书的阅读时间a[i],必须按照顺序来阅读,问最多能够阅读多少本书

有点像紫书的第七章讲的那个滑动区间貌似 维护一个区间的消耗的时间小于等于t,然后维护一个区间的最大值

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath> 
 5 #include<stack>
 6 #include<vector>
 7 #include<map> 
 8 #include<set>
 9 #include<queue> 
10 #include<algorithm>  
11 using namespace std;
12 
13 typedef long long LL;
14 const int INF = (1<<30)-1;
15 const int mod=1000000007;
16 const int maxn=100005;
17 int a[maxn],pre[maxn];
18 
19 int main(){
20     int n,t;
21     scanf("%d %d",&n,&t);
22     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
23     
24     pre[0]=0;
25     for(int i=1;i<=n;i++) pre[i]=pre[i-1]+a[i];
26     
27     
28     int l=0;
29     int r=1;
30     int sum=0,ans=-1;
31     while(r<=n){
32         if((pre[r]-pre[l])<=t){
33             ans=max(ans,r-l);
34             r++;
35         }
36         else l++;        
37     }
38     printf("%d
",ans);
39     return 0;
40 }
View Code
原文地址:https://www.cnblogs.com/wuyuewoniu/p/4433678.html