Kattis - Biased Standings

Posted tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kattis - Biased Standings相关的知识,希望对你有一定的参考价值。

Biased Standings

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 waitHow 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.

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,T20T,T≤20, 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 an integer N(N100000)N(N≤100000) – the number of teams participating in the competition. Each of the next NN lines contains a team name (a string of letters and numbers of length at most 20) and its preferred place (an integer between 1 and NN, inclusive). No two team names will be equal.

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 1Sample Output 1
2

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

3
ThreeHeadedMonkey 1
MoscowSUx13 1
NeedForSuccess 1
5
3

题意

 给出队名,首选位置,然后给他们排一个实际的表,求实际位置和首选位置的差值和最小

思路

只要将首选位置排个序然后就直接算差值

代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long
struct Node{
    string s;
    int id;
    int num;
}aa[1000000];
bool cmp(Node a,Node b){
    return a.num<b.num;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        LL sum=0;
        for(LL i=0;i<n;i++){
            cin>>aa[i].s>>aa[i].num;
        }
        sort(aa,aa+n,cmp);
        for(LL i=0;i<n;i++){
            aa[i].id=i+1;
            sum+=abs(aa[i].id-aa[i].num);
        }
        cout<<sum<<endl;
    }
    return 0;
}

 

 

以上是关于Kattis - Biased Standings的主要内容,如果未能解决你的问题,请参考以下文章

Biased Random Number Generator - Java

Java偏向锁实现原理(Biased Locking)

TPO-22 C1 Complain about a biased article

Kattis, Kattis 的一些问题题解

Kattis, Kattis 的一些问题题解

Kattis-Beekeeper