TOJ 2881.Biased Standings

题目链接:http://acm.tju.edu.cn/toj/showp2881.html


2881.    Biased Standings
Time Limit: 3.0 Seconds    Memory Limit: 65536K
Total Runs: 2321    Accepted Runs: 688     Multiple test files



Usually, results of competitions are based on the scores of participants. However, we are planning a change for the next year of IPSC. During the registration each team will be able to enter a single positive integer - their preferred place in the ranklist. We would take all these preferences into account, and at the end of the competition we will simply announce a ranklist that would please all of you.

But wait... How would that ranklist look like if it won't be possible to satisfy all the requests?

Suppose that we already have a ranklist. For each team, compute the distance between their preferred place and their place in the ranklist. The sum of these distances will be called the badness of this ranklist.

Problem specification

Given team names and their preferred placements find one ranklist with the minimal possible badness.

Input

The first line of the input file contains an integer  T specifying the number of test cases. Each test case is preceded by a blank line.

Each test case looks as follows: The first line contains N - the number of teams participating in the competition. Each of the next N lines contains a team name (a string of letters and numbers) and its preferred place (an integer between 1 and N, inclusive). No two team names will be equal.

You can assume that N ≤ 100,000 and the length of the team name is no more than 20.

Output

For each of the test cases output a single line with a single integer - the badness of the best ranklist for the given teams.

Sample Input

2

7
noobz 1
llamas 2
Winn3rz 2
5thwheel 1
NotoricCoders 5
StrangeCase 7
WhoKnows 7

3
ThreeHeadedMonkey 1
MoscowSUx13 1
NeedForSuccess 1

Sample Output

5
3

Hint: In the first test case, one possible ranklist with the minimal badness is:

1. noobz
2. llamas
3. Winn3rz
4. 5thwheel
5. NotoricCoders
6. WhoKnows
7. StrangeCase
In the second test case all ranklists are equally good.


Source: TJU Team Selection Contest 2007 (4)
Submit   List    Runs   Forum   Statistics

水题,sort,上代码:

#include <stdio.h>
#include <algorithm> 
#include <iostream>
#include <cmath> 
using namespace std;
struct Team{
	char name[21];
	int rank;
}team[100005];
bool cmp(Team a,Team b){
	return a.rank<b.rank;
}
int main(){
	int cast,n;
	scanf("%d",&cast);
	while(cast--){
		scanf("%d",&n);
		long long sum=0;
		for(int i=1;i<=n;i++)
			scanf("%s%d",team[i].name,&team[i].rank);
		sort(team+1,team+n+1,cmp);
		//cout<<team[1].name<<"  "<<team[1].rank<<endl;
		for(int i=1;i<=n;i++)
			sum+=(team[i].rank>i?team[i].rank-i:i-team[i].rank); 
		printf("%lld\n",sum);
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值