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;
}
- 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 数组属性和方法
- 缓冲流---为字节流和字符流复制文件增加缓冲流
- 字符流---输入输出与复制文本文件
- Spring 中的几个 PostProcessor 的区别与联系
- Spring 中基于注解的事务控制及原理分析
- Spring 中根据环境切换配置 @Profile
- Spring 中的属性赋值
- Spring 中控制 Bean 生命周期的几种方式及 BeanPostProcessor 执行原理
- Spring 中的 @Import 注解及向容器中添加 Bean 的几种方式
- Spring 注解开发之 @Conditional
- JDBC 技术应用实践:网上书城后台管理系统
- 思科模拟器:路由器基本配置
- 思科模拟器:实验五——高级路由实验
- 从零开始重新认识 SpringMVC
- 字节跳动Android实习面试凉凉经,两轮面试我被完虐了...
- 高效、简单、方便管理与维护的开源运维工单系统