专栏首页mlHDUOJ------2492Ping pong

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 Asia Regional Beijing

本题是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\n",res);
64     }
65     return 0;
66 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • HDUOJ----1165Eddy's research II

    Eddy's research II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536...

    Gxjun
  • HDUOJ----(1031)Design T-Shirt

    Design T-Shirt Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327...

    Gxjun
  • poj------2352 Stars(树状数组)

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30268 ...

    Gxjun
  • 使用模拟近似乘子的深度学习训练(performance)

    本文通过仿真提出了如何利用近似乘子提高卷积神经网络(CNNs)的训练性能。与精确乘法器相比,近似乘法器在速度、功率和面积方面具有更好的性能。然而,近似乘数有一个...

    用户6869393
  • Codeforces 626E Simple Skewness(暴力枚举+二分)

    E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megab...

    Angel_Kitty
  • 【Codeforces】1213B - Bad Prices

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

    喜欢ctrl的cxk
  • HDU 3032 Nim or not Nim?(Multi-Nim)

    Problem Description Nim is a two-player mathematic game of strategy in which ...

    attack
  • 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛- A. Banana

    In the forest, there is a Banana Company that provides bananas from different pl...

    Enterprise_
  • Angular component的一个例子

    官网:https://angular.io/guide/architecture-components

    Jerry Wang
  • POJ2318 TOYS 判断点与直线位置关系 【计算几何】

    Calculate the number of toys that land in each bin of a partitioned toy box.

    ACM算法日常

扫码关注云+社区

领取腾讯云代金券