#P16236. [蓝桥杯 2026 省 B] LQ 聚合

    ID: 18270 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及 上传者: 标签>数学贪心前缀和2026蓝桥杯省赛

[蓝桥杯 2026 省 B] LQ 聚合

Problem Description

In the year 2056, an expedition team discovered a signal transmission tower deep inside a crater on the far side of the Moon. Its core console is continuously flashing a particle sequence of length NN.

Each position in the sequence is strictly defined as an LL-type particle, a QQ-type particle, or an unknown state ??, blurred by the erosion of time. These particles will be injected into the reaction field one by one, and the stability of the field depends on the number of “LQLQ aggregations” in the sequence. This number is defined as the count of all pairs (i,j)(i, j) satisfying 1≤i<j≤N1 \le i < j \le N, where the ii-th position is LL and the jj-th position is QQ.

To restart this dormant giant tower, the expedition team needs to repair all ?? in the sequence into definite LL or QQ.

Now, please compute the maximum possible number of “LQLQ aggregations” among all possible repair plans.

Input Format

The first line contains an integer NN, representing the length of the particle sequence.

The second line contains a string of length NN, consisting only of characters L, Q, and ?, representing the currently detected state of the particle sequence.

Output Format

Output one integer, representing the maximum number of “LQLQ aggregations” that can be obtained after replacing all ? with L or Q.

5
??L??
6

Hint

Sample Explanation

One optimal strategy is to repair the sequence into LLLQQ. Then, the first 33 L and the last 22 Q can produce 3×2=63 \times 2 = 6 aggregations in total.

Test Case Scale and Assumptions

For 30%30\% of the test cases, the number of ? in the string does not exceed 1010.

For all test cases, 2≤N≤1052 \le N \le 10^5.

Translated by ChatGPT 5