Cod sursa(job #2139768)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 22 februarie 2018 19:42:20
Problema P-sir Scor 100
Compilator cpp Status done
Runda Simulare 46 Marime 1.08 kb
#include <fstream>
#include <map>

using namespace std;

ifstream fin ("psir.in"); ofstream fout ("psir.out");

typedef unsigned int u32;

const int nmax = 2000;
u32 s[nmax + 1][nmax + 1];

int n;
int v[nmax + 1];
map< int, int > mp;

void citire_si_norm () {
    fin >> n;
    for (int i = 1; i <= n; ++ i) {
        fin >> v[ i ];
        mp[ v[ i ] ] = 1;
    }

    int ind = 0;
    for (auto &i : mp) {
        i.second = ++ ind;
    }

    for (int i = 1; i <= n; ++ i) {
        v[ i ] = mp[ v[ i ] ];
    }
}

int main () {
    citire_si_norm ();

    u32 ans = 0;
    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j < i; ++ j) {
            u32 d = 1;

            if (v[ i ] > v[ j ]) {
                d += s[ j ][ n ] - s[ j ][ v[ i ] ];
            } else if (v[ i ] < v[ j ]) {
                d += s[ j ][v[ i ] - 1];
            }

            s[ i ][ v[ j ] ] += d;
        }

        for (int j = 1; j <= n; ++ j) {
            s[ i ][ j ] += s[ i ][j - 1];
        }
        ans += s[ i ][ n ];
    }

    fout << ans << "\n";

    fin.close();
    fout.close();
    return 0;
}