cow and treats

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

first insight

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 ith 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(n2log2n) 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		
	}

optimization

i haven't come up with a idea about how to narrate the following story. so i think may be i have screwed up the script in this part.

in the course of iteration you will definitely observe something boring are repeated with the ambition of optimization. let grass[i] be that kind of cows that enjoy that kind of grass in i th grid. you actually do see that when we move from the current grid to the next only two kinds of cows are needed to be recalculated:

  • grass[i+1]. obviously. maybe a lucky cow will sleep here this time.
  • grass[i]. it's not that obvious and you might feel puzzled since that kind of cows have been considered and no reason need it be calculated. well it does. recall that we have delt with that cows but specifically bacause we assume the situation that a cow would precisely stay here, which is a little bit different from where cows are not limited in that way.

again, please note that deal with that kind of cows specifically and deal with other kinds of cows are two different kind of functions

with theory above we can implement like this:

for every grid of grass 	
{
	move away sum and ways that is contributed by that certain kind of cows before 
	if it is reachable by any cow:
	{
		fix that cow and deal with that kind of cows specifically
	}
	add up sum and ways that is contributed by that certain kind of cows before 
}

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. but next time we will see.

原文地址:https://www.cnblogs.com/reshuffle/p/12503119.html