对于奇数长度的数组,为了让合法的元素最多,只有一种情况。
对于偶数长度的数组,如果把合法的点标记为 $1$,其它点标记为 $0$,可以发现 $0$ 和 $1$ 的排列规律,首先不能出现连续的 $1$,并且最多出现一次连续的两个 $0$,连续的两个 $0$ 出现的位置决定了最终的结果。
从 $a_2$ 开始,假设 $a_2 = 1, a_3 = 0, \ldots$ 这是一种情况。从 $a_{n - 1}$ 开始,假设 $a_{n - 1} = 1, a_{n - 2} = 0, \ldots$ 这是对称的另外一种情况。对这两种情况分别求出前缀和与后缀和,然后枚举 $i$,就可以求出答案。
// 2024/1/29
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7, MOD1 = 998'244'353;
const ll INFL = 0x3f3f3f3f'3f3f3f3f;
const double eps = 1e-8;
const int dir[8][2] = {
{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1},
};
const ull Pr = 131;
#define For(i, a, b) for (int i = int(a); i < int(b); ++i)
#define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define For1(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i)
#define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i])
#define f1 first
#define f2 second
#define pb push_back
#define has(a, x) (a.find(x) != a.end())
#define nemp(a) (!a.empty())
#define all(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define NL cout << '\n';
template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template <typename t> istream &operator>>(istream &in, vector<t> &vec) {
for (t &x : vec)
in >> x;
return in;
}
template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) {
int n = SZ(vec);
For(i, 0, n) {
out << vec[i];
if (i < n - 1)
out << ' ';
}
return out;
}
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ", ";
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? ", " : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifdef _DEBUG
#define debug1(x) cout << #x " = " << x << endl;
#define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl;
#define debug3(x, y, z) \
cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl;
#define dbg(x...) \
cerr << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \
_print(x); \
cerr << endl;
#else
#define debug1
#define debug2
#define debug3
#define dbg(x...)
#endif
const int N = 300100;
int n;
ll a[N], p[N], p1[N];
void solve() {
cin >> n;
For1(i, 1, n) { cin >> a[i]; }
ll ans{};
auto check = [](int start) {
ll ans = 0;
for (int i = start; i <= n - 1; i += 2) {
if (a[i] > a[i - 1] && a[i] > a[i + 1])
continue;
ll top = max(a[i - 1], a[i + 1]) + 1;
ans += top - a[i];
}
return ans;
};
if (n & 1) {
ans = check(2);
cout << ans << '\n';
return;
}
p[1] = 0;
for (int i = 2; i <= n; i += 2) {
ll top = max(a[i - 1], a[i + 1]) + 1, delta = max(top - a[i], 0LL);
p[i] = p[i + 1] = delta + p[i - 1];
}
p1[n] = 0;
for (int i = n - 1; i >= 1; i -= 2) {
ll top = max(a[i - 1], a[i + 1]) + 1, delta = max(top - a[i], 0LL);
p1[i] = p1[i - 1] = delta + p1[i + 1];
}
ans = -1;
for (int i = 2; i <= n; i += 2) {
ll tmp = p[i - 1] + p1[i];
if (ans == -1)
ans = tmp;
else
ckmin(ans, tmp);
}
cout << ans << '\n';
}
int main(void) {
#ifdef _DEBUG
// freopen("1706c.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}