1. 程式人生 > 其它 >Tokitsukaze and Strange Inequality Codeforces Round #789 (Div. 2) C

Tokitsukaze and Strange Inequality Codeforces Round #789 (Div. 2) C

覆盤

字首和的姿勢增加了

字首和的作法是處理下 a,c   b,d 
計算當c為第i個時,  l[c][a] 為滿足條件個數   c取 1 ~ n-1 a取1 ~ c-1  注意迴圈的順序的
  當b為第i個時 r[b][d] 為滿足條件的個數 b取 2 ~ n j取 n ~ b+1
注意迴圈的順序
最後列舉bc
b 2 ~ n-2
c b+1 ~ n-1
這時候由於有字首和處理,l[c][b+1]就是滿足條件pa<pc的數量
            r[b][c+1]就是滿足條件pb>pd的數量
實現見程式碼

正解

//  AC one more times

#pragma
warning(disable:4996) #include <iostream> #include <algorithm> #include <cstdio> #include<string> #include<string.h> #include <cstring> #include <complex> #include <cmath> #include <vector> #include <stack> #include <queue> #include
<map> #include <set> #include <string> #include <list> #include <bitset> #include <assert.h> #include <unordered_set> #include <unordered_map> #include <iomanip> #include <random> #include <iterator> #include <time.h> #define fi first #define
se second #define pb push_back #define endl '\n' #define all(x) (x).begin(), (x).end() using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> PII; typedef pair<long long,long long> PLL; #define inf64 0x3f3f3f3f3f3f3f3f const int inf = 0x3f3f3f3f; const int maxn = 2e6 + 6; const double eps = 1e-8; const long long mod = 1000000; template<typename T> void init(T q[], int n, T val){ for (int i = 0; i <= n; i++) q[i] = val; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } bool cmp(int c, int d) { return c > d; } int n,a[5010],l[5010][5010],r[5010][5010]; void solve() { LL ans=0; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n+1;i++) for(int j=1;j<=n+1;j++) r[i][j]=0; for(int i=1;i<=n-1;i++) for(int j=1;j<i;j++) l[i][j]=l[i][j-1]+(a[j]<a[i]); for(int i=n;i>=2;i--) for(int j=n;j>i;j--) r[i][j]=r[i][j+1]+(a[i]>a[j]); for(int i=2;i<=n-2;i++) for(int j=i+1;j<=n-1;j++) ans+=l[j][i-1]*r[i][j+1]; cout<<ans<<endl; } int main() { std::ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int T;cin>>T; while(T--) solve(); return 0; }

暴力:TLE5 的

//  AC one more times

#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include<string>
#include<string.h>
#include <cstring>
#include <complex>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <list>
#include <bitset>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <random>
#include <iterator>
#include <time.h>


#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<long long,long long> PLL;
#define inf64 0x3f3f3f3f3f3f3f3f
const int  inf = 0x3f3f3f3f;
const int maxn = 2e6 + 6;
const double eps = 1e-8;
const long long mod = 1000000;
template<typename T>
void init(T q[], int n, T val){   for (int i = 0; i <= n; i++) q[i] = val;  }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
bool cmp(int c, int d) { return c > d; }

void solve()
{
    int n;
    int s[5010];
    cin>>n;
    for(int i=1;i<=n;i++)   cin>>s[i];
    int ans=0;
    vector<PII> a;
    vector<PII> b;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(s[j]>s[i])   a.pb({i,j});    //a c
            if(s[i]>s[j])   b.pb({i,j});    //b d
        }
    }
    for(auto &it:a)
    {
        for(auto &iter:b)
        {

            if(iter.first>=it.second)    break;
        if(it.first<iter.first&&it.second>iter.first&&iter.second>it.second)
                ans++;

        }
    }
    cout<<ans<<endl;
}
int main()
{
    std::ios::sync_with_stdio(false);   cin.tie(nullptr), cout.tie(nullptr);
    int T;cin>>T;
    while(T--)
        solve();    
    return 0;
}