Ping pong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3674 Accepted Submission(s): 1352
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?
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).
1 3 1 2 3
1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lowbit(x) (x&(-x))
#define Maxn 20005
#define LL long long
using namespace std;
int tree[Maxn];
void add(int pos,int n)
{
for(int i=pos;i<=n;i+=lowbit(i))
tree[i]++;
}
int sum(int pos)
{
int ans=0;
for(int i=pos;i>0;i-=lowbit(i))
ans+=tree[i];
return ans;
}
struct PP
{
int pos;
int c;
}pp[Maxn];
bool cmp1(PP a,PP b)
{
return a.pos<b.pos;
}
bool cmp2(PP a,PP b)
{
return a.c<b.c;
}
int main()
{
int t,n,i;
LL ans;
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%d",&n);
memset(tree,0,sizeof(tree));
for(i=0;i<n;i++)
scanf("%d",&pp[i].c),pp[i].pos=i;
sort(pp,pp+n,cmp2);
for(i=0;i<n;i++)
pp[i].c=i+1;
sort(pp,pp+n,cmp1);
for(i=1;i<n-1;i++)
{
add(pp[i-1].c,n);
int lsumx=sum(pp[i].c-1);
int rsumd=n-i-pp[i].c+lsumx;
int lsumd=i-lsumx;
int rsumx=(pp[i].c-1-lsumx);
ans+=lsumx*rsumd+lsumd*rsumx;
}
printf("%I64d\n",ans);
}
return 0;
}
1万+




被折叠的 条评论
为什么被折叠?



