B. Books

题目链接: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 }

 

转载于:https://www.cnblogs.com/-Ackerman/p/11165666.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值