Cod sursa(job #1471830)

Utilizator AlexNiuclaeNiculae Alexandru Vlad AlexNiuclae Data 15 august 2015 13:03:23
Problema P-sir Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <cstdio>
#include <vector>
#include <algorithm>

#define ui unsigned int
#define ll long long

using namespace std;

const int Nmax = 2010;
const ll MOD = (1LL << 32);

int n , i , j;
int a[Nmax] , pos[Nmax];

vector < int > norm;

ll ans;
ui dp[Nmax][Nmax];


int main()
{
    freopen("psir.in","r",stdin);
    freopen("psir.out","w",stdout);

    scanf("%d", &n);
    for (i = 1; i <= n; ++i)
    {
        scanf("%d", &a[i]);
        norm.push_back(a[i]);
    }

    sort(norm.begin() , norm.end());
    auto it = unique(norm.begin() , norm.end());
    norm.resize(distance(norm.begin() , it));

    for (i = 1; i <= n; ++i)
        pos[i] = lower_bound(norm.begin() , norm.end() , a[i]) - norm.begin() + 1;

    /// dp[i][j] - numarul de p-subsiruri cu ultimul element a[i] si penultimul element norm[pos[j]]
    for (i = 1; i <= n; ++i)
    {
        for (j = 1; j < i; ++j)
        {
            dp[i][pos[j]]++; ///lenght = 2;
            if (a[i] > a[j])
                dp[i][pos[j]] = (1LL * dp[i][pos[j]] + 1LL * dp[j][pos[i]+1]) % MOD;
            else if (a[i] < a[j])
                dp[i][pos[j]] = (1LL * dp[i][pos[j]] + 1LL * dp[j][1] - 1LL * dp[j][pos[i]] + MOD) % MOD;
        }

        for (j = norm.size(); j ; --j)
            dp[i][j] = (1LL * dp[i][j] + 1LL * dp[i][j+1]) % MOD;

        ans += 1LL * dp[i][1];
    }

    printf("%lld\n", ans);


    return 0;
}