2022, UTM, add tests + 2 solutions
This commit is contained in:
95
2022/utm_tr/3prieteni/3prieteni.cpp
Normal file
95
2022/utm_tr/3prieteni/3prieteni.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user