树状数组+逆序数与顺序数——HDU 2492

对应HDU题目:点击打开链接


Ping pong
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

N(3$ \le$N$ \le$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$ \le$T$ \le$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 a1a2...aN follow, indicating the skill rank of each player, in the order of west to east ( 1$ \le$ai$ \le$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

题意:有N个人排成一行,每个人有不同的等级,两个人要决斗的话就要找一个裁判,裁判的等级要在他们之间,而且裁判也要站在他们之间。问一共能组成多少个不同的比赛。

思路:举个例子(等级不一定是1~N)

1 3 7 6 9 8 5

如果6是裁判,那左边等级比他低的人数(顺序数)a= 2;右边等级比他高的人数(顺序数)b = 2;左边等级比他高的人数(逆序数)c= 1;右边等级比他低的人数(逆序数)d = 1;

所以6做裁判的话就能组成a*b + c*d = 5个不同的比赛。

从左到右for一遍,把每个人做裁判能组成的比赛数目加起来就是结果。

逆序数与顺序数的求解可用树状数组

具体实现代码:


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
const int MAXN=20000+10;
using namespace std;
int a[MAXN];
int highl[MAXN];//highl[i]表示第i个数左边比a[i]大的数的个数
int highr[MAXN];//highr[i]表示第i个数右边比a[i]大的数的个数
int lowl[MAXN];//lowl[i]表示第i个数左边比a[i]小的数的个数
int lowr[MAXN];//lowr[i]表示第i个数右边比a[i]小的数的个数
int c[MAXN*5];

int lowbit(int x)
{
	return x&(-x);
}

void update(int x, int num)
{
	while(x <= 5*MAXN-2)
	{
		c[x] += num;
		x += lowbit(x);
	}
}

int sum(int x)
{
	int res = 0;
	while(x > 0)
	{
		res += c[x];
		x -= lowbit(x);
	}
	return res;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int n;
		memset(c,0,sizeof(c));
		scanf("%d", &n);
		for(int i=0; i<n; i++){
			scanf("%d", a+i);
			int x = a[i];
			highl[i] = sum(5*MAXN) - sum(x);
			lowl[i] = sum(x);
			update(x,1);
		}
		memset(c,0,sizeof(c));
		for(int i=n-1; i>=0; i--){
			int x = a[i];
			highr[i] = sum(5*MAXN) - sum(x);
			lowr[i] = sum(x);
			update(x,1);
		}
		long long Sum = 0;
		for(int i=0; i<n; i++){//乘法原理和加法原理
			Sum += (long long) highl[i] * lowr[i];
			Sum += (long long) lowl[i] * highr[i];
		}
		printf("%lld\n", Sum);
	}
	return 0;
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值