zoukankan      html  css  js  c++  java
  • Three Blocks Palindrome (hard version) CodeForces 1335E2

    The only difference between easy and hard versions is constraints.

    You are given a sequence aa consisting of nn positive integers.

    Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are aa and bb, aa can be equal bb) and is as follows: [a,a,,ax,b,b,,by,a,a,,ax][a,a,…,a⏟x,b,b,…,b⏟y,a,a,…,a⏟x]. There x,yx,y are integers greater than or equal to 00. For example, sequences [][], [2][2], [1,1][1,1], [1,2,1][1,2,1], [1,2,2,1][1,2,2,1] and [1,1,2,1,1][1,1,2,1,1] are three block palindromes but [1,2,3,2,1][1,2,3,2,1], [1,2,1,2,1][1,2,1,2,1] and [1,2][1,2] are not.

    Your task is to choose the maximum by length subsequence of aa that is a three blocks palindrome.

    You have to answer tt independent test cases.

    Recall that the sequence tt is a a subsequence of the sequence ss if tt can be derived from ss by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1,2,1,3,1,2,1]s=[1,2,1,3,1,2,1], then possible subsequences are: [1,1,1,1][1,1,1,1], [3][3] and [1,2,1,3,1,2,1][1,2,1,3,1,2,1], but not [3,2,3][3,2,3] and [1,1,1,1,2][1,1,1,1,2].

    Input

    The first line of the input contains one integer tt (1t1041≤t≤104) — the number of test cases. Then tt test cases follow.

    The first line of the test case contains one integer nn (1n21051≤n≤2⋅105) — the length of aa. The second line of the test case contains nn integers a1,a2,,ana1,a2,…,an (1ai2001≤ai≤200), where aiai is the ii-th element of aa. Note that the maximum value of aiai can be up to 200200.

    It is guaranteed that the sum of nn over all test cases does not exceed 21052⋅105 (n2105∑n≤2⋅105).

    Output

    For each test case, print the answer — the maximum possible length of some subsequence of aa that is a three blocks palindrome.

    Example

    Input
    6
    8
    1 1 2 2 3 2 1 1
    3
    1 3 3
    4
    1 10 10 1
    1
    26
    2
    2 1
    3
    1 1 1
    
    Output
    7
    2
    4
    1
    1
    3
    #include <bits/stdc++.h>
    typedef long long ll;
    using namespace std;
    const ll inf = 1e18;
    const int mod = 1000000007;
    const int mx = 100; //check the limits, dummy
    typedef pair<int, int> pa;
    const double PI = acos(-1);
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--)
    #define clr(a) memset(a, 0, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pair
    void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    int  m, n,t,x,k,ans=0,sum=0;
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        cin >> t;
        while (t--) {
            cin >> n;
            vector<int>a(n);
            re(i, 0, n) { cin >> a[i]; --a[i]; }
            const int M = 200;
            vector<vector<int>>pref(M, vector<int>(n + 1));
            vector<vector<int>>at(M);
            re(i, 0, M) {
                re(j, 0, n) {
                    pref[i][j + 1] = pref[i][j] + (a[j] == i);
                    if (a[j] == i)at[i].push_back(j);
                }
            }
            ans = 0;
            re(x, 0, M) {
                int u = pref[x][n];
                ans = max(ans, u);
                for (int c = 1; 2 * c <= u; c++) {
                    int from = at[x][c - 1];
                    int to = at[x][u - c];
                    for (int y = 0; y < M; y++) {
                        if (x != y) {
                            ans = max(ans, 2 * c + pref[y][to] - pref[y][from]);
                        }
                    }
                }
            }
            cout << ans << endl;
        }
    }
  • 相关阅读:
    NOIP2010提高组乌龟棋 -SilverN
    NOIP2009 提高组T3 机器翻译 解题报告-S.B.S
    controller向layout传值
    解决Yii2中刷新网页时验证码不刷新的问题
    关联表 重命名
    定义action的允许访问方式
    js 拷贝clone
    是否包含指定字符串
    indexOf()定义和用法
    直接借鉴的 ids拼接
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12697747.html
Copyright © 2011-2022 走看看