Books (双指针)
题目描述
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.
思路
使用前后指针,从头到尾开始加,如果和超过k,就从j所在的位置开始删去,最后取最大值即可
代码
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=1e5+10;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
int a[N];
void solve(){
int n,t;cin>>n>>t;
for(int i=0;i<n;i++) cin>>a[i];
int ans=0;
int sum=0;
for(int i=0,j=0;i<n;i++){
sum+=a[i];
while(sum>t && j<n){
sum-=a[j];
j++;
}
ans=max(ans,i-j+1);
}
cout<<ans<<endl;
}
int main(){
IOS;
solve();
return 0;
}
- 任意阶幻方..
- 大数乘大数
- HDUOJ-----2824The Euler function
- HDUOJ--1874 畅通工程续
- HDUOJ---2112HDU Today
- HDUOJ-----2066一个人的旅行
- HDUOJ --2544最短路(基础)
- 帮同学的忙,随意写了个课程设计(关于循环双向列表)
- HDUOJ---3371Connect the Cities
- 初学HTML一些基本控件语句
- java学习之第五章编程题示例(初学篇)
- java第四章编程题(初学篇)
- java测试Unicode编码以及数组的运用(初学篇)
- HDUOJ---1754 Minimum Inversion Number (单点更新之求逆序数)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 工作中常用的十款idea插件
- Linux系统rsync数据同步服务介绍
- 清空表与删除表mysql
- Java虚拟机-JVM组成结构详解
- 解决Chunkize warning while installing gensim问题
- numpy的random模块
- MySQL如何快速生成千万数据量?
- Linux系统rsync实战操作
- SQL线程状态分析:processlist
- 解决Fatal error in launcher: Unable to create process using '"'
- 责任链设计模式:老哥用程序生孩子
- LAMP架构应用实战—Apache服务介绍与安装01
- Excel合并
- 自定义异常为什么性能差,我来告诉你
- GitHub比较火的springBoot实战项目