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

Sample Output

1
题意:n个乒乓球爱好者,进行比赛。每个人都有一个技能值 ai。每场比赛需要 3 个人:两名选手,一名裁判。他们有一个奇怪的规定,即裁判必须住在两名选手之间,并且技能值也介于两名选手之间,问一共能组织多少种比赛
分析: 枚举裁判k,看看k前面有多小比他小,后面比他大 或者 前面有多少比他大后面有多少比他小,乘加
树状数组解决统计k后面有多少比他大的数
解决方案:每个数用一个结构体{id和value}表示,按照value从小到大排序,然后使让后面大的id加1,即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int Max = + ;
typedef long long LL;
struct Node
{
int id,value;
};
Node data[Max];
int n,c[Max];
int cmp(Node x, Node y)
{
return x.value < y.value;
}
int lowbit(int k)
{
return k & (-k);
}
LL sum(int k)
{
LL ans = ;
while(k > )
{
ans += c[k];
k -= lowbit(k);
}
return ans;
}
void modify(int k, int y)
{
while(k <= n)
{
c[k] += y;
k += lowbit(k);
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
{
scanf("%d", &data[i].value);
data[i].id = i;
}
sort(data + , data + + n, cmp);
memset(c, , sizeof(c));
LL l, r, ans = ;
for(int i = ; i <= n; i++)
{
l = sum(data[i].id); //比 data[i].value小的个数
r = sum(n) - l; //总共比data[i].value 小的个数 - 左边比他小的个数 == 右边比他小的个数
ans += (l * (n - data[i].id - r)) + (r * (data[i].id - - l));
modify(data[i].id, ); //本来一直觉着是修改id + 1的值,不是,就是修改id值,修改id + 1就要sum(id - 1),id - 1可以是0
}
printf("%I64d\n", ans);
}
return ;
}

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. 高端大气上档次Ergotron Neo-Flex+MBP Retina的组合~

  2. LiveSDK初始化/登录时失败的解决办法

    环境描述 Windows 8.1+VS 2013 Update3+Live SDK 5.6 Metro风格的程序,集成LIVE认证 问题描述 如下图,提示Null Reference的异常. 解决办法 ...

  3. Python3.5 + django1.8.5 安装”import pymysql pymysql.install_as_MySQLdb()”的解决方法

    最近在学习Python,打算先看两个在线教程,再在github上找几个开源的项目练习一下,在学到"被解放的姜戈"时遇到django同步数据库时无法执行的错误,记录一下. 错误现象: ...

  4. RabbitMQ 主题(Topic)

    我们进步改良了我们的日志系统.我们使用direct类型转发器,使得接收者有能力进行选择性的接收日志,,而非fanout那样,只能够无脑的转发. 虽然使用direct类型改良了我们的系统,但是仍然存在一 ...

  5. jQuery能做些什么

    来源于: Learning jQuery, 4th Edition What jQuery does: 1. Access elements in a document; $('div.content ...

  6. linux 安装samba

    1. yum -y install samba 2. 配置 vi /etc/samba/smb.conf [global] 下面的 修改 workgroup = MYGROUPsecurity = s ...

  7. LINUX 配置IP

    1. 用命令查看一下IP配置:ifconfig, 修改网络配置文件  vi  /etc/sysconfig/network-scripts/ifcfg-eht0 2.但是,很多时候,较难记住里面文件的 ...

  8. 使用D3绘制图表(7)--饼状图

    这次是绘制饼状图,也是这一次使用D3绘制图表的最后一篇,大家可以从其他地方深入学习D3绘制图表,也可以直接查看D3的API进行学习,本次绘制饼状图的数据跟之前的卸载数组里面的不一样,这一次是使用d3的 ...

  9. easyui-datagrid 报错:TypeError: col is null

    一般是由于设置的属性用到的列,如: idField:'aa', sortName:'bb' 等在 columns:[[{field:'cc',width:80,title:'列cc'}, {field ...

  10. git 创建别名

    git config --global alias.shortname command 例子如下 git config --global alias.psm 'push origin master' ...