【模拟】Books CodeForces 279B
2015-02-02 19:26
405 查看
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.
Sample test(s)
input
output
input
output
用两个指针从后往前扫一遍就行了。
#include
#include
using namespace std;
long long a[110000];
int main()
{
int n;
long long t;
scanf("%d %I64d", &n, &t);
for (int i = 1; i <= n; i++)
scanf("%I64d", &a[i]);
long long now = 0;
int nown = 0;
int max = 0;
int r = n;
for (int l = n; l > 0; l--)
{
now += a[l];
nown++;
if (now <= t)
{
if (nown > max) max = nown;
}
else
while (now > t &&
r>0)//注意while语句都要判断边界,尽管自己觉得不会有问题,但是什么奇葩的数据都有。。。
{
now -= a[r];
nown--;
r--;
}
}
printf("%d\n", max);
return 0;
}
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.
Sample test(s)
input
4 5 3 1 2 1
output
3
input
3 3 2 2 3
output
1
用两个指针从后往前扫一遍就行了。
#include
#include
using namespace std;
long long a[110000];
int main()
{
int n;
long long t;
scanf("%d %I64d", &n, &t);
for (int i = 1; i <= n; i++)
scanf("%I64d", &a[i]);
long long now = 0;
int nown = 0;
int max = 0;
int r = n;
for (int l = n; l > 0; l--)
{
now += a[l];
nown++;
if (now <= t)
{
if (nown > max) max = nown;
}
else
while (now > t &&
r>0)//注意while语句都要判断边界,尽管自己觉得不会有问题,但是什么奇葩的数据都有。。。
{
now -= a[r];
nown--;
r--;
}
}
printf("%d\n", max);
return 0;
}
相关文章推荐
- Codeforces Round #195 (Div. 2) / 336A Vasily the Bear and Triangle(模拟&数学)
- CodeForces #Bayan# -- C Kamal-ol-molk's Painting 模拟
- Permutation CodeForces 359B
- Codeforces ABBYY Cup 3.0 / 316A1 316A2 Special Task(模拟&贪心&组合数学)
- Codeforces Yandex.Algorithm 2011 Qualification 2 / 82A Double Cola (数学规律&模拟)
- Codeforces Beta Round #36 / 36A Extra-terrestrial Intelligence(模拟)
- Codeforces Round #182 (Div. 2) / 302A Eugeny and Array(模拟)
- Using VS 2005 Code Snippets to Write Better Code Faster
- Codeforces Beta Round #98 (Div. 2) / 137A Postcards and photos (模拟)
- snoopy&nbsp;模拟新浪微博登陆
- Codeforces Round #192 (Div. 2) <全>
- Codeforces Round #185 (Div. 1) && (Div. 2)
- Codeforces Round #196 (Div. 2) / 337C Quiz (贪心&快速幂取模)
- CodeForces Round #145(234E) - Champions' League
- ZOJ 2851 Code Formatter
- ZZULI 1497 Reading Books
- 美国ZIP&nbsp;Code&nbsp;一览表
- <codeforces>Little Elephant and Sorting
- <codeforces>A. Little Elephant and Rozdil
- Codeforces Round #173 (Div. 2) <全>