1. 程式人生 > 實用技巧 >codefoces 1400B RPG Protagonist

codefoces 1400B RPG Protagonist

https://codeforces.com/contest/1400/problem/D

i<j<k<l

假設x = list【j】

y = list【k】

可以列舉j和k,0----j-1有a個y,k+1 -- n有b個x,ans+=a*b列舉就好了

具體看程式碼

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3e3+11;
ll dp[maxn][maxn];
int list[maxn];

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>list[i];
            for(int j=1;j<=n;j++){
                dp[i][j] = dp[i-1][j];
            }
            dp[i][list[i]]++;
        }
        
        ll ans = 0;

        for(int j = 2;j<=n;j++){
            for(int k=j+1;k<=n-1;k++){
                int x = list[j];
                int y = list[k];
                ans += (dp[j-1][y])*(dp[n][x] - dp[k][x]);
            }
        }

        cout<<ans<<endl;
    }
    return 0;
}