• HDUOJ------2492Ping pong


    Ping pong

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3518    Accepted Submission(s): 1299


    Problem Description
    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
     
    Source
     

    本题是2008年北京赛区的H题,大致题意为两个乒乓选手a和b想提升排名,但是通过比赛来提升自己的名次,于是他们找来一个同样也是乒乓选手的c作为裁判来抉择,

    题目规定c必须在a和b之间,问对于所有的n个人,总共有多少中不同的可能....

    思路:如果改变a和b,那么需要三个循环才能解决,显然会超时...

       但是考虑c然后统计出小于c的有多少种可能x,大于c有多少种可能y..

      很容易知道结果为x*y....

    于是就可以推得公式: sum=Σi=0(xi*yi);

    剩下的便是统计了...

    这里我用的是BIT即树状数组

    代码如下:

     1 //@coder Gxjun
     2 //BIT algorithm
     3 #include<stdio.h>
     4 #include<string.h>
     5 #include<stdlib.h>
     6 #define maxn 20000
     7 #define val 100000
     8 int bb[val+5],aa[maxn];
     9 //低位技术
    10 int lowbit(int k)
    11 {
    12     return k&(-k);
    13 //    return k&(k&(k-1));
    14 //    return k&!(k-1);
    15 }
    16 void  ope(int x)
    17 {
    18     while(x<=val)
    19     {
    20         bb[x]++;
    21         x+=lowbit(x);
    22     }
    23 }
    24 
    25 int sum(int x)
    26 {
    27     int ans=0;
    28     while(x>0)
    29     {
    30         ans+=bb[x];
    31         x-=lowbit(x);
    32     }
    33     return ans;
    34 }
    35 int xx[maxn],yy[maxn];
    36 int main()
    37 {
    38     int tt,nn,i;
    39     scanf("%d",&tt);
    40     while(tt--)
    41     {
    42         scanf("%d",&nn);
    43         for(i=0;i<nn;i++)
    44             scanf("%d",&aa[i]);
    45      //求小于aa[i] 的个数在aa[0]~~aa[i-1]
    46        memset(bb,0,sizeof(bb));
    47         for(i=0;i<nn;i++)
    48         {
    49             xx[i]=sum(aa[i]-1);
    50             ope(aa[i]);
    51         }
    52        memset(bb,0,sizeof(bb));
    53         for(i=nn-1; i>=0;i--)
    54         {
    55           yy[i]=sum(aa[i]-1);
    56             ope(aa[i]);
    57         }
    58         __int64 res=0;
    59         for(i=0;i<nn ;i++)
    60         {
    61             res+=(i-xx[i])*yy[i]+xx[i]*(nn-i-1-yy[i]);
    62         }
    63         printf("%I64d
    ",res);
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    CSharpThinkingC# 要点(附加三)
    CSharpThinkingC#3 革新(附加二)
    CSharpThinking委托相关(二)
    C++之this指针与另一种“多态”
    《C++应用程序性能优化::第二章C++语言特性的性能分析》学习和理解
    《C++应用程序性能优化::第一章C++对象模型》学习和理解
    回答总结:C实现“动态绑定”
    编译器对临时变量的优化简单理解
    虚函数表里边保存的不一定是虚函数的地址
    C++对象内存布局测试总结
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3649378.html
Copyright © 2020-2023  润新知