forked from highpeaksw/coding-assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
71 lines (64 loc) · 1.75 KB
/
Copy pathcode.cpp
File metadata and controls
71 lines (64 loc) · 1.75 KB
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
66
67
68
69
70
71
#include<bits/stdc++.h>
using namespace std;
int getNumber(string temp)
{
string prev;
stringstream ss(temp);
while (ss >> temp)
prev = temp;
return stoi(prev);
}
string getName(string temp){
string res;
stringstream ss(temp);
while (ss >> temp){
if(res.empty())
res = temp;
else
res = res +" "+ temp;
if(temp.back()==':'){
res.pop_back();
return res;
}
}
return res;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int numberOfEmployees=0;
string temp, prev;
getline(cin, temp);
vector<int> prices;
unordered_map<int, string> gifts;
numberOfEmployees = getNumber(temp);
getline(cin, temp);
getline(cin, temp);
getline(cin, temp);
while (getline(cin, temp)){
if (temp.empty())
break;
string name = getName(temp);
int price = getNumber(temp);
gifts[price] = name;
prices.push_back(price);
}
sort(prices.begin(), prices.end());
int minDiff = INT_MAX;
int start = 0;
int n = prices.size();
for (int i = n - 1; i - numberOfEmployees>=0; i--){
int curDiff = prices[i]-prices[i-numberOfEmployees];
if(curDiff < minDiff){
minDiff = curDiff;
start = i - numberOfEmployees+1;
}
}
cout << "The goodies selected for distribution are:\n\n";
for (int i = start; i < start + numberOfEmployees; i++)
cout
<< gifts[prices[i]] << ": " << prices[i] << '\n';
cout << "And the difference between the chosen goodie with highest price and the lowest price is " << prices[start + numberOfEmployees-1] - prices[start] << '\n';
return 0;
}