CerealEater42's blog

By CerealEater42, history, 11 hours ago, In English

Codeforces Round 1096 (Div. 3) I can't find where this solution went wrong for Problem D: Palindromex...

For Problem D: Palindromex

Where could be my answer wrong? I checked 3 conditions

  1. I checked if the first 0 is in the center or not [Y 0 Y]
  2. I checked if the second 0 is in the center or not [Y 0 Y]
  3. I checked if a palindrome brackets the two zeroes or not (my logic was if I need to have 2 zeroes they must form a palindrome. If it's an odd palindrome then [Y 0 X 0 Y] or if even palindrome [Y 0 X X 0 Y].

(I take zero indexing)

#include<bits/stdc++.h>
#define p(x) cout<<x<<endl;
#define m(x) cout<<x<<" ";
#define ll long long
using namespace std;

int main (){
    ll t;
    cin>>t;

    while(t--) {
        ll n;
        cin>>n;
        ll n2 = n*2;
        vector<ll> v(n2);
        ll idx1=-1, idx2=-1;
        for(ll i=0;i<n2;i++){
            cin>>v[i];
            if(v[i]==0 && idx1==-1){
                idx1=i;
            } 
            else if (v[i]==0 && idx1>=0){
                idx2=i;
            }
        }

        // idx1 at middle
        ll left=0,right=0;
        vector<ll> arr1(n+1,0);
        arr1[0] = 1;
        left = idx1-1;
        right=idx1+1;
        while(left>=0 && right<n2){
            if(idx1==0){
                break;
            }
            if(v[left]==v[right]){
                arr1[v[left]] = 1;
            } else {
                break;
            }
            left--;
            right++;
        }
        // idx2 at middle
        left = 0, right=0;
        vector<ll> arr2(n+1, 0);
        arr2[0] = 1;
        left = idx2-1;
        right = idx2+1;
        while(left>=0 && right<n2){
            if(idx2==n2-1){
                break;
            }
            if(v[left]==v[right]){
                arr2[v[left]] = 1;
            } else {
                break;
            }
            left--;
            right++;
        }

        // combine
        ll diff = idx2+idx1;
        vector<ll> arr3(n+1, 0);
        arr3[0] = 1;
        if(diff&1){
            left = diff/2;
            right = left+1;
            while(left>=0 && right<n2){
                if(v[left]==v[right]){
                    arr3[v[left]] = 1;
                } else {
                    fill(arr3.begin(), arr3.end(), 0);
                    arr3[0] = 1;
                    break;
                }
                left--;
                right++;
            }
        } 
        else {
            ll mid = diff/2;
            arr3[v[mid]] = 1;
            left = mid-1, right= mid+1;
            while(left>=0 && right<n2){
                if(v[left]==v[right]){
                    arr3[v[left]] = 1;
                } else {
                    fill(arr3.begin(), arr3.end(), 0);
                    arr3[0] = 1;
                    break;
                }
                left--;
                right++;
            }
        }

        ll m1=0,m2=0,m3=0;

        for(ll i=0;i<=n;i++){
            if(arr1[i]==0){
                m1=i;
                break;
            }
        }

        for(ll i=0;i<=n;i++){
            if(arr2[i]==0){
                m2=i;
                break;
            }
        }

        for(ll i=0;i<=n;i++){
            if(arr3[i]==0){
                m3=i;
                break;
            }
        }

        p(max(m1,max(m2,m3)))
    }
    return 0;
}
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
9 hours ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

its wrong on case n = 5, a = [4, 0, 2, 1, 1, 2, 0, 3, 4, 3]

Max MEX is in arr3 (0 not the center), but you RESET the whole array if you find a mismatch, you should only do that if left > idx1

I haven't submitted that but I think that's the issue!

»
8 hours ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

The overall idea is correct; the problem lies in this line of code. fill(arr3.begin(), arr3.end(), 0); I guess your approach is to determine it as invalid and clear arr3 if the palindrome's interval cannot cover [idx1, idx2], but you forgot to add this check.

There's a simpler implementation: remove this line of codearr3[0] = 1;. Then you don't need to check whether to clear it, because if the palindrome cannot cover [idx1, idx2], arr3[0] will equal 0, which means mex=0. In this way, case 3 is the same as the implementation of the first two cases, only using different value of 'left' and 'right'. Then you can use a function like ans = fun(left, right) to unify them.