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

2021/10/12 23:16:16

本文主要是介绍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)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程