Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

SMU Summer 2023 Contest Round 7 Solutions

Tech Jun 4 1

A. Two Rival Students

The maximum possible dsitance between two students in a line of n positions is n - 1. Given their initial positions a and b, and up to x swaps, the achievable distance is the minimum of n - 1 and |a - b| + x.

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

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int T; cin >> T;
    while (T--) {
        int n, x, a, b;
        cin >> n >> x >> a >> b;
        cout << min(n - 1LL, abs(a - b) + x) << '\n';
    }
    return 0;
}

B. Magic Stick

The transformation rules are:

  • If x == 1, only y == 1 is reachable.
  • If x == 2 or x == 3, any y ≤ 3 is reachable.
  • If x ≥ 4, any positive integer y is reachable.
#include <bits/stdc++.h>
using namespace std;

int main() {
    int T; cin >> T;
    while (T--) {
        int x, y; cin >> x >> y;
        if (x >= 4) {
            cout << "YES\n";
        } else if (x == 1) {
            cout << (y == 1 ? "YES\n" : "NO\n");
        } else {
            cout << (y <= 3 ? "YES\n" : "NO\n");
        }
    }
    return 0;
}

C. Dominated Subarray

A dominated subarray requires at least one value appearing more then once. The shortest such subarray occurs when a repeated element appears with minimal spacing. Track the last occurrence of each value and compute the minimal window length where a duplicate appears.

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

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int T; cin >> T;
    while (T--) {
        int n; cin >> n;
        vector<int> arr(n), freq(n + 1, 0);
        for (int &x : arr) {
            cin >> x;
            freq[x]++;
        }

        if (n < 2 || *max_element(freq.begin(), freq.end()) == 1) {
            cout << "-1\n";
            continue;
        }

        vector<int> lastPos(n + 1, -1);
        int minLength = INT_MAX;
        for (int i = 0; i < n; ++i) {
            if (lastPos[arr[i]] != -1) {
                minLength = min(minLength, i - lastPos[arr[i]] + 1);
            }
            lastPos[arr[i]] = i;
        }
        cout << minLength << '\n';
    }
    return 0;
}

D. Yet Another Monster Killing Problem

Use preprocessing and greedy simulation:

  1. If the strongest hero can't defeat the strongest monster, output -1.
  2. Sort heroes by power. For equal or increasing power, maintain the maximum endurance from right to left.
  3. Iterate through monsters, tracking how many can be defeated in the current day based on available hero endurance.
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int T; cin >> T;
    while (T--) {
        int n; cin >> n;
        vector<int> monsters(n);
        int maxMonster = 0;
        for (int &x : monsters) {
            cin >> x;
            maxMonster = max(maxMonster, x);
        }

        int m; cin >> m;
        vector<pair<int, int>> heroes(m);
        int maxHero = 0;
        for (auto &[p, s] : heroes) {
            cin >> p >> s;
            maxHero = max(maxHero, p);
        }

        if (maxHero < maxMonster) {
            cout << "-1\n";
            continue;
        }

        sort(heroes.begin(), heroes.end());
        for (int i = m - 2; i >= 0; --i) {
            heroes[i].second = max(heroes[i].second, heroes[i + 1].second);
        }

        int days = 1, defeated = 0, currentEndurance = INT_MAX;
        for (int i = 0; i < n; ++i) {
            auto it = lower_bound(heroes.begin(), heroes.end(), make_pair(monsters[i], 0));
            int idx = it - heroes.begin();
            currentEndurance = min(currentEndurance, heroes[idx].second);

            if (defeated + currentEndurance <= i) {
                days++;
                defeated = i;
                currentEndurance = heroes[idx].second;
            }
        }
        cout << days << '\n';
    }
    return 0;
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.