Ping pong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4094    Accepted Submission(s):
1522

Problem 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

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=;
int c[MS];
int n; struct node
{
int rank;
int id;
bool operator <(const node &a)const
{ // rank unique
return rank>a.rank||(rank==a.rank&&id<a.id);
}
}nodes[MS]; int lowbit(int x)
{
return x&(-x);
} void updata(int x)
{
while(x<=n)
{
c[x]+=;
x+=lowbit(x);
}
} int getsum(int x)
{
int ret=;
while(x>)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
{
scanf("%d",&nodes[i].rank);
nodes[i].id=i;
}
sort(nodes+,nodes+n+);
LL ans=; // 一定要 LL 不然WA
for(int i=;i<=n;i++)
{
int id=nodes[i].id;
int x=getsum(id); // 值比它大,坐标比它小的数量
int y=i--x; // 值比它大的数总共有i-1,-x就是
// 值它大,坐标比它大
ans+=x*(n-id-y)+y*(id--x);
updata(id);
}
printf("%lld\n",ans);
}
return ;
}

Ping pong的更多相关文章

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

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

  2. UVALive 4329 Ping pong

                                      Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Fo ...

  3. POJ 3928 Ping pong(树状数组)

                                                                          Ping pong Time Limit: 1000MS   ...

  4. LA4329 Ping pong(树状数组与组合原理)

    N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...

  5. POJ 3928 Ping pong

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

  6. Frequent values && Ping pong

    Frequent values 题意是不同颜色区间首尾相接,询问一个区间内同色区间的最长长度. 网上流行的做法,包括翻出来之前POJ的代码也是RMQ做法,对于序列上的每个数,记录该数向左和向右延续的最 ...

  7. 【暑假】[实用数据结构]UVAlive 4329 Ping pong

    UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: % ...

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

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

  9. hdu2492 Ping pong

    hdu2492 Ping pong 题意:一群乒乓爱好者居住在一条直线上,如果两个人想打比赛需要一个裁判,裁判的 位置 必须在两者之间 ,裁判的能力也必须不大于 参赛者最大的,不小于参赛者最小的 白皮 ...

随机推荐

  1. nodejs学习笔记之mongoDB

    这两天在学习nodejs,但是发现那本书nodejs入门指南上所用的好多方法都报错. 这里主要说下数据库部分 关于注册部分:书上创建数据库那里可能要小心点,用户名不存在的时候,下面调用save的对象要 ...

  2. bzoj 2095: [Poi2010]Bridges(二分法+混合图的欧拉回路)

    [题意] 给定n点m边的无向图,对于边u,v,从u到v边权为c,从v到u的边权为d,问能够经过每条边一次且仅一次,且最大权值最小的欧拉回路. [思路] 二分答案mid,然后切断权值大于mid的边,原图 ...

  3. 12306验证图片的bug

    刚才有人告诉我12306验证码换了,于是我就打开了看了看,点了点.oh no , i am really sorry ,12306.

  4. 虚拟桌面基础架构(VDI)与终端服务和传统PC对比

    VDI(Virtual Desktop Infrastructure),即虚拟桌面基础架构,正迅速成为一个热门词汇,它将颠覆企业向终端用户交付应用的游戏规则.这篇专题就是想通过VDI与两种传统技术的对 ...

  5. 【转】Maven实战(八)---模块划分

    本博文出自于:http://blog.csdn.net/liutengteng130/article/details/47000217    感谢! 为了防止传递依赖,我们各个模块之间尽量用直接依赖的 ...

  6. Spark SQL概念学习系列之为什么使用 Spark SQL?(二)

    简单地说,Shark 的下一代技术 是Spark SQL. 由于 Shark 底层依赖于 Hive,这个架构的优势是对传统 Hive 用户可以将 Shark 无缝集成进现有系统运行查询负载. 但是也看 ...

  7. homework_06 围棋程序改进

    1) 把程序编译通过, 跑起来. 读懂程序,在你觉得比较难懂的地方加上一些注释,这样大家就能比较容易地了解这些程序在干什么. 把正确的 playPrev(GoMove) 的方法给实现了. 注释见Git ...

  8. JavaFX 2 Dialogs

    http://edu.makery.ch/blog/2012/10/30/javafx-2-dialogs/ ———————————————————————————————————————————— ...

  9. log4j2使用总结

    一.Log4j有三个主要的组件:Loggers,Appenders和Layouts,这里可简单理解为日志级别,日志要输出的地方和日志格式 1. Logger Logger的日志级别有6级,分别是TRA ...

  10. Bootstrap栅格系统(布局)

    栅格系统(布局) Bootstrap内置了一套响应式.移动设备优先的流式栅格系统,随着屏幕设备或视口(viewport)尺寸的增加,系统会自动分为最多12列. 我在这里是把Bootstrap中的栅格系 ...