1. 程式人生 > 其它 >Codeforces Round #789 (Div. 2) C- Tokitsukaze and Strange Inequality(二維字首和)

Codeforces Round #789 (Div. 2) C- Tokitsukaze and Strange Inequality(二維字首和)

思路:見程式碼註釋

#pragma GCC optimize(2)
#pragma GCC optimize(1)
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
const ull base=131;
#define MAX 5009
#define PI 3.141592653589793
using namespace std;
inline void solve(){
    int len;cin>>len;
    vector<int> a(len+1
); for(int i=1;i<=len;i++) cin>>a[i]; vector<vector<int>> dp(len+1,vector<int>(len+1,0)); for (int i = 1; i <= len; ++i) { for (int j = 1; j <= len; ++j) if (a[j] < i) dp[i][j] = 1;//如果小於,設這點為一個逆序對 for (int j = 1; j <= len; ++j) dp[i][j] += dp[i][j - 1
];//字首和求出在1-j內逆序對個數 } ll ans=0; for(int b=1;b<=len;b++) for(int c=b+1;c<=len;c++){ ans+=(dp[a[c]][b-1]*(dp[a[b]][len]-dp[a[b]][c]));//列舉c下標的數字在1-b(即a的範圍數字)的逆序對*(列舉b下標狀態下的數字在總長範圍內-列舉b狀態下的數字在1-c內(即所有不是b範圍的) } cout<<ans<<"\n"; } int main() { std::ios::sync_with_stdio(
false); std::cin.tie(0);std::cout.tie(0); int sum;cin>>sum; while(sum--){ solve(); } }