BAISED - Biased Standings

no tags 

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 specification

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.

Output specification

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



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

ThreeHeadedMonkey 1
MoscowSUx13 1
NeedForSuccess 1


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.

Note: the input file will not exceed 5MB.

hide comments
Vale: 2017-03-24 13:31:42

AC in two go because forgot to delete the dummy line that I use for debug

deep_1: 2017-03-18 13:18:07

AC in one go!!!

Vipul Srivastava: 2017-03-12 20:57:58

long long is required.. Constraints should be given

k_nushka: 2017-02-13 15:54:45

Long long -_-

papan_97: 2016-05-18 17:35:28

easy one ..AC in one go :)

Babu: 2015-08-23 21:50:57

what the hell dude! we have to assume all the constraints!! Guys,use long long int in the array and take a string of length 10000 .It worked

SangKuan: 2015-08-13 06:51:07

haha,after 4 wa(forgot memset),my O(n) code got ac.

SangKuan: 2015-08-13 04:53:06

easy,but use O(nlogn).

detel: 2015-07-16 09:33:46

There is very small change in time by switching from O(nlnn) to O(n) in Python....

krish: 2015-04-15 10:04:23

Easy one:)

Added by:Fudan University Problem Setters
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO
Resource:IPSC 2006