Блог пользователя OddOneOut

Автор OddOneOut, история, 3 часа назад, По-английски

I solved 2074A — A. Draw a Square in a slightly different way.

Idea

We are given four values:

l, r, d, u

For these four points to form a square, all four distances need to be equal.

The usual way is to check:

l == r && r == d && d == u

But I used a different and simple way.

First, I find the minimum and maximum among the four values.

ll mn = min({l, r, d, u});
ll mx = max({l, r, d, u});

Now notice:

  • If all four values are equal, the minimum and maximum will also be equal.
  • If the minimum and maximum are different, then at least one value is different.

So the condition becomes:

mn == mx  →  YES
mn != mx  →  NO

Code

#include <bits/stdc++.h>
using namespace std;

#define ll long long

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    ll t;
    cin >> t;

    while (t--)
    {
        ll l, r, d, u;
        cin >> l >> r >> d >> u;

        ll mn = min({l, r, d, u});
        ll mx = max({l, r, d, u});

        if (mn == mx)
           {
                cout<<"Yes\n";
           }
        else{
            cout<<"No\n";
        }
    }
}

Example 1

Suppose: text 2 2 2 2

Then: text mn = 2 mx = 2 So mn == mx, therefore the answer is:

YES

Example 2

Suppose: text 1 2 1 1

Then: text mn = 1 mx = 2

So mn != mx, therefore the answer is: text NO

Complexity

For each test case: * Time: O(1) * Space: O(1)

This is basically the same condition as checking all four values directly, but using the minimum and maximum gives another simple way to think about the problem.

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
3 часа назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OddOneOut (previous revision, new revision, compare).

»
105 минут назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by OddOneOut (previous revision, new revision, compare).