Cod sursa(job #3224247)

Utilizator xXoctavianXxStanescu Matei Octavian xXoctavianXx Data 14 aprilie 2024 23:29:03
Problema P-sir Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <bits/stdc++.h>
#define int long long

using namespace std;

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

const int mod = (1ll << 32) - 1;
const int nmax = 2005;
int v[nmax],d[nmax][nmax],sp[nmax][nmax];
int n;

inline void citire()
{
    fin>>n;
    int cnt=1;
    map<int,int> norm;
    for(int i=1; i<=n; i++)
    {
        fin>>v[i];
        norm[v[i]] = 1;
    }
    for(auto [i,j] : norm)
    {
        norm[i] = cnt++;
    }
    for(int i=1; i<=n; i++)
    {
        v[i] = norm[v[i]];
    }
}

signed main()
{
    citire();
    int total = 0;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<i; j++)
        {
            d[i][v[j]] ++;
            if(v[j] > v[i])
            {
                d[i][v[j]] += sp[j][v[i]-1];
            }
            else if(v[j] < v[i])
            {
                d[i][v[j]] += sp[j][nmax - 1] - sp[j][v[i]];
            }
            d[i][v[j]] &= mod;
        }
        for(int j=1; j<nmax; j++)
        {
            total += d[i][j];
            sp[i][j] = sp[i][j-1] + d[i][j];
        }
    }
    fout<<total;
    return 0;
}