Codeforces Round #627 (Div. 3) E. Sleeping Schedule (DP)
E. Sleeping Schedule
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Vova had a pretty weird sleeping schedule. There are hh hours in a day. Vova will sleep exactly nn times. The ii-th time he will sleep exactly after aiai hours from the time he woke up. You can assume that Vova woke up exactly at the beginning of this story (the initial time is 00). Each time Vova sleeps exactly one day (in other words, hh hours).
Vova thinks that the ii-th sleeping time is good if he starts to sleep between hours ll and rr inclusive.
Vova can control himself and before the ii-th time can choose between two options: go to sleep after aiai hours or after ai−1ai−1 hours.
Your task is to say the maximum number of good sleeping times Vova can obtain if he acts optimally.
Input
The first line of the input contains four integers n,h,ln,h,l and rr (1≤n≤2000,3≤h≤2000,0≤l≤r<h1≤n≤2000,3≤h≤2000,0≤l≤r<h) — the number of times Vova goes to sleep, the number of hours in a day and the segment of the good sleeping time.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai<h1≤ai<h), where aiai is the number of hours after which Vova goes to sleep the ii-th time.
Output
Print one integer — the maximum number of good sleeping times Vova can obtain if he acts optimally.
Example
input
Copy
7 24 21 23
16 17 14 20 20 11 22
output
Copy
3
Note
The maximum number of good times in the example is 3.
思路:dp,dp[i][j]表示第i次睡觉时,当前时间是j的最大答案,转移方程:
dp[i][(j+a[i])%h]=dp[i-1][j]+((j+a[i])%h>=l&&(j+a[i])%h<=r));
dp[i][(j+a[i]-1)%h]=dp[i-1][j]+((j+a[i]-1)%h>=l&&(j+a[i]-1)%h<=r));
最后在dp[n][0~h-1]之间取最大就好,注意初始化全部为-1表示不能到达的状态,dp[0][0]=0
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
ll sz[200005],n;
template <typename T> inline void read(T& x)
{
x=0;char ch=getchar();ll f=1;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}x*=f;
}
inline ll query(ll x){ll res=0;while(x){res+=sz[x];x-=lb(x);}return res;}
inline void add(ll x,ll val){while(x<=n){sz[x]+=val;x+=lb(x);}}//第x个加上val
ll h,l,r,a[2005],dp[2005][2005];
int main()
{
cin>>n>>h>>l>>r;
for(rg i=1;i<=n;i++)cin>>a[i];
memset(dp,-1,sizeof(dp));
dp[0][0]=0;
ll maxx=-1;
for(rg i=1;i<=n;i++)
{
for(rg j=0;j<h;j++)
{
if(dp[i-1][j]>=0)
{
ll k=(j+a[i])%h;
dp[i][k]=max(dp[i-1][j]+(l<=k&&r>=k),dp[i][k]);
k=(j+a[i]-1)%h;
dp[i][k]=max(dp[i-1][j]+(l<=k&&r>=k),dp[i][k]);
}
}
}
for(rg i=0;i<h;i++)maxx=max(dp[n][i],maxx);
cout<<maxx<<endl;
return 0;
}
- 谁能拯救我糟心的出行?
- Python常见数据结构整理 Python常见数据结构整理
- “共享洗车机”落户咸阳,你怎么看?
- 蒋涛——软件是一个非常特殊的产业,一旦进入就再难挣脱
- Pechkin:html -> pdf 利器
- C#执行XSL转换
- javascript:算法笔记
- spring boot 登录注册 demo (一)
- linux学习:CentOS、Mac上SSH的设置以及SceureCRT中的文件上传下载
- 中关村成为北京“高精尖”产业发展主阵地
- jquery-barcode:js实现的条码打印
- 页面json 格式化+颜色高亮
- Python 里 and、or 的计算规则
- Python中赋值、浅拷贝与深拷贝
- 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 数组属性和方法
- Laravel5.1 框架表单验证操作实例详解
- 通过实例了解Python异常处理机制底层实现
- header函数设置响应头解决php跨域问题实例详解
- Linux采用双网卡bond、起子接口的方式
- PHP实现字母数字混合验证码功能
- php+pdo实现的购物车类完整示例
- CentOS7怎么执行PHP定时任务详解
- Linux下PHP+Apache的26个必知的安全设置
- linux中ssh免密通信的实现
- 怎么修改CentOS服务器时间为北京时间
- Laravel5.1 框架控制器基础用法实例分析
- Laravel5.1 框架模型软删除操作实例分析
- Laravel 手动开关 Eloquent 修改器的操作方法
- 怎么测试Linux下tcp最大连接数限制详解
- Laravel 5.1 框架Blade模板引擎用法实例分析