简单模拟

B. Books
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

Examples

input

4 5
3 1 2 1

output

3

input

3 3
2 2 3

output

1

题意:有n本书,编号1-n;随机选取一本开始读,然后依次往读,直到所剩时间不足读下一本书或者读完所选书之后的所有书。
求最多能读几本书。

思路:做法类似最大连续子列和,从零开始搜索,只要时间没有超过t即继续向后,纪录最后下标pos;然后起点后调,在原来所求得得sum基础上减去前一个起始结点,因为要书尽量的多,sum是可以直接利用的,因为这pos-1本书肯定是可以读完的,所以可以省去算这部分sum的时间(不这样做会超时)。最后取最大的j-i即可;
代码如下:

#include<bits/stdc++.h>
typedef long long ll;

ll a[100010];
int main(){
    memset(a,0,sizeof(a));
    ll i,j,n,t,ans=0,tmp=-1,tsum=0;
    cin>>n>>t;
    for(i=0;i<n;i++)
        scanf("%lld",&a[i]);
    for(i=0;i<n;i++){
        if(i!=0){
            tsum-=a[i-1];
        }
        if(tmp<i-1){
            tmp=i-1;
            tsum=0;
        }
        for(j=tmp+1;j<n;j++){
            if(tsum+a[j]<=t){
                tsum+=a[j];
                tmp=j;
            }
            else break;
        }
        ans=max(j-i,ans);
        if(n-1-i<=ans){
            break;
        }
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值