题目链接

题意:

有n个人要进行乒乓球比赛,每一个人都一个能力值。每一个人出现的次序就是他们住的位置

如今要求进行一场比赛,三个人,裁判的能力值在两个选手之间,住的位置也在两个人的之间

问这样的比赛一共能够进行多少次

思路:

用树状数组做,否则TLE,先从左到右扫一遍,计算每点左边大的个数和小的个数,

再从右到左扫一遍,计算每点右边大和小的个数,然后交叉相乘取和就能够了

代码例如以下:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<string>
  4. #include<iostream>
  5. #include<algorithm>
  6. using namespace std;
  7. typedef long long ll;
  8. const int N = 1e5+10;
  9. int n;
  10. int p[N], c[N], li[N], la[N], ri[N], ra[N];
  11.  
  12. inline int Lowbit(int x){ return x&(-x); }
  13.  
  14. void change(int u, int x)
  15. {
  16. while(u < N)
  17. {
  18. c[u] += x;
  19. u += Lowbit(u);
  20. }
  21. }
  22.  
  23. int get_sum(int x)
  24. {
  25. int ans = 0;
  26. for(int i = x; i > 0; i -= Lowbit(i))
  27. {
  28. ans += c[i];
  29. }
  30. return ans;
  31. }
  32.  
  33. int main()
  34. {
  35. int t;
  36. scanf("%d", &t);
  37. while(t--)
  38. {
  39. memset(c, 0, sizeof(c));
  40. scanf("%d", &n);
  41. for(int i = 1; i <= n; i++)
  42. {
  43. scanf("%d", &p[i]);
  44. int cnt = get_sum(p[i]);
  45. li[i] = cnt; // i点左边比它小的
  46. la[i] = i - cnt - 1; //i点左边比它大的
  47. change(p[i], 1);
  48. }
  49. memset(c, 0, sizeof(c));
  50. for(int i = n; i > 0; i--)
  51. {
  52. int cnt = get_sum(p[i]);
  53. ri[i] = cnt; // i点右边比它小的
  54. ra[i] = n - i - cnt; //i点右边比它大的
  55. change(p[i], 1);
  56. }
  57. ll ans = 0;
  58. for(int i = 1; i <= n; i++)
  59. {
  60. ans += li[i] * ra[i] + la[i] * ri[i];
  61. }
  62. printf("%I64d\n", ans);
  63. }
  64. return 0;
  65. }

HDU2491 Priest John&#39;s Busiest Day的更多相关文章

  1. POJ 3684 Priest John&#39;s Busiest Day 2-SAT+输出路径

    强连通算法推断是否满足2-sat,然后反向建图,拓扑排序+染色. 一种选择是从 起点開始,还有一种是终点-持续时间那个点 開始. 若2个婚礼的某2种时间线段相交,则有矛盾,建边. easy出错的地方就 ...

  2. POJ 3683 Priest John&#39;s Busiest Day (2-SAT+输出可行解)

    题目地址:POJ 3683 第一次做须要输出可行解的题目. . .大体思路是先用强连通来推断是否有可行解,然后用逆序建图.用拓扑排序来进行染色.然后输出可行解. 详细思路见传送门 由于推断的时候少写了 ...

  3. 图论(2-sat):Priest John's Busiest Day

    Priest John's Busiest Day   Description John is the only priest in his town. September 1st is the Jo ...

  4. POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题)

    POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题) Descripti ...

  5. 【POJ3683】Priest John's Busiest Day

    题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...

  6. poj 3686 Priest John's Busiest Day

    http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...

  7. POJ 3683 Priest John's Busiest Day (2-SAT)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6900   Accept ...

  8. POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10010   Accep ...

  9. Priest John's Busiest Day(POJ 3683)

    原题如下: Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12162   ...

随机推荐

  1. CUDA核函数调用基础数学API的一个奇葩情况

    今天测试在核函数在GTX 950M上运行的情况,核函数中的pow竟然出不来结果...在网上查了一圈,说是要改成powf,结果确实就好了. 但是,奇怪的是,CUDA版本都是最新的8.0,之前在GT 72 ...

  2. V-Hyper安装ubuntu-13.10-server-amd64

    1.在windws8上的V_Hyper虚拟机上安装Ubuntu虚拟机服务器版.遇到的问题和解决方案 2.正确的在V-Hyper配置方法参考文章:在Hyper-V中安装和配置Ubuntu Server ...

  3. GIT指令简洁篇

    查看.添加.提交.删除.找回,重置修改文件 git help <command> # 显示command的help git show # 显示某次提交的内容 git show $id gi ...

  4. javascript:入门笔记

    1:html注释: <html> <body> <script type="text/javascript"> <!-- document ...

  5. KVM(一)简介及安装

    1. KVM 介绍 1.0 虚拟化简史 其中,KVM 全称是 基于内核的虚拟机(Kernel-based Virtual Machine),它是一个 Linux 的一个内核模块,该内核模块使得 Lin ...

  6. Qt笔记——Event

    #ifndef MYBUTTON_H #define MYBUTTON_H #include <QPushButton> class MyButton : public QPushButt ...

  7. 如果想从jenkins直接生成docker镜像,并推送到harbor中,最简单的脚本如何实现?

    如果不考虑意外, 第一版最简单的构思如下: #!/usr/bin/env python # -*- coding: utf-8 -*- import getopt, sys import subpro ...

  8. JS—正则表达式

    正则表达式的元字符是包含特殊含义的字符,它们有一些特殊的功能,可以控制匹配模式的方式,反斜杠后的元字符失去其特殊含义. 单个字符和数字 .表示匹配除换行符外的单个字符,两个.就表示匹配两个字符 var ...

  9. Codeforces Round 253 (Div. 2)

    layout: post title: Codeforces Round 253 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  10. SQL Loader with utf8

    alter this line in your control file characterset UTF8 to this characterset UTF8 length semantics ch ...