cow and treats

https://codeforces.com/problemset/problem/1307/E

consideration of orders is needless

不需要考虑牛吃草的顺序.首先,如果能确定合法的顺序,则不同种类的牛相互之间一定不会冲突,因为他们最后一定停靠在不同的格子里。
然后,只要两边的牛不相遇,那么每次优先先让能走得更远的牛进去吃草,一定不会引起冲突。一种合法的顺序必然存在且唯一。
first of all, if FJ orders optimally(which means cows moving farther move first), two different kinds of cows will not bother one another since they have different taste and will finally fall asleep in different places, which suggests that order doesn't matter because of its certainty.

it leads to a facinating conclusion: since the only thing that matters is how FJ chooses a subset of cows, that's what we are going to focus on. and when we are to translate that idea into code, we can just simply iterate every grid of grass as long as it can be walked through only by the rightmost cow come from the left size. note that the limitation ' as long as ' is vital because only in this way can we build up a one-to-one correspondence between what we iterate and one certain kind of distribution schemes.

in any one of the optimal ways, no more than two of a certain kind of cows are given the chance to enjoy dinner simultaneously

在一个方案中每种牛只会出场零到两次。因为每一端一次只能安排吃同一种农作物的牛,或者不安排这种牛。
that's because FJ can only choose one of a certain kind of cows at either side, or at least one of them is guaranteed to be upset。

that's inspiring, since there are not so many cases to be considered.we can classify all the situation like this:

  1. no cow from either side fall asleep or the minimum hunger of this kind of cows can not be satisfied from either direction. then the contribution to the sum of cows asleep is 0 and to the number of ways i would say is 1.
  2. only x cows from either side (left or right, whatever) fall asleep or several cows of this kind can be satisfied from only one direction. then the contribution to the sum of cows asleep is 1. and to the number of ways is x.
  3. we are glad to find that any one of x cows from the left side can fall asleep while any one of y cows from the right side can have their dreams. but in the middle of exitement the calculation is a little bit more tricky.
    let's assume x<=y without losing the university.
    • without any constraint condition, the contribution to the sum of cows asleep is obviously 2 and to the number of ways is *x times (y-1). it may take some time for you to think about it but it more or less adhere to your institution.
    • there is one situation where things become different. when we are iterating i ^th^ grid of grass, it means exactly one of a certain kind of cows will fall asleep in exactly this grid (or the boundary) , in this way the left side is fixed, and we need to modify our method moderately. let's assume that any one of y cows from the right side can enjoy their leisure time.
      • if that cow fixed in the i^th^ grid can have been a member of these y cows, the contribution to sum 2 and to ways y-1
      • if that cow can not, sum 2 and ways y

so here's the O(n^2^log~2~n) solution:
(when talk about 'deal with' i mean to calculate it and record it and finnally update the answer)

for every grid of grass
    if it is reachable by any cow:
    {
        fix that cow and deal with that kind of cows specifically
        deal with other kinds of cows       
    }

the theory is simple and the preocess is direct but i unbelievably have to toil on and off for several days in implementation and difficulty2400 is so much for me
now how to speed up the solution to complexity of O(nlog~2~n)?
to be continued....

猜你喜欢

转载自www.cnblogs.com/reshuffle/p/12503119.html
今日推荐