[UCF HSPT 2021] Sharon’s Sausages | 思维 暴力

简介: 题意:给出一个长度为n nn的序列,问有多少种方案数使得挑选四个位置满足ABBA这种情况即 1 ≤ a < b < c < d ≤ n 1 \leq a < b < c < d \leq n1≤a<b<c<d≤n,且满足① n u m [ a ] = n u m [ d ] n u m [ b ] = n u m [ c ] num[a] = num[d] \ \ num[b] = num[c]num[a]=num[d] num[b]=num[c]② n u m [ a ] ≠ n u m [ b ] num[a] \neq num[b]num[a]

Description


Sharon is a food-loving individual, and recently he has discovered that he loves the taste of different types of sausages!


When he was invited to the HSPT potluck, he decided that he would contribute 4 sausages for the cause in order to spread his love for sausages. Sharon also loves symmetry, so he chose the 4 sausages in such a way that the first and fourth sausages have the same length, and the second and third sausages have the same length.


While on his way to the potluck, Sharon came across a sausage field, which consisted of many sausages lined up in a row (how glorious!). To his horror, though, he dropped his 4 sausages into the field and now they’re lost! Luckily he explained the situation to the owner of the sausage field and he has allowed Sharon to choose 4 sausages from the field. Sharon doesn’t remember the lengths of the original sausages that he chose, but he remembers that he dropped the first sausage to the left of the second, the second to the left of the third, and the third to the left of the fourth. Sharon now wants to know how many candidates there are for choosing 4 sausages from the field such that his conditions are met. Can you help him?


Given n sausage lengths, output the number of ways to choose 4 of them with positions a,b,c,d such that 1 ≤ a < b < c < d ≤ n 1≤a<b<c<d≤n1≤a<b<c<d≤n, while also making sure that l e n g t h a = l e n g t h d lengtha=lengthdlengtha=lengthd and l e n g t h b = l e n g t h c lengthb=lengthclengthb=lengthc.


Input


The first line of the input contains a single, positive integer, p, representing the number of potlucks in which Sharon is invited. Each potluck is then defined on two lines. The first line of each potluck contains a single integer, n (4 ≤ n ≤ 1 0 5 4≤n≤10^54≤n≤10

5

 ), representing the number of sausages in the sausage field. The next line contains n space-separated integers, lengthi (1 ≤ l e n g t h i ≤ 100 1≤length_i≤1001≤length

i

≤100), representing the length of the i th sausage, respectively.


Output


Output the number of ways to select 4 sausages that meet Sharon’s requirement.


Samples


Input Copy


2

8

2 1 3 4 2 1 2 3

8

1 3 3 7 1 3 3 7


Output


2

3


题意:


给出一个长度为n nn的序列,问有多少种方案数使得挑选四个位置满足ABBA这种情况

即 1 ≤ a < b < c < d ≤ n 1 \leq a < b < c < d \leq n1≤a<b<c<d≤n,且满足

① n u m [ a ] = n u m [ d ]    n u m [ b ] = n u m [ c ] num[a] = num[d] \ \ num[b] = num[c]num[a]=num[d]  num[b]=num[c]

② n u m [ a ] ≠ n u m [ b ] num[a] \neq num[b]num[a]

=num[b]


对于ABBA这种,因为不能选同一个位置,我们暂且把他们看作是不同的,即A1 B1 B2 A2

然后我们暴力枚举B2

然后对于每一个A 属于 [1,100],此时的贡献都应该 + 前面<A1,B2> 的个数 * A2的个数,(A1 = A2),这里的贡献通过乘法原理可以得出

为了算出这个贡献,我们要开三个数组

preCnt[x] -> 用来记录前面有多少个x

bakCnt[x] -> 用来记录后面有多少个x

pairCnt[a][b] -> 用来记录前面有多少对<a,b>


具体细节


① 因为我们是从前向后遍历数组元素,所以说要提前预处理出bakCnt[],在输入的时候顺便记录一下即可

② 因为bakCnt[]记录的是后面的个数,所以不包括当前位置,所以在遍历到这个数的时候,bakCnt[a[i]] - 1

③ 在计算完贡献之后,一定要加上此时这个位置i ii对pairCnt[][] 的贡献

④ 最后对preCnt[a[i]] + 1 意思是,遍历完这个元素之后,对于后面的每一个位置,这个数的个数都增加了1


时间复杂 sum(n) * 100


ac_code:


#define Clear(x,val) memset(x,val,sizeof x)
int n;
int a[maxn];
int preCnt[101];
int bakCnt[101];
ll pairCnt[101][101];
int main() {
    int _ = read;
    while(_ --) {
        Clear(preCnt, 0);
        Clear(bakCnt, 0);
        Clear(pairCnt, 0);
        n = read;
        for(int i = 1; i <= n; i++) {
            a[i] = read;
            bakCnt[a[i]] ++;
        }
        ll ans = 0LL;
        for(int i = 1; i <= n; i++) { /// A1 B1 B2 A2  -> B2
            int B2 = a[i];
            bakCnt[B2] --;
            for(int A1 = 1; A1 <= 100; A1 ++) {
                int A2 = A1;
                ans += 1LL * pairCnt[A1][B2] * 1LL * bakCnt[A2] * 1LL;///
            }
            for(int A1 = 1; A1 <= 100; A1 ++) {
                pairCnt[A1][B2] += preCnt[A1];
            }
            preCnt[B2] ++;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
/**
2
8
2 1 3 4 2 1 2 3
8
1 3 3 7 1 3 3 7
**/




目录
相关文章
|
13天前
|
算法
枚举算法:解决问题的穷举之道(一)
枚举算法:解决问题的穷举之道(一)
|
13天前
|
算法
枚举算法:解决问题的穷举之道(二)
枚举算法:解决问题的穷举之道(二)
|
13天前
|
Java C++ Python
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-161 Abbott’s Revenge(C++写法)
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-161 Abbott’s Revenge(C++写法)
127 42
|
9月前
|
PHP
BugKu 矛盾 解题思路
BugKu 矛盾 解题思路
37 0
|
9月前
Bugku CTF Simple_SSTI_1 解题思路
Bugku CTF Simple_SSTI_1 解题思路
57 0
|
9月前
Bugku CTF Simple_SSTI_2 解题思路
Bugku CTF Simple_SSTI_2 解题思路
77 0
|
9月前
Bugku CTF alert 解题思路
Bugku CTF alert 解题思路
43 1
|
9月前
|
JavaScript 前端开发
Bugku CTF web 你必须让他停下来 解题思路
Bugku CTF web 你必须让他停下来 解题思路
49 2
|
10月前
|
算法
算法思维之穷举法
算法思维之穷举法
|
11月前
|
算法 C语言
【算法】一篇文章弄清楚KMP算法的实现
【算法】一篇文章弄清楚KMP算法的实现
67 0

热门文章

最新文章