zoukankan      html  css  js  c++  java
  • LA 4329 PingPong

    Ping pong

    N (3 ≤ N ≤ 20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank, they often compete with each other. If two players want to compete, they must choose a referee among other ping pong players and hold the game in the referee’s house. For some reason, the contestants can’t choose a referee whose skill rank is higher or lower than both of theirs. The contestants have to walk to the referee’s house, and because they are lazy, they want to make their total walking distance no more than the distance between their houses. Of course all players live in different houses and the position of their houses are all different. If the referee or any of the two contestants is different, we call two games different. Now is the problem: how many different games can be held in this ping pong street?
    Input The first line of the input contains an integer T (1 ≤ T ≤ 20), indicating the number of test cases, followed by T lines each of which describes a test case. Every test case consists of N + 1 integers. The first integer is N, the number of players. Then N distinct integers a1,a2 ...aN follow, indicating the skill rank of each player, in the order of west to east (1 ≤ ai ≤ 100000, i = 1...N).
    Output
    For each test case, output a single line contains an integer, the total number of different games.


    Sample Input
    1

    3 1 2 3


    Sample Output
    1

     1 #include<cstdio>
     2 #include<vector>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 typedef long long ll;
     7 const int maxn=100005;
     8 const int maxm=20005;
     9 int C[maxn];
    10 int n;
    11 int lowbit(int x)
    12 {
    13     return x&(-x);
    14 }
    15 int sum(int x)
    16 {
    17     int res=0;
    18     while(x>0)
    19     {
    20         res+=C[x];
    21         x-=lowbit(x);
    22     }
    23     return res;
    24 }
    25 void add(int x,int d)
    26 {
    27     while(x<maxn)
    28     {
    29         C[x]+=d;
    30         x+=lowbit(x);
    31     }
    32 }
    33 int main()
    34 {
    35     int T;
    36     int a[maxm];
    37     ll c[maxm],d[maxm];
    38     scanf("%d",&T);
    39     while(T--)
    40     {
    41         scanf("%d",&n);
    42         memset(C,0,sizeof(C));
    43         for(int i=1;i<=n;i++)
    44         {
    45             scanf("%d",&a[i]);
    46             add(a[i],1);
    47             c[i]=sum(a[i])-1;
    48         }
    49         memset(C,0,sizeof(C));
    50         for(int i=n;i>0;i--)
    51         {
    52             add(a[i],1);
    53             d[i]=sum(a[i])-1;
    54         }
    55         ll ans=0;
    56         for(int i=2;i<=n-1;i++)
    57             ans+=c[i]*(n-i-d[i])+(i-c[i]-1)*d[i];
    58         printf("%lld
    ",ans);
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    The formatter threw an exception while trying to deserialize the message in WCF
    通过Web Deploy方式部署WCF
    The Managed Metadata Service or Connection is currently not available
    How to create Managed Metadata Column
    冒泡算法
    asp.net core 实战项目(一)——ef core的使用
    Vue学习笔记入门篇——安装及常用指令介绍
    Vue学习笔记入门篇——数据及DOM
    Vue学习笔记目录
    Chart.js在Laravel项目中的应用
  • 原文地址:https://www.cnblogs.com/homura/p/4993729.html
Copyright © 2011-2022 走看看