【33.20%】【LA 4320】【Ping pong】

【Description】

N (3 ≤ N ≤ 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. Iftwo players want to compete, they must choose a referee among other ping pong players and hold thegame in the referee’s house. For some reason, the contestants can’t choose a referee whose skill rank ishigher or lower than both of theirs. The contestants have to walk to the referee’s house, and becausethey are lazy, they want to make their total walking distance no more than the distance between theirhouses. Of course all players live in different houses and the position of their houses are all different. Ifthe 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 ≤ 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 Ndistinct 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).

【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


【题解】

就是说要进行乒乓球比赛。

必须两个player和一个裁判。

然后裁判的能力值和住的地方都在两个players之间。

然后每个人住的房间在一条由西到东的笔直街道上。

每个人的能力值都不一样。

每个人都能当裁判。

设c[i]表示前i个人里面有几个人的能力值比第i个人小。

设d[i]表示i+1..n这些人里面有几个人的能力值比第i个人小。

i-1-ci 就是前i个人里面比第i个人的能力值大的人的个数

n-i-di就是i+1..n这些人里面比第i个人的能力值大的人的个数。

则答案就是

∑ ci*(n-i-di) + (i-ci-1)*di

因为ai不大最多为10W.

则用树状数组处理出ci和di即可。

然后ci是从前往后。di是从后往前。

LA不支持%I64d的输出

【代码】

#include <cstdio>
#include <cstring>

const int MAXN = 101000;

int n,a[MAXN],bmi[MAXN],ami[MAXN],c[MAXN],d[MAXN];
long long ans;

void init()
{
	memset(bmi, 0, sizeof(bmi));
	memset(ami, 0, sizeof(ami));
	ans = 0;
}

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

void input_data()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++)
	{
		int leijia = 0;
		int temp = a[i]; //累加a[i]的前缀和
		while (temp > 0)
		{
			leijia += bmi[temp];
			temp -= lowbit(temp);
		}
		c[i] = leijia;
		temp = a[i];
		while (temp <= 100000)
		{
			bmi[temp]++; //只加1
			temp += lowbit(temp);
		}
	}
	for (int i = n; i >= 1; i--)//di与ci处理相同
	{
		int leijia = 0;
		int temp = a[i];
		while (temp > 0)
		{
			leijia += ami[temp];
			temp -= lowbit(temp);
		}
		d[i] = leijia;
		temp = a[i];
		while (temp <= 100000)
		{
			ami[temp]++;
			temp += lowbit(temp);
		}
	}
}

void get_ans()
{
	for (int i = 2; i <= n - 1; i++)
	{
		ans += (long long) c[i] * (n - i - d[i]);
		ans += (long long ) (i - 1 - c[i]) * d[i];
	}
}

void output_ans()
{
	printf("%lld\n", ans);
}

int main()
{
	//freopen("F:\\rush.txt", "r", stdin);
	int T;
	scanf("%d", &T);
	while (T--)
	{
		init();
		input_data();
		get_ans();
		output_ans();
	}
	return 0;
}



转载于:https://www.cnblogs.com/AWCXV/p/7632256.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值