#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <climits>
#include <string>
#include <stack>
#define all(x) x.begin()+1, x.end()
#define ALL(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
const int INF = 1e9 + 7;
const double PI = acos(-1);
bool cond = true;
template <class T>
void check(const vector<T>& vec)
{
for(const T &val : vec)
cout << val << ' ';
cout << '\n';
return;
}
ll fastpow(ll base, ll exp, ll mod)
{
ll ret = 1;
base %= mod;
while(exp) {
if(exp & 1) ret = (ret * base) % mod;
base = (base * base) % mod;
exp >>= 1;
} return ret;
}
void solve(void)
{
int n;
cin >> n;
int Max = 0;
unordered_map<int, vector<int>> pos;
vector<int> org(n), diff(n), pref(n);
for(int i=0; i<n; i++) cin >> org[i];
for(int i=0; i<n; i++) {
int target;
cin >> target;
diff[i] = target - org[i];
} pref[0] = (diff[0] == 0 ? 1 : 0);
for(int i=1; i<n; i++) {
pref[i] = pref[i-1];
if(!diff[i]) pref[i]++;
}
for(int i=0; i<n; i++) {
int k = diff[i];
if(k) pos[k].push_back(i);
}
for(const auto &pairs : pos) {
int k = pairs.first;
const vector<int>& vec = pairs.second;
int sz = vec.size();
vector<int> dp(sz);
dp[0] = 1;
for(int i=1; i<sz; i++)
dp[i] = max(1, dp[i-1] - (pref[vec[i]] - pref[vec[i-1]]) + 1);
// check(dp);
Max = max(Max, *max_element(ALL(dp)));
}
cout << pref[n-1] + Max << endl;
return;
}
signed main(void)
{
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}
//5
//1 2 3 4 5
//2 3 2 3 2
//5
//0 0 0 0 0
//1 0 2 1 0
//6
//1 2 3 4 5 6
//2 2 4 5 5 7首先对于每一个位置,我们可以知道其与目标值的差为 ,由此我们可以先开一个diff数组进行记录。然后对于diff数组里面每一个为0的地方记录前缀和,比如diff数组是 ,对应的前缀和数组为 。这样一来我们就得到了对于每一个位置而言,前面的0有多少个。0说白了就是你不需要改变的位置,但如果你改变了这个位置的温度,那么总值会减少。
接下来对于diff数组里面的每一个值(称为 k )都统计其位于数组中的哪一个位置,并把相同的k放入同一个数组中,这个过程的代价一般为O(nlogn),然后对于每一个k的数组,我们采用dp的思想进行处理。
首先可以确定的是,如果单改一个非零的位置,那么最后的答案就是原先的diff数组中的0的数量+1,于是我们可以把dp数组的首个元素设置为1,对于后面的每一个位置,其最优解要么是继承前面这个位置的最优解(再加上自己的1,同时减去自己与前面那个位置之间牺牲的0),要么就是单改自己,也就是1,dp的状态转移差不多就是这样,最后对整个dp数组取max,得到这个k值最大的贡献,同时更新外面的Max,最后的答案就是diff数组中所有的0(即)再加上最优贡献Max(Max一开始不能直接设置为1,虽然改动任何一个非零的位置一定会带来至少为1的贡献,但diff可能是全0数组)