Queen's College Coding Competition Round 1 Editorial
Difference between en29 and en30, changed 2 character(s)
Special thanks to [user:Michael276382,2026-10-25], [user:itslucaslam,2026-10-25], [user:ansonlee2025.yay,2026-10-25], [user:cl_lmc,2026-10-25], [user:KTMC_Jack_Hui,2026-10-25], [user:jonahgamer110,2026-10-25], [user:sqrteipi,2026-10-25], [user:kevinxiehk,2026-10-25] for testing!↵
↵
In addition, congratulations to [user:SaiBadrik012,2026-10-25], [user:elliotchl,2026-10-25], and [user:i_love_mickey_0331,2026-10-25] for solving all problems in this contest!↵
↵
[problem:725524A]↵
↵
Author: [user:jonahgamer110,2026-10-25]↵
↵
<spoiler summary="Tutorial">↵
Output $2 \times (L \times W + W \times H + L \times H)$.↵
</spoiler>↵
↵
<spoiler summary="Code (Python)">↵
~~~~~↵
import sys↵
↵
def solve():↵
    input = sys.stdin.read↵
    data = input().split()↵
    if not data:↵
        return↵
    l = int(data[0])↵
    w = int(data[1])↵
    h = int(data[2])↵
    print(2 * (l * w + w * h + l * h))↵
↵
if __name__ == "__main__":↵
    solve()↵
~~~~~↵
</spoiler>↵
↵
<spoiler summary="Code (C++)">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵
↵
int main() {↵
    ios::sync_with_stdio(false);↵
    cin.tie(NULL);↵
    int l, w, h;↵
    if (!(cin >> l >> w >> h)) return 0;↵
    cout << 2 * (l * w + w * h + l * h) << "\n";↵
    return 0;↵
}↵
~~~~~↵
</spoiler>↵
↵
[problem:725524B]↵
↵
Adapted from: [2026 WYIOI P2](https://oj.wyk.edu.hk/task/T262)↵
↵
<spoiler summary="Tutorial">↵
Loop through the array to compute the sum, the maximum and minimum element of↵
$[S_1, S_2, \dots , S_N]$. Output the sum minus the maximum and minimum.↵
↵
Note that there are built-in functions for these operations. Refer to the code sections below.↵
</spoiler>↵
↵
<spoiler summary="Code (Python)">↵
~~~~~↵
import sys↵
↵
def solve():↵
    input = sys.stdin.read↵
    data = input().split()↵
    if not data:↵
        return↵
    n = int(data[0])↵
    s = [int(x) for x in data[1 : n + 1]]↵
    print(sum(s) - min(s) - max(s))↵
↵
if __name__ == "__main__":↵
    solve()↵
~~~~~↵
</spoiler>↵
↵
<spoiler summary="Code (C++)">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵
↵
int main() {↵
    ios::sync_with_stdio(false);↵
    cin.tie(NULL);↵
    int n;↵
    if (!(cin >> n)) return 0;↵
    vector<int> s(n);↵
    for (int i = 0; i < n; i++) cin >> s[i];↵
    int sum = accumulate(s.begin(), s.end(), 0);↵
    int min_s = *min_element(s.begin(), s.end());↵
    int max_s = *max_element(s.begin(), s.end());↵
    cout << sum - min_s - max_s << "\n";↵
    return 0;↵
}↵
~~~~~↵
</spoiler>↵
↵
[problem:725524C]↵
↵
Author: [user:cl_lmc,2026-10-25]↵
↵
<spoiler summary="Tutorial">↵
A constructive approach is to place the sequence elements by alternating steps starting from the center:↵
↵
- $A_1 = \lfloor \frac{N}{2} \rfloor + 1$.↵
- $A_2 = A_1 + 1$↵
- $A_3 = A_2 - 2$↵
- $A_4 = A_3 + 3$↵
- $\dots$↵
↵
By alternating directions between adding and subtracting the step index $i$, the values trace out all distinct integers from $1$ to $N$ without ever falling out of bounds or repeating. This guarantees a valid sequence for any $N \ge 3$.↵
</spoiler>↵
↵
<spoiler summary="Code (Python)">↵
~~~~~↵
import sys↵
↵
def solve():↵
    input = sys.stdin.read↵
    n = int(input().strip())↵
    ans = [0] * n↵
    ans[0] = n // 2 + 1↵
    sign = 1↵
    for i in range(1, n):↵
        ans[i] = ans[i - 1] + sign * i↵
        sign = -sign ↵
    print(*ans)↵
↵
if __name__ == "__main__":↵
    solve()↵
~~~~~↵
</spoiler>↵
↵
<spoiler summary="Code (C++)">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵
↵
int main() {↵
    ios::sync_with_stdio(false);↵
    cin.tie(NULL);↵
    int n;↵
    if (!(cin >> n)) return 0;↵
    vector<int> a(n);↵
    a[0] = n / 2 + 1;↵
    int sign = 1;↵
    for (int i = 1; i < n; i++) {↵
        a[i] = a[i - 1] + sign * i;↵
        sign = -sign;↵
    }↵
    for (int i = 0; i < n; ++i) cout << a[i] << (i == n - 1 ? "" : " ");↵
    cout << "\n";↵
    return 0;↵
}↵
~~~~~↵
</spoiler>↵
↵
[problem:725524D]↵
↵
Author: [user:jiangordon,2026-10-25]↵
↵
<spoiler summary="Tutorial">↵
First, convert the current time into total minutes elapsed from midnight:↵
$$\text{total_minutes} = H \times 60 + M + K$$↵
↵
Since a day has $1440$ minutes ($24 \times 60$), we can find the time on the clock after any number of days by taking the modulo:↵
$$\text{final_minutes} = \text{total_minutes} \pmod{1440}$$↵
↵
Finally, extract the new hour and minute from the time:↵
↵
- $H' = \lfloor \frac{\text{final_minutes}}{60} \rfloor$↵
- $M' = \text{final_minutes} \pmod{60}$↵
</spoiler>↵
↵
<spoiler summary="Code (Python)">↵
~~~~~↵
import sys↵
↵
def solve():↵
    input = sys.stdin.read↵
    data = input().split()↵
    if not data:↵
        return↵
    h = int(data[0])↵
    m = int(data[1])↵
    k = int(data[2])↵
    total_minutes = h * 60 + m + k↵
    final_minutes = total_minutes % 1440↵
    new_h = final_minutes // 60↵
    new_m = final_minutes % 60↵
    print(new_h, new_m)↵
↵
if __name__ == "__main__":↵
    solve()↵
~~~~~↵
</spoiler>↵
↵
<spoiler summary="Code (C++)">↵
~~~~~↵
#include <iostream>↵
↵
using namespace std;↵
↵
int main() {↵
    ios::sync_with_stdio(false);↵
    cin.tie(NULL);↵
    long long h, m, k;↵
    if (!(cin >> h >> m >> k)) return 0;↵
    long long total_minutes = h * 60 + m + k;↵
    long long final_minutes = total_minutes % 1440;↵
    long long new_h = final_minutes / 60;↵
    long long new_m = final_minutes % 60;↵
    cout << new_h << " " << new_m << "\n";↵
    return 0;↵
}↵
~~~~~↵
</spoiler>↵
↵
[problem:725524E]↵
↵
Adapted from: [problem:2022A]↵
↵
<spoiler summary="Tutorial">↵
To maximize the number of happy people, we want to pair up as many family members together as possible. Since each table holds at most 2 people:↵
↵
- For even-sized families, every member can be paired up with another member of the same family. Thus, all members of even families will be happy.↵
- For an odd-sized family of size $A_i$, we can pair up $A_i - 1$ members, leaving 1 member unpaired. Ideally, we want to seat this person alone in a row to make them happy. However, if there are no remaining tables to seat them alone, we’ll have to seat them with someone from another family. This means the other person might no longer be happy since they are no longer seated alone.↵
- If the number of remaining tables is greater than or equal to the number of unpaired people, all unpaired people can sit alone and remain happy. Otherwise, some people will have to share a tables, and their happiness will be affected. In that case, the number of happy people will be $2 \times \text{remaining_tables} - \text{remaining_people}$.↵
↵
</spoiler>↵
↵
<spoiler summary="Code (Python)">↵
~~~~~↵
import sys↵
↵
def solve():↵
    input = sys.stdin.read↵
    data = input().split()↵
    if not data:↵
        return↵
    n = int(data[0])↵
    k = int(data[1])↵
    a = [int(x) for x in data[2 : n+2]]↵
    happy = 0↵
    leftalone = 0↵
    for x in a:↵
        happy += (x // 2) * 2↵
        k -= x // 2↵
        leftalone += x % 2↵
        ↵
    if leftalone > k:↵
        happy += k * 2 - leftalone↵
    else:↵
        happy += leftalone  ↵
    print(happy)↵
↵
if __name__ == "__main__":↵
    solve()↵
~~~~~↵
</spoiler>↵
↵
<spoiler summary="Code (C++)">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵
↵
int main() {↵
    ios::sync_with_stdio(false);↵
    cin.tie(NULL);↵
    int n;↵
    long long k;↵
    if (!(cin >> n >> k)) return 0;↵
    long long happy = 0;↵
    long long leftalone = 0;↵
    for (int i = 0; i < n; ++i) {↵
        long long a;↵
        cin >> a;↵
        happy += (a / 2) * 2;↵
        k -= a / 2;↵
        leftalone += a % 2;↵
    }↵
    if (leftalone > k) {↵
        happy += k * 2 - leftalone;↵
    } else {↵
        happy += leftalone;↵
    }↵
    cout << happy << "\n";↵
    return 0;↵
}↵
~~~~~↵
</spoiler>↵
↵
[problem:725524F]↵
↵
Author: [user:jiangordon,2026-10-25]↵
↵
<spoiler summary="Tutorial">↵
This problem asks us to count how many streetlights illuminate each query point $(R_j, C_j)$, where each streetlight $i$ illuminates a square region from $(X_i - B_i, Y_i - B_i)$ to $(X_i + B_i, Y_i + B_i)$.↵
↵
A query point $(R, C)$ is illuminated by streetlight $i$ if and only if:↵
↵
$$X_i - B_i \le R \le X_i + B_i \quad \text{and} \quad Y_i - B_i \le C \le Y_i + B_i$$↵
↵
which can be rewritten as:↵
↵
$$R - B_i \le X_i \le R + B_i \quad \text{and} \quad C - B_i \le Y_i \le C + B_i$$↵
↵
Given the constraints where $K, Q \le 5000$, we can iterate through each of the $Q$ queries and check all $K$ streetlights using a simple loop.↵
</spoiler>↵
↵
<spoiler summary="Code (Python)">↵
~~~~~↵
import sys↵
↵
def solve():↵
    input = sys.stdin.read↵
    data = input().split()↵
    if not data:↵
        return↵
    n = int(data[0]);↵
    m = int(data[1]);↵
    k = int(data[2]);↵
    idx = 3↵
    lights = []↵
    for _ in range(k):↵
        x, y, b = map(int, input_data[index : index + 3])↵
        lights.append((x, y, b))↵
        idx += 3↵
    q = int(data[idx]);↵
    idx += 1↵
    ans = []↵
    for _ in range(q):↵
        r = int(data[idx]);↵
        c = int(data[idx + 1]);↵
        idx += 2↵
        count = 0↵
        for x, y, b in lights:↵
            if abs(x - r) <= b and abs(y - c) <= b:↵
                count += 1↵
        ans.append(str(count))↵
    print("\n".join(ans) + "\n")↵
↵
if __name__ == "__main__":↵
    solve()↵
~~~~~↵
</spoiler>↵
↵
<spoiler summary="Code (C++)">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵
↵
struct light {↵
    long long x, y, b;↵
};↵
↵
int main() {↵
    ios::sync_with_stdio(false);↵
    cin.tie(NULL);↵
    long long n, m, k;↵
    if (!(cin >> n >> m >> k)) return 0;↵
    vector<light> lights(k);↵
    for (int i = 0; i < k; i++) cin >> lights[i].x >> lights[i].y >> lights[i].b;↵
    int q;↵
    if (!(cin >> q)) return 0;↵
    while (q--) {↵
        long long r, c;↵
        cin >> r >> c;↵
        int count = 0;↵
        for (auto [x, y, b] : lights) if (abs(x - r) <= b && abs(y - c) <= b) count++;↵
        cout << count << "\n";↵
    }↵
    return 0;↵
}↵
~~~~~↵
</spoiler>↵
↵
[problem:725524G]↵
↵
Author: [user:jiangordon,2026-10-25]↵
↵
<spoiler summary="Tutorial">↵
From the statement, the total force $B_i$ on planet $i$ is given as:↵
$$B_i = \sum_{j \neq i} (A_j - \vert{}j - i\vert{})$$↵
↵
We can separate this sum into two parts: the sum of the masses of all other planets, and the sum of the distance offsets:↵
$$B_i = \sum_{j \neq i} A_j - \sum_{j \neq i} \vert{}j - i\vert{}$$↵
↵
Let $S = \sum_{j=1}^{N} A_j$ be the sum of all planet masses. Then $\sum_{j \neq i} A_j = S - A_i$.↵
↵
Let $D_i = \sum_{j \neq i} \vert{}j - i\vert{}$ represent the total distance penalty from all other planets to planet $i$. We can precompute $D_i$ in $O(N)$ using the positions to the left and right of $i$:↵
$$D_i = \frac{i(i-1)}{2} + \frac{(n-i)(n-i+1)}{2}$$↵
↵
Substituting these back into the equation:↵
$$B_i = (S - A_i) - D_i \implies A_i + B_i + D_i = S$$↵
↵
Notice that $S$ is a constant total sum across all planets. If we sum the equation $A_i + B_i + D_i = S$ for all $i$ from $1$ to $N$:↵
$$\sum_{i=1}^{N} (A_i + B_i + D_i) = \sum_{i=1}^{N} S = N \cdot S$$↵
↵
Since $\sum_{i=1}^{N} A_i = S$, this simplifies to:↵
$$S + \sum_{i=1}^{N} B_i + \sum_{i=1}^{N} D_i = N \cdot S \implies \sum_{i=1}^{N} B_i + \sum_{i=1}^{N} D_i = (N - 1)S$$↵
↵
Alternatively, by looking at any individual pair of equations or summing all $B_i + D_i$, we can deduce the total mass sum $S$. In fact, summing $B_i + D_i$ for all planets gives $(N-1)S$, allowing us to find:↵
$$S = \frac{\sum_{i=1}^{N} (B_i + D_i)}{N - 1}$$↵
↵
Once we find $S$, each individual planet's mass can be directly recovered:↵
$$A_i = S - B_i - D_i$$↵
</spoiler>↵
↵
<spoiler summary="Code (Python)">↵
~~~~~↵
import sys↵
↵
def solve():↵
    input = sys.stdin.read↵
    data = input().split()↵
    if not data:↵
        return↵
    n = int(data[0])↵
    b = [0] + [int(x) for x in data[1 : n+1]]↵
    d = [0] * (n + 1)↵
    for i in range(1, n + 1):↵
        d[i] = i * (i - 1) // 2 + (n - i) * (n - i + 1) // 2↵
    total_sum = 0↵
    for i in range(1, n + 1):↵
        total_sum += b[i] + d[i]↵
    s = total_sum // (n - 1)↵
    a = [0] * (n + 1)↵
    for i in range(1, n + 1):↵
        a[i] = s - b[i] - d[i]↵
    print(*(a[1:]))↵
↵
if __name__ == "__main__":↵
    solve()↵
~~~~~↵
</spoiler>↵
↵
<spoiler summary="Code (C++)">↵
~~~~~↵
#include <bits/stdc++.h>↵
using namespace std;↵
↵
int main() {↵
    ios::sync_with_stdio(false);↵
    cin.tie(NULL);↵
    int n;↵
    if (!(cin >> n)) return 0;↵
    vector<long long> b(n + 1);↵
    for (int i = 1; i <= n; i++) cin >> b[i];↵
    vector<long long> d(n + 1);↵
    for (int i = 1; i <= n; i++) d[i] = i * (i - 1) / 2 + (n - i) * (n - i + 1) / 2;↵
    long long s = 0LL;↵
    for (int i = 1; i <= n; i++) s += b[i] + d[i];↵
    s /= n - 1;↵
    vector<long long> a(n + 1);↵
    for (int i = 1; i <= n; i++) a[i] = s - b[i] - d[i];↵
    for (int i = 1; i <= n; i++) cout << a[i] << (i == n ? "" : " ");↵
    cout << '\n';↵
    return 0;↵
}↵
~~~~~↵
</spoiler>↵
↵

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en30 English jiangordon 2026-10-25 08:19:29 2 (published)
en29 English jiangordon 2026-10-25 08:18:16 7320
en28 English jiangordon 2026-10-25 07:47:05 3317 Tiny change: ' - 2$\n- $\dot' -> ' - 2$\n- $A_4 = A_3 + 3$\n- $\dot'
en27 English jiangordon 2026-10-25 07:32:21 1095 Tiny change: 'ent of\n$[s_1, s_2, \dots , s_N]$. Outp' -> 'ent of\n$[S_1, S_2, \dots , S_N]$. Outp'
en26 English jiangordon 2026-10-25 07:21:03 545
en25 English jiangordon 2026-10-25 07:15:24 11 Tiny change: '="Code">\nPython:\n\n~~~~~\nl' -> '="Code">\n~~~~~\nl'
en24 English jiangordon 2026-10-25 07:15:14 14 Tiny change: ')">\n~~~~~l = int(in' -> ')">\n~~~~~ l = int(in'
en23 English jiangordon 2026-10-25 07:14:34 3 Tiny change: ')">\n~~~~~\nl = int(in' -> ')">\n~~~~~l = int(in'
en22 English jiangordon 2026-10-25 07:14:17 152
en21 English jiangordon 2026-10-25 07:12:59 23
en20 English jiangordon 2026-10-25 07:12:42 90
en19 English jiangordon 2026-10-25 07:11:06 149
en18 English jiangordon 2026-10-25 07:08:38 13 Tiny change: 'test!\n\n[cut]\n\n\n\n[probl' -> 'test!\n\n[probl'
en17 English jiangordon 2026-10-25 07:08:23 4 Tiny change: 'n[cut]\n\n[probl' -> 'n[cut]\n\n\n\n[probl'
en16 English jiangordon 2026-10-25 07:08:08 36 Tiny change: 'n[problem:https://codeforces.me/gym/725524/problem/A]' -> 'n[problem:725524A]'
en15 English jiangordon 2026-10-25 07:07:44 53 Tiny change: 'n[cut]\n\n' -> 'n[cut]\n\n[problem:https://codeforces.me/gym/725524/problem/A]'
en14 English jiangordon 2026-10-25 07:07:04 17
en13 English jiangordon 2026-10-25 07:05:56 288
en12 English jiangordon 2026-10-25 07:03:13 1
en11 English jiangordon 2026-10-25 07:02:28 1 Tiny change: 'sonlee2025,2026-10-25.yay\n\nIn' -> 'sonlee2025\.yay\n\nIn'
en10 English jiangordon 2026-10-25 07:02:16 2 Tiny change: '026-10-25 , ~ansonlee' -> '026-10-25 ~ansonlee'
en9 English jiangordon 2026-10-25 07:01:09 11 Tiny change: '2026-10-25.2026-10-25' -> '2026-10-25'
en8 English jiangordon 2026-10-25 07:00:55 13 Tiny change: 'tslucaslam,2026-10-25 ~ansonlee' -> 'tslucaslam.2026-10-25 , ~ansonlee'
en7 English jiangordon 2026-10-25 07:00:36 209
en6 English jiangordon 2026-10-25 06:58:52 17
en5 English jiangordon 2026-10-25 06:58:13 2 Tiny change: '~ansonlee2-25,2026-10-25.yay, ~c' -> '~ansonlee2025.yay, ~c'
en4 English jiangordon 2026-10-25 06:57:46 357
en3 English jiangordon 2026-10-25 06:54:01 7
en2 English jiangordon 2026-10-25 06:53:43 49
en1 English jiangordon 2026-10-25 06:52:54 199 Initial revision (saved to drafts)