Cod sursa(job #69038)

Utilizator mariusdrgdragus marius mariusdrg Data 30 iunie 2007 20:32:41
Problema P-sir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.75 kb
#include<stdio.h>
#include<vector>
#define vii vector <int> :: iterator
#define vi vector <int>
#define pb push_back
#include<algorithm>

using namespace std;

const int maxn = 2001;


int a[maxn];
unsigned int mat[maxn][maxn];
int i;
unsigned int sol;
int n;
int ind1[maxn];
int ind[maxn];
int j;
vi vect[maxn];

bool cmpf(const int i,const int j)
{
        return a[i] < a[j];
}


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]);
                ind[i] = i;
                ind1[i] = i;
        }


        sort(ind + 1,ind + n + 1,cmpf);
        sort(ind1 + 1,ind1 + n + 1,cmpf);

        for(i = 1;i <= n; ++i)
        {
                if (a[ind[i]] == a[ind[i - 1]]) ind[i] = ind[i - 1];
        }

        for(i = n; i; --i)
        {
                if (a[ind1[i]] == a[ind1[i + 1]]) ind1[i] = ind1[i + 1];
        }

        vii it;

        for(i = 1;i <= n; ++i)
        {
                for(j = 1;j <= n; ++j)
                {
                        for(it = vect[j].begin();it != vect[j].end(); ++it)
                        {
                                mat[i][j] += 1;
                                if (ind[i] > j) mat[i][j] += mat[*it][ind[i] - 1];
                                        else mat[i][j] += mat[*it][n] - mat[*it][ind1[i]];
                        }
                        mat[i][j] += mat[i][j - 1];
                }
                vect[ind[i]].pb(i);
        }
        
        for(i = 1;i <= n; ++i)
        {
                sol += mat[i][n];
        }

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

        return 0;
        
}