Ping pong
Ping pong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4094 Accepted Submission(s):
1522
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?
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).
integer, the total number of different games.
3 1 2 3
#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的更多相关文章
- HDU 2492 Ping pong (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...
- UVALive 4329 Ping pong
Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Fo ...
- POJ 3928 Ping pong(树状数组)
Ping pong Time Limit: 1000MS ...
- LA4329 Ping pong(树状数组与组合原理)
N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...
- POJ 3928 Ping pong
题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...
- Frequent values && Ping pong
Frequent values 题意是不同颜色区间首尾相接,询问一个区间内同色区间的最长长度. 网上流行的做法,包括翻出来之前POJ的代码也是RMQ做法,对于序列上的每个数,记录该数向左和向右延续的最 ...
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: % ...
- Ping pong(树状数组经典)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu2492 Ping pong
hdu2492 Ping pong 题意:一群乒乓爱好者居住在一条直线上,如果两个人想打比赛需要一个裁判,裁判的 位置 必须在两者之间 ,裁判的能力也必须不大于 参赛者最大的,不小于参赛者最小的 白皮 ...
随机推荐
- java 复习003
今天主要复习下数据结构的东西 树 自平衡二叉查找树 AVL树(高平衡树)(wiki) 特性:任何节点的两个子树的高度最大差别为一 时间复杂度:查找.插入和删除在平均和最坏情况下都是O(log n) 红 ...
- 轻松突击ThreadLocal
本文出自 代码大湿 代码大湿 ThreadLocal是用来保存线程的本地变量,可以保证每个线程都有一个自己的变量(包括static变量). 本文所有代码请点击我 1 看个实际场景. 我们要设计一个序列 ...
- JEE , EJB概念深入概括
说起EJB,不得不提JEE,java EE 英文全称为:java Enterprise Edition企业级应用的软件架构,是一种思想,也是一种规范,方便从事这方面的开发者以及开发厂商进行规范性的开发 ...
- 第二百六十一、二天 how can I坚持
昨天在家宅了一天,晚上去看了个电影<寻龙诀>,感觉一般,虽然有黄渤.舒淇.十分打7分吧,画面倒是很震撼. 今天,去了趟东升八家郊野公园,想买个篮球玩玩来,可是超市里的太坑了,都是赠品还拿出 ...
- Java设计模式系列之观察者模式
观察者模式 Observer的定义 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象. 这个主题对象在状态上发生变化时,会通知所有观察者对象,让它们能够自动更新自己. 第一 ...
- iOS中的谓词(NSPredicate)使用
http://www.cocoachina.com/ios/20160111/14926.html 首先,我们需要知道何谓谓词,让我们看看官方的解释: The NSPredicate class is ...
- [MAC OSX - 1] OSX10.10不能安装JKD8,不能使用eclipse
(1)电脑升级为10.10后,打开eclipse总是提示"您需要安装旧 Java SE 6 运行环境才能打开"Eclipse". 解决:安装JKD (2)不能安装JK ...
- Django 使用原生SQL
def dictfetchall(cursor): "将游标返回的结果保存到一个字典对象中" desc = cursor.description return [ dict(zip ...
- C# random(number)
C#随机函数Random()的用法 出自:http://www.cnblogs.com/wang726zq/archive/2012/04/28/2474711.html http://blog.cs ...
- oracle连接由于防火墙设置导致超时的问题
当应用程序使用数据库连接池进行数据连接时,防火墙的设置有可能会导致连接出现超时或者被重置的问题.当从数据库读数据的时候 有可能会 Connection timed out, 这是由于应用会缓存 ...