-
Notifications
You must be signed in to change notification settings - Fork 0
/
BAISED.cpp
43 lines (33 loc) · 858 Bytes
/
BAISED.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// https://www.spoj.com/problems/BAISED/
/*
Greedy approach will work in O(n) time.
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll minBadness(vector < ll > v){
ll s = v.size();
sort(v.begin(),v.end()); // sort the rank value
ll ans = 0;
for(ll i=0;i<s;i++){ // start assigning rank to each one
ans = ans + abs(v[i] - i - 1); // calculate absolute difference between the possible rank and desired rank
}
return ans;
}
int main(){
ll t;
cin>>t; // number of test cases
while(t--){
vector< ll > v;
ll n ;
cin >> n ;
string s;
ll temp;
for(ll i=0;i<n;i++){
cin>>s>>temp;
v.push_back(temp);
}
cout<<minBadness(v)<<endl;
}
return 0;
}