CodeForces 1335E2 : Three Blocks Palindrome (hard version) 二分 + 思维

传送门

题目描述

CodeForces 1335E2 : Three Blocks Palindrome (hard version) 二分 + 思维

分析

最近人有点颓废,状态不好,菜到1800的题都切不出来,唉,不能再消沉了

这个题我们贪心的去想,我们去过枚举了前面一个区间的话,那么最后那个区间的左端点一定是越靠右越好,这样我们中间可以选择的部分就越多,所以我们可以去枚举第一个区间的右端点,二分找第三个区间的左端点,然后中间那部分因为值域比较小可以去枚举找出现次数最多的数字即可

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
    char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
    while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int a[N][210];
int b[N];
int n;

int main() {
    int T;
    read(T);
    while(T--){
        read(n);
        for(int i = 1;i <= n;i++){
            read(b[i]);
            for(int j = 1;j <= 200;j++) a[i][j] = a[i - 1][j];
            a[i][b[i]]++;
        }
        int ans = 0;
        for(int i = 0;i <= n;i++){
            int l = 0,r = n + 1;
            while(l < r){
                int mid = (l + r + 1) >> 1;
                if(a[n][b[i]] - a[mid - 1][b[i]] >= a[i][b[i]]) l = mid;
                else r = mid - 1;
            }
            if(l <= i || a[n][b[i]] - a[l - 1][b[i]] != a[i][b[i]]) continue;
            int t = 0;
            for(int j = 1;j <= 200;j++) t = max(t,a[l - 1][j] - a[i][j]);
            ans = max(ans,t + a[i][b[i]] * 2);
        }
        di(ans);
    }

}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/



上一篇:Codeforces Round #721 (Div. 2)


下一篇:C. A-B Palindrome