File failed to load: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/extensions/TeX/TeX/AMSmath.js

Educational Codeforces Round #105 1D Sokoban - Binary Search

Solution:

Classic binary search problem. We only consider the positive part. The negative part can be similar.

Let's consider to push boxes to bi. We binary search a with bi to find the largest j so that ajbi.

If there is a box at bi. Which means aj=bi. Then we plus one to the basic result (base_ans++). No need for other things because push all boxes to here can't be the best solution.

If there is no box at bi. Which means aj<bi. Then for all boxes between 0 and aj, we push it to bi,bi1,bi2,.... And then we binary search b with the number of boxes between 0 and aj (Which is j+1). Find the smallest k so that bkj+1. So the result of pushing all boxes to bi is ik+1+base_ans.

Enumerate i. We pick the maximum of results.

Code

Submission #114380700 - Codeforces
Codeforces. Programming competitions and contests, programming community

Tips:

Convert the negative part to the positive. And run the solution again. It can make the implementation easier.

DigitalOcean Referral Badge 蜀ICP备19018968号