Ping pong
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2691   Accepted: 996

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. 1
  2. 3 1 2 3

Sample Output

  1. 1
    题意:n个乒乓球爱好者,进行比赛。每个人都有一个技能值 ai。每场比赛需要 3 个人:两名选手,一名裁判。他们有一个奇怪的规定,即裁判必须住在两名选手之间,并且技能值也介于两名选手之间,问一共能组织多少种比赛
    分析: 枚举裁判k,看看k前面有多小比他小,后面比他大 或者 前面有多少比他大后面有多少比他小,乘加
    树状数组解决统计k后面有多少比他大的数
    解决方案:每个数用一个结构体{idvalue}表示,按照value从小到大排序,然后使让后面大的id1,即可
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. const int Max = + ;
  7. typedef long long LL;
  8. struct Node
  9. {
  10. int id,value;
  11. };
  12. Node data[Max];
  13. int n,c[Max];
  14. int cmp(Node x, Node y)
  15. {
  16. return x.value < y.value;
  17. }
  18. int lowbit(int k)
  19. {
  20. return k & (-k);
  21. }
  22. LL sum(int k)
  23. {
  24. LL ans = ;
  25. while(k > )
  26. {
  27. ans += c[k];
  28. k -= lowbit(k);
  29. }
  30. return ans;
  31. }
  32. void modify(int k, int y)
  33. {
  34. while(k <= n)
  35. {
  36. c[k] += y;
  37. k += lowbit(k);
  38. }
  39. }
  40. int main()
  41. {
  42. int t;
  43. scanf("%d", &t);
  44. while (t--)
  45. {
  46. scanf("%d", &n);
  47. for(int i = ; i <= n; i++)
  48. {
  49. scanf("%d", &data[i].value);
  50. data[i].id = i;
  51. }
  52. sort(data + , data + + n, cmp);
  53. memset(c, , sizeof(c));
  54. LL l, r, ans = ;
  55. for(int i = ; i <= n; i++)
  56. {
  57. l = sum(data[i].id); //比 data[i].value小的个数
  58. r = sum(n) - l; //总共比data[i].value 小的个数 - 左边比他小的个数 == 右边比他小的个数
  59. ans += (l * (n - data[i].id - r)) + (r * (data[i].id - - l));
  60. modify(data[i].id, ); //本来一直觉着是修改id + 1的值,不是,就是修改id值,修改id + 1就要sum(id - 1),id - 1可以是0
  61. }
  62. printf("%I64d\n", ans);
  63. }
  64. return ;
  65. }

POJ3928 Pingpong(统计比 K 小的个数 + 树状数组)的更多相关文章

  1. 第k小整数(树状数组)

    洛谷传送门 入门难度.. 没错,但是我并不是要暴力做. 而是用树状数组来做. 先离散化,然后随便搞一搞就可以了.(晕.比暴力还慢) 如果要查找某一区间的的话可以把区间取出重新建树,然后再求.(更暴力) ...

  2. hdu 5869 区间不同GCD个数(树状数组)

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  3. BZOJ3110[Zjoi2013]K大数查询(树状数组+整体二分)

    3110 [Zjoi2013]K大数查询 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a ...

  4. BZOJ 2738 子矩阵第k大 | 二维树状数组 整体二分 分治

    BZOJ 2738 "矩阵乘法"(子矩阵第k大) 题意 给出一个矩阵,多次询问子矩阵中第k大的数是多少. 题解 我做这道题之前先照着这道题出了一道题,是这道题的一维版本,在这里:h ...

  5. 算法笔记求序列A每个元素左边比它小的数的个数(树状数组和离散化)

    #include <iostream> #include <algorithm> #include <cstring> using namespace std ; ...

  6. ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  7. CF 61E 树状数组+离散化 求逆序数加强版 三个数逆序

    http://codeforces.com/problemset/problem/61/E 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 会 ...

  8. 牛客网 暑期ACM多校训练营(第一场)J.Different Integers-区间两侧不同数字的个数-离线树状数组 or 可持久化线段树(主席树)

    J.Different Integers 题意就是给你l,r,问你在区间两侧的[1,l]和[r,n]中,不同数的个数. 两种思路: 1.将数组长度扩大两倍,for(int i=n+1;i<=2* ...

  9. 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题

    达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...

随机推荐

  1. Scala入门之函数进阶

    /** * 函数式编程进阶: * 1,函数和变量一样作为Scala语言的一等公民,函数可以直接赋值给变量: * 2, 函数更长用的方式是匿名函数,定义的时候只需要说明输入参数的类型和函数体即可,不需要 ...

  2. Windows Phone 8 显示当前项目的使用内存,最大峰值,最大内存上限

    public static class MemoryDiagnosticsHelper { public static bool isStart = false; static Popup popup ...

  3. Android之Activity跳转

    简述 如果把每个activity看成一个页面的话,那么activity之间的跳转和页面的之间的跳转基本上是一样的.首先需要监听一个事件,当这个事件发生的时候,就进行跳转.html中有个<a sr ...

  4. SqlServer——全文索引

    当我们想要模糊查询时,之前用like %来进行查询,但是为了提高查询速度,提出了全文索引. 全文索引是用空间换取了时间,它将每个表中的数据进行切分存储,这样就能很快的定位到模糊查询的数据. 全文索引快 ...

  5. 13-mv 命令总结

  6. webpack 插件: html-webpack-plugin

    插件地址:https://www.npmjs.com/package/html-webpack-plugin 这个插件用来简化创建服务于 webpack bundle 的 HTML 文件,尤其是对于在 ...

  7. display:block、display:inline与displayinline:block的概念和区别

    总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) 和 inline elements (内联元素).block元素通 ...

  8. QMenu,contextmenuevent,窗体透明

    void MainWindow::contextMenuEvent(QContextMenuEvent *event) { QMenu *menu=newQMenu; menu->addActi ...

  9. Kernel Methods - An conclusion

    Kernel Methods理论的几个要点: 隐藏的特征映射函数\(\Phi\) 核函数\(\kappa\): 条件: 对称, 正半定; 合法的每个kernel function都能找到对应的\(\P ...

  10. mysql查询时间戳和日期的转换

    mysql提供了两个函数: from_unixtime(time_stamp) -> 将时间戳转换为日期 unix_timestamp(date) -> 将指定的日期或者日期字符串转换为时 ...