[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
**/




目录
相关文章
|
3月前
|
Java C++ Python
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-161 Abbott’s Revenge(C++写法)
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-161 Abbott’s Revenge(C++写法)
126 42
|
9月前
|
PHP
BugKu 矛盾 解题思路
BugKu 矛盾 解题思路
36 0
|
9月前
|
算法 测试技术
算法强化每日一题--倒置字符串
算法强化每日一题--倒置字符串
|
算法 Python
【完虐算法】「字符串-最长公共前缀」5种方法脑洞大开
最近在专题制作过程中遇到了**最长前缀公共子串**的问题,也是读者最近校招面试到的一个题目。为什么拿出这个来说呢? 可怕的是,他居然给了 5 种解题方法。 更可怕的是,因此他直接少了一轮面试,天哪!!
339 0
|
算法 C++
【数独 1】不回溯,试试候选数法1ms高效解数独谜题-C++实现(上)
【数独 1】不回溯,试试候选数法1ms高效解数独谜题-C++实现(上)
108 0
|
算法 C++
【数独 1】不回溯,试试候选数法1ms高效解数独谜题-C++实现(下)
【数独 1】不回溯,试试候选数法1ms高效解数独谜题-C++实现(下)
84 0
每天一道 CodeForces 构造/思维题 难度1500 (day3)
每天一道 CodeForces 构造/思维题 难度1500 (day3)
|
人工智能
每天两道 CodeForces 构造/思维题 (day5)
每天两道 CodeForces 构造/思维题 (day5)
每天两道 CodeForces 构造/思维题 (day5)
每天一道 CodeForces 构造/思维题 (day2)
每天一道 CodeForces 构造/思维题 (day2)