标签:NPU amp mem memory enc process ESS needed rom
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
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.
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;
}
标签:NPU amp mem memory enc process ESS needed rom
原文地址:https://www.cnblogs.com/code-addicted/p/CF279B.html