summaryrefslogtreecommitdiff
path: root/Year_1/Programming_2/exercises/exam_20_07_20/ex2.cpp
blob: aed25e45eb2ce3f6f9424ab432f614946303859b (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
#include<iostream>
#include<sstream>
#include<fstream>
#include<queue>

using namespace std;
using pi = tuple<int, int, vector<int>>;

class comp {
public:
    bool operator()(const pi& lhs, const pi& rhs) const {
        auto xl = get<0>(lhs);
        auto yl = get<0>(rhs);

        auto il = get<1>(lhs);
        auto jl = get<1>(rhs);
        if(xl == yl)
            return il > jl;
        return xl > yl;
    }
};

int main() {
    ifstream in("input.txt");
    ofstream out("output.txt");

    for(int ts = 0; ts < 100; ++ts) {
        int R, C;
        in >> R >> C;
        vector<vector<int>> v;
        priority_queue<pi, vector<pi>, comp> pq;
        int k;
        for(int i = 0; i < R; ++i) {
            v.push_back(vector<int>{});
            for(int j = 0; j < C; ++j) {
                in >> k;
                v[i].push_back(k);
            }
        }

        int index = 0;
        for(auto const& i : v) {
            int s = 0;
            pi qq;
            for(auto const& j : i)
                s += j;
            get<0>(qq) = s;
            get<1>(qq) = index++;
            get<2>(qq) = i;
            pq.push(qq);
        }
        while(!pq.empty()) {
            auto q = pq.top();
            pq.pop();
            for(auto const& i : get<2>(q))
                out << i << ' ';
        }
        out << endl;
    }
    in.close();
    out.close();
    return 0;
}