Solutions and Implementations for ACGO Ranked Match #10
Problem 1 – ASCII Code Conversion
Link: A24630.ASCII
Reading an integer from input, output its corresponding ASCII character by casting the integer to char. In C++, a simple static cast or C-style cast handles this424‑bit conversion.
#include <iostream>
int main() {
int code;
std::cin >> code;
std::cout << static_cast<char>(code) << '\n';
}
Problem 2 – Picky Eater
Encoding the dishes' tastiness in an array,400 the largest value among all dishes is30D determined first. Then, for every dish index that the little coder dislikes, if its tastiness equals the maximum, output "Yes"; otherwise, after checking all disliked items, print "No".
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int n, k;
std::cin >> n >> k;
std::vector<int> taste(n + 1);
int best = 0;
for (int i = 1; i <= n; ++i) {
std::cin >> taste[i];
if (taste[i] > best) best = taste[i];
}
for (int i = 0, idx; i < k; ++i) {
std::cin >> idx;
if (taste[idx] == best) {
std::cout << "Yes\n";
return 0;
}
}
std::cout << "No\n";
}
Problem 3 – Peculiar Machine
erno Given n7B dials each showing a single digit, the task is to make all dials display the same digit k (0–9). One can turn any dial forward by one position per second. If the same digit a appears x times in a column, turning all those dials to a requires an extra (x‑1)*10 seconds beyond the individual rotation times. The minimal total time is found by brute‑forcing all target digits.
#include <iostream>
#include <string>
#include <algorithm>
#include <climits>
using namespace std;
int n;
string dials[105];
int count_occurrences(int col, int digit) {
int cnt = 0;
for (int i = 0; i < n; ++i)
if (dials[i][col] == digit + '0')
++cnt;
return cnt;
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) cin >> dials[i];
int best = INT_MAX;
for (int target = 0; target <= 9; ++target) {
int worst = 0;
for (int i = 0; i < n; ++i) {
int pos = dials[i].find(target + '0');
int freq = count_occurrences(pos, target);
int time = (freq - 1) * 10 + pos;
worst = max(worst, time);
}
best = min(best, worst);
}
cout << best << '\n';
}
Problem 4 – Unique Triplets
We need the number of triples (i, j, k) with distinct values in each position. Starting from all posssible triples C(n,3),6C we subtract those that contain duplicates. If a value appears c times:
- Triples where all three are the same value:
C(c,3) - Triples where exactly two are the same and the third differs:
C(c,2) * (n - c)
The result equals n*(n-1)*(n-2)/6 minus these quantities.
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int MAXV = 200005;
int freq[MAXV];
int main() {
ll n;
cin >> n;
ll total = n * (n - 1) * (n - 2) / 6;
for (ll i = 0, v; i < n; ++i) {
cin >> v;
freq[v]++;
}
for (int v = 1; v < MAXV; ++v) {
if (freq[v] == 0) continue;
ll c = freq[v];
ll three = 0, two = 0;
if (c >= 3) three = c * (c - 1) * (c - 2) / 6;
if (c >= 2) two = c * (c - 1) / 2 * (n - c);
total -= three + two;
}
cout << total << '\n';
}
Problem 5 – Road Reduction
Constructing a shortest‑path tree from vertex 1. Perform Dijkstra; whenever relaxing an edge (u, v, w, id) improves the distance to v, record id as the predecessor edge of v. After the algorithm, each reachable vertex (except the source) has exactly one predecessor edge. Output those edge IDs (N‑1 edges in total). Using unsigned long long for distances and taking care of large initial values avoids overflow.
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
typedef unsigned long long ull;
struct Edge {
int to, weight, id;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<Edge>> graph(n + 1);
for (int i = 1, u, v, w; i <= m; ++i) {
cin >> u >> v >> w;
graph[u].push_back({v, w, i});
graph[v].push_back({u, w, i});
}
vector<ull> dist(n + 1, ULLONG_MAX);
vector<int> parent_edge(n + 1, -1);
dist[1] = 0;
priority_queue<pair<ull, int>, vector<pair<ull,int>>, greater<>> pq;
pq.emplace(0, 1);
while (!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
if (d != dist[u]) continue;
for (auto &e : graph[u]) {
ull nd = d + e.weight;
if (nd < dist[e.to]) {
dist[e.to] = nd;
parent_edge[e.to] = e.id;
pq.emplace(nd, e.to);
}
}
}
for (int i = 2; i <= n; ++i)
if (parent_edge[i] != -1)
cout << parent_edge[i] << ' ';
}
Problem 6 – Dividing Bread
reverse the process: instead of cutting a loaf of length L into N pieces of given sizes, start with the N target pieces (and possibly a leftover piece of length L - sum) and repeatedly combine the two smallest pieces. The cost of each merge equals the sum of the two lengths. Using a min‑heap implements this Huffman‑like greedy strategy efficient.
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
ll L, total = 0, piece;
cin >> N >> L;
priority_queue<ll, vector<ll>, greater<ll>> pq;
for (int i = 0; i < N; ++i) {
cin >> piece;
total += piece;
pq.push(piece);
}
if (L != total)
pq.push(L - total);
ll cost = 0;
while (pq.size() > 1) {
ll a = pq.top(); pq.pop();
ll b = pq.top(); pq.pop();
cost += a + b;
pq.push(a + b);
}
cout << cost << '\n';
}