Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Pair {
- using value_t = long;
- using pair_t = pair<value_t, value_t>;
- using return_t = long long;
- pair_t pair{};
- Pair(const pair_t& pair) : pair(pair) {
- if (this->pair.first > this->pair.second) {
- swap(this->pair.first, this->pair.second);
- }
- }
- ~Pair() {}
- return_t multiply() const {
- return (return_t)pair.first * (return_t)pair.second;
- }
- virtual void insert(value_t value) = 0;
- private:
- static friend ostream& operator<<(ostream& out, const Pair& p) {
- return out << p.pair.first << ' ' << p.pair.second;
- }
- static friend bool operator>(const Pair& a, const Pair& b) {
- return a.multiply() > b.multiply();
- }
- };
- struct MaxPair : virtual Pair {
- inline static const auto min = numeric_limits<Pair::value_t>::min();
- MaxPair() : Pair({ min, min }) {}
- void insert(value_t value) override {
- if (value > pair.second) {
- pair.first = pair.second;
- pair.second = value;
- } else if (value > pair.first) {
- pair.first = value;
- }
- }
- };
- struct MinPair : virtual Pair {
- inline static const auto max = numeric_limits<Pair::value_t>::max();
- MinPair() : Pair({ max, max }) {}
- void insert(value_t value) override {
- if (value < pair.first) {
- pair.second = pair.first;
- pair.first = value;
- } else if (value < pair.second) {
- pair.second = value;
- }
- }
- };
- class Sequence {
- public:
- Sequence() : length(0) {}
- explicit Sequence(const size_t length) : length(length) {}
- void result() const {
- if (length < 2) puts("No result");
- else {
- const Pair* max = &max_pair;
- const Pair* min = &min_pair;
- cout << (*max > *min ? *max : *min) << '\n';
- }
- }
- private:
- size_t length;
- MaxPair max_pair;
- MinPair min_pair;
- static friend istream& operator>>(istream& inp, Sequence& solution) {
- Pair::value_t value{};
- for (size_t i = 0; i < solution.length; ++i) {
- inp >> value;
- solution.max_pair.insert(value);
- solution.min_pair.insert(value);
- }
- return inp;
- }
- };
- static size_t parse(const char* prompt = "") {
- cout << prompt;
- size_t value;
- cin >> value;
- cin.ignore();
- return value;
- }
- int main() {
- const auto quantity = parse();
- Sequence sequence(quantity);
- cin >> sequence;
- sequence.result();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement