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 ;
}