I was trying this problem SFLIP but I am not able to come up with a solution. Is the intended solution requires a segment tree approach ? I would be happy if somebody helps me in solving this problem ? Thank you in advance.

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

1 | tourist | 3778 |

2 | Benq | 3592 |

3 | ecnerwala | 3521 |

4 | Um_nik | 3423 |

5 | jiangly | 3375 |

6 | Petr | 3342 |

7 | Radewoosh | 3337 |

8 | scott_wu | 3313 |

9 | maroonrk | 3265 |

10 | yosupo | 3259 |

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

1 | Errichto | 201 |

2 | 1-gon | 200 |

3 | rng_58 | 194 |

4 | SecondThread | 193 |

5 | awoo | 187 |

6 | vovuh | 183 |

7 | Um_nik | 182 |

8 | antontrygubO_o | 177 |

9 | Ashishgup | 175 |

10 | -is-this-fft- | 171 |

I was trying this problem SFLIP but I am not able to come up with a solution. Is the intended solution requires a segment tree approach ? I would be happy if somebody helps me in solving this problem ? Thank you in advance.

↑

↓

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jan/21/2021 20:40:27 (i2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

It seems like it's possible to use a segment tree, but it's not necessary. A key insight that admits a priority queue based approach is that if you've flipped some segment, then later on find out that you can do better by not flipping it, you can "fix" the end result and pretend you never flipped that segment at all.

Say you flip 3-7 here:

becomes

But you later find out that you actually want to flip 1-3 and 7-9:

It's possible to quickly find and effect this "fix" operation using a priority queue.

Actually, if you consider that you can just treat all contiguous runs of numbers of the same sign as single numbers, you can see that this is equivalent to finding a shortest augmenting path in a weighted bipartite graph.

See http://codeforces.com/problemset/problem/280/D and the APIO task "Backup" for more.