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 题意:一群乒乓爱好者居住在一条直线上,如果两个人想打比赛需要一个裁判,裁判的 位置 必须在两者之间 ,裁判的能力也必须不大于 参赛者最大的,不小于参赛者最小的 白皮 ...
随机推荐
- 线段相交 poj 1066
// 线段相交 poj 1066 // 思路:直接枚举每个端点和终点连成线段,判断和剩下的线段相交个数 // #include <bits/stdc++.h> #include <i ...
- MFC工程目录
如果已经以Debug方式编译链接过程序,则会在解决方案文件夹下和工程子文件夹下各有一个名为“Debug”的文件夹,而如果是Release方式编译则会有名为“Release”的文件夹.这两种编译方式将产 ...
- 基础排序算法,java实现(快速,冒泡,选择,堆排序,插入)
1.冒泡排序: (1)比较相邻的元素.如果第一个比第二个大,就交换他们两个. (2)外面再套个循环就行. 算法复杂度:O(N2) 不罗嗦,上代码: //冒泡排序(两两交换,外加一个外循环) pub ...
- j2ee指导型框架或示例
springside appfuse springfuse
- 无线 WIFI 的13个信道频率范围
目前主流的无线WIFI网络设备不管是802.11b/g还是802.11b/g/n 一般都支持13个信道.它们的中心频率虽然不同,但是因为都占据一定的频率范围,所以会有一些相互重叠的情况.下面是13个信 ...
- CF#190DIV.1
/* CF#190DIV.1-C 题意:给你n个结点的树,给这些结点标记字母AB..Z,对于标记相同的结点路径上 的结点的标记必须有一个是大于该标记的:问是否可以标记(A是最大标记) 分析:整天思路就 ...
- UVA 11300 Spreading the Wealth
题目大意:n个人手中有些金币,每个人可给相邻两个人一些金币,使得最终每个人手中金币数相同,求被转手的金币最少数 m为最终每个人手中的金币数,a1,a2,a3,...,an为每个人开始时手中的金币数,x ...
- [iOS基础控件 - 6.11.5] 沙盒 & 数据存储
A.沙盒 每个APP都有一个沙盒,是独立存在的 1.Xcode5和Xcode6的模拟器文件目录 a.模拟器路径改版 (1)Xcode5中模拟器路径为:/Users/用户名/Library/Appl ...
- php-fpm 启动不了 libiconv.so.2找不到
service php-fpm start Starting php-fpm /alidata/server/php/sbin/php-fpm: error while loading shared ...
- UVaLive 7269 Snake Carpet (找规律,模拟)
题意:给定一个数字n,表示有n条蛇,然后蛇的长度是 i ,如果 i 是奇数,那么它只能拐奇数个弯,如果是偶数只能拐偶数个,1, 2除外,然后把这 n 条蛇, 放到一个w*h的矩阵里,要求正好放满,让你 ...