forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
baised_standings.cpp
66 lines (60 loc) · 1.7 KB
/
baised_standings.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*Problem Statement:
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. */
#include<bits/stdc++.h>
using namespace std;
int SumDistances(int n,int arr[],int rank)
{
int sum=0;
int actual_rank=1;
for(int i=1;i<=n;i++)
{
while(arr[i])
{
sum+=abs(actual_rank-i);
arr[i]--;
actual_rank++;
}
}
return sum;
}
int main()
{
int n,rank,arr[1000]={0};
string name;
cout<<"Enter the number of teams participating: "<<endl;
cin>>n;
cout<<"Enter the team names and preffered place respectively: "<<endl;
for(int i=0;i<n;i++)
{
cin>>name>>rank;
arr[rank]++;
}
cout<<"The badness of the best ranklist: "<<SumDistances(n,arr,rank)<<endl;
return 0;
}
/*
Example:
Input:-
Enter the number of teams participating:
7
Enter the team names and preffered place respectively:
noobz 1
llamas 2
winn3rz 2
5thwheel 1
notoricoders 5
strangecase 7
whoknows 2
Output:-
The badness of the best ranklist: 5
Time Complexity: O(n)
Space Complexity: O(n)
*/