1870E - Another MEX Problem - CodeForces Solution


bitmasks brute force dp graphs shortest paths

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

#define int long long
#define endl '\n'
#define x first
#define y second

using namespace std;

const int N = 5010, M = N*2, mod = 998244353, INF = 0x3f3f3f3f3f3f3f3f,P = 131;

//typedef __int128 LL;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<long long , long long> PLL;
typedef pair<char,int> PCI;
typedef pair<int,char> PIC;
typedef pair<int,pair<int,int>> PIII;
typedef pair<string,int> PSI;
typedef pair<double,double> PDD;

int n,m;
int a[N];
bool f[N][N];
bool st[N][N];
bool st2[N];

void solve()
{
    cin>>n;
    for (int i=1;i<=n;i++)cin>>a[i];
    
   // vector<vector<bool>> f(n+10,vector<bool>(n+10));
    //vector<vector<bool>> st(n+10,vector<bool>(n+10));
    for (int i=0;i<=n;i++)
        for (int j=0;j<=n;j++)
            f[i][j]=st[i][j]=0;
    
    int ans = 0;
    f[0][0]=1;
    for (int i=1;i<=n;i++)
    {
        for (int j=0;j<=n;j++)st2[j]=0;
        for (int j=0;j<=n;j++)f[i][j]=f[i-1][j];
        //vector<bool> st2(n+10);
        //f[i]=f[i-1];
        int mex = 0;
        for (int j=i;j;j--)
        {
            st2[a[j]]=true;
            if(st2[mex])
            {
                while (st2[mex])mex++;
                if(st[j][mex])continue;
                st[j][mex]=true;
                for (int k=0;k<=j;k++)
                    if(f[j-1][k])
                    {
                        f[i][mex^k]=true;
                    }
            }
        }
        for (int j=0;j<=i;j++)
            if(f[i][j])ans = max(ans,j);
    }
    cout<<ans<<endl;
}

signed main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    //init(N-1);
    int T=1;
    //scanf("%lld",&T);
    cin>>T;
    while (T--) solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1585C - Minimize Distance
1506E - Restoring the Permutation
1539A - Contest Start
363D - Renting Bikes
1198D - Rectangle Painting 1
1023B - Pair of Toys
1725A - Accumulation of Dominoes
1675E - Replace With the Previous Minimize
839A - Arya and Bran
16B - Burglar and Matches
1625B - Elementary Particles
1725G - Garage
1725B - Basketball Together
735A - Ostap and Grasshopper
1183B - Equalize Prices
1481A - Space Navigation
1437B - Reverse Binary Strings
1362B - Johnny and His Hobbies
1299A - Anu Has a Function
1111A - Superhero Transformation
954A - Diagonal Walking
39F - Pacifist frogs
1451C - String Equality
386A - Second-Price Auction
1690E - Price Maximization
282B - Painting Eggs
440A - Forgotten Episode
233B - Non-square Equation
628B - New Skateboard
262B - Roma and Changing Signs