SMU Summer 2023 Contest Round 7 Solutions
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, onlyy == 1is reachable. - If
x == 2orx == 3, anyy ≤ 3is reachable. - If
x ≥ 4, any positive integeryis 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:
- If the strongest hero can't defeat the strongest monster, output
-1. - Sort heroes by power. For equal or increasing power, maintain the maximum endurance from right to left.
- 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;
}