Codeforces Round #171 (Div. 2) B. Books(DP)

B. Books
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the i-th book.

Valera decided to choose an arbitrary book with number i and read the books one by one, starting from this book. In other words, he will first read book number i, then book number i + 1, then book number i + 2 and so on. He continues the process until he either runs out of the free time or finishes reading the n-th book. Valera reads each book up to the end, that is, he doesn't start reading the book if he doesn't have enough free time to finish reading it.

Print the maximum number of books Valera can read.

Input

The first line contains two integers n and t (1 ≤ n ≤ 105; 1 ≤ t ≤ 109) — the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 104), where number ai shows the number of minutes that the boy needs to read the i-th book.

Output

Print a single integer — the maximum number of books Valera can read.

Sample test(s)
Input
4 5
3 1 2 1
Output
3
Input
3 3
2 2 3
Output
1
最直观的思路是取a[i],由a[i]开始求和直到sum>t,然后再从a[i+1]开始求和直到sum>t,一直进行到a[n-1].
这样效率很低,可以采取两步优化:
(1)求a[i]+a[i+1]+a[i+2]……和求a[i+1]+a[i+2]+……是有重叠部分的,因此在由a[i]开始累加时,假设累加到a[j],记录j和sum,在由a[i+1]
开始累加时,执行sum-=a[i],那么sum = a[i+1]+……+a[j]就已经求得。
(2)记录已经算得的可读书的最大本数b,如果a[0……(n-1)]中余下的未检测的数的个数小于等于b,可提前结束循环。
下面AC Code只进行了第一步优化。
 1 #include <iostream>
 2 #include <string>
 3 #include <set>
 4 #include <map>
 5 #include <vector>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <cstdio>
10 #include <cstring>
11 #include <algorithm>
12 #include <utility>
13 using namespace std;
14 #define ll long long
15 #define cti const int
16 #define ctll const long long
17 #define dg(i) cout << '*' << i << endl;
18 
19 typedef pair<int, int> p;
20 cti N = 100001;
21 int n, t;
22 int a[N];
23 vector<p> v;
24 
25 bool cmp(const p& a, const p& b){return a.second < b.second;}
26 
27 int main()
28 {
29     int i, j;
30     while(scanf("%d %d", &n, &t) != EOF)
31     {
32         v.clear();
33         for(i = 0; i < n; i++) scanf("%d", a + i);
34         if(n == 1)
35         {
36             if(t >= a[0]) puts("1");
37             else puts("0");
38             continue;
39         }
40         int k;
41         int sum;
42         for(i = 0; i < n; i++)
43         {
44             if(!i)
45             {
46                 k = 0;
47                 sum = 0;
48             }
49             else
50             {
51                 if(sum) sum -= a[i-1];
52             }
53             for(j = k; j < n; j++)
54             {
55                 if(t >= sum + a[j])
56                 {
57                     sum += a[j];
58                 }
59                 else
60                 {
61                     k = j == i ? k + 1 : j;  //记录下次累加开始的位置
62                     break;
63                 }
64             }
65             if(j == n)
66             {
67                 v.push_back(make_pair(sum, n - i));
68                 break;
69             }
70             if(j - i) v.push_back(make_pair(sum, j - i));
71         }
72         if(v.empty()) puts("0");
73         else
74         {
75             sort(v.begin(), v.end(), cmp);
76             printf("%d\n", (v.end()-1)->second);
77         }
78     }
79     return 0;
80 }

 

 
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值