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




目录
相关文章
|
4月前
|
算法
LeetCode回文数(暴力解,求更好的思路)
这篇博客讨论了如何判断一个整数是否为回文数,提供了暴力解法的代码,并寻求更优的算法建议。
68 1
LeetCode回文数(暴力解,求更好的思路)
|
8月前
|
存储 人工智能 决策智能
每日一题——博弈论(枚举与暴力)
每日一题——博弈论(枚举与暴力)
49 1
每日一题——博弈论(枚举与暴力)
|
Web App开发 PHP
fileclude-CTF 解题思路
fileclude-CTF 解题思路
125 2
|
9月前
|
Java C++ Python
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-161 Abbott’s Revenge(C++写法)
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-161 Abbott’s Revenge(C++写法)
168 42
Bugku CTF Simple_SSTI_2 解题思路
Bugku CTF Simple_SSTI_2 解题思路
129 0
Bugku CTF Simple_SSTI_1 解题思路
Bugku CTF Simple_SSTI_1 解题思路
102 0
算法强化每日一题--排序子序列
算法强化每日一题--排序子序列
|
算法 测试技术
蓝桥算法_单词分析-wordAnalysis
蓝桥算法_单词分析-wordAnalysis
107 1
|
算法 Java
LeetCode150道面试经典题--单词规律(简单)
给定一种规律 pattern 和一个字符串 s ,判断 s 是否遵循相同的规律。 这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 s 中的每个非空单词之间存在着双向连接的对应规律
106 0
|
文件存储
Easy Number Challenge(埃式筛思想+优雅暴力)
Easy Number Challenge(埃式筛思想+优雅暴力)
97 0