summaryrefslogtreecommitdiff
path: root/Year_2/Algorithms/coppie-counting.cc
blob: 47acb14d573c641f9ce6770b64b3d88eaad245c9 (plain)
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
#include <fstream>
#include <iostream>

using namespace std;
using pii = pair<int, int>;

void
countingsort(pii* A, int n, ostream& out)
{
    int min = A[0].first;
    int max = A[0].first;

    for (int i = 1; i < n; ++i) {
        if (A[i].first < min)
            min = A[i].first;
        if (A[i].first > max)
            max = A[i].first;
    }

    int range = max - min + 1;

    int* freq = new int[range];
    pii* C = new pii[n];

    for (int i = 0; i < range; ++i)
        freq[i] = 0;

    for (int i = 0; i < n; ++i) {
        freq[A[i].first - min]++;
    }
    for (int i = 1; i < range; ++i) {
        freq[i] += freq[i - 1];
    }

    out << "0 ";
    for (int i = 0; i < range - 1; ++i)
        out << freq[i] << ' ';

    for (int i = n - 1; i >= 0; --i) {
        C[freq[A[i].first - min] - 1] = A[i];
        freq[A[i].first - min]--;
    }

    for (int i = 0; i < n; ++i)
        out << "(" << C[i].first / 10.0 << ' ' << C[i].second / 10.0 << ") ";

    out << endl;
    delete[] C;
    delete[] freq;
}

int
main(int argc, char** argv)
{
    int ts = (argc > 1) ? stoi(argv[1]) : 100;
    ifstream fin("input.txt");
    ofstream fout("output.txt");
    string ch;
    int n;
    double x1, x2;

    for (int i = 0; i < ts; ++i) {
        fin >> n;
        pii* a = new pii[n];
        for (int j = 0; j < n; ++j) {
            fin >> ch;
            x1 = stod(ch.substr(1, ch.length()));
            fin >> ch;
            x2 = stod(ch.substr(0, ch.length() - 1));
            a[j] = { x1 * 10, x2 * 10 };
        }

        countingsort(a, n, fout);
        delete[] a;
    }

    fin.close();
    fout.close();

    return 0;
}