# | User | Rating |
---|---|---|

1 | Benq | 3783 |

2 | jiangly | 3666 |

3 | tourist | 3611 |

4 | Um_nik | 3536 |

5 | inaFSTream | 3477 |

6 | fantasy | 3468 |

7 | maroonrk | 3464 |

8 | QAQAutoMaton | 3428 |

9 | ecnerwala | 3427 |

10 | Ormlis | 3396 |

# | User | Contrib. |
---|---|---|

1 | Um_nik | 184 |

2 | adamant | 178 |

3 | awoo | 177 |

4 | nor | 169 |

5 | maroonrk | 165 |

6 | -is-this-fft- | 164 |

7 | antontrygubO_o | 152 |

8 | ko_osaga | 151 |

9 | dario2994 | 150 |

10 | SecondThread | 149 |

↑

↓

Codeforces (c) Copyright 2010-2023 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/04/2023 20:06:29 (k1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

The full brute-force solution works in $$$O((n-1)\cdot (n-3)...\cdot 3\cdot 1)$$$ which should fit in 1 seconds (at least I hope in Google's servers).

I tried. Giving TLE. Please help.

Question on Hackerearth

My attempt DP+Bitmasking

please explain your approach.

There are N (even) total elements which have to paired up. For each i-th element we can pair it up with a j-th element that has not yet been paired up, and pick the one that results in the maximum (or minimum) value. To represent which elements are already paired up we can use a bitmask of length N. As N <= 20, this is feasible. Notice that the order of picking pairs does not affect the answer. That is, if we pair up {1,2,3,4} as (1,2) first and then (3,4) or (3,4) first and then (1,2), the answer does not change. We can now build a brute force recursion (which we can memo later). Alternatively this can be done with bottom-up DP.

Let's try for the maximum value that we can obtain, the only difference between this and the minimum one is we will try to pick the pair which minimizes the total value.

We currently want to pair up position i with some unused position j. In our mask, if j-th bit is 1, then j-th position has been paired up and we cannot pick it. If j-th bit is 0, we can try to pair up our position i with position j.

What is the time complexity of this approach? I though this has a time complexity of (n-1)*(n-3)*...1

No more than N*2^N I think.

This is one of those questions where I think the recursive approach is better then the iterative as in every transition the number of set bits in the mask will be even. It's most likely be of complexity $$$O(2^{n/2} \times n)$$$.

Second solution is hard to implement, we can build a weighted graph between index i and all other indexes the weight will be what we will gain from that (+X) or (-X), then find max flow. for minimizing we can reverse the sign of the weight (+ -> -) and vise verse. the find max flow again. i'm not sure if this works well.

I done it with some bitmask + dp can be said

let represent every index as a bit and initially the mask = 111111..n times

This is decomposed to (n-2 times) with 2 of them processed And so on

We create dp since there are some common point in recursion tree we are not gonna solve repeatedly Check my code here

https://www.hackerearth.com/submission/72546942/