NOIP2015 PJ T3,T4 Solution
By-jim H
Sum ... ... ... ... ... ... ... ... ... ... ... ... .... ... .... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ....... 2
Salesman ... ..... .... ... ..... ..... .... ..... ..... ........, ... and .... .....-........... 3
Sum
40%
And the color[z]=color[x],x is the same as the parity of Z
Complexity O (N2)
70%
It means I don't know what this part is for.
100%
Consider optimization
For each x,z we can disassemble the formula
X*NUM[X]+Z*NUM[Z]+X*NUM[Z]+Z*NUM[X]
For each z, we can add a valid prefix with him while doing the edge, plus just fine.
Complexity O (N)
Salesman
We can consider greed.
Because we need to walk the fatigue value, is to go to the far right of the person's fatigue value, the left all have no effect on him.
We set the right side for last.
Obviously this last one divides the whole street into the left and right parts, part of the walking fatigue value has no effect, part of the impact.
It can be seen that, with the increase of X, the best last must be monotonous and non-decreasing.
Because he this last is our combined on both sides of the best value selected, choose the other must not his excellent.
If there are multiple identical values, we can completely greedy to choose the leftmost one. Because he has the least effect on the answer (in fact all the same), but also to maintain the last monotonous non-reduction of
We can maintain the maximum value with two heaps, each x, to see if the new one is on the left or the right, and if you add ans to the left, then delete the total left heap. If you are on the right, add the answer and change last to this number. At this point, we make it from the old last+1 to the new last-1 this part becomes the walking fatigue value has no effect, so we can put each of them from the right side of the heap to delete, add to the left heap is good.
The complexity of the heap is log n, and then the enumeration x is N, and then all the changes in the last operation all add up to N, the complexity is O (2N log N)