Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Solutions to Problems 1–5 of the 1024 2nd Lanqiao Cup Biweekly Algorithm Contest

Tech May 14 2

Problem 1: Freshman Challenge

The answer is a constant value.

#include <cstdio>

int main() {
    printf("%d\n", 15);
    return 0;
}

Problem 2: Flooring

We only need the product of the dimensions to be divisible by 6, and both dimensions must be large enough for a 2×3 or 3×2 tile.

#include <iostream>
using namespace std;
using ll = long long;

bool canTile(ll width, ll height) {
    return (width * height) % 6 == 0 &&
           ((width >= 2 && height >= 3) || (width >= 3 && height >= 2));
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        ll w, h;
        cin >> w >> h;
        cout << (canTile(w, h) ? "Yes" : "No") << '\n';
    }
    return 0;
}

Problem 3: Toy Arrangement

Compute the gaps between adjcaent toys, sort them, and sum the smallest (n-1) - (m-1) gaps. This effectively drops the largest gaps that correspond to boundaries between the m groups.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, groups;
    cin >> n >> groups;
    vector<ll> heights(n);
    for (auto &val : heights) cin >> val;

    vector<ll> gaps;
    for (int i = 1; i < n; ++i)
        gaps.push_back(heights[i] - heights[i - 1]);

    sort(gaps.begin(), gaps.end());
    ll result = 0;
    for (int i = 0; i < gaps.size() - groups + 1; ++i)
        result += gaps[i];

    cout << result << '\n';
    return 0;
}

Problem 4: Clearing Levels

Start with level 1 in a min‑priority queue ordered by the required power. While the queue is not empty and the current power meets the top element’s requirement, clear the level, gain its bonus, and push all its children into the queue.

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;

struct Level {
    int bonus;
    int requirement;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    ll power;
    cin >> n >> power;

    vector<vector<int>> children(n + 1);
    vector<Level> stages(n + 1);
    for (int i = 1; i <= n; ++i) {
        int parent;
        cin >> parent >> stages[i].bonus >> stages[i].requirement;
        children[parent].push_back(i);
    }

    ll cleared = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    pq.push({stages[1].requirement, 1});

    while (!pq.empty() && power >= pq.top().first) {
        auto [req, id] = pq.top(); pq.pop();
        cleared++;
        power += stages[id].bonus;
        for (int child : children[id]) {
            pq.push({stages[child].requirement, child});
        }
    }

    cout << cleared << '\n';
    return 0;
}

Problem 5: Visiting All Neighbor (Tree Diameter)

Every edge must be traversed at least twice except for the edges on a single "main path" which are traversed only once. Thus the answer is 2 × (total edge weight) − (diameter of the tree).

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;

vector<vector<pair<int, ll>>> adj;
vector<ll> maxPath;
ll diameter = 0;

ll dfs(int u, int parent) {
    ll best1 = 0, best2 = 0;
    for (auto [v, w] : adj[u]) {
        if (v == parent) continue;
        ll distance = dfs(v, u) + w;
        if (distance > best1) {
            best2 = best1;
            best1 = distance;
        } else if (distance > best2) {
            best2 = distance;
        }
    }
    diameter = max(diameter, best1 + best2);
    maxPath[u] = best1;
    return maxPath[u];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    adj.assign(n + 1, {});
    maxPath.resize(n + 1);

    ll totalWeight = 0;
    for (int i = 1; i < n; ++i) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        adj[u].emplace_back(v, w);
        adj[v].emplace_back(u, w);
        totalWeight += 2 * w;
    }

    dfs(1, 0);
    cout << totalWeight - diameter << '\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.