codeforces1324——E.Sleeping Schedule(DP+初始化)

简介: codeforces1324——E.Sleeping Schedule(DP+初始化)

原题链接

题意:

(from洛谷)

20200401134307494.png思路:

很容易想到DP。

dp[i][j]表示选完前i个元素前缀和对h取余的值为j的优秀睡眠的个数。

划分依据就是选择第i个元素时是否将其减一,如果不减一的话,上一个状态就是dp[i-1][j-a[i]],即选完前i-1个元素后前缀和对h取余的值为j-a[i]的优秀睡眠的个数;减一的话也类似。

有两个需要注意的点:

一是初始化,因为有些状态不合法,要初始化为极小值。

二是关于取余的问题,应该要保证dp数组的第二维为正数。

代码:

#pragma GCC optimize("Ofast",3,"inline")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
char F[200];
inline void out(I_int x)
{
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0)
    {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
ll ksm(ll a,ll b,ll p)
{
    ll res=1;
    while(b)
    {
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int maxn=1e5+100,mod=1e8;
const double PI = atan(1.0)*4;
const double eps=1e-6;
int n,h,l,r;
int a[maxn],dp[2100][2100];
int main()
{
    n=read(),h=read(),l=read(),r=read();
    for(int i=1;i<=n;i++) a[i]=read();
    memset(dp,-0x3f,sizeof dp);
    dp[0][0]=0;
    for(int i=1;i<=n;i++)
        for(int j=0;j<h;j++){
            int tmp=0;
            if(j>=l&&j<=r) tmp++;
            dp[i][j]=max(dp[i-1][(j-a[i]+h)%h],dp[i-1][(j-a[i]+1+h)%h])+tmp;
        }
    int maxx=0;
    for(int i=0;i<h;i++)
        maxx=max(maxx,dp[n][i]);
    out(maxx);
    return 0;
}

参考

目录
相关文章
|
1月前
|
人工智能
E. Sleeping Schedule(记忆化搜索)
E. Sleeping Schedule(记忆化搜索)
|
1月前
|
算法 测试技术
Big Event in HDU(dp算法)
Big Event in HDU(dp算法)
|
6月前
|
C++
INT_MIN,max函数和accumulate函数
INT_MIN,max函数和accumulate函数
|
10月前
UVa11549 - Calculator Conundrum (Floyd判圈法)
UVa11549 - Calculator Conundrum (Floyd判圈法)
34 0
|
人工智能
CF 859C - Pie Rules(dp好题)
CF 859C - Pie Rules(dp好题)
97 0
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
Codeforces 1485C Floor and Mod (枚举)
Codeforces 1485C Floor and Mod (枚举)
63 0
|
人工智能
codeforces859——C. Pie Rules(思维+DP)
codeforces859——C. Pie Rules(思维+DP)
93 0
|
Java 调度
Leetcode-Medium 621. Task Scheduler
Leetcode-Medium 621. Task Scheduler
99 0
Leetcode-Medium 621. Task Scheduler
Leetcode --- 动态规划系列(背包_True/False问题)
Leetcode --- 动态规划系列(背包_True/False问题)

热门文章

最新文章