Bessie is a robovine, also known as a cowborg. She is on a number line trying to shoot a series of T (1≤T≤10**5) targets located at distinct positions. Bessie starts at position 0 and follows a string of C (1≤C≤10**5) commands, each one of L, F, or R:
● L: Bessie moves one unit to the left.
● R: Bessie moves one unit to the right.
● F: Bessie fires. If there is a target at Bessie's current position, it is hit and destroyed, and cannot be hit again.If you are allowed to change up to one command in the string to a different command before Bessie starts following it, what is the maximum number of targets that Bessie can hit?
解析:
先求所有的步骤整体平移-2,-1,0,+1,+2时可以击中多少个目标。答案在change[1]-change[5]。再用hit[1-5][位置]记录每个位置被击中了多少次。
然后i从左往右一位位求i之前已经固定,i改变以后,能击中多少次;
这样求完以后只需要把第i位固定带来的影响,更新到change和hit里就可以了。
i从左往右固定过程中,如果这一位是L和R只影响位置p;
但如果这一位是F,那么固定以后,就要更新hit里的次数。因为它-2 -1 +1 +2都不可能发生了。
所以更新了两部分内容。
1. 当前位置p,如果之后的F因为-2 -1 +1 +2击中过它,现在应该i已经固定,它一定会被这一次射击击中。所以把-2 -1 +1 +2以后的p位置的hit数都清0,change对应更新。
2. 当前位置p,-2 -1 +1 +2以后的位置的hit数减少1次,如果减到0就更新change
最终就是,确定会击中的数量cnt,和change数组维护的i之后的-2 -1 +1 +2以后的4种不同击中数量,根据L->R R->L之类的变化情况相加。
时间复杂度: O(n)
知识点:思维难题