Problem Statement: 1999A — A+B Again?
Since $$$n$$$ is a two-digit number, you can access each digit by simple arithmetic operations.
To find the sum of digits of a two-digit number $$$n$$$, divide $$$n$$$ by 10 to get the first digit and take $$$n$$$ modulo 10 to get the second digit. Adding these gives the desired result.
Time Complexity: $$$O(1)$$$ per test case
Space Complexity: $$$O(1)$$$
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cout << (n / 10) + (n % 10) << endl;
return 0;
}
Problem Statement: B — Card Game
For each pair of cards, consider all possible matchups and count how many games Suneet can win by winning more rounds.
Each player has two cards, and the game consists of two rounds. For each matchup of cards, there are four possible ways for the players to reveal their cards. We simulate each of these cases and count how many result in Suneet winning more rounds than Slavic.
Time Complexity: $$$O(1)$$$ per test case
Space Complexity: $$$O(1)$$$
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int a1, a2, b1, b2;
cin >> a1 >> a2 >> b1 >> b2;
int count = 0;
int a[2] = {a1, a2}, b[2] = {b1, b2};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
int suneet = 0, slavic = 0;
if (a[i] > b[j]) suneet++;
else if (a[i] < b[j]) slavic++;
if (a[1 - i] > b[1 - j]) suneet++;
else if (a[1 - i] < b[1 - j]) slavic++;
if (suneet > slavic) count++;
}
}
cout << count << endl;
}
return 0;
}
Problem Statement: C — Showering
Check each gap between tasks, as well as the beginning and end of the day, to see if there’s enough free time for Alex to shower.
Given Alex's tasks, represented as non-overlapping time intervals, we need to find a continuous free interval of at least $$$s$$$ minutes.
i. Sort and scan the intervals:
- Check the time from the start of the day to the beginning of the first task.
- Check the gaps between tasks.
- Check from the end of the last task to the end of the day.
ii. If any of these free intervals is at least $$$s$$$ minutes, Alex can shower.
Time Complexity: $$$O(n)$$$ per test case
Space Complexity: $$$O(n)$$$ per test case
#include <bits/stdc++.h>
using namespace std;
int main () {
int t;
cin >> t;
while (t--) {
int n, time, total, temp;
cin >> n >> time >> total;
temp = n;
vector<pair<int, int>> v;
while (temp--) {
int x, y;
cin >> x >> y;
v.push_back({x, y});
}
int mx = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
mx = max(mx, v[i].first);
} else {
mx = max(mx, v[i].first - v[i - 1].second);
}
}
if (total - v[n - 1].second >= time) {
mx = time;
}
cout << ((mx >= time) ? "YES" : "NO") << endl;
}
return 0;
}
Problem Statement: D — Slavic's Exam
Check if the string $$$t$$$ can appear as a subsequence in string $$$s$$$, replacing '?' with appropriate letters from $$$t$$$ wherever possible. Fill remaining '?' characters with any letter.
The goal is to replace each '?' in string $$$s$$$ with lowercase letters so that $$$t$$$ becomes a subsequence of $$$s$$$.
Attempt to match each character of $$$t$$$ with a character in $$$s$$$ by iterating through $$$s$$$.
For each character in $$$t$$$, try to find a match in $$$s$$$. If you encounter '?', replace it with the matching character in $$$t$$$.
If $$$t$$$ becomes a subsequence of $$$s$$$, fill remaining '?' with any valid letter (e.g., 'a') and print the modified $$$s$$$.
If it is not possible to make $$$t$$$ a subsequence of $$$s$$$, print "NO".
Time Complexity: $$$O(|s|)$$$ per test case
Space Complexity: $$$O(|s|)$$$ per test case
#include <bits/stdc++.h>
using namespace std;
int main () {
int t;
cin >> test;
while (t--) {
string a, b;
cin >> a >> b;
int count = 0;
int j = -1;
for (int i = 0; i < b.length(); i++) {
for (j = j + 1; j < a.length(); j++) {
if (b[i] == a[j]) {
count++;
break;
} else if (a[j] == '?') {
count++;
a[j] = b[i];
break;
}
}
}
if (count >= b.length()) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
continue;
}
for (int i = 0; i < a.length(); i++) {
if (a[i] != '?') {
cout << a[i];
} else {
cout << 'a';
}
}
cout << endl;
}
return 0;
}
Problem Statement: E — Triple Operations
Observe that each operation reduces numbers effectively by dividing and multiplying, so precompute the minimum number of operations needed to reduce each possible integer to zero.
The goal is to minimize the number of operations needed to reduce every number between $$$l$$$ and $$$r$$$ to zero.
i. Precompute Operations for Each Number:
- Use dynamic programming to calculate the minimum operations required to make any number from 1 up to $$$2 \times 10^5$$$ equal zero.
- For each integer $$$x$$$, recursively compute the minimum operations to reduce it by dividing by 3 until reaching zero.
ii. Result Calculation for Each Range $$$[l, r]$$$:
- With the precomputed values, sum the operations needed for each integer from $$$l$$$ to $$$r$$$.
Time Complexity: $$$O(\text{precompute}) + O(r - l)$$$ per query.
Space Complexity: $$$O(\text{range})$$$
#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 1;
vector<int> dp(N, -1);
int search (int value) {
if (value == 0) {
return 0;
}
if (dp[value] != -1) {
return dp[value];
}
return dp[value] = search(value / 3) + 1;
}
int main () {
int t;
cin >> t;
for (int i = N; i >= 0; i--) {
search(i);
}
while (test--) {
int l, r;
cin >> l >> r;
int as = dp[l] * 2;
for (int i = l + 1; i <= r; i++) {
as += dp[i];
}
cout << as << endl;
}
return 0;
}