WA on 13~15
查看原帖
WA on 13~15
486001
Knighthood楼主2024/11/8 18:47
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 2e5 + 5;
const int V = 1e6 + 5;

int n, a[N];
ll pre[N], g[N];

namespace ktd {
	ll ft, F, G, fx[V], gx[V];
	
	void clear() {
		ft = 0;
		memset(fx, -63, sizeof fx), F = fx[0];
		memset(gx, -63, sizeof gx), G = gx[0];
	}
	
	ll F_query(int x, ll y) { return max(F, fx[x] + y) + ft; }
	
	ll G_query(int x, ll y) { return max(G, gx[x] + y); }
	
	void F_max(int x, ll y) { fx[x] = max(fx[x], y - ft), F = max(F, y - ft); }
	
	void G_max(int x, ll y) { gx[x] = max(gx[x], y), G = max(G, y); }
	
	void F_add(ll y) { ft += y; }
}

int main() {
//	freopen("color2.in", "r", stdin);
//	freopen("color.out", "w", stdout);

	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int T;
	cin >> T;
	while (T--) {
		cin >> n;
		for (int i = 1; i <= n; ++i) cin >> a[i];
		for (int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + a[i] * bool(a[i] == a[i - 1]);
		ktd::clear();
		ktd::F_max(0, 0);
		for (int i = 1; i <= n; ++i) {
			if (a[i] == a[i - 1]) {
				ktd::F_add(a[i]);
			}
			
			ll now = 0;
			if (i >= 3) {
				now = ktd::G_query(a[i], a[i]) + pre[i - 1];
			}
			ktd::F_max(a[i - 1], now);
			
			g[i] = ktd::F_query(a[i + 1], a[i + 1]);
			
			if (i >= 2) {
				ktd::G_max(a[i - 1], g[i - 1] - pre[i]);
			}
		}
		cout << ktd::F_query(0, 0) << '\n';
	}

	return 0;
}
/*
4
8
3 5 2 5 1 2 1 4
3
1 2 1
4
1 2 3 4
15
5 3 7 2 4 13 11 6 5 5 3 5 12 8 13
*/
2024/11/8 18:47
加载中...