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. 2015/11/9用Python写游戏,pygame入门(8):按钮和游戏结束

    昨天没有更新内容,今天相对多写一些. 因为我们已经基本完成游戏框架,但是游戏结束后,并不知道怎样比较好开始.我本来本着懒的原则,想结束后显示一个黑屏,然后你重新点一下鼠标就重新开始.但是那样实在太不像 ...

  2. .Net下一个类型转换神器

    引言 类型转换经常遇到,最常用的应该是string类型转换为其它基元类型,常见于http参数类型转换.Convert静态类的Convert.ChangeType()方法可以把实现IConvertibl ...

  3. [POJ2104]K-th Number

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 34048   Accepted: 10810 Ca ...

  4. android 按钮点击效果实现

    在其他人的博客里看到其实实现按钮点击效果的方法有很多,这里提到的只是其中一个办法 图片素材(我自己用截图截的,可以自己搞) 放到mipmap目录下(studio是在这个目录下 , eclipse 直接 ...

  5. C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据

    转载:http://www.cnblogs.com/kivenhou/archive/2013/02/22/2921954.html 操作List前请设置SPWeb的allowUnsafeUpdate ...

  6. 利用反射,泛型,静态方法快速获取表单值到Model

    在项目中经常需要处理表单,给model赋值,很烦人的一些重复代码.如下边的代码: News news = new News(); news.Id = int.Parse(Request.Form[&q ...

  7. Chrome开发工具Elements面板(编辑DOM和CSS样式)详解

    Element 译为“元素”,Element 面板可以让我们动态查看和编辑DOM节点和CSS样式表,并且立即生效,避免了频繁切换浏览器和编辑器的麻烦. 我们可以使用Element面板来查看源代码,它不 ...

  8. 关于 HTTP 请求头的内容

    HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应.就整个网络资源传 ...

  9. javascript 红宝书笔记之函数、变量、参数、作用域

    ECMAScript 不介意传进来多少个参数,也不介意传进来的参数类型.     理解参数:      命名的参数只提供便利,不是必需的.     ECMAScript 的变量包含两种不同的数据类型的 ...

  10. Cannot change version of project facet Dynamic Web Module to 3.1

    最近项目一直报错,看的极度的不爽,于是找了很长时间的解决方案. 先说一下环境Spring + SpringMVC + MAVEN + jdk 1.8 + servlet 3.1 由于是web 项目,这 ...