Cod sursa(job #69059)

Utilizator mariusdrgdragus marius mariusdrg Data 30 iunie 2007 22:10:05
Problema P-sir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.43 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 poz[maxn];
int ind1[maxn];
int ind[maxn];
int j;
vi vect[maxn];

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


void printare(int a[])
{
        int i;
        for(i = 1;i <= n; ++i)
        {
                printf("%d ",a[i]);
        }
        printf("\n");
}

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


        sort(poz + 1,poz + n + 1,cmpf);

        for(i = 1;i <= n; ++i)
        {
                ind[poz[i]] = i;
                ind1[poz[i]] = i;
        }

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

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

        vii it;

   //     printare(ind);
    //    printare(ind1);

        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] == ind[j]) continue;
                                if (ind[i] < j) mat[i][j] += mat[*it][ind[i] - 1];
                                else
                                {
                                        mat[i][j] += mat[*it][n] - mat[*it][ind[i]];
                                }
                        }
                }
                for(j = 1;j <= n; ++j)
                {
                        mat[i][j] += mat[i][j - 1];
                }
                vect[ind[i]].pb(i);
        }

        int aux[maxn];

    /*    for(i = 1;i <= n; ++i) aux[i] = mat[2][i];
          printare(aux);
      */
        for(i = 1;i <= n; ++i)
        {
                sol += mat[i][n];
        }
//        sol = (1 << 31) - 1 +  (1 << 31);
        printf("%u\n",sol);

        return 0;
        
}