Cod sursa(job #2437544)

Utilizator ApostolIlieDanielApostol Daniel ApostolIlieDaniel Data 9 iulie 2019 18:28:37
Problema P-sir Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <bits/stdc++.h>

using namespace std;
typedef unsigned int unt;
const int MAX_N = 2000;
unt sum[1 + MAX_N][1 + MAX_N];
int a[1 + MAX_N];

int main() {
    freopen ("psir.in", "r", stdin);
    freopen ("psir.out", "w", stdout);
    int n;
    cin >> n;
    map <int, int> mp;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        mp[a[i]] = 0;
    }
    int nr = 0;
    for (auto &x : mp)
        x.second = ++nr;
    for (int i = 1; i <= n; i++)
        a[i] = mp[a[i]];
    unt ans = 0; // nu trebuie mod
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j < i; j++) {
            unt keep;
            if (a[i] > a[j])
                keep = sum[j][nr] - sum[j][a[i]] + 1;
            if (a[i] < a[j])
                keep = sum[j][a[i] - 1] + 1;
            if (a[i] == a[j])
                keep = 1;
            ans += keep; sum[i][a[j]] += keep;
        }
        for (int j = 2; j <= nr; j++)
            sum[i][j] += sum[i][j - 1];
    }
    cout << ans << "\n";
    return 0;
}