π§© AtCoder Beginner Contest 376
October 19, 2024About 3 min
π§© AtCoder Beginner Contest 376
# Info & Summary
- Date:
2024-10-19
- Completion Status: A β / B β / C β / D β / E β / F β / G β
- Problem Type:
Problem | Type(s) | Data Structure / Algo | π₯ Key Insight / Mark |
---|---|---|---|
D | Shortest distance | BFS | βπ₯ |
E | Math | Math | π§ π |
F | DP | DP | π |
G | Math | 01 on Tree | π |
Details
Emoji | Label | Meaning |
---|---|---|
β | Important | Problem is strategically useful or has reusable patterns |
π₯ | Must master | Classic problem or technique that appears often and is worth mastering |
β οΈ | Reattempt | You attempted but failed; worth reviewing and solving again |
π§ | New to me | Introduced a new idea, technique, or pattern you hadnβt seen before |
π οΈ | Template | You built or improved a code template from solving this problem |
π | Too hard now | Beyond current ability to understand/solve; revisit after more practice |
π A - Candy Button

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, C;
cin >> N >> C;
int ans = 0;
int lst = -1;
for (int i = 0; i < N; i++)
{
int T;
cin >> T;
if (lst == -1 || T - lst >= C)
{
ans += 1;
lst = T;
}
}
cout << ans << endl;
return 0;
}
π B - Hands on Ring (Easy)

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
int ans = 0;
int L = 0, R = 1;
for (int i = 0; i < Q; i++)
{
char H;
int T;
cin >> H >> T;
T--;
if (H == 'L')
{
if ((L < R && R < T) || (T < R && R < L))
{
ans += N - abs(T - L);
}
else
{
ans += abs(T - L);
}
L = T;
}
else
{
if ((R < L && L < T) || (T < L && L < R))
{
ans += N - abs(T - R);
}
else
{
ans += abs(T - R);
}
R = T;
}
}
cout << ans << endl;
return 0;
}
π C - Prepare Another Box

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<long long> A(N), B(N - 1);
for (int i = 0; i < N; i++)
{
cin >> A[i];
}
for (int i = 0; i < N - 1; i++)
{
cin >> B[i];
}
sort(A.begin(), A.end(), greater());
sort(B.begin(), B.end(), greater());
int i = 0;
while (i < N - 1 && A[i] <= B[i])
{
i++;
}
for (int j = i; j < N - 1; j++)
{
if (B[j] < A[j + 1])
{
cout << -1 << endl;
return 0;
}
}
cout << A[i] << endl;
return 0;
}
π D - Cycle

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
vector<vector<int>> adj(N);
for (int i = 0; i < M; i++)
{
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
}
vector<int> dis(N, -1);
dis[0] = 0;
queue<int> q;
q.push(0);
int ans = N + 1;
while (!q.empty())
{
auto u = q.front();
q.pop();
for (auto v : adj[u])
{
if (v == 0)
{
ans = min(ans, dis[u] + 1);
}
if (dis[v] == -1)
{
dis[v] = dis[u] + 1;
q.push(v);
}
}
}
cout << (ans == N + 1 ? -1 : ans) << endl;
return 0;
}
π E - Max Γ Sum

Tips
Sort ascendingly, so
For each position , we only need to choose items from to put into
And, regardless of our choice
So the new objective function becomes:
Tips
For all , how to compute the sum of the smallest values in ?
To do this efficiently, we use:
- A max-heap (priority queue) to track the smallest values among current prefix
- If size exceeds , remove the largest(i.e., worst)
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int N, K;
cin >> N >> K;
vector<long long> A(N), B(N);
for (int i = 0; i < N; i++)
{
cin >> A[i];
}
for (int i = 0; i < N; i++)
{
cin >> B[i];
}
long long ans = 1e18;
vector<int> idx(N);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int i, int j)
{ return A[i] < A[j]; });
priority_queue<long long> pq;
long long sum = 0;
for (auto i : idx)
{
sum += B[i];
pq.push(B[i]);
while (pq.size() > K)
{
sum -= pq.top();
pq.pop();
}
if (pq.size() == K)
{
ans = min(ans, sum * A[i]);
}
}
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
π F - Hands on Ring (Hard)

Warning
Hard Problems to Tackle Later
// TODO
π G - Treasure Hunting

Warning
Hard Problems to Tackle Later
// TODO