【Codeforces 279B】books

原创 2018年04月16日 21:21:59

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.

Examples
input
Copy
4 5
3 1 2 1
output
Copy
3
input
Copy
3 3
2 2 3
output
Copy
1

经典的二分查找问题:

#include<bits/stdc++.h>
using namespace std; 
#define MAXN 100005
long long sum[MAXN];
int a[1000005];
int n,t;
bool check(int l)//判断函数 
{
	int x=1;
	int y=x+l-1;
	while(x<=n)
	{
		if(sum[y]-sum[x-1]>0&&sum[y]-sum[x-1]<=t)
		{
			return true;
		}
		if(sum[y]-sum[x-1]<0) return false;
		++x;
		y=l+x-1;
	}
	return false;
 } 
 int main()
 {
 	cin>>n>>t;
 	for(int i=1;i<=n;i++)
 	{
 		cin>>a[i];
	 }
	 for(int i=1;i<=n;i++)
	 {
	 	sum[i]=sum[i-1]+a[i];
	 }
	 int left=0,right=n;
	 int mid;
	 while(left<right)//二分 
	 {
	    mid=(left+right+1)/2;
	 	if(check(mid))
	 	{
	 		left=mid;
		}
		else
		{
			right=mid-1; 
		}
	 }
	 cout<<left<<endl;
	 return 0;
 }

[Codeforces] 279B - Books

统计加二分查找。。 #include #include #include #include #include #include #include #include #include ...
  • x_iaow_ei
  • x_iaow_ei
  • 2014-08-06 10:05:38
  • 842

Codeforces 279B Books

#include using namespace std; int a[100020]; int main() { int n,t; scanf("%d%d",&n,&t); ...
  • u010863471
  • u010863471
  • 2014-07-26 19:40:07
  • 580

codeforces 279-B. Books(尺取)

http://codeforces.com/problemset/problem/279/B简单尺取,当k>=0的时候更新ans值。#include #include using namespace ...
  • feizaoSYUACM
  • feizaoSYUACM
  • 2017-08-31 13:28:26
  • 177

codeforces 279B - Books

二分查找。 #include int n,t,a[100000+5]; bool judge(int num) { int sum=0; for(int j=1;j
  • Freenm
  • Freenm
  • 2017-04-07 23:18:38
  • 112

Books CodeForces - 279B

Books CodeForces - 279B When Valera has got some free time, he goes to the library to read some bo...
  • yong_ye_
  • yong_ye_
  • 2017-12-14 23:46:12
  • 41

Codeforces 279B books

题目链接 题意:求数组中某一区间数值和不大于t的区间最大长度。 思路1 模拟 起始先固定左端点,找右边端点,然后移动左端点 #include #include #include #in...
  • xingkongyidian
  • xingkongyidian
  • 2016-10-08 19:35:03
  • 144

CodeForces 279B

题解思路: 假设从i位置开始读可以读到j位置,所花分钟数为s( 代码:#include #include #include #include #include #incl...
  • a1214034447
  • a1214034447
  • 2017-10-16 22:19:37
  • 80

Codeforces 279B Books(dp大法好)

When Valera has got some free time, he goes to the library to read some books. Today he's got t free...
  • qq_37325947
  • qq_37325947
  • 2017-10-20 19:17:07
  • 74

CodeForces - 552B Vanya and Books (数学阶乘)水

CodeForces - 552B Vanya and Books Time Limit: 1000MS   Memory Limit: 262144KB   64...
  • yanghui07216
  • yanghui07216
  • 2016-04-21 20:24:36
  • 389

Codeforces Round 877 F - Ann and Books (莫队算法)

题意:详见原题? 思路:   显然我们是可以采用莫队来解决。用一个前缀和来维护一下的话,就转变成在区间[l,r]内,x [l,r+1],此时,a[r + 1]这个数出现的次数++,对答案的贡献度应该...
  • qq_29556211
  • qq_29556211
  • 2017-10-26 22:33:35
  • 301
收藏助手
不良信息举报
您举报文章:【Codeforces 279B】books
举报原因:
原因补充:

(最多只允许输入30个字)