【模拟】Books  CodeForces 279B

来源:互联网 发布:淘宝手机营销中心 编辑:程序博客网 时间:2024/05/16 11:53
Books
timelimit per test
2 seconds
memorylimit per test
256 megabytes
input
standard input
output
standard output

When Valera has got some free time, he goes to the library to readsome books. Today he's got t freeminutes to read. That's why Valeratook n booksin the library and for each book he estimated the time he is goingto need to read it. Let's number the books by integers from 1to n.Valera needs ai minutesto read the i-thbook.

Valera decided to choose an arbitrary book withnumber i andread 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 andso on. He continues the process until he either runs out of thefree time or finishes reading the n-thbook. Valera reads each book up to the end, that is, he doesn'tstart reading the book if he doesn't have enough free time tofinish reading it. 

Print the maximum number of books Valera can read.

Input

The first line contains two integers n and t (1 ≤ n ≤ 1051 ≤ t ≤ 109) —the number of books and the number of free minutes Valera's got.The second line contains a sequenceof n integers a1, a2, ..., an (1 ≤ ai ≤ 104),where number ai showsthe number of minutes that the boy needs to readthe i-thbook.

Output

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

Sample test(s)
input
4 5
3 1 2 1
output
3
input
3 3
2 2 3
output
1

用两个指针从后往前扫一遍就行了。


#include

#include

using namespace std;


long long  a[110000];


int main()

{

int n;

long long t;

scanf("%d %I64d", &n, &t);

for (int i = 1; i <= n; i++)

scanf("%I64d", &a[i]);


long long now = 0;

int nown = 0;

int max = 0;

int r = n;

for (int l = n; l > 0; l--)

{

now += a[l];

nown++;

if (now <= t)

{

if (nown > max) max = nown;

}

else 

while (now > t &&r>0)//注意while语句都要判断边界,尽管自己觉得不会有问题,但是什么奇葩的数据都有。。。

{

now -= a[r];

nown--;

r--;

}

}

printf("%d\n", max);

return 0;

}


0 0
原创粉丝点击