[URI Online Judge] – 1023 – Drought

Due to the continuous drought that happened recently in some regions of Brazil, the Federal Government created an agency to assess the consumption of these regions in order to verify the behavior of the population at the time of rationing. This agency will take some cities (for sampling) and it will verify the consumption of each of the townspeople and the average consumption per inhabitant of each town.

Input

The input contains a number of test cases. The first line of each test case contains an integer N (1 ≤ N ≤ 1 * 10 6), indicating the amount properties. The following N lines contains a pair of values X (1 ≤ X ≤ 10) and Y( 1 ≤ Y ≤ 200) indicating the number of residents of each property and its total consumption (m3). Surely, no residence consumes more than 200 m3 per month. The end of input is represented by zero.

Output

For each test case you must present the message “Cidade# n:”, where n is the number of the city in the sequence (1, 2, 3, …), and then you must list in ascending order of consumption, the amount people followed by a hyphen and the consumption of these people, rounding the value down. In the third line of output you should present the consumption per person in that town, with two decimal places without rounding, considering the total real consumption. Print a blank line between two consecutives test cases. There is no blank line at end of output.

Sample Input Sample Output
3
3 22
2 11
3 39
5
1 25
2 20
3 31
2 40
6 70
0
Cidade# 1:
2-5 3-7 3-13
Consumo medio: 9.00 m3.

Cidade# 2:
5-10 6-11 2-20 1-25
Consumo medio: 13.28 m3.

My solution:

<pre class="brush: cpp; , toolbar: false">#include <iostream>
#include <iomanip>
#include <map>
#include <cmath>

using namespace std;

map<int, int> r;

int main(){
    int n, nn = 0, x, y, s, c;
    double xs, ys;
    bool first = true;

    while (cin >> n && n != 0){
        if (!first) {
            cout << endl;
        }
        first = false;

        xs = 0.0;
        ys = 0.0;
        nn++;

        r.clear();

        while (n--){
            cin >> x >> y;
            ys += y;
            xs += x;

            if (r[y / x] != 0) r[y / x] += x;
            else r[y / x] = x;
        }

        s = r.size();
        c = 0;

        cout << "Cidade# " << nn << ":" << endl;
        for(map<int, int>::iterator iter = r.begin(); iter != r.end(); iter++){
            cout << iter->second << "-" << iter->first;
            if (c < s - 1) cout << " ";
            c++;
        }

        cout << endl;
        cout << "Consumo medio: ";
        cout << fixed << setprecision(2) << floor((ys / xs) * 100) / 100;
        cout << " m3." << endl;
    }
    return 0;
}</pre>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s