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 ⌀ |
|---|---|---|---|---|---|---|---|
2114 | E | Kirei Attacks the Estate | Once, Kirei stealthily infiltrated the trap-filled estate of the Ainzbern family but was discovered by Kiritugu's familiar. Assessing his strength, Kirei decided to retreat. The estate is represented as a tree with $n$ vertices, with the \textbf{root} at vertex $1$. Each vertex of the tree has a number $a_i$ recorded, ... | Let's consider each vertex separately. The sign with which its danger enters the sign-variable sum depends on the parity of its index on the path. When we calculate $f(v)$ - the maximum value of the threat of the vertex, we can either stay in it or subtract the minimum value of the threat of our parent: $f(v) = \max(a_... | [
"dfs and similar",
"dp",
"greedy",
"trees"
] | 1,400 | from math import inf
from sys import setrecursionlimit
def solve(v, p, mini, maxi):
global res
res[v] = max(arr[v], mini * -1 + arr[v])
mini = min(arr[v], maxi * -1 + arr[v])
for u in gr[v]:
if u == p:
continue
solve(u, v, mini, res[v])
setrecursionlimit(400_000)
t = int(... |
2114 | F | Small Operations | Given an integer $x$ and an integer $k$. In one operation, you can perform one of two actions:
- choose an integer $1 \le a \le k$ and assign $x = x \cdot a$;
- choose an integer $1 \le a \le k$ and assign $x = \frac{x}{a}$, where the value of $\frac{x}{a}$ must be an integer.
Find the minimum number of operations re... | It is not difficult to guess that first we need to use operations of the second type to make $x$ equal to $gcd(x, y)$, and only then use operations of the first type to make it equal to $y$. That is, now we need to decompose the numbers $x/gcd(x, y)$ and $y/gcd(x, y)$ into the minimum number of factors not exceeding $k... | [
"binary search",
"brute force",
"dfs and similar",
"dp",
"math",
"number theory",
"sortings"
] | 2,000 | #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 int inf = 1e9... |
2114 | G | Build an Array | Yesterday, Dima found an empty array and decided to add some integers to it. He can perform the following operation an unlimited number of times:
- add any integer to the left or right end of the array.
- then, as long as there is a pair of identical adjacent elements in the array, they will be replaced by their sum.
... | Let's start with a slow solution: we will iterate through the element that we will add first for as many operations as possible and will add the other elements to the left and right, using as many operations as possible. When adding a new number, we will also look at the number that was added before it from the same si... | [
"brute force",
"constructive algorithms",
"dp",
"greedy",
"math",
"number theory"
] | 2,200 | #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 int inf = 1e9;
con... |
2115 | A | Gellyfish and Flaming Peony | Gellyfish hates math problems, but she has to finish her math homework:
Gellyfish is given an array of $n$ positive integers $a_1, a_2, \ldots, a_n$.
She needs to do the following two-step operation until all elements of $a$ are equal:
- Select two indexes $i$, $j$ satisfying $1 \leq i, j \leq n$ and $i \neq j$.
- R... | Try to think about why Gellyfish can always achieve her goal, and ultimately what all the elements will turn into. When you've figured out Hint 1, try using dynamic programming to reach your goal. Let $g = \gcd(a_1, a_2, \dots, a_n)$, It can be shown that eventually all elements become $g$. Consider the assumption that... | [
"constructive algorithms",
"dp",
"math",
"number theory"
] | 1,500 | #include<bits/stdc++.h>
using namespace std;
const int N = 5000 + 5;
inline void checkmax(int &x, int y){
if(y > x) x = y;
}
inline void checkmin(int &x, int y){
if(y < x) x = y;
}
int n = 0, m = 0, k = 0, a[N] = {}, f[N] = {};
int g[N][N] = {}, ans = 0;
inline void solve(){
scanf("%d", &n); m = k = 0;
for(in... |
2115 | B | Gellyfish and Camellia Japonica | Gellyfish has an array of $n$ integers $c_1, c_2, \ldots, c_n$. In the beginning, $c = [a_1, a_2, \ldots, a_n]$.
Gellyfish will make $q$ modifications to $c$.
For $i = 1,2,\ldots,q$, Gellyfish is given three integers $x_i$, $y_i$, and $z_i$ between $1$ and $n$. Then Gellyfish will set $c_{z_i} := \min(c_{x_i}, c_{y_i... | Try working backwards from the final sequence, to the initial. If you're confused about Hint 1, it's probably because the result isn't unique each time. Think carefully about whether you can just take the "tightest" result possible Let's think of the problem in another way, if we only require that for all $i$, the fina... | [
"brute force",
"constructive algorithms",
"dfs and similar",
"dp",
"graphs",
"greedy",
"trees"
] | 2,100 | #include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n = 0, q = 0, a[N] = {}, b[N] = {}, c[N] = {};
int x[N] = {}, y[N] = {}, z[N] = {};
inline void init(){
for(int i = 1 ; i <= n ; i ++) a[i] = b[i] = c[i] = 0;
for(int i = 1 ; i <= q ; i ++) x[i] = y[i] = z[i] = 0;
n = q = 0;
}
inline void so... |
2115 | C | Gellyfish and Eternal Violet | There are $n$ monsters, numbered from $1$ to $n$, in front of Gellyfish. The HP of the $i$-th monster is $h_i$.
Gellyfish doesn't want to kill them, but she wants to keep these monsters from being a threat to her. So she wants to reduce the HP of all the monsters to exactly $1$.
Now, Gellyfish, with The Sword Sharpen... | Try to find an $O(nm h^2)$ solution using dynamic programming. Re-examining Gellyfish's strategy, there are definitely situations where she chooses to carry out an attack. Can we divide the $m$ rounds into two phases by some nature? Considering all current monsters, if the lowest HP of the monsters is $l$, Gellyfish ca... | [
"combinatorics",
"dp",
"greedy",
"math",
"probabilities"
] | 2,700 | #include<bits/stdc++.h>
using namespace std;
const int N = 22, K = 4000 + 5, M = 400 + 5, Inf = 0x3f3f3f3f;
inline void checkmin(double &x, double y){
if(y < x) x = y;
}
int n = 0, m = 0, s = 0, k = 0, p0 = 0, h[N] = {};
double p = 0, f[K][K] = {}, g[K][N][M] = {}, ans = 0;
inline void init(){
for(int i = 0 ; i ... |
2115 | D | Gellyfish and Forget-Me-Not | Gellyfish and Flower are playing a game.
The game consists of two arrays of $n$ integers $a_1,a_2,\ldots,a_n$ and $b_1,b_2,\ldots,b_n$, along with a binary string $c_1c_2\ldots c_n$ of length $n$.
There is also an integer $x$ which is initialized to $0$.
The game consists of $n$ rounds. For $i = 1,2,\ldots,n$, the r... | Consider if $c$ consists only of $0$, this problem turned out to be another classic problem. So you need at least something that you know what it is. "linear basis" is the answer to Hint 1. Please try to understand this: all addition operations are interpreted as XOR operations. We can assume that the initial value of ... | [
"bitmasks",
"dp",
"games",
"greedy",
"math"
] | 2,900 | #include <bits/stdc++.h>
using i64 = long long;
constexpr int L = 60;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0);
int T;
for(std::cin >> T; T; T --) {
int n;
std::cin >> n;
std::vector<i64> a(n), b(n);
std::string str;
i64 all = 0;
for(auto &x : a) std::cin >> x, all ^= x;
for(int ... |
2115 | E | Gellyfish and Mayflower | \begin{quote}
Mayflower by Plum
\end{quote}
May, Gellyfish's friend, loves playing a game called "Inscryption" which is played on a directed acyclic graph with $n$ vertices and $m$ edges. All edges $ a \rightarrow b$ satisfy $a<b$.
You start in vertex $1$ with some coins. You need to move from vertex $1$ to the verte... | There is an easy way to solve the problem in $O(m \max(r) + q)$ time complexity. Thus for cases with small $r$, we can easily solve them, but what about cases with large $r$? There is a classic but mistaken greed where we only take the item with the largest $\frac w c$. This is obviously wrong, but Hint 1 lets us rule ... | [
"dp",
"graphs"
] | 3,500 | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 200 + 5, Inf = 0xcfcfcfcfcfcfcfcf;
inline ll sqr(ll x){
return x * x;
}
inline ll gcd(ll x, ll y){
if(y) return gcd(y, x % y);
else return x;
}
inline void checkmax(ll &x, ll y){
if(y > x) x = y;
}
ll n = 0, m = 0, magic = 0, w[N]... |
2115 | F1 | Gellyfish and Lycoris Radiata (Easy Version) | \textbf{This is the easy version of the problem. The difference between the versions is that in this version, the time limit and the constraints on $n$ and $q$ are lower. You can hack only if you solved all versions of this problem.}
Gellyfish has an array consisting of $n$ sets. Initially, all the sets are empty.
No... | We apply block decomposition to the operations, dividing every $B$ operations into a single round. Within each round, the sequence is partitioned into $O(B)$ segments. For each segment, we maintain a queue that records all elements added to that segment during the round. Type 1 and 2 operations can be handled directly ... | [
"data structures"
] | 3,500 | #pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
// MagicDark
#include <bits/stdc... |
2115 | F2 | Gellyfish and Lycoris Radiata (Hard Version) | \textbf{This is the hard version of the problem. The difference between the versions is that in this version, the time limit and the constraints on $n$ and $q$ are higher. You can hack only if you solved all versions of this problem.}
Gellyfish has an array consisting of $n$ sets. Initially, all the sets are empty.
N... | We consider using leafy persistent balanced trees to maintain the sequence. At each non-leaf node, we store a set $S_u$ as a lazy tag, indicating that every set in the subtree rooted at $u$ contains $S_u$. However, since $|S_u|$ can be large, it's difficult to push down the tag efficiently. To address this, we split ea... | [
"data structures"
] | 3,500 | #include <bits/stdc++.h>
constexpr int N = 3e5 + 10, S = 1.1e7, SS = 2 * S;
int n, q;
int next[SS], val[SS], cnt;
struct queue {
int head, tail;
void push(int x) {
if(head) {
next[tail] = ++ cnt, val[cnt] = x; tail = cnt;
} else {
head = tail = ++ cnt, val[cnt] = x;
}
assert(cnt < SS - 100);
}
void po... |
2116 | A | Gellyfish and Tricolor Pansy | Gellyfish and Flower are playing a game called "Duel".
Gellyfish has $a$ HP, while Flower has $b$ HP.
Each of them has a knight. Gellyfish's knight has $c$ HP, while Flower's knight has $d$ HP.
They will play a game in rounds until one of the players wins. For $k = 1, 2, \ldots$ in this order, they will perform the ... | Please think carefully about what happens after the death of either knight. While a player who goes to $0$ HP will lose the game outright, when a player's knight dies, she loses the ability to attack; then in future rounds, she can only be attacked by her opponent and thus lose the game. Thus it can be found that the p... | [
"games",
"greedy"
] | 800 | #include<bits/stdc++.h>
using namespace std;
inline void solve(){
int a = 0, b = 0, c = 0, d = 0;
scanf("%d %d %d %d", &a, &b, &c, &d);
if(min(a, c) >= min(b, d)) printf("Gellyfish\n");
else printf("Flower\n");
}
int T = 0;
int main(){
scanf("%d", &T);
for(int i = 0 ; i < T ; i ++) solve();
return 0;
} |
2116 | B | Gellyfish and Baby's Breath | Flower gives Gellyfish two permutations$^{\text{∗}}$ of $[0, 1, \ldots, n-1]$: $p_0, p_1, \ldots, p_{n-1}$ and $q_0, q_1, \ldots, q_{n-1}$.
Now Gellyfish wants to calculate an array $r_0,r_1,\ldots,r_{n-1}$ through the following method:
- For all $i$ ($0 \leq i \leq n-1$), $r_i = \max\limits_{j=0}^{i} \left(2^{p_j} +... | How to quickly compare $2^a + 2^b$ and $2^c + 2^d$ for given integers $a, b, c, d$ is the key. We are given two permutations of $p$ and $q$, which means that each element will appear only once in both $p$ and $q$, respectively. What's the point of this? For given integers $a, b, c, d$ , if we want to compare $2^a+2^b$ ... | [
"greedy",
"math",
"sortings"
] | 1,300 | #include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, Mod = 998244353;
int n = 0, s[N] = {}, p[N] = {}, q[N] = {}, r[N] = {};
inline void solve(){
scanf("%d", &n);
for(int i = 0 ; i < n ; i ++) scanf("%d", &p[i]);
for(int i = 0 ; i < n ; i ++) scanf("%d", &q[i]);
for(int i = 0, j = 0, k = 0 ; k < n... |
2117 | A | False Alarm | Yousef is at the entrance of a long hallway with $n$ doors in a row, numbered from $1$ to $n$. He needs to pass through all the doors from $1$ to $n$ in order of numbering and reach the exit (past door $n$).
Each door can be open or closed. If a door is open, Yousef passes through it in $1$ second. If the door is clos... | When is the optimal time to use the button? It's not necessary to use the button when a door is already open. Therefore, it's always optimal to use the button as soon as we hit a closed door. Let's call the position of the first closed door $l$ and the position of the last closed door $r$. The length of this interval i... | [
"greedy",
"implementation"
] | 800 | #include <bits/stdc++.h>
using namespace std;
void solve() {
int n, x;
cin >> n >> x;
int l = 1e5, r = -1;
for(int i = 0; i < n; i++) {
int door;
cin >> door;
if(door == 1) {
l = min(l, i);
r = max(r, i);
}
}
cout << (x >= r - l + 1 ?... |
2117 | B | Shrink | A shrink operation on an array $a$ of size $m$ is defined as follows:
- Choose an index $i$ ($2 \le i \le m - 1$) such that $a_i \gt a_{i - 1}$ and $a_i \gt a_{i + 1}$.
- Remove $a_i$ from the array.
Define the score of a permutation$^{\text{∗}}$ $p$ as the maximum number of times that you can perform the shrink oper... | When are we unable to remove the maximum element? We can always remove the maximum element as long as it exists between two elements. How can we generalize this? In a permutation, the maximum element can only occur once. Since all other elements are guaranteed to be smaller than maximum, we can remove the maximum if th... | [
"constructive algorithms"
] | 800 | #include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
for(int i = 2; i <= n; i++) cout << i << ' ';
cout << 1 << endl;
}
int main() {
int t;
cin >> t;
while(t--) solve();
} |
2117 | C | Cool Partition | Yousef has an array $a$ of size $n$. He wants to partition the array into one or more contiguous segments such that each element $a_i$ belongs to exactly one segment.
A partition is called cool if, for every segment $b_j$, all elements in $b_j$ also appear in $b_{j + 1}$ (if it exists). That is, every element in a seg... | The last segment of a valid partition must contain all distinct elements of the array. From the first hint, we can say that any segment ending at some position $r$ must contain all distinct elements of the prefix $[1, r]$. Claim: In a valid partition, if a segment ends at some position $r$, it must contain all distinct... | [
"data structures",
"greedy"
] | 1,200 | #include <bits/stdc++.h>
using namespace std;
void solve(){
int n, ans = 0;
cin >> n;
vector<int> a(n);
for(int i=0; i<n; i++) cin >> a[i];
set<int> cur, seen;
for(int i=0; i<n; i++){
cur.insert(a[i]);
seen.insert(a[i]);
if(cur.size() == seen.size()){
ans++;... |
2117 | D | Retaliation | Yousef wants to explode an array $a_1, a_2, \dots, a_n$. An array gets exploded when all of its elements become equal to zero.
In one operation, Yousef can do \textbf{exactly} one of the following:
- For every index $i$ in $a$, decrease $a_i$ by $i$.
- For every index $i$ in $a$, decrease $a_i$ by $n - i + 1$.
Your ... | What happens when we do $1$ operation of the first type and $1$ operation of the second type? If we perform both types of the operation once, each element $a_i$ is decreased by $n + 1$. Suppose we are able to explode the array with $x$ operations of the first type and $y$ operations of the second type. Then, let's pair... | [
"binary search",
"math",
"number theory"
] | 1,200 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve() {
ll n;
cin >> n;
vector<ll> v(n);
for(auto &it : v) cin >> it;
ll y = (2 * v[0] - v[1]) / (n + 1);
ll x = v[1] - v[0] + y;
if(y < 0 || x < 0) {
cout << "NO" << endl;
return;
}
for(i... |
2117 | E | Lost Soul | You are given two integer arrays $a$ and $b$, each of length $n$.
You may perform the following operation any number of times:
- Choose an index $i$ $(1 \le i \le n - 1)$, and set $a_i := b_{i + 1}$, \textbf{or} set $b_i := a_{i + 1}$.
\textbf{Before} performing any operations, you are allowed to choose an index $i$... | If we can get a match at position $i$, then we can have at least $i$ matches. Why is this true? For a certain position $i$, given the ability to remove index $i + 1$, we can pull any value from the range $[i + 2, n]$. It's clear to see that if a match exists at position $i$, then we can repeatedly set $a_j := b_{j+1}$ ... | [
"brute force",
"greedy"
] | 1,600 | #include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n), b(n);
for(auto &it : a) cin >> it;
for(auto &it : b) cin >> it;
vector<bool> seen(n + 1);
if(a.back() == b.back()) {
cout << n << endl;
return;
}
int ans = -1;
for... |
2117 | F | Wildflower | Yousef has a rooted tree$^{\text{∗}}$ consisting of exactly $n$ vertices, which is rooted at vertex $1$. You would like to give Yousef an array $a$ of length $n$, where each $a_i$ $(1 \le i \le n)$ \textbf{can either be $1$ or $2$}.
Let $s_u$ denote the sum of $a_v$ where vertex $v$ is in the subtree$^{\text{†}}$ of v... | There can only be at most $2$ leaves. This means the tree can either be a chain or Y-shaped. If the tree is a chain, subtree sums will always be strictly increasing as you move towards the root, which means that the answer for any chain is $2^n$. Claim: For an answer to exist, the tree should have at most $2$ leaves. W... | [
"combinatorics",
"dfs and similar",
"trees"
] | 1,800 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
#define int long long
vector<int> adj[N], lens;
int pw[N];
int lca;
void dfs(int u, int par, int len) {
if(adj[u].size() > 2) lca = len;
bool leaf = true;
for(int v : adj[u]) {
if(v != par) {
... |
2117 | G | Omg Graph | You are given an undirected connected weighted graph. Define the cost of a path of length $k$ to be as follows:
- Let the weights of all the edges on the path be $w_1,...,w_k$.
- The cost of the path is $(\min_{i = 1}^{k}{w_i}) + (\max_{i=1}^{k}{w_i})$, or in other words, the maximum edge weight + the minimum edge wei... | Try fixing the minimum edge on the path. There are two common ways to solve this problem; one uses DSU and the other uses Dijkstra. I will explain the Dijkstra approach here: The main idea here is to pick some edge $e$ and pretend that $e$ is the minimum-weight edge, then find the smallest maximum edge weight across al... | [
"brute force",
"dsu",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 1,900 | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout << #x << " = " << x << "\n";
#define vdebug(a) cout << #a << " = "; for(auto x: a) cout << x << " "; cout << "\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int uid(int a, int b) { return uniform_int_dist... |
2117 | H | Incessant Rain | \textbf{Note the unusual memory limit.}
Silver Wolf gives you an array $a$ of length $n$ and $q$ queries. In each query, she replaces an element in $a$. After each query, she asks you to output the maximum integer $k$ such that there exists an integer $x$ such that it is the $k$-majority of a subarray$^{\text{∗}}$ of ... | Try to think of an offline solution. Rephrase the problem to maximum subarray sums. The unusual memory limit is meant to cut persistent segment tree solutions. Obviously it isn't perfect (I apologize if it caused issues), but it's the best I could do. Let's first pretend that $x$ in the problem is fixed. For example, i... | [
"data structures",
"divide and conquer",
"sortings"
] | 2,500 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define trav(a,x) for(auto& a: x)
#define sz(x) (int)x.size()
template<typename T> istream& operator>>(istream& in... |
2118 | A | Equal Subsequences | We call a bitstring$^{\text{∗}}$ perfect if it has the same number of $\mathtt{101}$ and $\mathtt{010}$ subsequences$^{\text{†}}$. Construct a perfect bitstring of length $n$ where the number of $\mathtt{1}$ characters it contains is exactly $k$.
It can be proven that the construction is always possible. If there are ... | It is somehow difficult to count the number of such subsequences in an arbitrary string. Can you think of strings where it is trivial? The number of such subsequences can be $0$. Key observation: A bitstring where all $\mathtt{1}$ bits come before all $\mathtt{0}$ bits is perfect as it has no $\mathtt{101}$ or $\mathtt... | [
"constructive algorithms",
"greedy"
] | 800 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
for (int tc = 1; tc <= t; tc++) {
int n, k; cin >> n >> k;
for (int i = 0; i < n-k; i++) cout << 0;
for (int i = 0; i < k; i++) cout << 1;
cout << "\n";
}
return 0;
} |
2118 | B | Make It Permutation | There is a matrix $A$ of size $n\times n$ where $A_{i,j}=j$ for all $1 \le i,j \le n$.
In one operation, you can select a row and reverse any subarray$^{\text{∗}}$ in it.
Find a sequence of at most $2n$ operations such that every column will contain a permutation$^{\text{†}}$ of length $n$.
It can be proven that the... | The answer matrix will have permutations in every row and column. Can you think of such a matrix? Let our final matrix have all cyclic shifts of the identity permutation in each row. Can you perform a cyclic shift using $3$ operations? One of the operations performed on each row becomes redundant. Key observation: You ... | [
"constructive algorithms"
] | 1,200 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
for (int tc = 1; tc <= t; tc++) {
int n; cin >> n;
cout << 2*n-1 << "\n";
for (int i = 1; i < n; i++) {
cout << i << " " << 1 << " " ... |
2118 | C | Make It Beautiful | You are given an array $a$ of $n$ integers. We define the $\text{beauty}$ of a number $x$ to be the number of $1$ bits in its binary representation. We define the beauty of an array to be the sum of beauties of the numbers it contains.
In one operation, you can select an index $i$ $(1 \le i \le n)$ and increase $a_i$ ... | Think in binary. Let $x$ be an integer. What is the smallest number $y>x$ that is more beautiful than $x$? $y$ always equals $x$ with the smallest valued $0$ bit set to $1$. Key observation: To increase the beauty of a number, it is always optimal to set the least valued $0$ bit to $1$. Let our number be $x$ and call t... | [
"bitmasks",
"data structures",
"greedy",
"math"
] | 1,300 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
ll n, k; cin >> n >> k;
ll ans = 0;
vector<ll> a(n);
for (ll &i : a) {
cin >> i;
ans += __builtin_popcountll(i);
}
for (int j = 0; j <= 60; j++) {
ll bb = (1ll<<j);
for (ll x : ... |
2118 | D2 | Red Light, Green Light (Hard version) | \textbf{This is the hard version of the problem. The only difference is the constraint on $k$ and the total sum of $n$ and $q$ across all test cases. You can make hacks only if both versions of the problem are solved.}
You are given a strip of length $10^{15}$ and a constant $k$. There are exactly $n$ cells that conta... | Divide the problem into some subproblems: Find the next traffic light efficiently. Detect cycles efficiently. For D1 it is enough to do the first subproblem in $O(N)$, the second subproblem with $O(N)$ calls of the first subproblem. For D2 we need to do better. Try to simulate the problem by hand. Iterate over all traf... | [
"binary search",
"brute force",
"data structures",
"dfs and similar",
"dp",
"graphs",
"implementation",
"math",
"number theory"
] | 2,200 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
ll m, k; cin >> m >> k;
vector<ll> p(m+1), d(m+1);
for (int i = 1; i <= m; i++) cin >> p[i];
for (int i = 1; i <= m; i++) cin >> d[i];
map<ll, vector<ll>> mpl, mpr;
map<ll, ll> traffic;
for (int i = 1; i <... |
2118 | E | Grid Coloring | There is a $n\times m$ grid with each cell initially white. You have to color all the cells one-by-one. After you color a cell, all the \textbf{colored cells} furthest from it receive a penalty. Find a coloring order, where no cell has more than $3$ penalties.
\textbf{Note that $n$ and $m$ are both odd.}
The distance... | Why is it important that both $N$ and $M$ are odd? Try generalizing the trivial case when $N=1$. Try extending the solution from a smaller case... in a literal sense. Key observation: Having $N \ge M$ and N, M both odd, an $(N - 2) \times M$ grid can easily be extended into a $N \times M$ grid To achieve this first col... | [
"constructive algorithms",
"geometry",
"greedy",
"math"
] | 2,400 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n, m; cin >> n >> m;
auto print = [&](int x, int y) {
if (1 <= x && x <= n && 1 <= y && y <= m) {
cout << x << " " << y << "\n";
}
};
int cx = (n+1)/2;
int cy = (m+1)/2;
... |
2118 | F | Shifts and Swaps | You are given arrays $a$ and $b$ of length $n$ and an integer $m$.
The arrays only contain integers from $1$ to $m$, and both arrays contain all integers from $1$ to $m$.
You may repeatedly perform either of the following operations on $a$:
- cyclic shift$^{\text{∗}}$ the array to the left
- swap two neighboring ele... | The order of two elements only matters if the absolute value of their difference is at most 1. Try to store the positions of elements with value $v$ relative to elements with value $v+1$. (The next hint will be about what to do with these.) We want to somehow hash the relative positions. (The next hint will be about a ... | [
"data structures",
"graphs",
"hashing",
"trees"
] | 3,100 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline int sign_of_non_zero(const int x) {
return x > 0 ? 1 : -1;
}
struct IllegalTransformationException : public std::runtime_error {
using std::runtime_error::runtime_error;
};
template <
std::uint64_t ELEMENT_MULTIPLIER,
s... |
2120 | A | Square of Rectangles | Aryan is an ardent lover of squares but a hater of rectangles (Yes, he knows all squares are rectangles). But Harshith likes to mess with Aryan. Harshith gives Aryan three rectangles of sizes $l_1\times b_1$, $l_2\times b_2$, and $l_3\times b_3$ such that $l_3\leq l_2\leq l_1$ and $b_3\leq b_2\leq b_1$. Aryan, in order... | There are only two possible ways to arrange rectangles into a square if possible. The only cases possible to arrange rectangles into a square are: All three rectangles are put side by side, i.e. $l_1=l_2=l_3=b_1+b_2+b_3$ or $b_1=b_2=b_3=l_1+l_2+l_3$. Rectangles $2$ and $3$ are side by side with rectangle $1$ above it, ... | [
"geometry",
"math"
] | null | #include <iostream>
using namespace std;
int main(){
int t;
cin >> t;
int l1, b1, l2, b2, l3, b3;
auto check = [&] () {
if (l1 == l2 && l2 == l3) return (l1 == b1 + b2 + b3 || (b1 == b2 + b3 && 2*l1 == b1));
if (l2 == l3) return (b2 + b3 == b1 && b1 == l2 + l1);
retur... |
2120 | B | Square Pool | Aryan and Harshith are playing pool in universe AX120 on a fixed square pool table of side $s$ with \textbf{pockets} at its $4$ corners. The corners are situated at $(0,0)$, $(0,s)$, $(s,0)$, and $(s,s)$. In this game variation, $n$ identical balls are placed on the table with integral coordinates such that no ball lie... | What happens to the balls that collide with an edge, eventually? How does a collision between two balls affect the outcome? Observations Any ball on the diagonals of the square that is shot towards a pocket will be potted on a free table. Any ball that collides with an edge will be in a $4$-periodic path colliding with... | [
"geometry"
] | null | #include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
int n, s, ans = 0, dxi, dyi, xi, yi;
while(t--) {
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> dxi >> dyi >> xi >> yi;
if (dxi == dyi) ans += (xi == yi);
else ... |
2120 | C | Divine Tree | Harshith attained enlightenment in Competitive Programming by training under a Divine Tree. A divine tree is a rooted tree$^{\text{∗}}$ with $n$ nodes, labelled from $1$ to $n$. The divineness of a node $v$, denoted $d(v)$, is defined as the smallest node label on the unique simple path from the root to node $v$.
Arya... | What are bounds on $m$ for a given $n$ to have a divine tree? And how does the tree look for the lower bound and the upper bound? The $\text{min}$ and $\text{max}$ value of $m$ for a divine tree to exist are $n$ and $\frac{n \cdot (n + 1)}{2}$ respectively. $\text{POC:}$ Any $m \in [\text{min}, \text{max}]$ can be achi... | [
"constructive algorithms",
"greedy",
"math",
"sortings",
"trees"
] | null | #include <iostream>
#include <cstdint>
#include <cassert>
#include <vector>
using namespace std;
#define i64 int64_t
void solve() {
i64 n, sum;
cin >> n >> sum;
if(sum < n || sum > n * (n + 1) / 2) {
cout << "-1\n";
return;
}
i64 k = sum - n;
vector<i64> ans;
i64 curr =... |
2120 | D | Matrix game | Aryan and Harshith play a game. They both start with three integers $a$, $b$, and $k$. Aryan then gives Harshith two integers $n$ and $m$. Harshith then gives Aryan a matrix $X$ with $n$ rows and $m$ columns, such that each of the elements of $X$ is between $1$ and $k$(inclusive). After that, Aryan wins if he can find ... | Use pigeonhole principle to get minimum $n$ and then minimum $m$. If each row is of size $k(a-1)+1$, by pigeonhole principle, it will have at least $a$ elements with the same value. Let those elements appear positions $p_1<p_2<...<p_a$($p_i$ is the column number where the element occurs) and let the value be $v$. Consi... | [
"combinatorics",
"math"
] | null | #include <bits/stdc++.h>
using namespace std;
const long long mod=1000000007;
long long inv[100001];
int main(){
ios::sync_with_stdio(false),cin.tie(0);
int T;
long long i,a,b,k,d,ans;
inv[1]=1;
for(i=2;i<=100000;i++)inv[i]=(mod-mod/i)*inv[mod%i]%mod;
for(cin>>T;T>0;T--)
{
cin>>a>>b>>k;
d=k*a-k+1;
ans=k;
... |
2120 | E | Lanes of Cars | Harshith is the president of TollClub. He tasks his subordinate Aryan to oversee a toll plaza with $n$ lanes. Initially, the $i$-th lane has $a_i$ cars waiting in a queue. Exactly one car from the front of each lane passes through the toll every second.
The angriness of a car is defined as the number of seconds it had... | Use binary search to find minimum number of cars in a lane after optimal number lane shifts. Adjust cars afterwards such that minimum number of cars remains same as found in binary search, but angriness is minimized. Observe the following(Let $1$ car shift lanes at a time): It is always optimal to shift the car at the ... | [
"binary search",
"dp",
"ternary search"
] | null | //��
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 1111111;
const LL inf = 1e18;
int n,k,a[N];
LL s[N];
int main(){
int T,i,l,r,h;
LL x,y,z,o,p,t;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
scanf("%d",a+i);
sort(a+1,a+n+1);
for... |
2120 | F | Superb Graphs | As we all know, Aryan is a funny guy. He decides to create fun graphs. For a graph $G$, he defines fun graph $G'$ of $G$ as follows:
- Every vertex $v'$ of $G'$ maps to a non-empty independent set$^{\text{∗}}$ or clique$^{\text{†}}$ in $G$.
- The sets of vertices of $G$ that the vertices of $G'$ map to are pairwise di... | If two vertices have same open neighborhood in some graph $G_i$, atleast one of them has to correpond to a clique in every $G_i, H_i$ pair. If two vertices have same closed neighborhood in some graph $G_i$, atleast one of them has to correpond to an independent set in every $G_i, H_i$ pair. In a graph $G$, two vertices... | [
"2-sat",
"graphs"
] | null | #include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
bool twoSAT(vector<vector<int>> &adj, vector<vector<int>> &adj_rev, vector<bool> &assignment) {
int n = adj.size();
vector<int> order;
vector<bool> used(n, false);
function<void(int)> dfs1 = [&](int v) {
used... |
2120 | G | Eulerian Line Graph | Aryan loves graph theory more than anything. Well, no, he likes to flex his research paper on line graphs to everyone more. To start a conversation with you, he decides to give you a problem on line graphs. In the mathematical discipline of graph theory, the line graph of a simple undirected graph $G$ is another simple... | If $G$ has an Euler cycle, what can we say about $L^k(G), k\geq1$ If $L(G)$ has Euler path, how to determine if $L^k(G), k\geq2$ has an Euler path? If $L(G)$ doesn't Euler tour, but $L^2(G)$ does, what can we say about structure of $G$? Can there exist another graph $H$ such that $L(H)=G$? Following are the cases where... | [
"graphs",
"greedy",
"math"
] | null | #include "bits/stdc++.h"
using namespace std;
using ll = long long int;
mt19937_64 RNG(chrono::high_resolution_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using Tree = tree<T, null_type, less<T>, r... |
2121 | A | Letter Home | You are given an array of distinct integers $x_1, x_2, \ldots, x_n$ and an integer $s$.
Initially, you are at position $pos = s$ on the $X$ axis. In one step, you can perform exactly one of the following two actions:
- Move from position $pos$ to position $pos + 1$.
- Move from position $pos$ to position $pos - 1$.
... | Notice that if we visit positions $x_1$ and $x_n$, we will necessarily visit all other positions $x_i$. Therefore, our goal will be to visit positions $x_1$ and $x_n$. We then have two options: From position $s$, go to position $x_1$, and from there go to position $x_n$. We will need to make $|s - x_1| + |x_n - x_1|$ s... | [
"brute force",
"math"
] | null | #include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, s;
cin >> n >> s;
vector<int> x(n);
for (int i = 0; i < n; i++) cin >> x[i];
int ans = min(abs(s - x[0]... |
2121 | B | Above the Clouds | You are given a string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. Determine whether there exist three \textbf{non-empty} strings $a$, $b$, and $c$ such that:
- $a + b + c = s$, meaning the concatenation$^{\text{∗}}$ of strings $a$, $b$, and $c$ equals $s$.
- The string $b$ is a substring... | Notice that if there exist three non-empty strings $a$, $b$, and $c$ that satisfy the condition, then there exist three non-empty strings $a'$, $b'$, and $c'$ that satisfy the condition, where the length of string $b'$ is equal to $1$. To achieve this, one can choose any character from string $b$, append all characters... | [
"constructive algorithms",
"greedy",
"strings"
] | null | #include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
vector<int> cnt(26, 0);
for (auto c : s) cnt[c - 'a']++;
... |
2121 | C | Those Who Are With Us | You are given a matrix of integers with $n$ rows and $m$ columns. The cell at the intersection of the $i$-th row and the $j$-th column contains the number $a_{ij}$.
You can perform the following operation \textbf{exactly once}:
- Choose two numbers $1 \leq r \leq n$ and $1 \leq c \leq m$.
- For all cells $(i, j)$ in ... | Let $mx$ be the maximum value in the matrix. Note that the answer to the problem will be either $mx - 1$ or $mx$. When will the answer be $mx - 1$? If there exists a pair $(r, c)$ such that all values of $mx$ are contained in row $r$ or column $c$. Let $row_r$ be the number of times $mx$ appears in row $r$; $col_c$ be ... | [
"greedy",
"implementation"
] | null | #include<bits/stdc++.h>
using namespace std;
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int> (m));
int mx = 0, cnt_mx = 0;
for (int i = 0; i < n;... |
2121 | D | 1709 | You are given two arrays of integers $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$. It is guaranteed that each integer from $1$ to $2 \cdot n$ appears in exactly one of the arrays.
You need to perform a certain number of operations (possibly zero) so that \textbf{both} of the following conditions are satisfied:
... | Using bubble sort, we will sort array $a$: while there exists an index $1 \leq i < n$ such that $a_i > a_{i + 1}$, we will swap them. Similarly, we will sort array $b$. In this part of the task, we will perform no more than $\frac{n \cdot (n - 1)}{2} + \frac{n \cdot (n - 1)}{2} = n \cdot (n - 1)$ operations, since the ... | [
"implementation",
"sortings"
] | null | #include<bits/stdc++.h>
using namespace std;
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin ... |
2121 | E | Sponsor of Your Problems | For two integers $a$ and $b$, we define $f(a, b)$ as the number of positions in the decimal representation of the numbers $a$ and $b$ where their digits are the same. For example, $f(12, 21) = 0$, $f(31, 37) = 1$, $f(19891, 18981) = 2$, $f(54321, 24361) = 3$.
You are given two integers $l$ and $r$ of the \textbf{same}... | Note that the number $x$ will have the same length in decimal representation as the numbers $l$ and $r$. Consider the example $l = 12345$ and $r = 12534$. For the number $x$ to be in the range between $l$ and $r$ inclusive, it must be of length $5$ and start with the digits $12$. In other words, the greatest common pre... | [
"dp",
"greedy",
"implementation",
"strings"
] | null | #include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
string l, r;
cin >> l >> r;
if (l == r) {
cout << 2 * l.size() << '\n';
continue;
}
in... |
2121 | F | Yamakasi | You are given an array of integers $a_1, a_2, \ldots, a_n$ and two integers $s$ and $x$. Count the number of subsegments of the array whose sum of elements equals $s$ and whose maximum value equals $x$.
More formally, count the number of pairs $1 \leq l \leq r \leq n$ such that:
- $a_l + a_{l + 1} + \ldots + a_r = s$... | To start, let's recall how to solve the following problem: how many subsegments of the array have a sum of elements equal to $s$. Let $pref_i$ be the sum of the first $i$ elements of the array. Then the sum of a subsegment will be equal to $pref_r - pref_{l - 1}$. We will iterate over the right boundary $r$ and count t... | [
"binary search",
"brute force",
"data structures",
"greedy",
"two pointers"
] | null | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const maxn = 2e5 + 5;
int a[maxn];
ll pref[maxn], s;
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, x;
cin >> n >> s >> x;
for (int i = 1; i <=... |
2121 | G | Gangsta | You are given a binary string $s_1s_2 \ldots s_n$ of length $n$. A string $s$ is called binary if it consists only of zeros and ones.
For a string $p$, we define the function $f(p)$ as the maximum number of occurrences of any character in the string $p$. For example, $f(00110) = 3$, $f(01) = 1$.
You need to find the ... | Let $c_0$ be the number of zeros in the subsegment, and $c_1$ be the number of ones. Notice that $\max(c_0, c_1) = \frac{c_0 + c_1 + |c_0 - c_1|}{2}$. Then we will find the sum $c_0 + c_1 + |c_0 - c_1|$ over all subsegments, and this sum divided by two will be the answer. Let $pref_i$ be the difference between the numb... | [
"data structures",
"divide and conquer",
"math",
"sortings"
] | null | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const maxn = 2e5 + 5;
int pref[maxn];
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
ll ans = 0;
... |
2121 | H | Ice Baby | The longest non-decreasing subsequence of an array of integers $a_1, a_2, \ldots, a_n$ is the longest sequence of indices $1 \leq i_1 < i_2 < \ldots < i_k \leq n$ such that $a_{i_1} \leq a_{i_2} \leq \ldots \leq a_{i_k}$. The length of the sequence is defined as the number of elements in the sequence. For example, the ... | Let $dp[i][j]$ be the minimum number $x$ such that we can choose the values of the elements $a_1, a_2, \ldots, a_i$ in such a way that there exists a non-decreasing subsequence of length $j$ with the last element of the subsequence equal to $x$. If it is impossible to choose a subsequence of length $j$, we will conside... | [
"binary search",
"brute force",
"data structures",
"dp",
"implementation",
"sortings"
] | null | #include<bits/stdc++.h>
using namespace std;
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
multiset<int> dp;
for (int i = 1; i <= n; i++) {
int l, r;
cin >> l >> r;
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.