summaryrefslogtreecommitdiff
path: root/I_anno/Programmazione_2/exercises/exam_20_07_20/ex2.cpp
diff options
context:
space:
mode:
authorSanto Cariotti <dcariotti24@gmail.com>2020-07-20 12:26:37 +0200
committerSanto Cariotti <dcariotti24@gmail.com>2020-07-20 12:26:37 +0200
commite06d662de2a01673ce6151ef66d2aa00b271a44c (patch)
treec7ed19827bc5b769d67ba9a9eaca06ab578c48f2 /I_anno/Programmazione_2/exercises/exam_20_07_20/ex2.cpp
parent7468f361189f9a32fb7d02e033d7174a3c1ef182 (diff)
feat: add exercises of 20/07/2020 exam
Diffstat (limited to 'I_anno/Programmazione_2/exercises/exam_20_07_20/ex2.cpp')
-rw-r--r--I_anno/Programmazione_2/exercises/exam_20_07_20/ex2.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/I_anno/Programmazione_2/exercises/exam_20_07_20/ex2.cpp b/I_anno/Programmazione_2/exercises/exam_20_07_20/ex2.cpp
new file mode 100644
index 0000000..aed25e4
--- /dev/null
+++ b/I_anno/Programmazione_2/exercises/exam_20_07_20/ex2.cpp
@@ -0,0 +1,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;
+}