Codeforces 279B Books

来源:互联网 发布:查看linux系统内存大小 编辑:程序博客网 时间:2024/04/30 01:19
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 gott free minutes to read. That's why Valera tookn 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 ton. Valera needs ai minutes to read thei-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 numberi, 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 then-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 andt (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 numberai shows the number of minutes that the boy needs to read thei-th book.

Output

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

Sample test(s)
Input
4 53 1 2 1
Output
3
Input
3 32 2 3
Output
1


  这是昨天晚上比赛的一个题目,要说难吧,真是不难,但是由于做完没有处理好bug,到最后也没检查出来。

  

#include <stdio.h>#include <string.h>#include <math.h>int a[100010];int main(){    int i,j,n,m,s,t,k;    int pos,sum,max;    while(scanf("%d %d",&n,&m)!=EOF)    {        for(i=0;i<=n-1;i++)        {            scanf("%d",&a[i]);        }        max=0; k=0;        for(i=0,pos=-1,sum=0;i<=n-1;i++)        {            if(i!=0)            {                sum-=a[i-1];            }            k=0;            if(pos+1<i)            {                pos=i-1;                sum=0;            }            for(j=pos+1;j<=n-1;j++)            {                if(sum+a[j]<=m)                {                    k=1;                    sum+=a[j];                    pos=j;                }else                {                    break;                }            }            if((j-i)>max)            {                max=j-i;            }            if(n-i-1<=max)            {                break;            }        }        printf("%d\n",max);    }    return 0;}


 

原创粉丝点击