-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1023.cpp
More file actions
104 lines (87 loc) · 1.76 KB
/
1023.cpp
File metadata and controls
104 lines (87 loc) · 1.76 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <iostream>
#include <vector>
#define fr(i, a, c) for (int i = a; i < c; i++)
#define sc1(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d %d", &a, &b)
typedef long long int lli;
typedef unsigned long long ull;
using namespace std;
struct Data
{
int pessoas;
int consumo;
};
typedef Data data;
void swap(data* a, data* b)
{
data t = *a;
*a = *b;
*b = t;
}
int partition (data arr[], int low, int high)
{
int pivot = arr[high].consumo;
int i = (low - 1);
for (int j = low; j <= high- 1; j++)
{
if (arr[j].consumo <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(data arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main(int argc, char const *argv[])
{
int n;
string value;
float consumo = 0;
int k,pessoas = 0;
k=1;
while(1){
scanf("%d", &n);
if (!n) break;
consumo = 0;
pessoas = 0;
data *arr = (data *) malloc(n*sizeof(data));
for (int i = 0; i < n; ++i)
{
scanf("%d %d", &arr[i].pessoas, &arr[i].consumo);
}
for (int i = 0; i < n; ++i)
{
consumo += arr[i].consumo;
pessoas += arr[i].pessoas;
arr[i].consumo = arr[i].consumo/arr[i].pessoas;
}
quickSort(arr, 0, sizeof(arr)/sizeof(arr[0]));
consumo = consumo/pessoas;
printf("Cidade# %d:\n", k);
for (int i = 0; i < n; ++i)
{
printf("%d-%d ", arr[i].pessoas, arr[i].consumo);
}
value.push_back(to_string(consumo));
value.erase(value.begin()+value.size()-1);
value.erase(value.begin()+value.size()-1);
cout << "\nConsumo medio: " << value << "m3.\n";
printf("\n");
k++;
}
return 0;
}