Ranks Average(归并排序求逆序数)

本文介绍了一种用于计算游戏成绩平均排名的算法,通过分析每个游戏结束后的得分及其排名来得出整体平均排名,并进行四舍五入处理。

Judge Info


  • Memory Limit: 32768KB
  • Case Time Limit: 5000MS
  • Time Limit: 5000MS
  • Judger: Number Only Judger


Description


We are playing a game, with the objective of accumulating as many points as possible. At the end of each game, the player's score and rank are displayed. The score, an integer between 0 and 1,000,000,000, is that achieved by the player in the game just ended. The rank is displayed as "r of n". n is the total number of games ever played, and r is the position of the score for the just-ended game within this set. More precisely, r is one greater than the number of games whose score exceeds that of the game just ended.


Your job is simply, find out the average of the ranks (rounded to two digits after the decimal).


Input


The first line of the input contains a positive t(1 \leq t \leq 10), integer indicating the number of test cases in the input. For each test case, the first line contains a positive integer, n(1 \leq n \leq 100,000), the total number of games played. n lines follow, given the scores of these games, in order.


Output


Output the average of the ranks (rounded to two digits after the decimal) in one line, for each test case.


Sample Input

2
5
10
20
15
17
5
5
10
20
15
17
5


Sample Output

2.20
2.20


大致意思是用户输入n表示n场游戏,然后输入每场游戏得分,例如第一个测试

最开始是10,此时10的排名是1,接着是20,20在当前的排名是1,15在当前排名2,17在当前排名2,最后5在当前排名5

则输出(1+1+2+2+5)/5的值保留两位。

无非就是某个数字之前比自己大的数字数量之和,赤果果的逆序数哟,归并是逆序数的好基友。

#include<stdio.h>
int ta[100000];
int aa[100000];
double rank;
void sort(int *a,int x,int y,int *t)
{
	if(y-x>1)
	{
		int m=x+(y-x)/2;
		int p=x;
		int q=m;
		int i=x;
		sort(a,x,m,t);
		sort(a,m,y,t);
		while(p<m||q<y)
		{
			if(p==m)
			{
				t[i++]=a[q++];
				continue;
			}
			if(q==y)
			{
				t[i++]=a[p++];
				continue;
			}
			if(a[p]<=a[q])
			t[i++]=a[p++];
			else
			{
				t[i++]=a[q++];
				rank+=(m-p);
			}
		}
		for(int j=x;j<y;j++)
		a[j]=t[j];
	}
}
 
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		rank=0;
		int n;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		scanf("%d",&aa[i]);
		sort(aa,0,n,ta);
		rank+=n;
		double sum=rank/n;
		printf("%.2f\n",sum);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值