Let,

tit_i == Number of battles on iith day == ni{n - i}

wiw_i == Number of wins of iith fighter on iith day

lil_i == Number of defeated battle of iith fighter on iith day =tiwi=niwi{= t_i - w_i = n - i - w_i}

So, amount of money, a bettor can win by betting on iith day =wili=wi(niwi)=2×win+i{= w_i - l_i = w_i - (n - i - w_i) = 2 \times w_i - n + i}

Here, we can calculate wiw_i by counting the number of inversions of iith element of the given array which can be found by Binary Indexed Tree or Segment Tree while iterating from the nnth element to the 11st element of the array. We can use the coordinate compression technique to compress the array values so that the array values become smaller.

Now, the maximum amount of money can be found by using dynamic programming.

We have to run a 2D2D dpdp of size n×kn \times k where dp[i][j]dp[i][j] is the maximum amount of money after ii days and total jj bets are done previously.

As, a bettor has to bet on exactly 22 days in a row, on each day there are exactly 22 options:: do not bet and go to the next day or bet on iith and (i+1)(i+1)th day and go to (i+3)(i+3)th day as go to (i+2)(i+2)th day is forbidden if a bettor bets on iith day. So, the transition of dpdp states will be as below.

dp[i][j]=max(dp[i+1][j],dp[i+3][j+2]+money[i]+money[i+1]){dp[i][j] = max(dp[i+1][j], dp[i+3][j+2] + money[i] + money[i+1])}

Complexity: O(nlogn+n×k)\mathcal{O}(nlogn + n\times k)

[Note: The iith element and the jjth element forms an inversion if ai>aja_i > a_j and i<ji<j. The number of inversions of the iith element is the count of all jj for which the iith element and the jjth element forms an inversion.]

Statistics

88% Solution Ratio
BrehamPieEarliest, Dec '21
S_SubrataFastest, 0.2s
S_SubrataLightest, 9.3 MB
serotoninShortest, 958B
Toph uses cookies. By continuing you agree to our Cookie Policy.