Codeforces 279B Books(dp大法好)
来源:互联网 发布:淘宝直通车有用么 编辑:程序博客网 时间:2024/05/17 05:58
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 ≤ 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.
Print a single integer — the maximum number of books Valera can read.
4 53 1 2 1
3
3 32 2 3
1
题意:
列出看每本书所需要的时间,给你 t 时间,问怎样才能看最多本书。要注意的是,看完最后一本之后不能回头看前面的。
用dp的方法一段段求。
#include<iostream>#include<cstdio> #include<algorithm>#include<cmath>using namespace std;const int mx = 1e5 + 5;int lt[mx], dp[mx], sum, n ,t;int main(){scanf("%d%d", &n, &t);for(int i =1; i <= n; i++)scanf("%d", <[i]);dp[0] = 1; lt[0] = sum = 0;int j = 1; for(int i = 1; i <= n; i++){dp[i] = dp[i-1] - 1;sum = sum - lt[i-1];//cout<<"sum="<<sum<<endl;for( ; j <= n; j++){if(sum + lt[j] <= t){sum += lt[j];//cout<<"sum="<<sum<<endl;dp[i]++;} else break;}//cout<<"i=="<<i<<"dp=="<<dp[i]<<endl; //cout<<"sum=="<<sum<<endl;}int re = dp[1];for(int i = 2; i <= n; i++)re =max(re, dp[i]); printf("%d\n", re);//for(int i = 1; i <= n; i++) // cout<<dp[i]<<endl;return 0;}
- Codeforces 279B Books(dp大法好)
- Codeforces 279B Books
- codeforces 279 B. Books
- Codeforces 279B Books
- [Codeforces] 279B - Books
- Codeforces 279B books
- codeforces 279-B. Books(尺取)
- Codeforces #275 (Div. 2) B - Friends and Presents(二分大法好)
- CodeForces 527C(set大法好)
- Codeforces 278B Books
- 【模拟】Books CodeForces 279B
- B. Vanya and Books(Codeforces Round #308 (Div. 2))
- CodeForces - 552B Vanya and Books (数学阶乘)水
- CodeForces-552B - Vanya and Books
- CodeForces 552B-Vanya and Books
- 【33.33%】【codeforces 552B】Vanya and Books
- codeforces 552B Vanya and Books
- Codeforces 407B Long Path【dp】好题
- 路由和侧边栏结合使用
- 使用 sudo nautilus 进入ubuntu 文件管理器。可以随意复制,删除,粘贴,无权限限制
- 远程连接ubuntu mysql出现2003错误 cant connect to mysql
- 隐马尔可夫模型hidden Markov model
- 单例模式(Singleton)的创建
- Codeforces 279B Books(dp大法好)
- 2017不一样的大数据
- Python3与Python2版本的一点区别
- 新闻类缓存的SqlDao
- 关于VS环境下制作和使用静态库和动态库
- Codeforces 427D Match & Catch 后缀自动机
- 属性动画
- leetcode 620. Not Boring Movies(SQL,查询排序)38
- 条件随机场conditional random field