题目大意
给出一个由n条线段连接的图像,第i条线段有ci个点,ai和bi表示第i条线段和i-1条线段的连接点,求出最长的环长。
思路
首先求前两个线段组成的环,然后进行迭代
对于第i条线段,判断该线段作为起始线段和中介线段哪种情况最优进行选择,如果ai = bi的话则只能充当起点。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
long long c[N], a[N], b[N];
int main(){
int t;
cin >> t;
while (t --){
int n, k;
cin >> n;
for (int i = 0; i < n; i ++) cin >> c[i];
for (int i = 0; i < n; i ++) cin >> a[i];
for (int i = 0; i < n; i ++) cin >> b[i];
long long maxx = c[1] + abs(b[1] - a[1]) + 1, temp = maxx;
for (int i = 2; i < n; i ++) {
if (a[i] == b[i]) temp = 1 + c[i];
else {
temp = max(temp - abs(b[i] - a[i]) + 1, abs(b[i] - a[i]) + 1);
temp += c[i];
}
//cout << temp << endl;
maxx = max(temp, maxx);
}
cout << maxx << endl;
}
return 0;
}