zoukankan      html  css  js  c++  java
  • CF1324E Sleeping Schedule(基础dp)

    Sleeping Schedule

    思路:一个睡眠时间和下一个睡眠时间的情况是传递关系,所以我们需要记录每一层的信息,一天的时间h可以压缩信息,dp[i][j]可以表示在第i个睡眠阶段,第j时刻“good sleep”的次数。

    如果dp[i - 1][j]是合法情况,即状态转移过程中,出现过这个过程,则分两种情况转移:

    dp[i][(j + ai) % h] = max(dp[i][(j + ai) % h], dp[i - 1][j] + is_good_sleep?);

    dp[i][(j + ai - 1) % h] = max(dp[i][(j + ai - 1) % h], dp[i - 1][j] + is_good_sleep?);

     1 #include <iostream>
     2 #include <deque>
     3 #include <algorithm>
     4 #include <stack>
     5 #include <vector>
     6 #include <cstring>
     7 
     8 using namespace std;
     9 
    10 const int N = 2e3 + 10;
    11 int dp[N][N];
    12 int n, h, l, r;
    13 
    14 inline int add(int x){
    15     return x >= l && x <= r;
    16 }
    17 
    18 void solve(){
    19 
    20     cin >> n >> h >> l >> r;
    21     int ai;
    22     memset(dp, -1, sizeof(dp));
    23     //cout << dp[0][0] << endl;
    24     dp[0][0] = 0;
    25     for(int i = 1; i <= n; ++i){
    26         cin >> ai;
    27         for(int j = 0; j < h; ++j){
    28             if(dp[i - 1][j] != -1){
    29                 dp[i][(j + ai) % h] = 
    30                 max(dp[i][(j + ai) % h], dp[i - 1][j] + add((j + ai) % h));
    31                 
    32                 dp[i][(j + ai - 1) % h] = 
    33                 max(dp[i][(j + ai - 1) % h], dp[i - 1][j] + add((j + ai - 1) % h)); 
    34             }
    35         }
    36     }
    37     int Max_tot = -1;
    38     for(int i = 0; i < h; ++i){
    39         Max_tot = max(Max_tot, dp[n][i]);
    40     }
    41     cout << Max_tot << endl;
    42 }
    43 
    44 int main(){
    45     
    46     ios::sync_with_stdio(false);
    47     cin.tie(0);
    48     cout.tie(0);
    49     solve();
    50     
    51     return 0;
    52 }
  • 相关阅读:
    html css div img垂直居中
    jquery 多选框 checkbox 获取选中的框
    css 滚动条样式
    css 翻牌 翻转 3d翻转 特效
    css强制不换行 多出的字省略号
    jquery获取元素坐标获取鼠标坐标
    鸡汤 咯咯
    <bean> 中配置详解 </bean>
    正则表达式的囧
    我的天$删除注册表$安装mysql最后一步不能启动服务的解决办法
  • 原文地址:https://www.cnblogs.com/SSummerZzz/p/12705980.html
Copyright © 2011-2022 走看看