contest_id stringlengths 1 4 | index stringclasses 43
values | title stringlengths 2 63 | statement stringlengths 51 4.24k | tutorial stringlengths 19 20.4k | tags listlengths 0 11 | rating int64 800 3.5k ⌀ | code stringlengths 46 29.6k ⌀ |
|---|---|---|---|---|---|---|---|
1930 | I | Counting Is Fun | You are given a binary$^\dagger$ pattern $p$ of length $n$.
A binary string $q$ of the same length $n$ is called \textbf{good} if for every $i$ ($1 \leq i \leq n$), there exist indices $l$ and $r$ such that:
- $1 \leq l \leq i \leq r \leq n$, and
- $p_i$ is a mode$^\ddagger$ of the string $q_lq_{l+1}\ldots q_r$.
Cou... | A167510 It is convinient here to assign weights to $\mathtt{0} \to -1$ and $\mathtt{1} \to 1$. Given a string $t$, we can define the prefix sum $p$ of it's weights. For example, if $t=\mathtt{0010111}$, then $p=[0,-1,-2,-1,-2,-1,0,1]$. So that if $t$ is bad and $i$ is a index that violates the definition, then $\max(p_... | [
"combinatorics"
] | 3,500 | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ii pair<int,int>
#define iii tuple<int,int,int>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#defi... |
1931 | A | Recovering a Small String | Nikita had a word consisting of exactly $3$ lowercase Latin letters. The letters in the Latin alphabet are numbered from $1$ to $26$, where the letter "a" has the index $1$, and the letter "z" has the index $26$.
He encoded this word as the sum of the positions of all the characters in the alphabet. For example, the w... | The problem can be solved by simply going through all the $3$ letter combinations and searching for the lexicographically minimal one among them. It is also possible to consider a string consisting of three letters "a", and, going through it from the end until the value of $n$ is greater than zero, increase the letters... | [
"brute force",
"strings"
] | 800 | #include<bits/stdc++.h>
using namespace std;
void solve(){
int n, sz = 26;
cin >> n;
string mins = "zzz", cur;
for(int i = 0; i < sz; i++){
for(int j = 0; j < sz; j++){
for(int k = 0; k < sz; k++){
if(i + j + k + 3 == n){
cur += char(i + 'a');
... |
1931 | B | Make Equal | There are $n$ containers of water lined up, numbered from left to right from $1$ to $n$. Each container can hold any amount of water; initially, the $i$-th container contains $a_i$ units of water. The sum of $a_i$ is divisible by $n$.
You can apply the following operation any (possibly zero) number of times: pour any ... | Since the number of operations does not matter, let's find any suitable sequence of operations. Each vessel should contain $k = \frac{\sum a_i}{n}$ units of water. Water can only be poured to the right, so we will iterate over $i$ from $1$ to $n-1$ and pour all the excess water from the $i$-th vessel to the $(i+1)$-th.... | [
"greedy"
] | 800 | def solve():
n = int(input())
a = [int(x) for x in input().split()]
k = sum(a) // n
for i in range(n - 1):
if a[i] < k:
print('NO')
return
a[i + 1] += a[i] - k
a[i] = k
print('YES')
for _ in range(int(input())):
solve() |
1931 | C | Make Equal Again | You have an array $a$ of $n$ integers.
You can \textbf{no more than once} apply the following operation: select three integers $i$, $j$, $x$ ($1 \le i \le j \le n$) and assign all elements of the array with indexes from $i$ to $j$ the value $x$. The price of this operation depends on the selected indices and is equal ... | If all the elements of the arrays are equal, then nothing additional needs to be done, and the answer is $0$. Otherwise, you need to apply the assignment operation on the segment alone. Our goal is to choose the shortest possible segment, which means to exclude as many elements as possible from the beginning and end of... | [
"brute force",
"greedy",
"math"
] | 1,000 | def solve():
n = int(input())
a = list(map(int, input().split()))
i1 = 0
i2 = 0
while i1 < n and a[i1] == a[0]:
i1 += 1
while i2 < n and a[n - i2 - 1] == a[n - 1]:
i2 += 1
res = n
if a[0] == a[n - 1]:
res -= i1
res -= i2
else:
res -= max(i1, i2... |
1931 | D | Divisible Pairs | Polycarp has two favorite integers $x$ and $y$ (they can be equal), and he has found an array $a$ of length $n$.
Polycarp considers a pair of indices $\langle i, j \rangle$ ($1 \le i < j \le n$) beautiful if:
- $a_i + a_j$ is divisible by $x$;
- $a_i - a_j$ is divisible by $y$.
For example, if $x=5$, $y=2$, $n=6$, $... | Let's consider a good pair. Since $(a_i + a_j) \bmod x = 0$, it follows that $(a_i \bmod x + a_j \bmod x) \bmod x = 0$, which implies that $a_i \bmod x + a_j \bmod x$ is either $x$ or $0$. Therefore, for some $j$, this holds true if $a_i \bmod x = (x - a_j \bmod x) \bmod x$. Since $(a_i - a_j) \bmod y = 0$, it follows ... | [
"combinatorics",
"math",
"number theory"
] | 1,300 | def solve():
n, x, y = map(int, input().split())
a = [int(x) for x in input().split()]
cnt = dict()
ans = 0
for e in a:
xx, yy = e % x, e % y
ans += cnt.get(((x - xx) % x, yy), 0)
cnt[(xx, yy)] = cnt.get((xx, yy), 0) + 1
print(ans)
for _ in range(int(input())):
... |
1931 | E | Anna and the Valentine's Day Gift | Sasha gave Anna a list $a$ of $n$ integers for Valentine's Day. Anna doesn't need this list, so she suggests destroying it by playing a game.
Players take turns. Sasha is a gentleman, so he gives Anna the right to make the first move.
- On her turn, \textbf{Anna must} choose an element $a_i$ from the list and reverse... | If the decimal representation of a number $x$ has exactly $c$ digits, then $x \ge 10^{c - 1}$. From this, it can be concluded that Sasha is not required to maximize the final number; it is sufficient for him to maximize the number of digits in it. During his turn, Sasha does not change the total number of digits, but A... | [
"games",
"greedy",
"math",
"sortings"
] | 1,400 | #include <bits/stdc++.h>
#define all(arr) arr.begin(), arr.end()
using namespace std;
const int MAXN = 200200;
int n, m;
string arr[MAXN];
int len[MAXN], zrr[MAXN];
void build() {
memset(zrr, 0, sizeof(*zrr) * n);
for (int i = 0; i < n; ++i) {
len[i] = arr[i].size();
for (auto it = arr... |
1931 | F | Chat Screenshots | There are $n$ people in the programming contest chat. Chat participants are ordered by activity, but each person sees himself at the top of the list.
For example, there are $4$ participants in the chat, and their order is $[2, 3, 1, 4]$. Then
- $1$-st user sees the order $[1, 2, 3, 4]$.
- $2$-nd user sees the order $... | The author of the screenshot is always in the first position, so based on his screenshot, nothing can be said about his first position. The rest of the chat participants are ordered based on the real order. Let's build a graph of $n$ vertices. For each screenshot, add $n - 2$ edges. For all $2 \le i <n$, add an edge be... | [
"combinatorics",
"dfs and similar",
"graphs"
] | 1,700 | #include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
int timer = 0;
void dfs(int v, vector<vector<int>> &g, vector<bool> &vis, vector<int> &tout) {
vis[v] = true;
for (int u: g[v]) {
if (!vis[u]) {
dfs(u... |
1931 | G | One-Dimensional Puzzle | You have a one-dimensional puzzle, all the elements of which need to be put in one row, connecting with each other. All the puzzle elements are completely white and distinguishable from each other only if they have different shapes.
Each element has straight borders at the top and bottom, and on the left and right it ... | Note that the elements of the $3$ and $4$ types on the right have a connection type opposite to that on the left. This means that what type of connection should be at the end of the chain to attach an element of any of these types will remain the same. Therefore, elements of these types can be combined into separate ch... | [
"combinatorics",
"math",
"number theory"
] | 2,000 |
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const int mod = 998244353;
ll pow_mod(ll x, ll p) {
if (p == 0) {
return 1;
}
if (p % 2 == 0) {
ll y = pow_mod(x, p / 2);
return (y * y) % mo... |
1932 | A | Thorns and Coins | During your journey through computer universes, you stumbled upon a very interesting world. It is a path with $n$ consecutive cells, each of which can either be empty, contain thorns, or a coin. In one move, you can move one or two cells along the path, provided that the destination cell does not contain thorns (and be... | Let's move forward by $1$ if the next cell does not have spikes, and by $2$ otherwise. By doing so, we will visit all spike-free cells that we can reach, and thus collect all the coins in those cells. Note that if we are in cell $i$, and cells $i+1$ and $i+2$ have spikes, then we can only jump into the spikes and thus ... | [
"dp",
"greedy",
"implementation"
] | 800 | #include <bits/stdc++.h>
using namespace std;
signed main() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
int t;
cin >> t;
for(int _ = 0; _ < t; ++_){
int n, ans = 0;
cin >> n;
string s;
cin >> s;
for (int i = 1; i < n; i++) {
... |
1932 | B | Chaya Calendar | The Chaya tribe believes that there are $n$ signs of the apocalypse. Over time, it has been found out that the $i$-th sign occurs every $a_i$ years (in years $a_i$, $2 \cdot a_i$, $3 \cdot a_i$, $\dots$).
According to the legends, for the apocalypse to happen, the signs must occur sequentially. That is, first they wai... | The tribe will wait for the first sign in year $a_1$. They will expect the second event in some year $x > a_1$, which is divisible by $a_2$, this will happen after $a_2 - a_1 \bmod a_2$ years. Let's maintain the number $cur$ of the year in which the $i$-th sign occurred, then the $(i+1)$-th will occur in the year $cur ... | [
"number theory"
] | 1,100 | def solve():
n = int(input())
a = [int(x) for x in input().split()]
cur = 0
for e in a:
cur += e - cur % e
print(cur)
for _ in range(1, int(input()) + 1):
solve() |
1932 | C | LR-remainders | You are given an array $a$ of length $n$, a positive integer $m$, and a string of commands of length $n$. Each command is either the character 'L' or the character 'R'.
Process all $n$ commands in the order they are written in the string $s$. Processing a command is done as follows:
- First, output the remainder of t... | If we perform all deletions except the last one, only one element will remain. Let's find its index in the array. Now we will perform the operations in reverse order, then the deletion operations will become additions, which are much easier to maintain. We will store the remainder of the division of the product of the ... | [
"brute force",
"data structures",
"implementation",
"math",
"two pointers"
] | 1,400 | #include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
int main() {
int t;
cin >> t;
forn(tt, t) {
int n, m;
cin >> n >> m;
vector<int> a(n);
forn(i, n)
cin >> a[i];
string s;
cin >> s;
int... |
1932 | D | Card Game | Two players are playing an online card game. The game is played using a 32-card deck. Each card has a suit and a rank. There are four suits: clubs, diamonds, hearts, and spades. We will encode them with characters 'C', 'D', 'H', and 'S', respectively. And there are 8 ranks, in increasing order: '2', '3', '4', '5', '6',... | Let's start by solving the problem separately for each suit, except for the trump suit. To do this, we will form the maximum possible number of pairs, after which there will be no more than one card of this suit without a pair. Thus, we will need the minimum number of trump cards to beat the non-trump cards. Now we wil... | [
"greedy",
"implementation"
] | 1,400 | #include <bits/stdc++.h>
#define long long long int
#define DEBUG
using namespace std;
// @author: pashka
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
for (int tt = 0; tt < t; tt++) {
int n;
cin >> n;
string suites = "CDHS";
string ts;
... |
1932 | E | Final Countdown | You are in a nuclear laboratory that is about to explode and destroy the Earth. You must save the Earth before the final countdown reaches zero.
The countdown consists of $n$ ($1 \le n \le 4 \cdot 10^5$) mechanical indicators, each showing one decimal digit. You noticed that when the countdown changes its state from $... | Let's assume that the number $s$ is initially displayed on the countdown. Let's see how many times each of the indicators will switch. Indicator number $i$ (if we number the indicators from right to left, starting with 0) will switch exactly $\lfloor s / 10^i \rfloor$ times. Thus, the answer is equal to $\sum_{i=0}^{n-... | [
"implementation",
"math",
"number theory"
] | 1,600 | #include <bits/stdc++.h>
#define long long long int
#define DEBUG
using namespace std;
// @author: pashka
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
reverse(s.begin(), s.end());
ve... |
1932 | F | Feed Cats | There is a fun game where you need to feed cats that come and go. The level of the game consists of $n$ steps. There are $m$ cats; the cat $i$ is present in steps from $l_i$ to $r_i$, inclusive. In each step, you can feed all the cats that are currently present or do nothing.
If you feed the same cat more than once, i... | Let's use dynamic programming. Let $dp_i$ be the answer for the first $i$ moves ($dp_0 = 0$). Then there are two possible cases: we fed the cats on step $i$ or not. If we did not feed the cats on step $i$, then $dp_i = dp_{i - 1}$, because this is the best result for the first $i-1$ moves, and nothing has changed on th... | [
"data structures",
"dp",
"sortings"
] | 1,900 | #include <bits/stdc++.h>
//#define int long long
#define pb emplace_back
#define mp make_pair
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
typedef long double ld;
typedef long long ll;
using namespace std;
mt19937 rnd(time(nullptr));
const ll inf = 1e... |
1932 | G | Moving Platforms | There is a game where you need to move through a labyrinth. The labyrinth consists of $n$ platforms, connected by $m$ passages.
Each platform is at some level $l_i$, an integer number from $0$ to $H - 1$. In a single step, if you are currently on platform $i$, you can stay on it, or move to another platform $j$. To mo... | First, note that the event of two platforms being on the same level at a given moment does not depend on your moves. Hence, it is always optimal to get to any vertex you may need to reach the vertex $n$ as soon as possible. It means that you can simply run Dijkstra's algorithm to calculate the minimal number of moves n... | [
"graphs",
"math",
"number theory",
"shortest paths"
] | 2,300 | #include <bits/stdc++.h>
#define long long long int
#define DEBUG
using namespace std;
// @author: pashka
struct triple {
long d, x, y;
};
triple eucl(long a, long b) {
if (b == 0) {
return {a, 1, 0};
}
long k = a / b;
auto [d, x, y] = eucl(b, a - k * b);
return {d, y, x - k * y};
... |
1933 | A | Turtle Puzzle: Rearrange and Negate | You are given an array $a$ of $n$ integers. You must perform the following two operations on the array (the first, then the second):
- Arbitrarily rearrange the elements of the array or leave the order of its elements unchanged.
- Choose at most one contiguous segment of elements and replace the signs of all elements ... | Neither of the operations change the absolute value of any $a_i$. Therefore, the maximum answer we can get is $|a_1| + |a_2| + \ldots + |a_n|$. Now we will show there is a way to obtain this sum. The way is simple. Firstly use operation 1 to sort the array, so all the negative elements come before the non-negative elem... | [
"greedy",
"math",
"sortings"
] | 800 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += abs... |
1933 | B | Turtle Math: Fast Three Task | You are given an array $a_1, a_2, \ldots, a_n$.
In one move, you can perform either of the following two operations:
- Choose an element from the array and remove it from the array. As a result, the length of the array decreases by $1$;
- Choose an element from the array and increase its value by $1$.
You can perfor... | Let's denote the sum of elements as $s$. If $s$ is already divisible by $3$, then the answer is $0$. The answer is $1$ in the following cases: If $s \bmod 3 = 2$, then we can add $1$ to any element to make the sum divisible by $3$; If there exists an $a_i$ such that $s \bmod 3 = a_i \bmod3$, then we can remove such $a_... | [
"implementation",
"math",
"number theory"
] | 800 | #include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int k;
cin>>k;
int ACC=0;
bool hv=false;
for(int i=0;i<k;i++){
int x;
cin>>x;
ACC+=x;
if(x%3==1){
hv=true;
}
}
if(ACC%3... |
1933 | C | Turtle Fingers: Count the Values of k | You are given three \textbf{positive} integers $a$, $b$ and $l$ ($a,b,l>0$).
It can be shown that there always exists a way to choose \textbf{non-negative} (i.e. $\ge 0$) integers $k$, $x$, and $y$ such that $l = k \cdot a^x \cdot b^y$.
Your task is to find the number of distinct possible values of $k$ across all suc... | Notice that there are not so many suitable values of $x$ and $y$ (since $2 \le a, b$ and $2^{20}=1\ 048\ 576$). This allows us to iterate through all suitable pairs of $x$ and $y$ and thus find all different suitable $k$. | [
"brute force",
"implementation",
"math",
"number theory"
] | 1,100 | #include <bits/stdc++.h>
#define int long long
using namespace std;
void solve(int tc){
int a, b, l;
cin >> a >> b >> l;
set<int> ans;
for(int i = 0; i <= 34; ++i){
int x = l;
bool fail = false;
for(int _ = 0; _ < i; ++_){
if(x % a){
fail = true;
... |
1933 | D | Turtle Tenacity: Continual Mods | Given an array $a_1, a_2, \ldots, a_n$, determine whether it is possible to \textbf{rearrange its elements} into $b_1, b_2, \ldots, b_n$, such that $b_1 \bmod b_2 \bmod \ldots \bmod b_n \neq 0$.
Here $x \bmod y$ denotes the remainder from dividing $x$ by $y$. Also, the modulo operations are calculated from left to rig... | Sort the array in non-decreasing order. Now, assume $a_1 \le a_2 \le \ldots \le a_n$. If $a_1 \neq a_2$, the minimum is unique. Therefore, place $a_1$ at the front, and the result after all modulo operations is just $a_1 > 0$. Hence the answer is yes for this case. If $a_1 = a_2$ and there exists some element $a_x$ suc... | [
"constructive algorithms",
"greedy",
"math",
"number theory",
"sortings"
] | 1,200 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
int a[n];
for(int i=0; i<n; i++) cin >> a[i];
sort(a, a + n);
if(a[0] != a[1]) {
cout << "YES\n";
}
else {
bool P... |
1933 | E | Turtle vs. Rabbit Race: Optimal Trainings | Isaac begins his training. There are $n$ running tracks available, and the $i$-th track ($1 \le i \le n$) consists of $a_i$ equal-length sections.
Given an integer $u$ ($1 \le u \le 10^9$), finishing each section can increase Isaac's ability by a certain value, described as follows:
- Finishing the $1$-st section inc... | Notice that if we choose some $r$ such that the sum of $a_l, a_{l+1}, \dots, a_r$ does not exceed $u$, then completing each of the sections will increase our abilities. Using prefix sums and binary search, we will find the largest such $r$. Smaller values will increase our abilities by a smaller amount, so there is no ... | [
"binary search",
"implementation",
"math",
"ternary search"
] | 1,500 | #include "bits/stdc++.h"
using namespace std;
#define int long long
#define double long double
void solve(int tc) {
int n;
cin >> n;
int a[n + 1];
for(int i = 1; i <= n; i++) cin >> a[i];
int ps[n + 1];
ps[0] = 0;
for(int i = 1; i <= n; i++) ps[i] = ps[i - 1] + a[i];
int q;
cin >> q;
while(q--) {
... |
1933 | F | Turtle Mission: Robot and the Earthquake | The world is a grid with $n$ rows and $m$ columns. The rows are numbered $0, 1, \ldots, n-1$, while the columns are numbered $0, 1, \ldots, m-1$. In this world, the columns are \textbf{cyclic} (i.e. the top and the bottom cells in each column are adjacent). The cell on the $i$-th row and the $j$-th column ($0 \le i < n... | Idea: erniepsycholone, prepared: erniepsycholone View the task in a relative perspective, with robot RT and the ending location moving downwards instead of rocks moving upwards. | [
"dfs and similar",
"dp",
"graphs",
"shortest paths"
] | 2,100 | null |
1933 | F | Turtle Mission: Robot and the Earthquake | The world is a grid with $n$ rows and $m$ columns. The rows are numbered $0, 1, \ldots, n-1$, while the columns are numbered $0, 1, \ldots, m-1$. In this world, the columns are \textbf{cyclic} (i.e. the top and the bottom cells in each column are adjacent). The cell on the $i$-th row and the $j$-th column ($0 \le i < n... | By viewing the robot's movement relative to the rocks, Robot RT's three moves become as follows: Up: Stationary Down, $(x,y)$ to $((x+2) \bmod n, y)$ Right, $(x,y)$ to $((x+1) \bmod n, y+1)$ As staying stationary is not necessary now when we are finding the minimum time, we can run a bfs/dp from $(0,0)$ to find the min... | [
"dfs and similar",
"dp",
"graphs",
"shortest paths"
] | 2,100 | #include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
int t;
cin >> t;
while (t--){
int n, m;
cin >> n >> m;
bool a[n][m + 1];
for (int i = 0; i < n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
int dp[n][m + 1];
... |
1933 | G | Turtle Magic: Royal Turtle Shell Pattern | Turtle Alice is currently designing a fortune cookie box, and she would like to incorporate the theory of LuoShu into it.
The box can be seen as an $n \times m$ grid ($n, m \ge 5$), where the rows are numbered $1, 2, \dots, n$ and columns are numbered $1, 2, \dots, m$. Each cell can either be \textbf{empty} or have a ... | We claim that there are only $8$ configurations that satisfy the condition. The proof is as follows. Firstly, consider a $2 \times 2$ subgrid that does not lie on any of the grid's corners. Claim 1. Within the $2 \times 2$ subgrid, there must be 2 Os and 2 Xs. Proof of Claim 1. Assume the contrary that there are 3 Os o... | [
"bitmasks",
"brute force",
"combinatorics",
"constructive algorithms",
"dfs and similar",
"math"
] | 2,300 | #include "bits/stdc++.h"
using namespace std;
void solve(int tc) {
int n, m, q;
cin >> n >> m >> q;
bool b[8] = {1, 1, 1, 1, 1, 1, 1, 1};
int ans = 8;
cout << ans << '\n';
while(q--) {
int r, c;
cin >> r >> c;
string shape;
cin >> shape;
if((r + (c+1) / 2) % 2) {
b[0] &= (shape =... |
1934 | A | Too Min Too Max | Given an array $a$ of $n$ elements, find the maximum value of the expression:
$$|a_i - a_j| + |a_j - a_k| + |a_k - a_l| + |a_l - a_i|$$
where $i$, $j$, $k$, and $l$ are four \textbf{distinct} indices of the array $a$, with $1 \le i, j, k, l \le n$.
Here $|x|$ denotes the absolute value of $x$. | What will be answer if there were only $4$ elements in the array? Suppose if there were only $4$ elements in the array. Let them be $a \leq b \leq c \leq d$. Then the answer will be maximum of the three cases which are listed as follows:- $|a-b|+|b-c|+|c-d|+|d-a| = 2*d - 2*a$ $|a-b|+|b-d|+|d-c|+|c-a| = 2*d-2*a$ $|a-c|+... | [
"greedy",
"math"
] | 800 | t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
ans = 2 * (a[n - 1] - a[0] + a[n - 2] - a[1])
print(ans) |
1934 | B | Yet Another Coin Problem | You have $5$ different types of coins, each with a value equal to one of the first $5$ triangular numbers: $1$, $3$, $6$, $10$, and $15$. These coin types are available in abundance. Your goal is to find the minimum number of these coins required such that their total value sums up to exactly $n$.
We can show that the... | At max how many $1$, $3$, $6$, $10$ are required? Fact: You will never need more than $2$ ones, $1$ threes, $4$ sixes and $2$ tens. Reason: For $1$: Suppose if you used $k$ > $2$ ones, then you could have used one $3$ and $k$ - $3$ ones. For $3$: Suppose if you used $k$ > $1$ threes, then you could have used one $6$ an... | [
"brute force",
"dp",
"greedy",
"math"
] | 1,200 | #include<bits/stdc++.h>
using namespace std;
int getAns(int n){
int ans=0;
ans+=n/15;
n%=15;
ans+=n/6;
n%=6;
ans+=n/3;
n%=3;
ans+=n;
return ans;
}
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr);
int testcases;
cin>>testcases;
for(int i=1;i<=testcase... |
1934 | C | Find a Mine | \textbf{This is an interactive problem.}
You are given a grid with $n$ rows and $m$ columns. The coordinates $(x, y)$ represent the cell on the grid, where $x$ ($1 \leq x \leq n$) is the row number counting from the top and $y$ ($1 \leq y \leq m$) is the column number counting from the left. It is guaranteed that ther... | After querying "$?$ $1$ $1$" What do we know about the location of mines? If the answer is $a_1$ the location of one of the mines is $(x, y)$ such that $x+y = a_1+2$. First, we query "$?$ $1$ $1$" then you get a value $a_1$ and you know that at least one of the mine is on the line $x+y=a_1+2$. Now we make two more quer... | [
"binary search",
"constructive algorithms",
"geometry",
"greedy",
"interactive",
"math"
] | 1,700 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll query(ll x, ll y) {
cout << "? " << x << ' ' << y << endl;
ll d;
cin >> d;
return d;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
ll t;
cin >> t;
while (t--) {
ll n, m;
... |
1934 | D1 | XOR Break --- Solo Version | \textbf{This is the solo version of the problem. Note that the solution of this problem may or may not share ideas with the solution of the game version. You can solve and get points for both versions independently.}
\textbf{You can make hacks only if both versions of the problem are solved.}
Given an integer variabl... | To generate any possible $m$, it takes at most two operations. Let's determine the achievable values of $m$ for a given $n$. If $n$ is a perfect power of $2$, then it cannot be broken down further, and no $m < n$ is achievable. Otherwise, if $n$ has at least two set bits, let's denote the most significant bit as $a$ an... | [
"bitmasks",
"constructive algorithms",
"greedy"
] | 2,100 | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
ll t;
cin >> t;
while (t--) {
ll n, m;
cin >> n >> m;
ll hi = 0, sec_hi = 0;
for (ll p = (1LL<<62); p > 0; p >>= 1) {
... |
1934 | D2 | XOR Break --- Game Version | \textbf{This is an interactive problem.}
\textbf{This is the game version of the problem. Note that the solution of this problem may or may not share ideas with the solution of the solo version. You can solve and get points for both versions independently.}
Alice and Bob are playing a game. The game starts with a pos... | If both $p_1$ and $p_2$ are perfect powers of $2$, it is a losing state since you cannot perform a break operation on either of those. If either $p_1$ or $p_2$ has a bit count of $2$, then this is a winning state. You can force your opponent into the state described in Hint $1$ using the number which has $2$ bitcount. ... | [
"bitmasks",
"games",
"greedy",
"interactive"
] | 2,400 | #include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int testcases;cin>>testcases;
for(int testcase=1;testcase<=testcases;testcase++){
long long n;cin>>n;
long long curr=n;
int parity=0;
if(__builtin_popcountll(n)... |
1934 | E | Weird LCM Operations | Given an integer $n$, you construct an array $a$ of $n$ integers, where $a_i = i$ for all integers $i$ in the range $[1, n]$. An operation on this array is defined as follows:
- Select three distinct indices $i$, $j$, and $k$ from the array, and let $x = a_i$, $y = a_j$, and $z = a_k$.
- Update the array as follows: $... | Fact 1: If for $(x, y, z)$ their pairwise GCDs are equal to their common GCD (this means that $(x, y, z)$ = $(g * X, g * Y, g * Z)$ where $(X, Y, Z)$ are pairwise coprime), then making an operation on them (gives $(g * X * Y, g * X * Z,g * Y * Z)$) and looking at the subsequences of size EXACTLY 2, we find all three GC... | [
"brute force",
"constructive algorithms",
"number theory"
] | 3,000 | #include<bits/stdc++.h>
using namespace std;
vector<vector<vector<int>>> pans(14);
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr);
int t;cin>>t;
pans[3]={{1,2,3}};
pans[4]={{1,3,4}};
pans[5]={{3,4,5}};
pans[6]={{1,3,5},{2,4,6}};
pans[7]={{2,4,6},{3,5,7}};
pans[8]={{2,6... |
1935 | A | Entertainment in MAC | Congratulations, you have been accepted to the Master's Assistance Center! However, you were extremely bored in class and got tired of doing nothing, so you came up with a game for yourself.
You are given a string $s$ and an \textbf{even} integer $n$. There are two types of operations that you can apply to it:
- Add ... | The answer will always have either the prefix $s$, or the reversed string $s$. Adding the string to the end is required no more than once. Let $t$ be the reversed string $s$. Notice that it is advantageous for us to use operation 1 (adding the reversed string at the end) no more than once. Indeed, having obtained some ... | [
"constructive algorithms",
"strings"
] | 800 | t = int(input())
for _ in range(t):
n = int(input())
s = input()
t = s[::-1]
print(min(s, t + s))
|
1935 | B | Informatics in MAC | In the Master's Assistance Center, Nyam-Nyam was given a homework assignment in informatics.
There is an array $a$ of length $n$, and you want to divide it into $k > 1$ subsegments$^{\dagger}$ in such a way that the $\operatorname{MEX} ^{\ddagger}$ on each subsegment is equal to the same integer.
Help Nyam-Nyam find ... | What is the minimum $k$ that can be in a division? Suppose $\operatorname{MEX}(x, y) = \operatorname{MEX}(y + 1, z)$, what can be said about $\operatorname{MEX}(x, z)$? Suppose we correctly divided the array into $k > 2$ segments - $(1, r_1), (l_2, r_2), \ldots, (l_k, r_k)$. Then, note that we can merge first two subse... | [
"constructive algorithms"
] | 1,200 | def solve():
n = int(input())
a = list(map(int, input().split()))
cur_mex = 0
cur_have = [0] * (n + 1)
for el in a:
cur_have[el] += 1
while cur_have[cur_mex]:
cur_mex += 1
another_mex = 0
another_have = [0] * (n + 1)
for i in range(n):
cur_have[a[i]] -= 1
... |
1935 | C | Messenger in MAC | In the new messenger for the students of the Master's Assistance Center, Keftemerum, an update is planned, in which developers want to optimize the set of messages shown to the user. There are a total of $n$ messages. Each message is characterized by two integers $a_i$ and $b_i$. The time spent reading the set of messa... | Try to find the answer to the problem by hand. How can changing the order of the messages reduce time? Let the order in the response be $p_1, p_2, \ldots, p_k$. It is always advantageous for the response to satisfy $b_{p_1} \leq b_{p_2} \leq \ldots \leq b_{p_k}$. Greedy? Let the order in the response be $p_1, p_2, \ldo... | [
"binary search",
"brute force",
"constructive algorithms",
"data structures",
"dp",
"greedy",
"sortings"
] | 1,800 | import heapq
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, L = map(int, input().split())
v = []
for i in range(n):
a, b = map(int, input().split())
v.append((b, a))
v.sort()
ans = 0
for l in range(n):
pq = []
heapq.heapify(pq)
... |
1935 | D | Exam in MAC | The Master's Assistance Center has announced an entrance exam, which consists of the following.
The candidate is given a set $s$ of size $n$ and some strange integer $c$. For this set, it is needed to calculate the number of pairs of integers $(x, y)$ such that $0 \leq x \leq y \leq c$, $x + y$ \textbf{is not} contain... | The principle of inclusion-exclusion. The equation $x+y = s_i$, $y-x=s_j$ has $0$ or $1$ solutions with integers $x, y$. Applying the formula of inclusion-exclusion, the answer to the problem will be: $\mathrm{cnt}(x, y) - \mathrm{cnt}(x, y: x + y \in s) - \mathrm{cnt}(x, y: y - x \in s) + \mathrm{cnt}(x, y: x + y, y -... | [
"binary search",
"combinatorics",
"implementation",
"math"
] | 1,800 | def solve_case() :
n, c = map(int, input().split())
s = list(map(int, input().split()))
ans = (c + 1) * (c + 2) // 2
even, odd = 0, 0
for i in range(n):
ans -= s[i] // 2 + 1
ans -= c - s[i] + 1
if s[i] % 2 == 0:
even += 1
else:
odd += 1
ans... |
1935 | E | Distance Learning Courses in MAC | The New Year has arrived in the Master's Assistance Center, which means it's time to introduce a new feature!
Now students are given distance learning courses, with a total of $n$ courses available. For the $i$-th distance learning course, a student can receive a grade ranging from $x_i$ to $y_i$.
However, not all co... | Try to solve the problem if $x_i = 0$ for each $i$. What will be the answer for two segments $(0, 2^i), (0, 2^i)$? Which bits will definitely be included in the answer? Let's solve the problem when $x = 0$. Then we will iterate over the bits from the most significant to the least significant and try to include them in ... | [
"bitmasks",
"brute force",
"data structures",
"greedy",
"math"
] | 2,400 | /* Includes */
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
/* Using libraries */
using namespace std;
/* Defines */
template <class T>
using vc = vector <T>;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
template<class T>
void output(T &a) {
for (auto i : a)
... |
1935 | F | Andrey's Tree | Master Andrey loves trees$^{\dagger}$ very much, so he has a tree consisting of $n$ vertices.
But it's not that simple. Master Timofey decided to steal one vertex from the tree. If Timofey stole vertex $v$ from the tree, then vertex $v$ and all edges with one end at vertex $v$ are removed from the tree, while the numb... | What different values can the answer for a vertex take? Edges with a weight of 1 can connect all vertices $[1, v - 1]$ and all vertices $[v + 1, n]$. Consider the edges of the form $(mn_u, mn_u - 1)$, $(mx_u, mx_u + 1)$, where $mn_u$, $mx_u$ are the minimum and maximum in the subtree of vertex u with respect to $v$. Le... | [
"binary search",
"constructive algorithms",
"data structures",
"dfs and similar",
"dsu",
"greedy",
"implementation",
"trees"
] | 2,800 | #include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> p, r;
int comp;
DSU(int n) : p(n), r(n) {
iota(p.begin(), p.end(), 0);
comp = n;
}
int find(int v) {
return (p[v] == v) ? v : p[v] = find(p[v]);
}
bool join(int a, int b) {
a = find(a... |
1936 | A | Bitwise Operation Wizard | \textbf{This is an interactive problem.}
There is a secret sequence $p_0, p_1, \ldots, p_{n-1}$, which is a permutation of $\{0,1,\ldots,n-1\}$.
You need to find any two indices $i$ and $j$ such that $p_i \oplus p_j$ is maximized, where $\oplus$ denotes the bitwise XOR operation.
To do this, you can ask queries. Eac... | What's the maximum value of $p_i \oplus p_j$? How to get $i$, such that $p_i = n-1$? How to get $j$, such that $p_i \oplus p_j$ reaches the maximum value? Step1:do queries $? \ x \ x \ y \ y$ like classic searching for the maximum value among $n$ numbers to get $p_i= n-1$; Step2:do queries $? \ x \ i \ y \ i$ to find a... | [
"bitmasks",
"constructive algorithms",
"greedy",
"interactive",
"math"
] | 1,700 |
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
const int N... |
1936 | B | Pinball | There is a one-dimensional grid of length $n$. The $i$-th cell of the grid contains a character $s_i$, which is either '<' or '>'.
When a pinball is placed on one of the cells, it moves according to the following rules:
- If the pinball is on the $i$-th cell and $s_i$ is '<', the pinball moves one cell to the left in... | Observe: which cells actually change the direction of the pinball placed at position $p$ initially? These cells are the $>$ to the left of $p$ and the $<$ to the right of $p$. Can you see the trace of the pinball? How to quickly calculate the time when a pinball leaves the grid? We observe that, in fact, only the $>$ t... | [
"binary search",
"data structures",
"implementation",
"math",
"two pointers"
] | 2,000 | #include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
const int N=... |
1936 | C | Pokémon Arena | You are at a dueling arena. You also possess $n$ Pokémons. Initially, only the $1$-st Pokémon is standing in the arena.
Each Pokémon has $m$ attributes. The $j$-th attribute of the $i$-th Pokémon is $a_{i,j}$. Each Pokémon also has a cost to be hired: the $i$-th Pokémon's cost is $c_i$.
You want to have the $n$-th Po... | In fact, you don't need to hire the same Pokemon more than once. Consider graph building. How to reduce the number of edges in the graph? Let's consider $n$ Pokémon as nodes, and defeating Pokémon $u$ by Pokémon $v$ as the edge $u \rightarrow v$. Then the problem is essentially finding the shortest path from $n$ to $1$... | [
"data structures",
"graphs",
"greedy",
"implementation",
"shortest paths",
"sortings"
] | 2,400 |
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
const int INFF = 1e18;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t --> 0) {
int n, m;
... |
1936 | D | Bitwise Paradox | You are given two arrays $a$ and $b$ of size $n$ along with a fixed integer $v$.
An interval $[l, r]$ is called a \textbf{good} interval if $(b_l \mid b_{l+1} \mid \ldots \mid b_r) \ge v$, where $|$ denotes the bitwise OR operation. The \textbf{beauty} of a good interval is defined as $\max(a_l, a_{l+1}, \ldots, a_r)$... | First we use the line segment tree to maintain sequence $b$. For the nodes $[l,r]$ on each line segment tree, we maintain the first and last occurrence positions of each binary bit in the interval. We need to merge the two intervals, whether it is modification or query. Suppose you want to use the information of $[l, m... | [
"binary search",
"bitmasks",
"data structures",
"greedy",
"two pointers"
] | 3,100 | #include <bits/stdc++.h>
using namespace std;
constexpr int N = 5e5 + 10, V = 30, inf = INT_MAX, L = 18;
int n, q, S, a[N], b[N];
struct Rmq
{
int st[L][N];
void init()
{
for (int i = 1; i <= n; i++)
st[0][i] = a[i];
for (int i = 1; i < L; i++)
{
for (int j = 1; j <= n - (1 ... |
1936 | E | Yet Yet Another Permutation Problem | You are given a permutation $p$ of length $n$.
Please count the number of permutations $q$ of length $n$ which satisfy the following:
- for each $1 \le i < n$, $\max(q_1,\ldots,q_i) \neq \max(p_1,\ldots,p_i)$.
Since the answer may be large, output the answer modulo $998\,244\,353$. | We found it difficult to directly calculate all valid permutations. How about calculating all invalid permutations? Can you come up with an $O(n^2)$ DP solution? How to optimize the $O(n^2)$ DP solution? We found it difficult to directly calculate all valid permutations. Consider calculating all invalid permutations an... | [
"divide and conquer",
"fft",
"math"
] | 3,400 | #include<bits/stdc++.h>
using namespace std;
#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) ((int)a.size())
using ll=long long;
using u32=unsigned int;
using u64=unsigned long long;
using i128=__int128;
using u128=unsigned __int128;
using f128=__float128;
using pii=pair<int,int>;
using pll=pair<... |
1936 | F | Grand Finale: Circles | You are given $n$ circles on the plane. The $i$-th of these circles is given by a tuple of integers $(x_i, y_i, r_i)$, where $(x_i, y_i)$ are the coordinates of its center, and $r_i$ is the radius of the circle.
Please find a circle $C$ which meets the following conditions:
- $C$ is contained inside all $n$ circles g... | For a given center coordinate $(x,y)$, we can model the objective function $r=f(x,y)$ to maximize as follows. $f(x,y)=\min_{1 \le i \le n} {\left({r_i-\sqrt{(x_i-x)^2+(y_i-y)^2}}\right)}$ Formally, this can be modeled as follows: $\begin{split} \max & \ r \\ \text{s.t.} & \ \sqrt{(x_i-x)^2+(y_i-y)^2} \le r_i-r \ \ \for... | [
"binary search",
"geometry"
] | 3,300 | #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using lf=long double;
using pt=pair<lf,lf>;
lf& real(pt& p){return p.first;}
lf& imag(pt& p){return p.second;}
pt midp(pt a,pt b){return pt{(real(a)+real(b))/2,(imag(a)+imag(b))/2};}
pt addi(pt a,pt b){return pt{(real(a)+real(b)),(imag(a)+imag(b))};}
pt... |
1937 | A | Shuffle Party | You are given an array $a_1, a_2, \ldots, a_n$. Initially, $a_i=i$ for each $1 \le i \le n$.
The operation $swap(k)$ for an integer $k \ge 2$ is defined as follows:
- Let $d$ be the largest divisor$^\dagger$ of $k$ which is not equal to $k$ itself. Then swap the elements $a_d$ and $a_k$.
Suppose you perform $swap(i)... | If you think in terms of $k$, it might be hard to find the solution. Maybe it will be helpful if you fix $d$ and find the $k$ which will be swapped with $d$. Try bruteforcing for, say, $n \le 20$. Do you see a pattern? On $n=1$, the answer is trivially $1$. On $n=2$, index $1$ and $2$ are swapped. The answer is therefo... | [
"implementation",
"math"
] | 800 | #include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main()
{
cin.tie(0)->sync_with_stdio(0);
ll q;cin>>q;
while(q--)
{
ll n,p=1;cin>>n;
while(p*2<=n)p<<=1;
cout<<p<<"\n";
}
}
|
1937 | B | Binary Path | You are given a $2 \times n$ grid filled with zeros and ones. Let the number at the intersection of the $i$-th row and the $j$-th column be $a_{ij}$.
There is a grasshopper at the top-left cell $(1, 1)$ that can only jump one cell right or downwards. It wants to reach the bottom-right cell $(2, n)$. Consider the binar... | Let's call path $(1,1) \rightarrow \ldots \rightarrow (1,i)\rightarrow(2,i) \rightarrow \ldots \rightarrow (2,n)$ the $i$-th path. What's the difference between the $i$-th path and the $(i+1)$-th path? What if $a_{2i}=1$ and $a_{1(i+1)}=0$ ? What if $a_{2i}=0$ and $a_{1(i+1)}=1$ ? Let the string achieved by moving down... | [
"dp",
"greedy",
"implementation"
] | 1,300 | #include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
const int N=... |
1941 | A | Rudolf and the Ticket | Rudolf is going to visit Bernard, and he decided to take the metro to get to him. The ticket can be purchased at a machine that accepts exactly two coins, the sum of which does not exceed $k$.
Rudolf has two pockets with coins. In the left pocket, there are $n$ coins with denominations $b_1, b_2, \dots, b_n$. In the r... | For each test case, we calculate all elements $b_i$ from the first array. Then we iterate through the elements $c_j$ in the second array and in a loop, we calculate each sum $b_i + c_j$. If this sum is less than or equal to $k$, we add $1$ to the answer. | [
"brute force",
"math"
] | 800 | #include <iostream>
#include <vector>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m, k;
cin >> n >> m >> k;
int ans = 0;
vector<int> v1(n);
for (int i = 0; i < n; i++) {
cin >> v1[i];
}
int o;
for (int j = 0; j < m; j++) {
cin >> o;
for (int i = 0; i < n; i++) {... |
1941 | B | Rudolf and 121 | Rudolf has an array $a$ of $n$ integers, the elements are numbered from $1$ to $n$.
In one operation, he can choose an index $i$ ($2 \le i \le n - 1$) and assign:
- $a_{i - 1} = a_{i - 1} - 1$
- $a_i = a_i - 2$
- $a_{i + 1} = a_{i + 1} - 1$
Rudolf can apply this operation any number of times. Any index $i$ can be us... | Let's consider the minimum $i$ such that $a_i > 0$. Making this element zero is only possible by choosing the $(i + 1)$-th element for the operation (applying the operation to a more leftward element is either impossible or will make some elements less than zero). We will apply operations in this way until we reach the... | [
"brute force",
"dp",
"greedy",
"math"
] | 1,000 | def solve():
n = int(input())
a = [int(x) for x in input().split()]
for i in range(n - 2):
if a[i] < 0:
print('NO')
return
op = a[i]
a[i] -= op
a[i + 1] -= 2 * op
a[i + 2] -= op
if a[-1] != 0 or a[-2] != 0:
print('NO')
else:
... |
1941 | C | Rudolf and the Ugly String | Rudolf has a string $s$ of length $n$. Rudolf considers the string $s$ to be ugly if it contains the substring$^\dagger$ "pie" or the substring "map", otherwise the string $s$ will be considered beautiful.
For example, "ppiee", "mmap", "dfpiefghmap" are ugly strings, while "mathp", "ppiiee" are beautiful strings.
Rud... | To solve this problem, you need to find all occurrences of the substrings "pie", "map", "mapie" in the string $s$ and remove the middle character in each of them. This way, you will remove the minimum number of characters to ensure that the string does not contain the substrings "pie" and "map". | [
"dp",
"greedy",
"strings"
] | 900 | #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main() {
long long t;
cin>>t;
for(long long c=0;c<t;c++){
long long n;
cin >> n;
string s;
cin >> s;
vector<long long> va;
for (string sul : {"mapie", "pie", "map"}) {
for (size_t... |
1941 | D | Rudolf and the Ball Game | Rudolf and Bernard decided to play a game with their friends. $n$ people stand in a circle and start throwing a ball to each other. They are numbered from $1$ to $n$ in the clockwise order.
Let's call a transition a movement of the ball from one player to his neighbor. The transition can be made clockwise or countercl... | Let's introduce a set of unique elements $q$, initially containing a single element $x$ - the index of the first player who started the game. For each $i$ from $1$ to $m$, we will update $q$ in such a way as to maintain the set of players who could have the ball after the $i$-th throw. For each element $q_j$ of the set... | [
"dfs and similar",
"dp",
"implementation"
] | 1,200 | #include <iostream>
#include <set>
using namespace std;
int main()
{
int t; cin >> t;
while (t--) {
int n, m, a; cin >> n >> m >> a;
set <int> q[2];
int ix = 0;
q[ix].insert(a);
while (m--) {
int x; char ch; cin >> x >> ch;
while (!q[ix].empty())... |
1941 | E | Rudolf and k Bridges | Bernard loves visiting Rudolf, but he is always running late. The problem is that Bernard has to cross the river on a ferry. Rudolf decided to help his friend solve this problem.
The river is a grid of $n$ rows and $m$ columns. The intersection of the $i$-th row and the $j$-th column contains the number $a_{i,j}$ — th... | First, for each string separately, we will calculate the minimum total cost of supports and write it to an array. This can be done, for example, using dynamic programming as follows. We will go through the string and maintain the last $d+1$ minimum total costs in a multiset. Then the answer for the current position wil... | [
"binary search",
"data structures",
"dp",
"two pointers"
] | 1,600 | #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int main() {
int t = 1;
cin >> t;
while (t--) {
int N, M, K, D;
cin >> N >> M >> K >> D;
vector<long long> a(N);
for (int i = 0; i < N; i++) {
vector<long long> dp(M... |
1941 | F | Rudolf and Imbalance | Rudolf has prepared a set of $n$ problems with complexities $a_1 < a_2 < a_3 < \dots < a_n$. He is not entirely satisfied with the balance, so he wants to add \textbf{at most one} problem to fix it.
For this, Rudolf came up with $m$ models of problems and $k$ functions. The complexity of the $i$-th model is $d_i$, and... | Let's consider the differences $a_i - a_{i-1}$. Since we can only insert one problem, we can reduce the difference in difficulty in only one place. If we insert a problem not between the tasks whose difference in difficulty is maximum (denote them as $a_p$ and $a_{p+1}$), then the imbalance will not change. The best wa... | [
"binary search",
"greedy",
"sortings",
"two pointers"
] | 1,800 | def solve():
n, m, k = map(int, input().split())
a = [int(x) for x in input().split()]
d = [int(x) for x in input().split()]
f = [int(x) for x in input().split()]
d.sort()
f.sort()
m1, m2 = 0, 0
ind = -1
for i in range(1, n):
e = a[i] - a[i - 1]
m2 = max(m2, e)
... |
1941 | G | Rudolf and Subway | Building bridges did not help Bernard, and he continued to be late everywhere. Then Rudolf decided to teach him how to use the subway.
Rudolf depicted the subway map as an undirected connected graph, without self-loops, where the vertices represent stations. There is at most one edge between any pair of vertices.
Two... | Let's construct a bipartite graph, where one part is the vertices of the original graph, i.e., subway stations, and the other part is the subway lines. We add an edge between a station vertex and a line vertex if in the original subway graph, the station is incident to an edge of the corresponding subway line. In the n... | [
"constructive algorithms",
"dfs and similar",
"graphs",
"shortest paths"
] | 2,000 | #include<bits/stdc++.h>
using LL = long long;
using ld = long double;
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(10);
int _ = 0, __ = 1;
cin >> __;
for (int _ = 0; _ < __; ++_) {
int n, m;
cin ... |
1942 | A | Farmer John's Challenge | \begin{quote}
Trade Winds - Patrick Deng
\hfill ⠀
\end{quote}
Let's call an array $a$ sorted if $a_1 \leq a_2 \leq \ldots \leq a_{n - 1} \leq a_{n}$.
You are given two of Farmer John's favorite integers, $n$ and $k$. He challenges you to find any array $a_1, a_2, \ldots, a_{n}$ satisfying the following requirements:
... | Solve for $k = 1$. $a = 1, 2, 3 \dots n$ works. Why? Solve for $k = n$. $a = 1, 1, 1 \dots 1$ works. Why? What other $k$ work for a given $n$? Only $k = 1$ and $k = n$ have possible answers. Read the hints. For $k=1$, the construction $1,2,3\dots n$ will always work because in any other cyclic shift, $n$ will be before... | [
"constructive algorithms",
"math"
] | 800 | #include <iostream>
using namespace std;
int main(){
int t; cin >> t;
while(t--){
int n, k; cin >> n >> k;
if(k == 1) for(int i = 0; i < n; i++) cout << i + 1 << " ";
else if(k == n) for(int i = 0; i < n; i++) cout << 1 << " ";
else cout << -1;
cout << "\n";
}
} |
1942 | B | Bessie and MEX | \begin{quote}
MOOO! - Doja Cat
\hfill ⠀
\end{quote}
Farmer John has a permutation $p_1, p_2, \ldots, p_n$, where every integer from $0$ to $n-1$ occurs exactly once. He gives Bessie an array $a$ of length $n$ and challenges her to construct $p$ based on $a$.
The array $a$ is constructed so that $a_i$ = $MEX(p_1, p_2,... | Solution 1 We will construct the solution forward. Separate the $a_i$'s given into negative and positive cases. What does this tell us about the $\texttt{MEX}$? We can find $p_1, p_2, ... , p_n$ in order, looking at positive and negative cases. Note that $a_i \neq 0$ because $p_i$ would equal $\texttt{MEX}$($p_1 \dots ... | [
"constructive algorithms",
"math"
] | 1,100 | #include <bits/stdc++.h>
using namespace std;
void solve(){
int n; cin >> n;
vector<int> a(n);
for(int& i: a) cin >> i;
vector<int> p(n), has(n + 1);
int mex = 0;
for(int i = 0; i < n; i++){
if(a[i] >= 0){
p[i] = mex;
}
else{
p[i] = mex - a[i];
}
has[p[i]] = true;
while(has[mex]) mex++;
}
for... |
1942 | C2 | Bessie's Birthday Cake (Hard Version) | \begin{quote}
Proof Geometric Construction Can Solve All Love Affairs - manbo-p
\hfill ⠀
\end{quote}
\textbf{This is the hard version of the problem. The only difference between the two versions is the constraint on $y$. In this version $0 \leq y \leq n - x$. You can make hacks only if both versions are solved.}
Bess... | Ignoring $n$ for now, let's just focus on the $x$ chosen vertices. Sort the $x$ vertices and connect adjacent vertices to form its own smaller polygon. By drawing out some cases or if you're familiar with triangulation (video that proves by induction), you can form $x - 2$ triangles by drawing diagonals in a polygon wi... | [
"geometry",
"greedy",
"math"
] | 1,700 | #include <bits/stdc++.h>
using namespace std;
void solve(){
int n, x, y; cin >> n >> x >> y;
int initial_y = y;
vector<int> chosen(x);
for(int& i: chosen) cin >> i;
sort(chosen.begin(), chosen.end());
int ans = x - 2;
int triangles_from_even_g = 0;
vector<int> odd_g;
auto process_gap = [&](int g) -> void{
i... |
1942 | D | Learning to Paint | \begin{quote}
Pristine Beat - Touhou
\hfill ⠀
\end{quote}
Elsie is learning how to paint. She has a canvas of $n$ cells numbered from $1$ to $n$ and can paint any (potentially empty) subset of cells.
Elsie has a 2D array $a$ which she will use to evaluate paintings. Let the maximal contiguous intervals of painted cel... | We can solve this with dynamic programming. Let $\texttt{dp}[i]$ store a list of all $\min(k, 2^i)$ highest beauties of a painting in non-increasing order if you only paint cells $1,2,\ldots ,i$. Transitioning boils down to finding the $k$ largest elements from $n$ non-increasing lists. Try to do this in $\mathcal{O}((... | [
"binary search",
"data structures",
"dfs and similar",
"dp",
"greedy",
"implementation",
"sortings"
] | 2,100 | #include <bits/stdc++.h>
using namespace std;
void solve(){
int n, k;
cin >> n >> k;
int A[n + 1][n + 1];
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
cin >> A[i][j];
// dp[i] = Answer if we consider 1...i
vector<int> dp[n + 1];
dp[0] = {0};
... |
1942 | E | Farm Game | \begin{quote}
Lunatic Princess - Touhou
\hfill ⠀
\end{quote}
Farmer Nhoj has brought his cows over to Farmer John's farm to play a game! FJ's farm can be modeled by a number line with walls at points $0$ and $l + 1$. On the farm, there are $2n$ cows, with $n$ of the cows belonging to FJ and the other $n$ belonging to ... | Think about the gaps between $(a_1,b_1), (a_2,b_2), ... ,(a_n,b_n)$. Let the gaps be $g_1,g_2,...,g_n$. What effect do the moves have on the gaps? WLOG let the first cow be FJ's cow. FN wins if all $g_i$ are even. Otherwise FJ wins. Note that the game will always end. Try to count the number of ways FN wins. We can ite... | [
"combinatorics",
"games"
] | 2,300 | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MAX = 2e6 + 5, MOD = 998244353;
ll fact[MAX], ifact[MAX];
ll bpow(ll a, int p){
ll ans = 1;
for (;p; p /= 2, a = (a * a) % MOD)
if (p & 1)
ans = (ans * a) % MOD;
return ans;
}
ll ncr(int n, int r){
i... |
1942 | F | Farmer John's Favorite Function | \begin{quote}
ΩΩPARTS - Camellia
\hfill ⠀
\end{quote}
Farmer John has an array $a$ of length $n$. He also has a function $f$ with the following recurrence:
- $f(1) = \sqrt{a_1}$;
- For all $i > 1$, $f(i) = \sqrt{f(i-1)+a_i}$.
Note that $f(i)$ is not necessarily an integer.
He plans to do $q$ updates to the array. E... | Consider the case where all $f(i)$ are integers. Decreasing any $a_i$ will decrease $\lfloor f(n) \rfloor$. So solutions that iterate over the last few elements will not work. If $n \ge 6$, changing $a_1$ will only affect $\lfloor f(n) \rfloor$ by at most $1$. We can take the floor each time we square root. Specificall... | [
"brute force",
"data structures",
"implementation",
"math"
] | 2,700 | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int B = 100, MAX = 2e5 + B + 5;
int n, q, offset, numBlocks; ll A[MAX], val[MAX], cut[MAX];
void buildBlock(int blk){
int l = blk * B;
int r = l + B - 1;
ll cur = 0;
for (int i = l; i <= r; i++)
cur = floor(sqrtl((lon... |
1942 | G | Bessie and Cards | \begin{quote}
Second Dark Matter Battle - Pokemon Super Mystery Dungeon
\hfill ⠀
\end{quote}
Bessie has recently started playing a famous card game. In the game, there is only one deck of cards, consisting of $a$ "draw $0$" cards, $b$ "draw $1$" cards, $c$ "draw $2$" cards, and $5$ special cards. At the start of the g... | "Draw $1$" cards do not matter because we can immediately play them when we draw them. Treat "draw $2$" as $+1$ and "draw $0$" as $-1$. We start with a balance of $+5$. We draw all the cards up to the first prefix where the balance dips to $0$. We are interested in the number of ways where the special cards lie in this... | [
"combinatorics",
"dp",
"math"
] | 2,800 | null |
1942 | H | Farmer John's Favorite Intern | \begin{quote}
Peaches...
\hfill ⠀
\end{quote}
Ruby just won an internship position at Farmer John's farm by winning a coding competition! As the newly recruited intern, Ruby is tasked with maintaining Farmer John's peach tree, a tree consisting of $n$ nodes rooted at node $1$. Each node initially contains $a_i = 0$ pe... | Model this as a flow problem where growth events are an input of some flow to a node and harvest events and the necessary amount of fruits are an output of flow to the sink. We will define the $a_i$ edge to be the input edge of every node, $b_i$ to be the output edge for necessary fruits, and $c_i$ to be the output edg... | [
"data structures",
"dp",
"flows",
"trees"
] | 3,500 | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 300000
#define N_ (1 << 19) /* N_ = pow2(ceil(log2(N))) */
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
int *ej[N], eo[N], n;
v... |
1943 | A | MEX Game 1 | Alice and Bob play yet another game on an array $a$ of size $n$. Alice starts with an empty array $c$. Both players take turns playing, with Alice starting first.
On Alice's turn, she picks one element from $a$, appends that element to $c$, and then deletes it from $a$.
On Bob's turn, he picks one element from $a$, a... | Alice can adapt to Bob's strategy. Try to keep that in mind. Whenever Bob chooses $i$, and if there are any copies of $i$ left, Alice can take $i$ on her next move. Let $f$ be the frequency array of $a$. You can ignore all $f(i) \ge 2$ due to the previous hint. Now the answer is some simple casework. For any $i$ s.t. $... | [
"games",
"greedy"
] | 1,300 | #include <bits/stdc++.h>
using namespace std;
int main(){
int t; cin >> t;
while (t--){
int n; cin >> n;
vector <int> f(n + 1, 0);
for (int i = 0; i < n; i++){
int x; cin >> x;
f[x]++;
}
int c = 0;
for (int i = 0; i <= n;... |
1943 | B | Non-Palindromic Substring | A string $t$ is said to be $k$-good if there exists at least one substring$^\dagger$ of length $k$ which is not a palindrome$^\ddagger$. Let $f(t)$ denote the sum of all values of $k$ such that the string $t$ is $k$-good.
You are given a string $s$ of length $n$. You will have to answer $q$ of the following queries:
... | When is a string not $k$-good? (Ignore the trivial edge cases of $k = 1$ and $k = n$). What happens when $s[i....j]$ and $s[i + 1....j + 1]$ are both palindromes? We first try to find the answer for a string Let $k = j - i + 1$, $s[i....j]$ -> I and $s[i + 1...j + 1]$ -> II are both palindromes. Then $s_i = s_j$ (due t... | [
"hashing",
"implementation",
"math",
"strings"
] | 2,000 | #include <bits/stdc++.h>
using namespace std;
#define INF (int)1e18
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
vector<int> manacher_odd(string s) {
int n = s.size();
s = "$" + s + "^";
vector<int> p(n + 2);
int l = 1, r = 1;
for(int i = 1; i <= n; i++) {
p[i] ... |
1943 | C | Tree Compass | You are given a tree with $n$ vertices numbered $1, 2, \ldots, n$. Initially, all vertices are colored white.
You can perform the following two-step operation:
- Choose a vertex $v$ ($1 \leq v \leq n$) and a distance $d$ ($0 \leq d \leq n-1$).
- For all vertices $u$ ($1 \leq u \leq n$) such that $\text{dist}^\dagger(... | Try to solve for line case. You may have to use more than $1$ node for certain cases. Extend the solution for the line to the general tree version (consider the diamater). For a line, an obvious bound on the answer is $\lceil \frac{n}{2} \rceil$, as we can colour atmost $2$ nodes per operation. I claim this is achievea... | [
"constructive algorithms",
"dfs and similar",
"greedy",
"trees"
] | 2,300 | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
void Solve()
{
int n;
cin >> n;
vector<vector<int>> E(n);
for (int i = 1; i < n; i++){
i... |
1943 | D1 | Counting Is Fun (Easy Version) | \textbf{This is the easy version of the problem. The only difference between the two versions is the constraint on $n$. You can make hacks only if both versions of the problem are solved.}
An array $b$ of $m$ non-negative integers is said to be good if all the elements of $b$ can be made equal to $0$ using the followi... | Try to come up with some necessary and sufficient conditions of a good array. Apply dp once you have a good condition. All operations which include $i$ must also either include $i - 1$ or $i + 1$. Hence $a_i \le a_{i - 1} + a_{i + 1}$ must hold. Throughout the editorial treat $a_i = 0$ for $(i \le 0)$ or $(i > n)$. But... | [
"brute force",
"combinatorics",
"dp",
"math"
] | 2,400 | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
void Solve()
{
int n, k, mod; cin >> n >> k >> mod;
vector<vector<int>> dp(k + 1, vector<int>(k + 1, 0));
... |
1943 | D2 | Counting Is Fun (Hard Version) | \textbf{This is the hard version of the problem. The only difference between the two versions is the constraint on $n$. You can make hacks only if both versions of the problem are solved.}
An array $b$ of $m$ non-negative integers is said to be good if all the elements of $b$ can be made equal to $0$ using the followi... | Try to apply Principle of Inclusion Exclusion (PIE). You do not need to store both last elements! Only the last is enough. Since there are $n^3$ states in our dp, we will have to optimize the number of states somehow. Let us consider all arrays and not just good arrays. An element is bad if $a_i > a_{i - 1} + a_{i + 1}... | [
"combinatorics",
"dp"
] | 2,800 | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
void Solve()
{
int n, k, mod; cin >> n >> k >> mod;
vector<vector<int>> dp(2, vector<int>(k + 1, 0));
aut... |
1943 | E1 | MEX Game 2 (Easy Version) | \textbf{This is the easy version of the problem. The only difference between the two versions is the constraint on $t$, $m$ and the sum of $m$. You can make hacks only if both versions of the problem are solved.}
Alice and Bob play yet another game on an array $a$ of size $n$. Alice starts with an empty array $c$. Bot... | It might be optimal for Bob to reduce multiple elements at the same time, thus making Alice choose between which element she wants to take. Suppose you are only checking whether $ans \ge i$ for now, what would Alice's strategy be? Try fixing the element that Bob wants to make sure Alice is not able to get. What would b... | [
"binary search",
"brute force",
"greedy"
] | 2,900 | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
void Solve()
{
int n, k; cin >> n >> k;
vector <int> a(n + 1);
for (auto &x : a) cin >> x;
auto check = ... |
1943 | E2 | MEX Game 2 (Hard Version) | \textbf{This is the hard version of the problem. The only difference between the two versions is the constraint on $t$, $m$ and the sum of $m$. You can make hacks only if both versions of the problem are solved.}
Alice and Bob play yet another game on an array $a$ of size $n$. Alice starts with an empty array $c$. Bot... | Instead of doing the check for $ans \geq i$ in $O(m^3)$, we will do it in $O(m)$. For an array $f$ of length $n$. Let $s=f_1+f_2+\ldots+f_n$ be the sum of the element. $f$ will be called flat if $f_i = \lfloor \frac{s+i-1}{n} \rfloor$. That is, it has the form $x, \ldots, x, x+1, \ldots x+1$. All flat array can be char... | [
"binary search",
"greedy",
"two pointers"
] | 3,300 | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof po... |
1943 | F | Minimum Hamming Distance | You are given a binary string$^\dagger$ $s$ of length $n$.
A binary string $p$ of the same length $n$ is called \textbf{good} if for every $i$ ($1 \leq i \leq n$), there exist indices $l$ and $r$ such that:
- $1 \leq l \leq i \leq r \leq n$
- $s_i$ is a mode$^\ddagger$ of the string $p_lp_{l+1}\ldots p_r$
You are gi... | Assumption: 0 is the mode of string t. If 1 occurs more times than 0 in t, we will flip all characters of s and t so that 0 is the mode of string t. Let us say index $i$ nice if there exists $l$ and $r$($1 \le l \le i \le r \le n$) such that $s_i$ is the mode of substring $t[l,r]$. So we might have some not nice indice... | [
"dp"
] | 3,500 | #include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
#define nline "\n"
#define f first ... |
1944 | A | Destroying Bridges | There are $n$ islands, numbered $1, 2, \ldots, n$. Initially, every pair of islands is connected by a bridge. Hence, there are a total of $\frac{n (n - 1)}{2}$ bridges.
Everule lives on island $1$ and enjoys visiting the other islands using bridges. Dominater has the power to destroy at most $k$ bridges to minimize th... | What is the minimum number of bridges to burn if we want to make exactly $i$ islands visitable from $1$? Atleast $i \cdot (n - i)$ bridges need to burnt (the bridges connecting the $i$ reachable islands and the $n - i$ non-reachable islands). A simple $O(n)$ solution is for every $i$ from $1$ to $n$, check if $i \cdot ... | [
"graphs",
"greedy",
"math"
] | 800 | #include <bits/stdc++.h>
using namespace std;
int main(){
int t; cin >> t;
while (t--){
int n, k; cin >> n >> k;
if (k >= n - 1) cout << 1 << "\n";
else cout << n << "\n";
}
return 0;
} |
1944 | B | Equal XOR | You are given an array $a$ of length $2n$, consisting of each integer from $1$ to $n$ exactly \textbf{twice}.
You are also given an integer $k$ ($1 \leq k \leq \lfloor \frac{n}{2} \rfloor $).
You need to find two arrays $l$ and $r$ each of length $\mathbf{2k}$ such that:
- $l$ is a subset$^\dagger$ of $[a_1, a_2, \l... | Group numbers according to how many times they occur in $a[1...n]$. The group of numbers having $0$ occurrences in $a[1...n]$ is of the same size as the group of numbers having $2$ occurences in $a[1...n]$. Try to use the $0$ and $2$ occurrence numbers first, and then if we still need more, we can use the $1$ occurence... | [
"bitmasks",
"constructive algorithms"
] | 1,100 | #include <bits/stdc++.h>
using namespace std;
int main(){
int t; cin >> t;
while (t--){
int n, k;
cin >> n >> k;
k = 2 * k;
vector <int> a(2 * n), occ(n + 1, 0);
for (auto &x : a) cin >> x;
for (int i = 0; i < n; i++) occ[a[i]]++;
... |
1945 | A | Setting up Camp | The organizing committee plans to take the participants of the Olympiad on a hike after the tour. Currently, the number of tents needed to be taken is being calculated. It is known that each tent can accommodate up to $3$ people.
Among the participants, there are $a$ introverts, $b$ extroverts, and $c$ universals:
- ... | First, let's consider introverts. Since each of them needs exactly one tent, we simply add $a$ to the answer. Then let's consider extroverts. If their number is divisible by 3, we add $\frac{b}{3}$ to the answer. Otherwise, we calculate $d = 3 - b \bmod 3$, where $x \bmod y$ denotes the remainder from dividing $x$ by $... | [
"greedy",
"math"
] | 800 | #include <iostream>
using namespace std;
using ll = long long;
void solve() {
ll single, poly, uni;
cin >> single >> poly >> uni;
ll needPoly = (3 - poly % 3) % 3;
if (poly > 0 && needPoly > uni) {
cout << "-1\n";
return;
}
uni -= needPoly;
poly += needPoly;
ll mn = s... |
1945 | B | Fireworks | One of the days of the hike coincided with a holiday, so in the evening at the camp, it was decided to arrange a festive fireworks display. For this purpose, the organizers of the hike bought two installations for launching fireworks and a huge number of shells for launching.
Both installations are turned on simultane... | Let's consider the moment in time $T = \text{LCM}(a, b)$ (least common multiple of numbers $a$ and $b$). It is easy to notice that since $T > 0$ and $T$ is divisible by both $a$ and $b$, at this moment both camps will release a firework, each of which will disappear after $m + 1$ minutes. Let's look at the sky at the $... | [
"math",
"number theory"
] | 900 | t = int(input())
for qi in range(t):
a, b, m = [int(x) for x in input().split()]
ans = m // a + m // b + 2
print(ans) |
1945 | C | Left and Right Houses | In the village of Letovo, there are $n$ houses. The villagers decided to build a big road that will divide the village into left and right sides. Each resident wants to live on either the right or the left side of the street, which is described as a sequence $a_1, a_2, \dots, a_n$, where $a_j = 0$ if the resident of th... | According to the statement, to the left of the road there should be no less elements $a_i$ such that $a_i = 0$ than such that $a_i = 1$, and to the right of the road there should be no less elements $a_i$ than such that $a_i = 1$ than such that $a_i = 0$. We will consider each position of the road and check the complia... | [
"brute force"
] | 1,200 | for case in range(int(input())):
n = int(input())
a = input()
suf_cnt = [0] * (n + 1)
for i in range(n - 1, -1, -1):
suf_cnt[i] = suf_cnt[i + 1] + (a[i] == '1')
pref_cnt = 0
opt_ans = -1
opt_dist = n * 2
threshold = (n + 1) // 2
for i in range(n + 1):
if p... |
1945 | D | Seraphim the Owl | The guys lined up in a queue of $n$ people, starting with person number $i = 1$, to ask Serafim the Owl about the meaning of life. Unfortunately, Kirill was very busy writing the legend for this problem, so he arrived a little later and stood at the end of the line after the $n$-th person. Kirill is completely dissatis... | Let's consider a greedy approach. Suppose we are standing at position $i$. Find the first $j$ such that $j < i$ and $a_j < b_j$. If such $j$ exists and $j > m$, then swap with $j$. This will be optimal, because in any case we will have to pay the people at positions $i - 1, i - 2, \dots, j$ some amount of coins, and in... | [
"dp",
"greedy"
] | 1,300 | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
void solve() {
ll n, k;
cin >> n >> k;
vector<ll> A(n);
for (auto& item : A) {
cin >> item;
}
reverse(A.begin(), A.end());
vector<ll> B(n);
for (auto& item : B) {
cin ... |
1945 | E | Binary Search | Anton got bored during the hike and wanted to solve something. He asked Kirill if he had any new problems, and of course, Kirill had one.
You are given a permutation $p$ of size $n$, and a number $x$ that needs to be found. A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in ... | In fact, the problem can be solved in a single operation. Let's start a binary search on the initial permutation. We will get some index $l$. Then, it is enough to swap $p_l$ and $x$. Now our binary search will stop exactly at the number $x$. Notice that in the search, any number less than or equal to $x$ affects the r... | [
"binary search",
"constructive algorithms",
"greedy"
] | 1,700 | #include <iostream>
#include <vector>
using namespace std;
void solve() {
int n, x;
cin >> n >> x;
vector<int> src(n);
int P = 0;
for (int i = 0; i < n; ++i) {
cin >> src[i];
if (src[i] == x) {
P = i;
}
}
int l = 0;
int r = n;
while (r - l > 1) ... |
1945 | F | Kirill and Mushrooms | As soon as everyone in the camp fell asleep, Kirill sneaked out of the tent and went to the Wise Oak to gather mushrooms.
It is known that there are $n$ mushrooms growing under the Oak, each of which has magic power $v_i$. Kirill really wants to make a magical elixir of maximum strength from the mushrooms.
The streng... | Consider a fixed number $k$ - the amount of mushrooms in the elixir. In this case, we need to maximize the minimum of the taken numbers. We will iterate through the numbers in descending order: until we collect $k$ numbers, we iterate over the next number. If its index is greater than or equal to $k$, we take this numb... | [
"data structures",
"sortings"
] | 1,900 | #include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
using ll = long long;
void solve() {
ll n;
cin >> n;
vector<ll> src(n);
vector<pair<ll, ll>> can(n);
for (ll i = 0; i < n; ++i) {
cin >> src[i];
can[i] = {src[i], i};
}
vector<ll... |
1945 | G | Cook and Porridge | Finally, lunchtime!
$n$ schoolchildren have lined up in a long queue at the cook's tent for porridge. The cook will be serving porridge for $D$ minutes. The schoolchild standing in the $i$-th position in the queue has a priority of $k_i$ and eats one portion of porridge in $s_i$ minutes.
\textbf{At the beginning} of ... | Let's divide the queue into two parts: the original queue $\text{Q1}$ and the returning people queue $\text{Q2}$. $\text{Q1}$ will simply be an array with a pointer $P$ to the current person at the front. And $\text{Q2}$ will be a priority queue. Now the problem can be formulated as follows: find out how much time it w... | [
"binary search",
"constructive algorithms",
"data structures",
"implementation"
] | 2,500 | #include <iostream>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;
const int MAX_N = 2e5;
const int MAX_D = 3e5;
struct Student {
int k;
int s;
int tin = 0;
bool operator<(const Student& other) const {
if (k == other.k) {
if (tin == other.tin) {
... |
1945 | H | GCD is Greater | In the evenings during the hike, Kirill and Anton decided to take out an array of integers $a$ of length $n$ from their backpack and play a game with it. The rules are as follows:
- Kirill chooses from $2$ to $(n-2)$ numbers and encircles them in red.
- Anton encircles all the remaining numbers in blue.
- Kirill calcu... | Let's notice that it is sufficient to take the greatest common divisor (GCD) of two numbers. Indeed, suppose we take the GCD of a larger number of numbers. Then we can move one of them to the set of AND. Then the GCD will not decrease, and the AND will not increase. Also, pre-calculate the number of ones in the given n... | [
"brute force",
"data structures",
"math",
"number theory"
] | 2,600 | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int BITS = 20;
const int MAX_N = 4e5 + 10;
int n;
int x;
int src[MAX_N];
void coutYES(int fId, int sId) {
cout << "YES\n";
cout << "2 " << src[fId] << ' ' << src[sId] << '\n';
cout << n - 2 << ' ';
for (int i = 0; ... |
1946 | A | Median of an Array | You are given an array $a$ of $n$ integers.
The median of an array $q_1, q_2, \ldots, q_k$ is the number $p_{\lceil \frac{k}{2} \rceil}$, where $p$ is the array $q$ sorted in non-decreasing order. For example, the median of the array $[9, 5, 1, 2, 6]$ is $5$, as in the sorted array $[1, 2, 5, 6, 9]$, the number at ind... | The median is defined as the number at index $\lceil \frac{n}{2} \rceil$ in the sorted array, so we can sort the array and work with it. So, let's start by sorting the array and finding the median in it, namely the number $a_{\lceil \frac{n}{2} \rceil}$, let it be equal to $x$. In order for the median to increase, that... | [
"greedy",
"implementation",
"sortings"
] | 800 | #include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::sort(a.begin(), a.end());
int p = (n + 1) / 2 - 1;
int res = std::count(a.begin() + p, a.end(), a[p]);
std::cou... |
1946 | B | Maximum Sum | You have an array $a$ of $n$ integers.
You perform exactly $k$ operations on it. In one operation, you select any contiguous subarray of the array $a$ (possibly empty) and insert the sum of this subarray anywhere in the array.
Your task is to find the maximum possible sum of the array after $k$ such operations.
As t... | Let's denote $s$ as the sum of the original array and $x$ as the sum of the subarray with the maximum sum from the original array. We solve the problem when $k$ equals $1$. In this case, we need to find the subarray of the array with the maximum sum and insert this sum anywhere in the array, so the answer is $s + x$. N... | [
"dp",
"greedy",
"math"
] | 1,100 | #include <bits/stdc++.h>
using namespace std;
#define int long long
const int P = 1e9 + 7;
void solve() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
int S = 0, sum = 0;
int cur = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
c... |
1946 | C | Tree Cutting | You are given a tree with $n$ vertices.
Your task is to find the maximum number $x$ such that it is possible to remove exactly $k$ edges from this tree in such a way that the size of each remaining connected component$^{\dagger}$ is at least $x$.
$^{\dagger}$ Two vertices $v$ and $u$ are in the same connected compone... | Let's hang the tree from an arbitrary vertex, for definiteness let's hang the tree from vertex $1$ (proof is given below). First of all, notice that if we can obtain some answer $x$, then we can also obtain the answer $x - 1$ (exactly the same way as for $x$), so we can do a binary search for $x$. To check the conditio... | [
"binary search",
"dp",
"greedy",
"implementation",
"trees"
] | 1,600 | #include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n, k;
std::cin >> n >> k;
std::vector<std::vector<int>> adj(n);
for (int i = 0; i < n - 1; i++) {
int v, u;
std::cin >> v >> u;
--v, --u;
adj[v].emplace_back(u);
adj[u].emplace_back(v);
}
... |
1946 | D | Birthday Gift | Yarik's birthday is coming soon, and Mark decided to give him an array $a$ of length $n$.
Mark knows that Yarik loves bitwise operations very much, and he also has a favorite number $x$, so Mark wants to find the maximum number $k$ such that it is possible to select pairs of numbers [$l_1, r_1$], [$l_2, r_2$], $\ldots... | For convenience, let's increase $x$ by $1$, and then iterate through the bit in which the final number is less than $x$. We will iterate from the most significant bit to the least significant bit, denoting it as $i$. The initial bit will be $30$. Let's look at all the numbers $a$ in which this bit is $1$. If there is a... | [
"bitmasks",
"brute force",
"constructive algorithms",
"greedy",
"implementation"
] | 1,900 | #include<bits/stdc++.h>
using namespace std;
int const maxn = 1e5 + 5;
int a[maxn];
int solve(int n, int x) {
int res = 0, curr = 0;
for (int i = 1; i <= n; i++) {
curr ^= a[i];
if ((curr|x) == x) curr = 0, res++;
else {
if (i == n) return -1;
}
}
return... |
1946 | E | Girl Permutation | Some permutation of length $n$ is guessed.
You are given the indices of its prefix maximums and suffix maximums.
Recall that a permutation of length $k$ is an array of size $k$ such that each integer from $1$ to $k$ occurs exactly once.
Prefix maximums are the elements that are the maximum on the prefix ending at th... | First, if $p_1$ is not equal to $1$, or $s_{m_2}$ is not equal to $n$, or $p_{m_1}$ is not equal to $s_1$, then the answer is $0$ for obvious reasons. Otherwise, we know exactly where the number $n$ is located, at position $s_1$. Next, we have $\binom{n - 1}{s_1 - 1}$ ways to divide the numbers from $1$ to $n - 1$ into... | [
"combinatorics",
"dp",
"math",
"number theory"
] | 2,200 | #include <bits/stdc++.h>
using i64 = long long;
template<class T>
constexpr T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
template<int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i6... |
1946 | F | Nobody is needed | Oleg received a permutation $a$ of length $n$ as a birthday present.
Oleg's friend Nechipor asks Oleg $q$ questions, each question is characterized by two numbers $l$ and $r$, in response to the question Oleg must say the number of sets of indices $(t_1, t_2, \ldots, t_k)$ of any length $k \ge 1$ such that:
- $l \le ... | Let's iterate the left boundary of our queries $L$ from $n$ to $1$ and maintain the Fenwick tree $F$, where $F_i$ = the number of sought sets of indices in which $t_k = i$, and $t_1 \ge L$. Then the answer to the query with the left boundary at $L$ will be the sum over the interval $[l_i; r_i]$ in our Fenwick tree. Now... | [
"2-sat",
"data structures",
"dfs and similar",
"dp"
] | 2,500 | #include <bits/stdc++.h>
using i64 = long long;
template<class Info>
struct Fenwick {
std::vector<Info> t;
int n;
Fenwick(int n = 0) : n(n) {
t.resize(n);
}
void add(int x, const Info &v) {
for (int i = x + 1; i <= n; i += i & -i) {
t[i - 1] = t[i - 1] + v;
... |
1948 | A | Special Characters | You are given an integer $n$.
Your task is to build a string of uppercase Latin letters. There must be exactly $n$ special characters in this string. Let's call a character special if it is equal to exactly one of its neighbors.
For example, there are $6$ special characters in the AAABAACC string (at positions: $1$, ... | Let's look at the blocks of consecutive equal characters (such that it cannot be extended to the left or to the right): if its length is $1$, then this block has $0$ special characters; if its length is $2$, then this block has $2$ special characters; if its length is at least $3$, then this block has $2$ special chara... | [
"brute force",
"constructive algorithms"
] | 800 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
if (n % 2 == 1) {
cout << "NO" << '\n';
continue;
}
cout << "YES" << '\n';
for (int i = 0; i < n / 2; ++i)
for (int j = 0; j < 2; ++j)
cout << "AB"[i & 1]... |
1948 | B | Array Fix | You are given an integer array $a$ of length $n$.
You can perform the following operation any number of times (possibly zero): take any element of the array $a$, which is at least $10$, delete it, and instead insert the digits that element consisted of in the same position, in order they appear in that element.
For e... | The key to solving the problem is the following observation: if $a_i > a_{i + 1}$, then the $i$-th element should always be split (since it is the only way to decrease the element compared with $a_{i+1}$). This observation allows us to solve the problem greedily as follows: iterate on the array $a$ from right to left, ... | [
"brute force",
"dp",
"greedy",
"implementation"
] | 1,100 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (auto& x : a) cin >> x;
vector<int> b({a[n - 1]});
for (int i = n - 2; i >= 0; --i) {
if (a[i] > b.back()) {
b.push_back(a[i] % 10);
b.p... |
1948 | C | Arrow Path | There is a grid, consisting of $2$ rows and $n$ columns. The rows are numbered from $1$ to $2$ from top to bottom. The columns are numbered from $1$ to $n$ from left to right. Each cell of the grid contains an arrow pointing either to the left or to the right. No arrow points outside the grid.
There is a robot that st... | There are multiple different approaches to this problem. We will describe a couple of them. The key observation for this problem that works for all these approaches is that, since we never skip a move and never try to move outside the grid, we can split the cells into two groups: if the sum of coordinates of the cell i... | [
"brute force",
"constructive algorithms",
"dfs and similar",
"dp",
"graphs",
"shortest paths"
] | 1,300 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<char> ok1(n / 2), ok2(n / 2);
for (int i = 0; i < 2; ++i) {
string s;
cin >> s;
for (int j = 0; j < n; ++j) if ((i + j) & 1) {
ok1[(i + j) / 2] |= (s[j] ==... |
1948 | D | Tandem Repeats? | You are given a string $s$, consisting of lowercase Latin letters and/or question marks.
A tandem repeat is a string of an even length such that its first half is equal to its second half.
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) ch... | The key idea of the problem is to believe that no algorithms are required to solve the problem. Now, we can start looking for ways to iterate over all substrings so that it doesn't degenerate into $O(n^3)$. One of the ways is the following. How to check if the substring $[l; r)$ is a tandem repeat? Let $d = \frac{r - l... | [
"brute force",
"strings",
"two pointers"
] | 1,700 | for _ in range(int(input())):
s = input()
n = len(s)
ans = 0
for d in range(1, n // 2 + 1):
cnt = 0
for i in range(n - d):
cnt += s[i] == s[i + d] or s[i] == '?' or s[i + d] == '?'
if i - d >= 0:
cnt -= s[i - d] == s[i] or s[i - d] == '?' or s[i] =... |
1948 | E | Clique Partition | You are given two integers, $n$ and $k$. There is a graph on $n$ vertices, numbered from $1$ to $n$, which initially has no edges.
You have to assign each vertex an integer; let $a_i$ be the integer on the vertex $i$. All $a_i$ should be distinct integers from $1$ to $n$.
After assigning integers, for every pair of v... | There are two main steps to solve the problem: analyzing the maximum size of a clique; showing a construction that always allows us to get a clique of the maximum possible size. Firstly, the maximum size of a clique cannot exceed $k$. If there are at least $k+1$ vertices in the same clique, then at least two of them (c... | [
"brute force",
"constructive algorithms",
"graphs",
"greedy",
"implementation"
] | 2,100 | #include<bits/stdc++.h>
using namespace std;
void solve()
{
int n, k;
cin >> n >> k;
vector<int> a(n), c(n);
for(int i = 0; i < n; i++)
{
a[i] = i + 1;
c[i] = i / k + 1;
}
int q = *max_element(c.begin(), c.end());
for(int i = 1; i <= q; i++)
{
int l = find... |
1948 | F | Rare Coins | There are $n$ bags numbered from $1$ to $n$, the $i$-th bag contains $a_i$ golden coins and $b_i$ silver coins.
The value of a gold coin is $1$. The value of a silver coin is either $0$ or $1$, determined for each silver coin independently ($0$ with probability $\frac{1}{2}$, $1$ with probability $\frac{1}{2}$).
You ... | To calculate the probability, we will calculate the number of ways to assign values to silver coins in such a way that the total value inside the segment is greater than the total value outside. Let define $x$ as the number of silver coins with value $1$ inside the segment, $y$ as the number of silver coins with value ... | [
"combinatorics",
"math",
"probabilities"
] | 2,500 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int x, int y) {
x += y;
if (x >= MOD) x -= MOD;
return x;
}
int mul(int x, int y) {
return x * 1LL * y % MOD;
}
int binpow(int x, int y) {
int z = 1;
while (y) {
if (y & 1) z = mul(z, x);
x = mul(x, x);
y >>= ... |
1948 | G | MST with Matching | You are given an undirected connected graph on $n$ vertices. Each edge of this graph has a weight; the weight of the edge connecting vertices $i$ and $j$ is $w_{i,j}$ (or $w_{i,j} = 0$ if there is no edge between $i$ and $j$). All weights are positive integers.
You are also given a positive integer $c$.
You have to b... | The key to solving this problem is applying Kőnig's theorem: in a bipartite graph, the size of the maximum matching is equal to the size of the minimum vertex cover. At the first glance it shouldn't work here, because our graph is not bipartite; however, when we leave only the edges belonging to the spanning tree we ch... | [
"bitmasks",
"brute force",
"dsu",
"graph matchings",
"trees"
] | 3,100 | #include<bits/stdc++.h>
using namespace std;
int n, c;
int g[21][21];
const int INF = int(1e9);
int primMST(int vertex_cover_mask)
{
vector<int> d(n, INF);
vector<bool> used(n, false);
d[0] = 0;
int ans = 0;
for(int i = 0; i < n; i++)
{
int idx = -1;
for(int j = 0; j < n; j++... |
1949 | A | Grove | You want to plant trees in a square lawn of size $n \times n$ whose corners have Cartesian coordinates $(0, 0)$, $(n, 0)$, $(0, n)$, and $(n, n)$. Trees can only be planted at locations with integer coordinates. Every tree will grow roots within a disk of radius $r$ centered at the location where the tree was planted; ... | Let $\delta = \lceil r \rceil$. Note that all trees need to be planted at least $\delta$ away from the boundary of the lawn, so their coordinates $(x, y)$ have to satisfy $\delta \leq x, y \leq n-\delta$. For integers $0 \leq a_\delta, \dots, a_{n-\delta} \leq n-\delta$, denote by $f(a_\delta, \dots, a_{n-\delta})$ any... | [
"brute force",
"dfs and similar",
"dp",
"geometry",
"probabilities"
] | 3,300 | null |
1949 | B | Charming Meals | The Czech cuisine features $n$ appetizers and $n$ main dishes. The $i$-th appetizer has spiciness $a_i$, and the $i$-th main dish has spiciness $b_i$.
A typical Czech meal consists of exactly one appetizer and one main dish. You want to pair up the $n$ appetizers and $n$ main dishes into $n$ meals with each appetizer ... | Let's sort spicinesses, assume $a_1 \leq a_2 \leq \ldots \leq a_n$, $b_1 \leq b_2 \leq \ldots \leq b_n$. Lemma: Let $c, d$ be some nondecreasing arrays of length $n$. If there exists some permutation $\sigma(1), \sigma(2), \ldots, \sigma(n)$, such that $c_i \leq d_{\sigma(i)}$ for all $i$, then $c_i \leq d_i$ for all $... | [
"binary search",
"brute force",
"greedy",
"sortings"
] | 1,500 | null |
1949 | C | Annual Ants' Gathering | Deep within a forest lies an ancient tree, home to $n$ ants living in $n$ tiny houses, indexed from $1$ to $n$, connected by the branches of the tree.
Once a year, all the ants need to gather to watch the EUC. For this, all ants move along the $n-1$ branches of the tree they live on to meet at the home of one ant.
Ho... | First Solution Since ants can not move to an empty house the region of non-empty homes always needs to stay connected. Therefore, only ants on a leaf of the tree (of non-empty homes) can ever move and they can only move in one direction. Further, notice that if the smallest group of ants on a leaf can not move the solu... | [
"dfs and similar",
"dp",
"greedy",
"trees"
] | 1,900 | null |
1949 | D | Funny or Scary? | You are designing a new video game. It has $n$ scenarios, which the player may play in any order, but each scenario must be played exactly once. When a player switches from a scenario to another scenario, the game shows a specially crafted transition video to make it all feel part of one big story. This video is specif... | In graph terms, this problem can be formulated as follows: you are given a complete undirected graph on $n$ vertices. Some of the edges of the graph are already colored with two colors. You need to color all remaining edges with two colors in such a way that the graph does not have long monochromatic simple paths. Firs... | [
"constructive algorithms"
] | 2,600 | null |
1949 | E | Damage per Second | You just created a new character in your favourite role-playing game and now have to decide how to skill him.
The two skill attributes to be chosen are: \underline{damage per hit} and \underline{hits per second}. Damage per hit is the amount of damage you deal with a single hit, while hits per second is the number of ... | Once the sugar-coating is removed, the problem boils down to: Let $f(x) = \sum_{i=1}^n \left\lceil\frac{h_i}{x}\right\rceil$, and $g(x) = \frac{f(x)}{k-x}$. Find the minimum of $g(x)$. Sort the values so that $h_1\geq h_2\geq\dots\geq h_n$. Define $H:=h_1+\dots+h_n$. From now on, we will assume that $k$ is even, so tha... | [
"brute force",
"math"
] | 2,900 | null |
1949 | F | Dating | You are the developer of a dating app which ignores gender completely. The app has $n$ users, indexed from $1$ to $n$. Each user's profile features a list of the activities they enjoy doing. There are $m$ possible activities, indexed from $1$ to $m$.
A match between two users is good if they share at least one activit... | After formalizing the statement, we get the following: Given $n$ sets $S_1, S_2, \ldots, S_n$ of activities, find a pair $(a, b)$ such that all three sets $S_a \setminus S_b, S_b \setminus S_a, S_a \cap S_b$ are non-empty. In essence, this means that for a pair $(a, b)$ to not be good, it must suffice that $S_a$ and $S... | [
"greedy",
"sortings",
"trees"
] | 2,200 | null |
1949 | G | Scooter | The Czech Technical University campus consists of $n$ buildings, indexed from $1$ to $n$. In each building, there can be a math class scheduled, or a computer science class, or neither (but not both). Additionally, in each building, there is at most one professor, and each professor is either an expert in mathematics o... | First and foremost, let's notice that we can ignore professors that are already in a proper building, as well as empty buildings (in essence, all positions $i$ such that $c_i = p_i$). More so, if there are no positions $i$ such that $c_i \neq p_i$ then we can simply answer $\texttt{YES}$ without doing any operations. F... | [
"graphs",
"greedy"
] | 2,300 | null |
1949 | H | Division Avoidance | A newly discovered organism can be represented as a set of cells on an infinite grid. There is a coordinate system on the grid such that each cell has two integer coordinates $x$ and $y$. A cell with coordinates $x=a$ and $y=b$ will be denoted as $(a, b)$.
Initially, the organism consists of a single cell $(0, 0)$. Th... | Looking at the solution for the sample, the most difficulty seems to come from the condition in bold: A division of a cell $(a, b)$ can only happen if the cells $(a+1, b)$ and $(a, b+1)$ are not yet part of the organism. It forces us to clean up space, potentially recursively, before a division that we want to happen c... | [
"greedy",
"math"
] | 3,100 | null |
1949 | I | Disks | You are given $n$ disks in the plane. The center of each disk has integer coordinates, and the radius of each disk is a positive integer. No two disks overlap in a region of positive area, but it is possible for disks to be tangent to each other.
Your task is to determine whether it is possible to change the radii of ... | To start, we build the graph that describes the tangency relation between the disks: each disk is represented by a node in the graph, and an edge connects two nodes if and only if the corresponding disks are tangent. Constructing this graph can be done in time $O(n^2)$ by checking all pairs of disks. In fact, it can be... | [
"dfs and similar",
"geometry",
"graph matchings",
"graphs"
] | 1,800 | null |
1949 | J | Amanda the Amoeba | \textbf{This problem has an attachment. You can use it to simulate and visualize the movements of the amoeba.}
Amoeba Amanda lives inside a rectangular grid of square pixels. Her body occupies some of these pixels. Other pixels may be either free or blocked. Amanda moves across the grid using the so-called amoeboid mo... | To solve this problem, we first find a path $P_{AB}$ connecting two pixels $A$ and $B$ such that Pixel $A$ is a part of the initial position. Pixel $B$ is a part of the final position. All inner pixels of $P_{AB}$ (let us denote their sequence as $Q_{AB}$) are free pixels, neither inside the initial position nor the fi... | [
"graphs",
"implementation",
"trees",
"two pointers"
] | 2,600 | null |
1949 | K | Make Triangle | You are given $n$ positive integers $x_1, x_2, \ldots, x_n$ and three positive integers $n_a, n_b, n_c$ satisfying $n_a+n_b+n_c = n$.
You want to split the $n$ positive integers into three groups, so that:
- The first group contains $n_a$ numbers, the second group contains $n_b$ numbers, the third group contains $n_c... | We will denote these groups as $A, B, C$ correspondingly. Wlog $n_a \leq n_b \leq n_c$, and $x_1 \leq x_2 \leq \ldots \leq x_n$. Let $x_1 + x_2 + \ldots + x_n = S$. We just want the sum in each group to be smaller than $\frac{S}{2}$. Let's note some obvious conditions for construction to be possible: The largest group ... | [
"constructive algorithms",
"math"
] | 2,800 | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.