splay exercise ranking system

Description

The score of a game will be given after the player finishes a game. Now game makers need to add a feature that gives them scores
At the same time, it can give the score of this game in the score of the game before the player (in short, calculate the score of how many games before)
The number is higher than the score of this game, and then add this value to the output). Now that the game company has found you, I hope you can solve their problems.

Input

In the first line, an integer n represents the total number of games the player has played. An integer in each of the following N lines indicates the score of the Bureau.

Output

Only one number, which means the average of n numbers your system needs to output. Keep two decimal places output.

Sample Input

5
100
200
150
170
50

Sample Output

2.20

explain:

The five ranking values to be output are: 1, 1, 2, 2, 5, with an average of 2.20.

Data range:

n not more than 100000, the score is a non negative integer, and not more than 109.

HINT

thinking

splay directly simulates the topic requirements.

Code

#include <cstdio>

const int maxn=100000;

struct splay_tree
{
  int fa[maxn+10],son[2][maxn+10],val[maxn+10],size[maxn+10],cnt[maxn+10],root,tot;

  inline int t(int x)
  {
    return son[1][fa[x]]==x;
  }

  inline int updata(int x)
  {
    return size[x]=size[son[0][x]]+size[son[1][x]]+cnt[x];
  }

  inline int rotate(int x)
  {
    int k=t(x),f=fa[x];
    if(fa[f])
      {
        son[t(f)][fa[f]]=x;
      }
    fa[x]=fa[f];
    if(son[!k][x])
      {
        fa[son[!k][x]]=f;
      }
    son[k][f]=son[!k][x];
    fa[f]=x;
    son[!k][x]=f;
    updata(f);
    updata(x);
    return 0;
  }

  inline int splay(int x,int c)
  {
    while(fa[x]!=c)
      {
        int f=fa[x];
        if(fa[f]==c)
          {
            rotate(x);
          }
        else if(t(x)==t(f))
          {
            rotate(f);
            rotate(x);
          }
        else
          {
            rotate(x);
            rotate(x);
          }
      }
    if(!c)
      {
        root=x;
      }
    return 0;
  }

  inline int work(int x)
  {
    if(!root)
      {
        ++tot;
        root=tot;
        fa[tot]=son[0][tot]=son[1][tot]=0;
        size[tot]=cnt[tot]=1;
        val[tot]=x;
        return 1;
      }
    int now=root;
    while(now)
      {
        if(val[now]==x)
          {
            ++cnt[now];
            splay(now,0);
            return size[son[1][now]]+1;
          }
        else
          {
            int k=(val[now]<x);
            if(!son[k][now])
              {
                ++tot;
                son[k][now]=tot;
                val[tot]=x;
                cnt[tot]=1;
                fa[tot]=now;
                son[0][tot]=son[1][tot]=0;
                break;
              }
            now=son[k][now];
          }
      }
    splay(tot,0);
    updata(tot);
    return size[son[1][tot]]+1;
  }
};

splay_tree st;
int n,a;
long long sum;
double ans;

int main()
{
  scanf("%d",&n);
  for(register int i=1; i<=n; ++i)
    {
      scanf("%d",&a);
      sum+=st.work(a);
    }
  ans=(1.0*sum)/n;
  printf("%.2lf\n",ans);
  return 0;
}

Posted by omprakash on Fri, 01 May 2020 08:56:36 -0700