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;
}

上一篇:API 双方认证探讨


下一篇:Flex 布局