题目:
https://codeforces.com/contest/1476/problem/C
You have n chains, the i-th chain consists of ci vertices. Vertices in each chain are numbered independently from 1 to ci along the chain. In other words, the i-th chain is the undirected graph with ci vertices and (ci−1) edges connecting the j-th and the (j+1)-th vertices for each 1≤j<ci.
Now you decided to unite chains in one graph in the following way:
- the first chain is skipped;
- the 1-st vertex of the i-th chain is connected by an edge with the ai-th vertex of the (i−1)-th chain;
- the last (ci-th) vertex of the i-th chain is connected by an edge with the bi-th vertex of the (i−1)-th chain.
Picture of the first test case. Dotted lines are the edges added during uniting process
Calculate the length of the longest simple cycle in the resulting graph.
A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.
Input
The first line contains a single integer t(1≤t≤1000) — the number of test cases.
The first line of each test case contains the single integer n(2≤n≤105) — the number of chains you have.
The second line of each test case contains n integers c1,c2,…,cn (2≤ci≤109) — the number of vertices in the corresponding chains.
The third line of each test case contains n integers a1,a2,…,an (a1=−1; 1≤ai≤ci−1).
The fourth line of each test case contains n integers b1,b2,…,bn (b1=−1; 1≤bi≤ci−1).
Both a1 and b1 are equal to −1, they aren’t used in graph building and given just for index consistency. It’s guaranteed that the sum of n over all test cases doesn’t exceed 105
Output
For each test case, print the length of the longest simple cycle.
Input
3
4
3 4 3 3
-1 1 2 2
-1 2 2 3
2
5 6
-1 5
-1 1
3
3 5 2
-1 1 1
-1 3 5
Output
7
11
8
Note
In the first test case, the longest simple cycle is shown below:
We can’t increase it with the first chain, since in such case it won’t be simple — the vertex 2 on the second chain will break simplicity.
思路:
记录每条chain的所选区域的长度,即chain上两个点的长度。
初始化:
第i条chain的状态:
如果第 i - 1 条chain 所选的两个点不想等(as:当前在第4条chain):
如果第 i - 1 条chain 所选的两个点想等(as:当前在第3条chain):
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 1e5 + 10;
ll a[MAXN], pre[MAXN], suc[MAXN], ans[MAXN], in[MAXN];
int main(){
// freopen("_in.txt", "r", stdin);
// freopen("_out1.txt", "w", stdout);
int t, n;
ll ans, head;
scanf("%d", &t);
while (t--){
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lld", &a[i]);
for (int i = 0; i < n; i++)
scanf("%lld", &pre[i]);
for (int i = 0; i < n; i++)
scanf("%lld", &suc[i]);
for (int i = 1; i < n; i++)
in[i] = abs(pre[i] - suc[i]);
ans = head = in[1] + a[1] + 1;
for (int i = 2; i < n; i++)
if (pre[i] != suc[i]){
head = max(head - in[i], in[i]) + a[i] + 1;
ans = max(ans, max(head, in[i] + a[i] + 1));
}
else{
head = a[i] + 1;
ans = max(ans, in[i] + a[i] + 1);
}
printf("%lld\n", ans);
}
return 0;
}