Ping pong
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3139   Accepted: 1157

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
【分析】没想到此题可以用树状数组做。首先用结构体记录输入数字的大小及下标,然后按数值从小到大排序。然后枚举中间数字,及a[i]<a[k]<a[j]中的a[k]。然后用排序后的数组依次计算,树状数组tree记录的是
到当前位置已经出现过的数的数目,那么到后面,这些数自然就比他小了,感觉很神奇。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e4+;
const int M = ;
const int mod=1e9+;
int tree[N],n,m;
struct man{
int pos,rank;
bool operator< (const man &it)const{
return rank<it.rank;
}
};
void add(int k,int num){
while(k<=n){
tree[k]+=num;
k+=k&(-k);
}
}
int Sum(int k){
int sum=;
while(k>){
sum+=tree[k];
k-=k&(-k);
}
return sum;
}
int main() {
int t;
scanf("%d",&t);
while(t--){
met(tree,);
man a[N];
ll ans=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i].rank);
a[i].pos=i;
}
sort(a+,a+n+);
add(a[].pos,);
for(int i=;i<n;++i){
int ls,lb,rs,rb;
ls=Sum(a[i].pos-);
lb=a[i].pos--ls;
rs=Sum(n)-Sum(a[i].pos);
rb=n-rs-a[i].pos;
ans+=ls*rb+lb*rs;
add(a[i].pos,);
}
printf("%lld\n",ans);
}
return ;
}

POJ 3928 Ping pong(树状数组)的更多相关文章

  1. POJ 3928 Ping pong 树状数组模板题

    開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...

  2. poj3928 Ping pong 树状数组

    http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  3. Ping pong(树状数组经典)

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. UVA 1428 - Ping pong(树状数组)

    UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...

  5. LA 4329 Ping pong 树状数组

    对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...

  6. LA4329 Ping pong 树状数组

    题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...

  7. UVALive - 4329 Ping pong 树状数组

    这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...

  8. HDU 2492 Ping pong (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...

  9. LA 4329 - Ping pong 树状数组(Fenwick树)

    先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...

  10. POJ 3928 Ping pong

    题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...

随机推荐

  1. how to reset mac root password

    Reset 10.5 Leopard & 10.6 Snow Leopard password Power on or restart your Mac. At the chime (or g ...

  2. 国产ProcessOn和国外gliffy的对比区别【原创】

    之前一直在用国外的作图工具gliffy,不足之处gliffy是英文的,很多国内相关从业者使用起来就有一定门槛,今天我给大家再推荐一款比gliffy更方便的作图工具ProcessOn,除了绘制UML建模 ...

  3. SQL中 char、varchar、text 和 nchar、nvarchar、ntext的区别

    1.char.char存储定长数据很方便,char字段上的索引效率级高,比如定义char(10),那么不论你存储的数据是否达到了10个字节,都要占去10个字节的空间.             2.va ...

  4. Python开发入门与实战1-开发环境

    1.搭建Python Django开发环境 1.1.Python运行环境安装 Python官网:http://www.python.org/ Python最新源码,二进制文档,新闻资讯等可以在Pyth ...

  5. java基础-003

    10.进程和线程 进程是执行者的应用程序,而线程是进程内部的一个执行序列.一个进程可以有多个线程.线程又叫轻量级进程. 创建线程的三种方式: I> 继承Thread类 II> 实现Runn ...

  6. Spring计划会议内容

    我们的小组成员是     王伟光,杨世超,苏海岩,曹锦锋,李夏蕾,闫立新.  组长为闫立新. 经过昨天课堂上的讨论,我们确定了未来一周里的工作内容和目标,以及每个人的任务. 我们确定本周的最终目标是实 ...

  7. 端口占用问题——netstat命令

    1.查看所有的端口占用情况 C:\>netstat -ano 协议 本地地址            外部地址   状态           PID(进程号) TCP 127.0.0.1:1434 ...

  8. jQuery之load、unload、onunload和onbeforeunload

    1.load:jQuery load() 方法是简单但强大的 AJAX 方法.load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法:$(selector).load(URL,dat ...

  9. VS2013失去智能提示如何恢复

    一般智能提示包括,输入智能提示,鼠标移到类,方法,接口,变量上面自动提示相关信息,VS2013常常会失去这种提示功能,遇到这种情况可以这样解决: 1.在开发环境中随便打开一个xxx.aspx页面,也就 ...

  10. Executing modules as scripts

    When you run a Python module with python fibo.py <arguments> the code in the module will be ex ...