# 34. 结对编程
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', (line) => {
rl.on('line', (line) => {
const levels = levelsStr.split(' ').map(Number);
console.log(countTeams(parseInt(n,10), levels));
});
});
function countTeams(n, levels) {
let ans = 0;
let smallLeft = new Array(n).fill(0);
let greatLeft = new Array(n).fill(0);
let smallRight = new Array(n).fill(0);
let greatRight = new Array(n).fill(0);
for(let i=1; i<n; i++) {
for(let j=0; j<i; j++) {
if (levels[j] < levels[i]) {
smallLeft++;
} else if(levels[j] > levels[i]) {
greatLeft++;
}
}
}
for(let i=n-2; i>=0; i--) {
for(let j=n-1; j>i; j--) {
if (levels[j] < levels[i]) {
smallRight++;
} else if(levels[j] > levels[i]) {
greatRight++;
}
}
}
for(let i=0;i<n; i++) {
ans += smallLeft[i]*greatRight[i] + greatLeft[i]*smallRight[i];
}
return ans;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
← 33. 符号运算 35. 考古学家考古问题 →