π§© AtCoder Beginner Contest 389
π§© AtCoder Beginner Contest 389
# Info & Summary
- Date:
2025-01-18
- Completion Status: A β / B β / C β / D β / E β / F β / G β
- Problem Type:
Problem | Type(s) | Data Structure / Algo | π₯ Key Insight / Mark |
---|---|---|---|
E | cheap-first strategy | Binary Search | π§ |
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 - 9x9

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
cout << (s[0] - '0') * (s[2] - '0') << endl;
return 0;
}
π B - tcaF

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long x;
cin >> x;
int ans = 0;
long long cur = 1;
while (cur != x)
{
ans++;
cur *= ans;
}
cout << ans << endl;
return 0;
}
π C - Snake Queue

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
cin >> q;
vector<long long> snake(q + 10, 0);
int left = 1, right = 0;
while (q--)
{
int a;
cin >> a;
if (a == 1)
{
int l;
cin >> l;
right++;
snake[right] = l + snake[right - 1];
}
else if (a == 2)
{
left++;
}
else
{
int k;
cin >> k;
k--;
cout << snake[left + k - 1] - snake[left - 1] << endl;
}
}
return 0;
}
π D - Squares in Circle

Tips
Let be the center of the circle
- Among the conforming pairs , we can easily count those with or ; there are of them, where one is on the origin and the others are in one of the four directions
- Next, for and , by symmetry, it is sufficient to count those with and , and multiply the count by
We enumerate for in order, then, the maximum integer with decreases monotonically
Thus, by managing the maximum in the manner of the sliding window trick, we obtain a solution with time complexity
Tips
Note that while we may using decimals for this problem, in general it is vulnerable to precision errors, so it is a good idea to stick to integers whenever possible
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long r;
cin >> r;
auto calc = [&](long long a, long long b)
{
return (2 * a + 1) * (2 * a + 1) + (2 * b + 1) * (2 * b + 1) <= 4 * r * r;
};
long long res = 0;
long long j = r - 1;
for (long long i = 1; calc(i, 1); i++)
{
while (!calc(i, j))
{
j--;
}
res += j;
}
long long ans = (r - 1) * 4 + 1;
ans += res * 4;
cout << ans << endl;
return 0;
}
π E - Square Price

Tips
That buying instances of the -th product cost yen means:
- each instance of the -th product has a different price
- the -th (-based indexing) cheapest instance costs yen
Now, we know the prices of the items, where we can maximize the number of items to buy by picking from the cheapest, but of course, we cannot enumerate all the prices of the items and sort them
Tips
Herem the cheap-first strategy has the following property:
- there exists an integer such that we buy all the items with price at most , as well as as many items of price as possible
This can be found with Binary Search
Once we find this , the maximum number of items that can be bought is also found
#include <bits/stdc++.h>
using namespace std;
using i128 = __int128_t;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long n, m;
cin >> n >> m;
vector<long long> P(n);
for (int i = 0; i < n; i++)
{
cin >> P[i];
}
auto check = [&](long long mid) -> pair<i128, i128>
{
i128 cost = 0, cnt = 0;
for (int i = 0; i < n; i++)
{
if (mid < P[i])
{
continue;
}
long long upper = mid / P[i];
long long k = (upper + 1) / 2;
cnt += k;
cost += i128(P[i]) * k * k;
if (cost > m)
{
break;
}
}
return {cost, cnt};
};
long long l = 0, r = m + 1;
while (r - l > 1)
{
long long mid = l + (r - l) / 2;
(check(mid).first <= m ? l : r) = mid;
}
auto [cost, cnt] = check(l);
long long ans = cnt + (m - cost) / r;
cout << ans << endl;
return 0;
}
π F - Rated Range

Tips
A naive solution is to simulate the contests one by one for each initial rating given in a query. This approach costs time, which is not fast enough this time
Here, instead of simulating the contests for each query, consider precalculating the answers for before processing the queries, and respond the queries based on these precalculated values
Warning
Hard Problems to Tackle Later
// TODO
π G - Odd Even Graph

Warning
Hard Problems to Tackle Later
// TODO