Cod sursa(job #2148554)

Utilizator smashsmash everything smash Data 1 martie 2018 19:53:29
Problema P-sir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <fstream>
#include<algorithm>
using namespace std;
ifstream cin ("psir.in");
ofstream cout ("psir.out");
unsigned int best[2010][2010],k,big[2010],small[2010],sol,t;
int n,date[2010];
struct bla
{
    int p,v;
} data[2010];
bool sortare (bla q,bla w)
{
    return q.v<w.v;
}
int mic (int value)
{
    int st=1,dr=n,mij=(st+dr)/2;
    while(st<=dr)
    {
        if(value>data[mij].v) st=mij+1;
        else dr=mij-1;
        mij=(st+dr)/2;
    }
    return dr;
}
int mare (int value)
{
    int st=1,dr=n,mij=(st+dr)/2;
    while(st<=dr)
    {
        if(value>=data[mij].v) st=mij+1;
        else dr=mij-1;
        mij=(st+dr)/2;
    }
    return dr;
}
void read ()
{
    cin>>n; for(int i=1;i<=31;i++) t*=2;
    t*=2;
    for(int i=1;i<=n;i++)
        cin>>date[i],data[i].v=date[i],data[i].p=i;
    sort(data+1,data+n+1,sortare);
    for(int i=1;i<=n;i++)
    {
        small[i]=mic(date[i]);
        big[i]=mare(date[i]);
    }
}
void solve ()
{
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(data[j].p<i)
            {   unsigned int sum=1;
                if(data[j].v>date[i]) sum=(sum+best[data[j].p][small[i]])%t;
                else if(data[j].v<date[i]) sum=(sum+best[data[j].p][n]-best[data[j].p][big[i]])%t;
               if(sum<0) sum+=t; best[i][j]=(best[i][j-1]+sum)%t; sol=(sol+sum)%t;
            } else best[i][j]=best[i][j-1];
        }
    }
    cout<<sol;
}
int main()
{
    read();
    solve();
    cin.close();
    cout.close();
    return 0;
}