Educational Codeforces Round 94 (Rated for Div. 2) D. Zigzags ###K ###K //K
题目链接:https://codeforces.ml/contest/1400/problem/D
思路:一开始考虑的是枚举i和k 然后用前缀和维护, 但是这样写 i和k 两个点就n^2了 还要枚举一次数字 会n^3 TLE
考虑枚举 j和k 那么 j 前面要枚举的数就确定了 为a[k] k后面枚举的数也确定了是a[j] 两数相乘即是这种情况下的组合
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define pb push_back 5 const int maxn =2e5+10; 6 const int mod=1e9+7; 7 int a[3010]; 8 int pre[3010][3010]; 9 10 11 int main() 12 { 13 ios::sync_with_stdio(false); 14 cin.tie(0); 15 int t; 16 cin>>t; 17 while(t--) 18 { 19 int n; 20 cin>>n; 21 for(int i=1;i<=n;i++) 22 { 23 for(int j=1;j<=n;j++) 24 { 25 pre[i][j]=0; 26 } 27 } 28 for(int i=1;i<=n;i++) 29 { 30 cin>>a[i]; 31 } 32 ll ans=0; 33 for(int i=1;i<=n;i++) 34 { 35 int x=a[i]; 36 for(int j=1;j<=n;j++) 37 { 38 pre[i][j]=pre[i-1][j]; 39 } 40 pre[i][x]++; 41 } 42 for(int j=1;j<=n;j++) 43 { 44 for(int k=j+1;k<=n;k++) 45 { 46 ans+=1ll*pre[j-1][a[k]]*(pre[n][a[j]]-pre[k][a[j]]); 47 } 48 } 49 50 cout<<ans<<'\n'; 51 52 } 53 54 55 }

浙公网安备 33010602011771号