4489: M. Youth Finale
内存限制:512 MB
时间限制:3 S
标准输入输出
题目类型:传统
评测方式:Special Judge
上传者:
提交:0
通过:0
题目描述
Finales are born to be exciting. Performers play hard to draw audiences' attention and then take a perfect curtain call. As the last problem and the finale of the problem set, however, we want you to recall a simple algorithm. Like me, it may be the first algorithm you've learned, called Bubble Sort.
Given a permutation of length n, as you might know, Bubble Sort runs in Ω (n2) in the worst case. It's quite a traditional idea to count the number of calls of "swap" in the algorithm. As you are stronger now, you want to count that number in a dynamic permutation with the following events that might happen:
All you need to do is to output the number of "swap" that would be called if we sort the permutation with the above Bubble Sort code after each operation.
Given a permutation of length n, as you might know, Bubble Sort runs in Ω (n2) in the worst case. It's quite a traditional idea to count the number of calls of "swap" in the algorithm. As you are stronger now, you want to count that number in a dynamic permutation with the following events that might happen:
-
Reverse the permutation, meaning that the permutation is replaced with
p′={pn,pn−1,…,p2,p1}.
-
Shift the permutation to the left by 1, meaning that the permutation is replaced with
p′={p2,p3,…,pn,p1}.
All you need to do is to output the number of "swap" that would be called if we sort the permutation with the above Bubble Sort code after each operation.
输入格式
The first line contains two integers n, m (1 <= n <= 3 × 105, 1 <= m <= 6 × 105), denoting the length of permutation and the number of operations.
The second line contains n integers separated by spaces, and the i-th denotes the initial pi.
The third line contains a single string containing m letters consisting of 'R' and 'S'. The i-th letter denotes the i-th operation, where 'R' or 'S' denotes the Reverse or Shift operation, respectively.
It's guaranteed that p forms a correct permutation of 1, 2, ..., n.
The second line contains n integers separated by spaces, and the i-th denotes the initial pi.
The third line contains a single string containing m letters consisting of 'R' and 'S'. The i-th letter denotes the i-th operation, where 'R' or 'S' denotes the Reverse or Shift operation, respectively.
It's guaranteed that p forms a correct permutation of 1, 2, ..., n.
输出格式
In the first line, print the number of "swap" would be called when Bubble Sort the initial p.
In the second line, print a single string of m digits. The i-th denotes the number of "swap" would be called to Bubble Sort the permutation, modulo 10.
In the second line, print a single string of m digits. The i-th denotes the number of "swap" would be called to Bubble Sort the permutation, modulo 10.
输入样例 复制
5 10
5 4 3 2 1
SSSSRSSSSR
输出样例 复制
10
6446466400