1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <utility> #include <vector> #include <istream> #include <map> #include <cmath> #include <stack> #include <set> #include <queue> #include <cstring> #include <string> #include <fstream> #define ll long long #define maxn 300005 #define mdl 1000000007 #define clr(a,n) for(int i=0;i<n;i++)a[i]=0 #define cfast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define pll pair<ll,ll> #define pii pair<int,int> #define inc(i,a,n) for(int i=a;i<n;i++) #define vset(a,n,m) for(int i=0;i<n;i++)a[i]=m; #define endl '\n' #define PI 3.14159265358979 using namespace std; ll gcd(ll a, ll b) { if (a < b)swap(a, b); if (b == 0)return a; return gcd(b, a % b); } ll cpow(ll x, ll n) { ll ans = 1; while (n > 0) { if (n & 1) ans = (ans * x) % mdl; x = (x * x) % mdl; n >>= 1; } return ans; } ll getpos(int p, ll n) { return n / cpow(10, p - 1) % 10; } ll getwei(ll n) { int res = 0; while (n) { res++; n /= 10; } return res; }
int a[5005],dp[5005][5005]; int cnt[5005]; vector<int> canto[5005]; int main() { cfast; int t = 1; cin >> t; while (t--) { int n; cin >> n; inc(i, 1, n + 1) { canto[i].clear(); cin >> a[i]; } inc(i, 1, n + 1) { inc(j, 1, n + 1) { cnt[j] = 0; } int mx = 0; inc(j, i, n + 1) { cnt[a[j]]++; mx = max(mx, cnt[a[j]]); if (mx <= (j - i + 1) / 2&&(j-i+1)%2==0) { canto[j].push_back(i); } } } inc(i, 1, n + 1) {
inc(j, 0, n + 1) { dp[i][j] = 0; } } inc(i, 1, n + 1) { int mx = 0; if (a[i] == a[i - 1]||i==1) { if(dp[a[i]][i-1]==0&&i!=1){} else { mx = dp[a[i]][i - 1] + 1; dp[a[i]][i] = dp[a[i]][i - 1] + 1; } } inc(j, 0, canto[i-1].size()) { int to = canto[i-1][j]-1; if (to == 0 || a[to] == a[i]) { if (dp[a[i]][to] == 0 && to != 0)continue; mx = max(mx, dp[a[i]][to] + 1); } } dp[a[i]][i] = mx; } int ans = dp[a[n]][n]; inc(j, 0, canto[n].size()) { int to = canto[n][j] - 1; inc(i, 1, n + 1) { ans = max(ans, dp[a[i]][to]); } } cout << ans << endl; } }
|