[NOIP simulation test]: sushi (violence)

Title Description

Small $ c $ is a $ oier $. Recently, he found that his data structures like school silly. Because he met in the brush sucker title of a data structure that, forced to use a balanced tree to solve, time card with $ AC $. To this end, he was mercilessly ridiculed a lot. So the young $ c $ find a lot of data structure that do.

Yesterday, a small $ c $'re eating sushi, sushi plate suddenly found that many were huddled in a circle, these sushi with red also blue. Due to the small c interleaved color look very unhappy, want some operations, such that all of the red sushi continuous area formed of a blue sushi also formed a continuous area. If every small $ c $ can only swap adjacent two sushi, how many steps you can reach a minimum of $ c $ small request? Because he did question to do more, we have a little head is not clear, so this problem on to you.


 

Input Format

The first line of a number $ T $, represents the number of data sets.
Then $ T $ rows, each row line of the string $ B $ and $ $ R & lt composition, expressed blue $ B $, $ $ R & lt represents red. The first $ i $ the number of characters to describe the color of the $ i $ clockwise plate of sushi. Note that the last one and the first $ 1 $ sushi plate of sushi are adjacent.


 

Output Format

For each test, the output line represents the minimum number of exchanges.


 

Sample

Sample input

1
BBRBBRBBBRRR

Sample Output

5


 

Data range and tips

Sample Description:

The following described steps exchange:
exchange positions 2, sushi at position 3;
switching position 1, sushi at the position 2;
exchange position 6, sushi at position 7;
exchange position 7, sushi on position 8;
exchange position 8, sushi at position 9;

data range:

$T\leqslant 10$

$n\leqslant 1,000,000$


 

answer

A algorithm:

Search explosion, enumerate all moves. Can be obtained ranging from 0-20 minutes.

Algorithms II:

It notes that each state can actually be compressed. Therefore, when searching for small data recorded in the state, with minimal to heavy notation for data may be slightly larger, for each state valuation IDA * search is performed with the acceleration. You can get 20-40 points range.

Algorithms III:

将环上的问题转化为序列上,我们只需要将环复制一遍即可转化为序列。

那么,考虑这样一个问题,对于最优解,一定存在一个点使得所有的交换都不经过这个点(它的左右不交换),下面简称这个点为断点。

这样我们就可以枚举断点,答案即为把每个$B$挪到两端,反之同理。

然后在进行统计答案,比较每一个断点哪个最优。

时间复杂度:$\Theta (n^2)$。

期望得分:40分。

算法四:

考虑决策的单调性问题,上面$\Theta (n^2)$的算法中,我们需要再枚举一个点,这个点左端的$B$都移向左端,右端的都移向右端,那么就满足了一个$\bigcup$型函数。

显然可以进行三分求解。

时间复杂度:$\Theta (n\log_{\frac{3}{2}}n)$。

期望得分:65分。

 

Guess you like

Origin www.cnblogs.com/wzc521/p/11246907.html