-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURI_1023.cpp
73 lines (57 loc) · 2.1 KB
/
URI_1023.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
66
67
68
69
70
71
72
73
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
int getRatioIndex(const std::map<int, int>& ratioAtIndex, int ratioToFind)
{
// If itr is equal to the end of this map, the given ratio wasn't inserted yet
auto itr = ratioAtIndex.find(ratioToFind);
return (itr != ratioAtIndex.end()) ? itr->second : -1;
}
int main()
{
int n{}, residents{}, consumption{}, city{};
int totalRes{}, totalCons{};
std::vector<std::pair<int, int>> res_cons;
std::map<int, int> ratioAtIndex;
std::cout.precision(2);
while(std::cin>>n && n)
{
res_cons.reserve(n);
int ratio{}, distinctRatios{};
for(int i = 0; i < n; ++i)
{
std::cin>>residents>>consumption;
ratio = consumption / residents;
int index = getRatioIndex(ratioAtIndex, ratio);
// If the current ratio was already inserted, just add residents to the pair at
// this index, so that we group all residents with the same consumption / residents
if(index != -1)
res_cons[index].first += residents;
else
{
res_cons.emplace_back(std::make_pair(residents, ratio));
ratioAtIndex.emplace(ratio, distinctRatios++);
}
totalRes += residents;
totalCons += consumption;
}
std::sort(res_cons.begin(), res_cons.end(), [](const auto& a, const auto& b)
{
return a.second < b.second;
});
if(city) std::cout<<'\n';
std::cout<<"Cidade# "<< ++city <<":\n";
for(auto [residents, consumption] : res_cons)
{
std::cout<<residents<<"-"<<consumption<<" ";
}
float average = std::trunc(100 * (float)totalCons / totalRes) / 100;
std::cout<<"\nConsumo medio: "<<std::fixed<<average<<" m3.\n";
res_cons.clear();
ratioAtIndex.clear();
totalRes = totalCons = 0;
}
return 0;
}