ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CodeForces 279B - Books

2021-05-28 13:05:01  阅读:1236  来源: 互联网

标签:Valera 279B CodeForces number int Books read book he


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 \(a_i\) 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 \(a_1, a_2, ..., a_n (1 ≤ a_i ≤ 104)\) , where number \(a_i\) 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

General Approach

Prefix-sum + two pointers

But we are not using simple two-pointers like this:
Normal Two-pointers

for (int i = 0; i < n; ++i) {
	int j = i;
	while(check(i, j)) j++;	// implementation not shown for brevity
	/*...*/
}

This is not efficient enough for our question and will cause a TLE. Instead, we are using Sliding Window Algorithm

Sliding Window Algorithm Implementation

int ans = 0;

int j = 0;
for (int i = 0; i < n; ++i) {
	while(query(i, j) <= t) j++;
	ans = max(ans, j - i);
}

So, we have a left pointer i pointing to the starting book, and we have a right pointer. We start from \(i=0,j=0\), keep increasing j until the sum of \(a_i\) to \(a_j\) exceeds \(t\) . In the second round, we add \(i\) by 1, \(i=1\) now and the sum of time needed decreases by \(a_1\), then we keep moving \(j\) to the right until the sum of time requried exceeds \(t\) .
We track the value of \(j - i\) and use it to undate the ans for every \(i\) .

This is not simply two-pointers, but emphasizes the use of Sliding Window Algorithm. It is not a formal algorithm but more like a technique that can be implemented in various algorithms.

Accepted Code

Lang: GNU C++14
Time: 92ms
Memory: 800KB

// Author        :zqsml
// Created Time  :2021/5/27 20:54:31
#include <cstdio>
#include <iostream>
using namespace std;

const int N = 1e5 + 10;
int a[N], s[N];
int ans;
 
int query(int l, int r) {
	if (l == 0)
		return s[r];
	return s[r] - s[l - 1];
}
 
int main(){
	int n, t;
	scanf("%d %d", &n, &t);
	for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
	s[0] = a[0];
	for (int i = 1; i < n; ++i) {
		s[i] = s[i - 1] + a[i];
	}
	int j = 0;
	for (int i = 0; i < n; ++i) {
		while(j < n && query(i, j) <= t) j++;
		ans = max(ans, j - i);
	}
	printf("%d\n", ans);
	return 0;
}

标签:Valera,279B,CodeForces,number,int,Books,read,book,he
来源: https://www.cnblogs.com/code-addicted/p/CF279B.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有