题意

给你一个长度为\(n\)的数组\(a\)和3个数字\(h,l和r\)。\(t\)初始为0,每次可以使\(t=(t+a_i) \% h\)或者\(t=(t+a_i-1)\%h\),如果这时\(t\in\left[l,r\right]\)就将\(ans\)加1。求\(ans\)的最大值。

解题思路

这场比赛的题感觉偏简单了。

这是一道显而易见的DP题。\(dp_{i,j,k}\)表示枚举到\(a_i\),当前\(t=j\),是否-1时的\(ans\)的最大值,很容易就能推导出转移公式。

AC代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef pair<int,int> pi;
  5. #define x first
  6. #define y second
  7. #define sz(x) ((int)(x).size())
  8. #define all(x) (x).begin(),(x).end()
  9. #define rall(x) (x).rbegin(),(x).rend()
  10. #define endl '\n'
  11. const double PI=acos(-1.0);
  12. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  13. int rnd(int l,int r){return l+rng()%(r-l+1);}
  14. namespace IO{
  15. bool REOF = 1; //为0表示文件结尾
  16. inline char nc() {
  17. static char buf[100000], *p1 = buf, *p2 = buf;
  18. return p1 == p2 && REOF && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? (REOF = 0, EOF) : *p1++;
  19. }
  20. template<class T>
  21. inline bool read(T &x) {
  22. char c = nc();bool f = 0; x = 0;
  23. while (c<'0' || c>'9')c == '-' && (f = 1), c = nc();
  24. while (c >= '0'&&c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = nc();
  25. if(f)x=-x;
  26. return REOF;
  27. }
  28. template<typename T, typename... T2>
  29. inline bool read(T &x, T2 &... rest) {
  30. read(x);
  31. return read(rest...);
  32. }
  33. inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')); }
  34. // inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || c==' '; }
  35. inline bool read_str(char *a) {
  36. while ((*a = nc()) && need(*a) && REOF)++a; *a = '\0';
  37. return REOF;
  38. }
  39. inline bool read_dbl(double &x){
  40. bool f = 0; char ch = nc(); x = 0;
  41. while(ch<'0'||ch>'9') {f|=(ch=='-');ch=nc();}
  42. while(ch>='0'&&ch<='9'){x=x*10.0+(ch^48);ch=nc();}
  43. if(ch == '.') {
  44. double tmp = 1; ch = nc();
  45. while(ch>='0'&&ch<='9'){tmp=tmp/10.0;x=x+tmp*(ch^48);ch=nc();}
  46. }
  47. if(f)x=-x;
  48. return REOF;
  49. }
  50. template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
  51. template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  52. while(*sdbg!=',')cerr<<*sdbg++;
  53. cerr<<'='<<h<<','<<' '; _dbg(sdbg+1, a...);
  54. }
  55. template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  56. os << "["; for (auto vv : V) os << vv << ","; return os << "]";
  57. }
  58. template<class T> ostream &operator<<(ostream& os, set<T> V) {
  59. os << "["; for (auto vv : V) os << vv << ","; return os << "]";
  60. }
  61. template<class T> ostream &operator<<(ostream& os, map<T,T> V) {
  62. os << "["; for (auto vv : V) os << vv << ","; return os << "]";
  63. }
  64. template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  65. return os << "(" << P.st << "," << P.nd << ")";
  66. }
  67. #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
  68. }
  69. using namespace IO;
  70. const int maxn=2e5+5;
  71. const int maxv=2e5+5;
  72. const int mod=998244353; // 998244353 1e9+7
  73. const int INF=1e9+7; // 1e9+7 0x3f3f3f3f 0x3f3f3f3f3f3f3f3f
  74. const double eps=1e-12;
  75. int dx[4]={0,1,0,-1};
  76. //int dx[8]={1,0,-1,1,-1,1,0,-1};
  77. int dy[4]={1,0,-1,0};
  78. //int dy[8]={1,1,1,0,0,-1,-1,-1};
  79. // #define ls (x<<1)
  80. // #define rs (x<<1|1)
  81. // #define mid ((l+r)>>1)
  82. // #define lson ls,l,mid
  83. // #define rson rs,mid+1,r
  84. // int tot,head[maxn];
  85. // struct Edge{
  86. // int v,nxt;
  87. // Edge(){}
  88. // Edge(int _v,int _nxt):v(_v),nxt(_nxt){}
  89. // }e[maxn<<1];
  90. // void init(){
  91. // tot=1;
  92. // memset(head,0,sizeof(head));
  93. // }
  94. // void addedge(int u,int v){
  95. // e[tot]=Edge(v,head[u]); head[u]=tot++;
  96. // e[tot]=Edge(u,head[v]); head[v]=tot++;
  97. // }
  98. // void addarc(int u,int v){
  99. // e[tot]=Edge(v,head[u]); head[u]=tot++;
  100. // }
  101. /**
  102. * ********** Backlight **********
  103. * 仔细读题
  104. * 注意边界条件
  105. * 记得注释输入流重定向
  106. * 没有思路就试试逆向思维
  107. * 加油,奥利给
  108. */
  109. int n,h,l,r,a[maxn];
  110. int dp[2005][2005][2];
  111. void solve(){
  112. read(n,h,l,r);
  113. for(int i=1;i<=n;i++)read(a[i]);
  114. memset(dp,-1,sizeof(dp));
  115. dp[0][0][0]=dp[0][0][1]=0;
  116. int sleep,delta;
  117. for(int i=1;i<=n;i++){
  118. for(int j=0;j<h;j++){
  119. if(dp[i-1][j][0]!=-1){
  120. sleep=(j+a[i])%h;
  121. delta=(sleep>=l && sleep<=r)?1:0;
  122. dp[i][sleep][0]=max(dp[i][sleep][0],dp[i-1][j][0]+delta);
  123. sleep=(j+a[i]-1)%h;
  124. delta=(sleep>=l && sleep<=r)?1:0;
  125. dp[i][sleep][1]=max(dp[i][sleep][1],dp[i-1][j][0]+delta);
  126. }
  127. if(dp[i-1][j][1]!=-1){
  128. sleep=(j+a[i])%h;
  129. delta=(sleep>=l && sleep<=r)?1:0;
  130. dp[i][sleep][0]=max(dp[i][sleep][0],dp[i-1][j][1]+delta);
  131. sleep=(j+a[i]-1)%h;
  132. delta=(sleep>=l && sleep<=r)?1:0;
  133. dp[i][sleep][1]=max(dp[i][sleep][1],dp[i-1][j][1]+delta);
  134. }
  135. }
  136. }
  137. int ans=0;
  138. for(int i=0;i<h;i++){
  139. ans=max(ans,dp[n][i][0]);
  140. ans=max(ans,dp[n][i][1]);
  141. }
  142. printf("%d\n",ans);
  143. }
  144. int main()
  145. {
  146. // freopen("in.txt","r",stdin);
  147. // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  148. // int _T; read(_T); for(int _=1;_<=_T;_++)solve();
  149. // while(read(n))solve();
  150. solve();
  151. return 0;
  152. }

Codeforces 1324E Sleeping Schedule DP的更多相关文章

  1. 1324E - Sleeping Schedule

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

  2. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

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

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

  4. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  5. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

  6. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

  7. codeforces round367 div2.C (DP)

    题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...

  8. CodeForces 176B Word Cut dp

    Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...

  9. codeforces 148D之概率DP

    http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test 2 seconds memory l ...

随机推荐

  1. WEB应用中的路径问题及乱码问题

    1 WEB应用中的路径问题  在web应用中,由于使用转发跳转路径时,地址栏不变.此时使用相对路径(../)存在404现象. 故使用绝对路径,解决web应用的路径问题. 什么是绝对路径,以 “/” 开 ...

  2. CustomPlot 在Qt下 鼠标点击曲线 显示当前坐标

    此次记录主要是为了下次使用时能回忆起来才做得笔记,若有需改进的地方,请不吝珠玉. widget.cpp #include "widget.h" #include "ui_ ...

  3. GPS坐标显示在百度地图上(Qt+百度地图)

    Qt在5.6以后的版本就不支持webview控件了,这里我用的是Qt5.4的版本,里面还有这个控件: 下面简单介绍下Qt与html中的javascript调用交互过程: 一.整体实现介绍 在html中 ...

  4. MyFirstJavaWeb

    源代码: Register.jsp <%@ page language="java" contentType="text/html; charset=utf-8&q ...

  5. MySQL--->高级对象

    本章目标: 掌握MySQL视图 掌握MySQL存储过程 掌握MySQL触发器 1.视图: 视图就是一条select语句执行后返回的结果集. 使用场景:权限控制的时候不希望用户访问表中某些敏感信息的列 ...

  6. 2020-06-13:Redis底层数据结构?

    福哥答案2020-06-13: 福哥口诀法:简链字跳整 压快压 SDS simple synamic string:简单动态字符串.支持自动动态扩容的字节数组 .list :链表 .双端链表.dict ...

  7. C#LeetCode刷题之#13-罗马数字转整数(Roman to Integer)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3842 访问. 罗马数字包含以下七种字符: I, V, X, L, ...

  8. 这都Java15了,Java7特性还没整明白?

    「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...

  9. 动态数组java实现

    数组是一种顺序存储的线性表,所有元素的内存地址是连续的. 动态数组相对于一般数组的优势是可以灵活地添加或删除元素.而一般数组则受限于固定的内存空间.只能有限的添加元素 动态数组(Dynamic Arr ...

  10. python基础 Day9

    python Day9 函数的初识 #代码的可读性较好 s=[1,2,3,4,5,5] def list_len(S): count=0 for i in s: count+=1 print(coun ...