-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabsdiff.cpp
More file actions
executable file
·66 lines (60 loc) · 1.41 KB
/
Copy pathabsdiff.cpp
File metadata and controls
executable file
·66 lines (60 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//https://www.hackerearth.com/practice/algorithms/dynamic-programming/introduction-to-dynamic-programming-1/practice-problems/algorithm/shivam-shantam-and-their-absolute-difference-3/description/
#include <bits/stdc++.h>
using namespace std;
int n,q;
void setreset(int &f,int j){
if(j<0)
{
f=1;
}
else{
f=0;
}
}
int solve(int i,int j,int f,vector<int> a,vector<int> b,vector<vector<vector<int>>> dp){
if(i==n){
if(j<=q)
return 1;
else
return 0;
}
if(f)
{
j=-j;
}
int ans=0;
int tempj;
if(dp[i][j][f]==-1){
tempj=j;
setreset(f,tempj);
ans += solve(i+1,tempj,f,a,b,dp);
tempj=j+a[i];
setreset(f,tempj);
ans += solve(i+1,tempj,f,a,b,dp);
tempj=j-b[i];
setreset(f,tempj);
ans += solve(i+1,tempj,f,a,b,dp);
tempj=j+abs(a[i]-b[i]);
setreset(f,tempj);
ans += solve(i+1,tempj,f,a,b,dp);
dp[i][j][f]=ans;
}
return dp[i][j][f];
}
int main()
{
cin>>n;
vector<int> a(n);
vector<int> b(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
cin>>b[i];
}
//int q;
cin>>q;
vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(2001,vector<int>(2,-1)));
cout<<solve(0,0,0,a,b,dp)<<endl;
return 0;
}