题目大意:一天有h个小时,一个人喜欢睡觉,一共睡n次,每次都睡h个小时,开始时间为0,间隔a[i]或a[i]-1个小时开始睡第i次觉,每天都有一个最好时间区间,问这n次觉,最多有多少次是在最好时间内睡的。

题解:定义状态dp[i][j]为第i次觉是在j时刻睡的,那么状态转移方程dp[i][j]=max(dp[i-1][(j-a[i]+h)%h],dp[i-1][(j-a[i]+1+h)%h]+

check(j)。

值得注意的是,不是每个状态都能够到达的,假设dp全部赋值为-1,当

dp[i-1][(j-a[i]+h)%h]==-1&&dp[i-1][(j-a[i]+1+h)%h]==-1时,状态dp[i][j]就无法到达......

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=2E3+;
ll dp[N][N];
ll arr[N];
int main(){
ll n,h,l,r;
cin>>n>>h>>l>>r;
for(ll i=;i<=n;i++) cin>>arr[i];
memset(dp,-,sizeof dp);
ll c1=arr[]-;
ll c2=arr[];
dp[][c1]=(c1>=l&&c1<=r ? :);
dp[][c2]=(c2>=l&&c2<=r ? :);
ll ans=max(dp[][c1],dp[][c2]);
for(ll i=;i<=n;i++){
for(ll j=;j<h;j++){
ll c1=(j-arr[i]+h)%h;
ll c2=(j-arr[i]++h)%h;
if(dp[i-][c1]<&&dp[i-][c2]<) continue ;
dp[i][j]=max(dp[i][j],max(dp[i-][c1],dp[i-][c2])+(j>=l&&j<=r ? :));
}
}
for(ll i=;i<h;i++) ans=max(ans,dp[n][i]);
cout<<ans<<endl;
return ;
}

1324E - Sleeping Schedule的更多相关文章

  1. Codeforces 1324E Sleeping Schedule DP

    题意 给你一个长度为\(n\)的数组\(a\)和3个数字\(h,l和r\).\(t\)初始为0,每次可以使\(t=(t+a_i) \% h\)或者\(t=(t+a_i-1)\%h\),如果这时\(t\ ...

  2. CF1324E Sleeping Schedule 题解

    原题链接 简要题意: 每次可以将 \(a_i\) 减 \(1\) 或不变.求让 \(a_i\) 的前缀和 \(\% h\) 的值在 \([l,r]\) 区间中的最多的个数. E题是个水dp,也不怎样 ...

  3. Codeforces Round #627 (Div. 3) E - Sleeping Schedule(递推)

    题意: 每天有 h 小时,有一序列 an,每次可以选择 ai 或 ai - 1 小时后睡觉,问从 0 次 0 时开始,最多在 l ~ r 时间段入睡多少次. 思路: 如果此时可达,计算此时可达的时间点 ...

  4. Educational Codeforces Round 21(A.暴力,B.前缀和,C.贪心)

    A. Lucky Year time limit per test:1 second memory limit per test:256 megabytes input:standard input ...

  5. Average Sleep Time CodeForces - 808B (前缀和)

    It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, on ...

  6. CF Educational Codeforces Round 21

    A. Lucky Year time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. [LeetCode] Course Schedule II 课程清单之二

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. [LeetCode] Course Schedule 课程清单

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. POJ 1325 Machine Schedule——S.B.S.

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13731   Accepted: 5873 ...

随机推荐

  1. python浅学【网络服务中间件】之Memcached

    一.缓存的由来: 提升性能 绝大多数情况下,select 是出现性能问题最大的地方.一方面,select 会有很多像 join.group.order.like 等这样丰富的语义,而这些语义是非常耗性 ...

  2. 【Excel使用技巧】vlookup函数

    背景 前不久开发了一个运营小工具,运营人员上传一个id的列表,即可导出对应id的额外数据.需求本身不复杂,很快就开发完了,但上线后,运营反馈了一个问题,导出后的数据跟导出之前的数据顺序不一致. 经过沟 ...

  3. tcp/ip面试题

    TCP协议  1.OSI与TCP/IP各层的结构和功能,协议和作用.     OSI七层模型对应TCP/IP四层模型,只是分法不同而已.     应用层:提供应用层服务,文件传输(FTP),电子邮件( ...

  4. PYTHON数据类型(进阶)

    PYTHON数据类型(进阶) 一.字符串.列表.字典.元祖.集合的补充 str #captalize 首字母大写,其余小写 s1.capitalize() #swapcase 大小写翻转 s1.swa ...

  5. c++第一个程序测试-----c++每日笔记!

    #include <iostream>int main(){ //std::cout << "Enter two number:" <<std: ...

  6. Building Applications with Force.com and VisualForce(Dev401)( 八):Designing Applications for Multiple users:Managing your users' experience II

    Dev 401-008: Design Applications for Multiple Users' Experience Part 2Universal Containers Scenario1 ...

  7. 通过带Flask的REST API在Python中部署PyTorch

    在本教程中,我们将使用Flask来部署PyTorch模型,并用讲解用于模型推断的 REST API.特别是,我们将部署一个预训练的DenseNet 121模 型来检测图像. 备注: 可在GitHub上 ...

  8. arcgis server10.7修改打印模板后前台调用不生效

    在10.5.1及之前的版本中,如果更改打印地图模板,如字体设置,那么直接修改[ArcGIS for Server 安装目录]/Templates/ExportWebMapTemplates下的mxd的 ...

  9. 最适合新手入门的SpringCloud教程 6—Ribbon负载均衡「F版本」

    SpringCloud版本:Finchley.SR2 SpringBoot版本:2.0.3.RELEASE 源码地址:https://gitee.com/bingqilinpeishenme/Java ...

  10. JavaScript实现哈希表

    JavaScript实现哈希表 一.哈希表简介 1.1.认识哈希表 哈希表通常是基于数组实现的,但是相对于数组,它存在更多优势: 哈希表可以提供非常快速的插入-删除-查找操作: 无论多少数据,插入和删 ...