dp(E. Sleeping Schedule)
https://codeforces.ml/contest/1324/problem/E
题意:一天有h(3 <= h <= 2000)个小时,n(1 <= n <= 2000)次睡觉,每次过ai(1 <= ai < h)时间就睡觉,有两种选择在过ai-1时间去睡或过ai时间去睡 , 准备开始睡觉时间在[l , r]( 0 <=l <= r < h)算是好的睡眠。问最多有几个好的睡眠时间。
解法:二维dp[i][j]表示第i次睡眠,时间为j的最大好睡眠。
//#include<bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <stack> #include <map> #include <set> #include <string.h> #include <vector> typedef long long ll ; #define int ll #define mod 1000000007 #define gcd __gcd #define rep(i , j , n) for(int i = j ; i <= n ; i++) #define red(i , n , j) for(int i = n ; i >= j ; i--) #define ME(x , y) memset(x , y , sizeof(x)) //ll lcm(ll a , ll b){return a*b/gcd(a,b);} //ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;} //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;} //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len} #define INF 0x3f3f3f3f #define PI acos(-1) #define pii pair<int,int> #define fi first #define se second #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define pb push_back #define mp make_pair #define cin(x) scanf("%lld" , &x); using namespace std; const int N = 1e7+9; const int maxn = 2e3+9; const double esp = 1e-6; int dp[maxn][maxn]; int a[maxn]; void solve(){ int n , h , l , r ; cin >> n >> h >> l >> r ; rep(i , 1 , n){ cin >> a[i]; } rep(i , 0 , n){ rep(j , 0 , h-1){ dp[i][j] = -1 ; } } dp[0][0] = 0; for(int i = 1 ; i <= n ; i++){ for(int j = 0 ; j < h ; j++){ if(dp[i-1][j] != -1){ int k = (j + a[i])%h; if(k >= l && k <= r){ dp[i][k] = max(dp[i][k] , dp[i-1][j] + 1) ; }else{ dp[i][k] = max(dp[i][k] , dp[i-1][j]); } k = (j + a[i] - 1) % h ; if(k >= l && k <= r){ dp[i][k] = max(dp[i][k] , dp[i-1][j] + 1); }else{ dp[i][k] = max(dp[i][k] , dp[i-1][j]); } } } } int ans = -1 ; for(int i = 0 ; i < h ; i++){ ans = max(ans , dp[n][i]); } cout << ans << endl; } signed main() { //ios::sync_with_stdio(false); //cin.tie(0); cout.tie(0); //int t ; //cin >> t ; //while(t--){ solve(); //} }
记忆化递归:
#include<bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <stack> #include <map> #include <set> #include <string.h> #include <vector> typedef long long ll ; #define int ll #define mod 1000000007 #define gcd __gcd #define rep(i , j , n) for(int i = j ; i <= n ; i++) #define red(i , n , j) for(int i = n ; i >= j ; i--) #define ME(x , y) memset(x , y , sizeof(x)) //ll lcm(ll a , ll b){return a*b/gcd(a,b);} ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;} //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;} //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len} #define SC scanf #define INF 0x3f3f3f3f #define PI acos(-1) #define pii pair<int,int> #define fi first #define se second #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 using namespace std; const int N = 1e6+100; const int maxn = 2e3+9; int dp[maxn][maxn]; int a[maxn]; int n , h , l , r; int dfs(int pos , int sum = 0){ if(pos > n) return 0; int& ret = dp[pos][sum]; if(ret != -1) return dp[pos][sum]; int c = (sum + a[pos] - 1) % h ; int d = (sum + a[pos]) % h ; int a = dfs(pos + 1 , c) + (c >= l && c <= r) ; int b = dfs(pos + 1 , d) + (d >= l && d <= r) ; return dp[pos][sum] = max(a , b) ; } void solve(){ cin >> n >> h >> l >> r ; rep(i , 1 ,n){ cin >> a[i]; } rep(i , 0 , n){ rep(j , 0 , h-1){ dp[i][j] = -1 ; } } cout << dfs(1) << endl; } signed main() { //ios::sync_with_stdio(false); //cin.tie(0); cout.tie(0); //int t ; //cin >> t ; //while(t--){ solve(); //} }