NYOJ 649 Books

Books

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 1
描述

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

输入
The first line contains two integers n and t (1 ≤ n ≤ 10^5; 1 ≤ t ≤ 10^9) — 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 ≤ 10^4), where number ai shows the number of minutes that the boy needs to read the i-th book
输出
Print a single integer — the maximum number of books Valera can read
样例输入
4 5
3 1 2 1

3 3
2 2 3
样例输出
3

1

题目大意就是:输入两个数,一个n(代表有n本书),一个t(代表t分钟),

然后输入n个数据 代表每本书读完所需要的时间

在t分钟之内 最多能读几本书


比较水 直接遍历就能过


#include<stdio.h>
int main() {
  int n, m;
  while(scanf("%d%d", &n, &m) != EOF) {
    int t[100005], max = 0;
    for(int i = 0; i < n; i++)
      scanf("%d", &t[i]);
    for(int i = 0; i < n; i++) {
      int temp = m, sum = 0;
      for(int j = i; j < n && temp >= 0; j++) {
        temp -= t[j];
        if(temp >= 0) sum++;
      } 
      if(sum > max) max = sum;
    }
    printf("%d\n", max);
  }
}


猜你喜欢

转载自blog.csdn.net/qq799028706/article/details/78537368