2022, UTM, add tests + 2 solutions

This commit is contained in:
2022-06-16 21:18:18 +03:00
parent 65b15a011b
commit 5e7875aa33
82 changed files with 42357 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
ifstream input_stream;
ofstream output_stream;
string tripletToStr(int64_t a, int64_t b, int64_t c) {
string res = to_string(a) + " " + to_string(b) + " " + to_string(c);
return res;
}
vector<string> findTriplets(vector<int64_t> nums, int N) {
vector<string> triplets;
unordered_map<string, bool> found;
int stopI = N - 2;
for (int i = 0; i < stopI; i++) {
int64_t a = nums[i];
if (a > 0) {
break;
}
int stopJ = stopI + 1; // N - 1
for (int j = i + 1; j < stopJ; j++) {
int64_t b = nums[j];
if (a + b > 0) {
break;
}
int stopK = stopJ + 1; // N
for (int k = j + 1; k < stopK; k++) {
int64_t c = nums[k];
if (a + b + c > 0) {
break;
}
if (a + b + c == 0) {
string tripletStr = tripletToStr(a, b, c);
if (found[tripletStr] == false) {
triplets.push_back(tripletStr);
}
}
}
}
}
return triplets;
}
int main() {
input_stream.open("tests/input.10");
int N;
input_stream >> N;
if (N < 3) {
cout << "NOPE" << endl;
return 0;
}
vector<int64_t> nums;
nums.reserve(N);
int64_t tmp;
for (int i = 0; i < N; i++) {
input_stream >> tmp;
nums.push_back(tmp);
}
sort(nums.begin(), nums.end());
vector<string> triplets = findTriplets(nums, N);
if (triplets.size() == 0) {
cout << "NOPE" << endl;
} else {
for (auto it = triplets.begin(); it != triplets.end(); it++) {
cout << (*it) << endl;
}
}
input_stream.close();
return 0;
}