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. CentOS VPS创建pptpd VPN服务

    原文地址http://www.hi-vps.com/wiki/doku.php?id=xen_vps_centos6_install_pptpd CentOS VPS创建pptpd VPN服务 Xen ...

  2. Ubuntu 软件包管理详解

    原文转载自:http://www.cppblog.com/jb8164/archive/2009/01/09/71583.html Ubuntu 方便宜用,最值得让人称道的便是其安装软件的方式, 一条 ...

  3. 题目1434:今年暑假不AC (项目安排类:结束时间快排,判断开始时间)

    题目描述: “今年暑假不AC?”“是的.”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%...”确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视作为 ...

  4. SRM 502 DIV1 500pt(DP)

    题目简述 给定比赛时间T和n个题目,你可以在任意时间提交题目,每个题目有一个初始分数maxPoints[i],每个单位时间题目的分数将会减少pointsPerMinute[i],即如果在时间t解决了第 ...

  5. UVa12657 - Boxes in a Line(数组模拟链表)

    题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...

  6. public, protected, private, internal, protected internal简析

    public是可访问权限最高的,比如姓名,每个人都可以知道别人的姓名,这个不是什么秘密 protected的访问权限要低些,只有子类才可以访问得到父类的protected属性.就好像老子的财产只有儿子 ...

  7. Linux下MongoDB备份脚本

    #!/bin/bash today=`date +%Y%m%d` mongodump -h localhost -d salary -o /home/chzhao/mongobackup/$today ...

  8. CCS 5 XDS100 仿真连接错误Error connecting to the target【瓦特芯笔记】

      问题现象:在点击仿真是出现连接错误: Error connecting to the target: (Error -151 @ 0x0) One of the FTDI driver funct ...

  9. fixed 定位 苹果手机输入框触发时内容全部隐藏

    问题出现在东钿微信公众号用户注册页面 页面中只有两个输入框 页面没有超过一屏,悬浮按钮也要出现在本页面 ,开始布局页面的时候没什么问题,然后我在我自己手机上测试 ,输入手机号码,非常奇怪的问题出现了, ...

  10. Erp:原料投入产出报表

    USE [ChangHongWMS612]GO /****** Object: StoredProcedure [dbo].[st_WMS_RptMaterialInOutDaily] Script ...