Cod sursa(job #940617)

Utilizator DaNutZ2UuUUBB Bora Dan DaNutZ2UuU Data 16 aprilie 2013 19:52:02
Problema P-sir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

#define maxn 2010
#define ui unsigned int

int n;
ui sol;
int a[maxn], p[maxn], b[maxn];
ui c[maxn];
ui s[maxn][maxn];
ui sum[maxn];

int cmp(int x, int y)
{
    return a[x] < a[y];
}

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

    scanf("%d ", &n);

    int i, j, k;

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

    sort(p+1, p+n+1, cmp);

    for (i=1; i<=n; i++)
        if (a[p[i]] == a[p[i-1]]) b[p[i]] = b[p[i-1]];
        else b[p[i]] = i;

    for (i=2; i<=n; i++)
    {
        memset(c, 0, sizeof(c));

        for (j=1; j<i; j++)
        {
            if (b[j] > b[i]) c[j] =  s[j][b[i]-1];
            else if (b[j] < b[i]) c[j] = sum[j] - s[j][b[i]];

            c[j] += 1;
            sum[i] += c[j];
        }

        k = 1;

        for (j=1; j<=n; j++)
        {
            s[i][j] = s[i][j-1];
            for (; k<=n && b[p[k]]==j; k++)
                if (p[k] < i) s[i][j] += c[p[k]];
        }

        sol += sum[i];
    }

    printf("%u\n", sol);

    return 0;
}