题目: You are given an array a1,a2…an. Calculate the number of tuples (i,j,k,l) such that:
1≤i<j<k<l≤n; ai=ak and aj=al; Input The first line contains a single integer t (1≤t≤100) — the number of test cases.
The first line of each test case contains a single integer n (4≤n≤3000) — the size of the array a.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — the array a.
It’s guaranteed that the sum of n in one test doesn’t exceed 3000.
Output For each test case, print the number of described tuples.
Example inputCopy 2 5 2 2 2 2 2 6 1 3 3 1 2 3 outputCopy 5 2 Note In the first test case, for any four indices i<j<k<l are valid, so the answer is the number of tuples.
In the second test case, there are 2 valid tuples:
(1,2,4,6): a1=a4 and a2=a6; (1,3,4,6): a1=a4 and a3=a6.
题目大意: 找到满足条件的所有组合 1≤i<j<k<l≤n; ai=ak and aj=al;
思路 :枚举j和l,然后用num来统计 j到l 有多少种组合。 cnt数组 记录好 j下标前面 出现过多少次这个数。 因为数据比较小,直接o(n^2).
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <cstdlib> #include <stack> #include <vector> #include <set> #include <map> #include <bitset> #define INF 0x3f3f3f3f3f3f3f3f #define inf 0x3f3f3f3f #define FILL(a,b) (memset(a,b,sizeof(a))) #define re register #define lson rt<<1 #define rson rt<<1|1 #define lowbit(a) ((a)&-(a)) #define ios std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0); #define fi first #define rep(i,n) for(int i=0;(i)<(n);i++) #define rep1(i,n) for(int i=1;(i)<=(n);i++) #define se second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pii; const ll mod=998244353; const ll N =3e6+10; const double eps = 1e-5; const double pi=acos(-1); ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);} int dx[4]= {-1,0,1,0}, dy[4] = {0,1,0,-1}; ll n,cnt[10005],a[10005],res,num; void solve() { cin>>n; res=0; memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;i++) { cin>>a[i]; } for(int j=1;j<n;j++) { num=0; for(int l=j+1;l<=n;l++) { if(a[j]==a[l]) { res+=num; } num+=cnt[a[l]]; } cnt[a[j]]++; } cout<<res<<endl; } int main() { ios int T; cin>>T; //T=1; while(T--) { solve(); } return 0; }