D - Between Two Arrays(前缀和优化dp)

Posted Harris-H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D - Between Two Arrays(前缀和优化dp)相关的知识,希望对你有一定的参考价值。

D - Between Two Arrays(前缀和优化dp)

见官方题解,先考虑朴素dp,然后在基础上前缀和优化。

时间复杂度: O ( n m ) O(nm) O(nm)

// Problem: D - Between Two Arrays
// Contest: AtCoder - Exawizards Programming Contest 2021(AtCoder Beginner Contest 222)
// URL: https://atcoder.jp/contests/abc222/tasks/abc222_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Date: 2021-10-12 21:45:04
// --------by Herio--------

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; 
const int N=3e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=998244353;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define ios ios::sync_with_stdio(false),cin.tie(nullptr) 
void Print(int *a,int n){
	for(int i=1;i<n;i++)
		printf("%d ",a[i]);
	printf("%d\\n",a[n]); 
}
int f[N][N],n;
int a[N],b[N];
int main(){
	int mx=3e3;
	scanf("%d",&n);
	rep(i,1,n) scanf("%d",&a[i]);
	rep(i,1,n) scanf("%d",&b[i]);
	rep(i,0,mx) f[0][i]=1;
	rep(i,1,n){
		rep(j,a[i],b[i]) f[i][j]=f[i-1][j]; //f[i-1][j]=dp[i][j]
		rep(j,1,mx) f[i][j]=(f[i][j]+f[i][j-1])%mod;
	} 
	printf("%d\\n",f[n][mx]);
	return 0;
}

以上是关于D - Between Two Arrays(前缀和优化dp)的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode --- 1385. Find the Distance Value Between Two Arrays 解题报告

Minimum difference between two arrays

LeetCode | 1385. Find the Distance Value Between Two Arrays两个数组间的距离值Python

CodeForces1288 C.Two Arrays(dp/组合数学)

Educational Codeforces Round 80 C. Two Arrays(组合数快速取模)

codeforces 620D D. Professor GukiZ and Two Arrays