Electro_Valkyrie's blog

By Electro_Valkyrie, 12 months ago, In English

You are given an array A of size N.

You must partition the array into one or more contiguous subarrays, with the following rules:

  1. Every element of A must belong to exactly one subarray.

  2. Each subarray must have length at least K.

The beauty of a subarray is defined as the maximum XOR value obtainable from any subset of the elements in that
subarray.

The amazingness of a partition is the sum of the beauties of all subarrays in the partition.

Your task is to determine the maximum possible amazingness of array A over all valid partitions.

Constraints : 1 <= N <= 100,000 , 1 <= K <= N , 1 <= $$$A_i$$$ <= 100,000

Any idea about how to solve this problem.

  • Vote: I like it
  • +10
  • Vote: I do not like it

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Link of the problem ?

»
12 months ago, hide # |
← Rev. 2  
Vote: I like it +11 Vote: I do not like it

Lets calculate $$$dp_i$$$ = maximum possible amazingness of $$$A_{1 \ldots i}$$$.

$$$dp_i = \max_{j=1}^{i-k}(dp_{j-1} + beauty(A_{j \ldots i}))$$$ (maybe I messed up some indexes by $$$\pm 1$$$, let it be an exercise for readers).

The idea is that for a fixed $$$i$$$, $$$beauty(A_{j \ldots i})$$$ changes at most $$$O(\log(C))$$$ times where $$$C=\max(A)$$$.

To find beauty of A, we can use xor basis. To quickly be able to find beauty of subarray of A, we can build sparse table over A where each table element will contain xor basis of a corresponding subsegment. Then to find a beauty of subsegment we split it into $$$2$$$ smaller segments for which we already know xor basis and merge them into one xor basis. Since merge of two xor basises takes $$$O(\log^2(C))$$$, total complexity for finding beauty of subsegment is $$$O(\log^2(C))$$$.

Then for each $$$i$$$ we can do binary search to find $$$O(\log(C))$$$ positions where beauty of $$$beauty(A_{j \ldots i})$$$ changes. Since each such position requires $$$O(\log(n))$$$ queries for beauty of subsegment, total complexity for calculating these indexes for all positions is $$$O(n\log(n) \log^3(C)) $$$. I'm pretty sure you can cut off at least one of four logs in this solution but the constant behind it is actually pretty good so it might be enough.

When for a fixed $$$i$$$ we found $$$O(\log(C))$$$ segments where $$$beauty(A_{j \ldots i})$$$ is constant all what's left to do is to query for a maximum $$$dp_j$$$ on each segment which is doable with segtree.

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Thanks for you time..i was also thinking in this way but haven't thought about how to find exactly those positions where XOR of segment(j...i) is changing because linear search could be very costly, but yeah it is pretty doable through binary search and using XOR basis over subsegments

    In last i think this dp is increasing so we don't need to use segment tree for finding the optimal postion, for each segment just use the largest j.