Awkwardness Minimization

All submissions for this problem are available.
You are teaching a class of $N$ students. Today, during the morning prayer, all the students are standing in a line. You are given a string $s$ with length $N$; for each valid $i$, the $i$th character of this string is 'b' if the $i$th student in the line is a boy or 'g' if this student is a girl. The *awkwardness* of this configuration of students is defined as the sum of pairwise distances between each boy and girl. The distance between the $i$th and $j$th student in the line is $i  j$. Formally, the awkwardness is $$\sum\limits_{i=1}^N \, \sum\limits_{j=i+1\\ s_j \neq s_i}^N (j  i)\,.$$ As the teacher, it is your responsibility to reorder the students in such a way that the awkwardness of the resulting configuration is the smallest possible. You may reorder the students in the line in any way you want. Find the minimum awkwardness that can be achieved. ### Input  The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.  The first and only line of each test case contains a single string $s$. ### Output For each test case, print a single line containing one integer  the minimum possible awkwardness. ### Constraints  $1 \le T \le 10^5$  $1 \le s \le 10^5$  each character of $s$ is either 'b' or 'g'  the sum of $s$ over all test cases does not exceed $4 \cdot 10^6$ ### Example Input ``` 3 gb bgg bbgg ``` ### Example Output ``` 1 2 6 ``` ### Explanation **Example case 1:** The total awkwardness is $1  0 = 1$, no matter how you arrange the students. **Example case 2:** Initially, the awkwardness is $1  0 + 2  0 = 1 + 2 = 3$. However, you can move the boy between the two girls, i.e. choose the configuration "gbg". Here, the awkwardness is $1  0 + 2  1 = 1 + 1 = 2$.Author:  dpraveen_adm 
Tags  dpraveen_adm 
Date Added:  12102019 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, SQL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 