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,T≤20T,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(N≤100000)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 1 Sample 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[];
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=;
for(LL i=;i<n;i++){
cin>>aa[i].s>>aa[i].num;
}
sort(aa,aa+n,cmp);
for(LL i=;i<n;i++){
aa[i].id=i+;
sum+=abs(aa[i].id-aa[i].num);
}
cout<<sum<<endl;
}
return ;
}
 

Kattis - Biased Standings的更多相关文章

  1. ural 1100. Final Standings(数据结构)

    1100. Final Standings Time limit: 1.0 secondMemory limit: 16 MB Old contest software uses bubble sor ...

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

    http://kenwublog.com/theory-of-java-biased-locking 阅读本文的读者,需要对Java轻量级锁有一定的了解,知道lock record, mark wor ...

  3. It's a Mod, Mod, Mod, Mod World Kattis - itsamodmodmodmodworld (等差数列求和取模)

    题目链接: D - It's a Mod, Mod, Mod, Mod World Kattis - itsamodmodmodmodworld 具体的每个参数的代表什么直接看题面就好了. AC代码: ...

  4. A - Piece of Cake Kattis - pieceofcake (数学)

    题目链接: A - Piece of Cake Kattis - pieceofcake 题目大意:给你一个多边形,然后给你这个多边形的每个点的坐标,让你从这个n个点中选出k个点,问这个k个点形成的面 ...

  5. Subsequences in Substrings Kattis - subsequencesinsubstrings (暴力)

    题目链接: Subsequences in Substrings Kattis - subsequencesinsubstrings 题目大意:给你字符串s和t.然后让你在s的所有连续子串中,找出这些 ...

  6. G - Intersecting Rectangles Kattis - intersectingrectangles (扫描线)(判断多个矩形相交)

    题目链接: G - Intersecting Rectangles Kattis - intersectingrectangles 题目大意:给你n个矩形,每一个矩形给你这个矩形的左下角的坐标和右上角 ...

  7. E - Emptying the Baltic Kattis - emptyingbaltic (dijkstra堆优化)

    题目链接: E - Emptying the Baltic Kattis - emptyingbaltic 题目大意:n*m的地图, 每个格子有一个海拔高度, 当海拔<0的时候有水. 现在在(x ...

  8. G - Galactic Collegiate Programming Contest Kattis - gcpc (set使用)

    题目链接: G - Galactic Collegiate Programming Contest Kattis - gcpc 题目大意:当前有n个人,一共有m次提交记录,每一次的提交包括两个数,st ...

  9. JVM内部细节之二:偏向锁(Biased Locking)

    在前面一片文章<JVM内部细节之一:synchronized关键字及实现细节>中已经提到过偏向锁的概念,在理解什么是偏向锁前必须先理解什么是轻量级锁(Lightweight Locking ...

随机推荐

  1. LinQ—扩展方法

    概述 本节主要解说扩展方法,涉及LinQ的详细知识不多. 扩展方法的描写叙述 .net framework为编程人员提供了非常多的类,非常多的方法,可是,不论.net framework在类中为我们提 ...

  2. jQuery中开发插件的两种方式

    jQuery中开发插件的两种方式(附Demo) 做web开发的基本上都会用到jQuery,jQuery插件开发两种方式:一种是类扩展的方式开发插件,jQuery添加新的全局函数(jQuery的全局函数 ...

  3. parent和top

    function show(){ //parent是获取本窗口的父窗口   //window.parent.location.href="http://www.baidu.com" ...

  4. [转]当图片源大小大于ImageView大小时的处理方式(缩放)

    转一篇当图片源大小大于ImageView大小定死时的处理方法,但不适用于图片大小小于ImageView时的情况,因为inSampleSize不能<1, 谁有特别好的放大的解决方案,除了设置Ima ...

  5. 微软 PowerShell Script Explorer

    微软 PowerShell Script Explorer 满血复活,正式发布 一年前的今天,微软在其Windows PowerShell官方博客声明中止 ‘Script Explorer’ 应用程序 ...

  6. NHibernate:教你如何搭建数据访问层?

    NHibernate:教你如何搭建数据访问层? 什么是NHibernate NHibernate 是一个基于.net 的针对关系型数据库的对象持久化类库.NHibernate 来源于非常优秀的基于Ja ...

  7. MVC视图中的@Html.xxx(...)

    ASP.NET MVC视图中的@Html.xxx(...)   问题 在视图页中@Html.xxx(...)是什么?如何被执行? 如下图所示: 解疑 视图页中@Html.xxx(...)涉及的内容有: ...

  8. iOS基础 - 数据存取

    一.iOS应用数据存储的常用方式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedArchiver归档 SQLite3 Core Data 二.应用沙盒 每个iOS应 ...

  9. 加载DLL模块

    关于Windows平台下应用程序加载DLL模块的问题. 本文将讨论以下问题: (1)Windows可执行程序会从哪些目录下加载DLL. (2)如何将可执行使用的DLL放置到统一的目录下,而不是与EXE ...

  10. json序列化NHibernate的实体

    在使用nhibernate时,想将实体对象序列化成json字符串,然后打印在日志中. 序列化时会出现问题,应该是因为这个实体被hibernate管理的原因.具体原因没有分析. 解决方案:为实体创建一个 ...