problem_id
stringlengths
4
4
problem_description
stringlengths
5
7.37k
c_code
stringlengths
87
16.5k
rust_code
stringlengths
101
4.89k
difficulty
stringclasses
3 values
0301
Alice and Bob like games. And now they are ready to start a new game. They have placed n chocolate bars in a line. Alice starts to eat chocolate bars one by one from left to right, and Bob — from right to left. For each chocololate bar the time, needed for the player to consume it, is known (Alice and Bob eat them with...
int solution() { long int n; long int n1 = 0; long int n2 = 0; long int i; scanf("%ld", &n); long int t[n]; for (i = 0; i < n; i++) { scanf("%ld", &t[i]); } long int *p = &t[0]; long int *q = &t[n - 1]; while (q >= p) { if (*p < *q) { *q -= *p; p++; n1++; if (p == q...
fn solution() { let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); let mut input = input.split_whitespace(); let n = read!(usize); let ts = read!([u64; n]); let mut ta = 0; let mut tb = 0; let mut ia = 0; let mut ib = n - 1; while ia <= ib { ...
medium
0302
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi is distributing <var>N</var> balls to <var>K</var> persons.</p> <p>If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received betw...
int solution() { int N = 0; int K = 0; int max = 0; scanf("%d %d\n", &N, &K); max = N - K; if (K == 1) { max = 0; } printf("%d", max); return 0; }
fn solution() { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); let nums: Vec<i32> = input .split_whitespace() .map(|x| x.parse().ok().unwrap()) .collect(); if nums[1] == 1 { println!("0"); } else { println!("{}", nums[0] - nums[1]);...
hard
0303
You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones. You can do the following operation: choose two indices $$$1 \le i , j \le n$$$, $$$i \ne j$$$, add $$$a_{i}$$$ to $$$a_{j}$$$, remove $$$a_{i}$$$ from $$$a$$$. Note that elements of $$$a$$$ can become bigger than $$$1$$$ after performing s...
int solution() { int n = 0; scanf("%d", &n); while (n--) { int x; int y = 0; int e = 0; int f = 0; int g = 0; int a[100000]; scanf("%d ", &x); for (int i = 0; i < x; i++) { scanf("%d", &a[i]); y++; if (a[i] == 1) { e++; } } for (int i = y - ...
fn solution() { let stdin = std::io::stdin(); let stdin_lock = stdin.lock(); let mut line_iter = stdin_lock.lines(); let t = line_iter .next() .unwrap() .unwrap() .as_str() .parse::<usize>() .unwrap(); for _ in 0..t { let n = line_iter.next().u...
hard
0304
The last contest held on Johnny's favorite competitive programming platform has been received rather positively. However, Johnny's rating has dropped again! He thinks that the presented tasks are lovely, but don't show the truth about competitors' skills.The boy is now looking at the ratings of consecutive participants...
int solution() { int t; long long n; long long twospower = 1; long long totalunfairness = 0; scanf("%d", &t); while (t) { scanf("%lld", &n); twospower = 1; totalunfairness = 0; while (n / twospower) { totalunfairness += n / twospower; twospower *= 2; } printf("%lld\...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let t: usize = itr.next().unwrap().parse().unwrap(); let mut out = Vec::new(); for _ in 0..t { let n: u64 = itr.next().unwrap().parse().unwrap(); l...
easy
0305
Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres.In the bookshop, Jack decides to buy two books of different genres.Based on the genre of books on sale in the shop, find the number...
int solution() { long long n = 0; long long m = 0; long long s = 0; scanf("%lld %lld", &n, &m); long long g[10] = {0}; long long g_; long long i = 0; for (i = 0; i < n; i++) { scanf("%lld", &g_); g[g_ - 1]++; } long long j = 0; for (i = 0; i < m - 1; i++) { for (j = i + 1; j < m; j+...
fn solution() { let mut buffer = String::new(); buffer.reserve(65536); let stdin = io::stdin(); let mut handle = stdin.lock(); let (n, m) = { handle.read_line(&mut buffer); let mut words = buffer.split_whitespace(); let n = words.next().unwrap().parse::<usize>().unwrap(); ...
hard
0306
There are two rival donut shops.The first shop sells donuts at retail: each donut costs $$$a$$$ dollars.The second shop sells donuts only in bulk: box of $$$b$$$ donuts costs $$$c$$$ dollars. So if you want to buy $$$x$$$ donuts from this shop, then you have to buy the smallest number of boxes such that the total numbe...
int solution() { int t; scanf("%d", &t); long long int a[t]; long long int b[t]; long long int c[t]; for (int i = 0; i < t; i++) { scanf("%lld%lld%lld", &a[i], &b[i], &c[i]); } int x[t]; int y[t]; for (int i = 0; i < t; i++) { if (a[i] * b[i] == c[i]) { x[i] = b[i] - 1; if (x[i] ...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); for _ in 0..t { let xs: Vec<i64> = lines .next() .unwrap() .unwrap() .split(' ') .map(|x| x.p...
easy
0307
Given an integer $$$n$$$, find the maximum value of integer $$$k$$$ such that the following condition holds: $$$n$$$ &amp; ($$$n-1$$$) &amp; ($$$n-2$$$) &amp; ($$$n-3$$$) &amp; ... ($$$k$$$) = $$$0$$$ where &amp; denotes the bitwise AND operation.
int solution() { int numTests = 0; int test = 1; scanf("%d", &numTests); while (test <= numTests) { unsigned long int n = 0; unsigned long int k = 1; scanf("%lu", &n); while (n >>= 1) { k <<= 1; } printf("%lu\n", k - 1); test++; } return 0; }
fn solution() { let mut buf = BufWriter::new(io::stdout().lock()); io::stdin() .lines() .skip(1) .flatten() .flat_map(|s| s.parse::<u32>()) .for_each(|n| { let ans = 1u32 .wrapping_shl(31u32.wrapping_sub(n.leading_zeros())) .wra...
hard
0308
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most <var>X</var> days earlier. He gets a stomachache if the "best-by" date of the food is <v...
int solution() { int a[4]; scanf("%d%d%d", &a[0], &a[1], &a[2]); a[1] *= -1; if (a[1] + a[2] <= 0) { printf("delicious"); } else if (a[1] + a[2] > a[0]) { printf("dangerous"); } else { printf("safe"); } }
fn solution() { let stdin = io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).unwrap(); let xab: Vec<isize> = buf.split_whitespace().map(|n| n.parse().unwrap()).collect(); if -xab[0] <= xab[1] - xab[2] { if xab[1] - xab[2] >= 0 { println!("delicious"); ...
easy
0309
All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of $$$n\cdot m$$$ different seals, denoted by distinct numbers. All of them were written in an $$$n\times m$$$ table.The table is lost now. Naruto managed to remember elements of each row from left ...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int m; scanf("%d%d", &n, &m); int ar[n][m]; int br[n]; int tmp[m][n]; for (int i = 0; i < n; i++) { for (int ii = 0; ii < m; ii++) { scanf("%d", &ar[i][ii]); } } for (int i = 0; i < m; i++) { ...
fn solution() { let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer).unwrap(); let mut lines = buffer.lines().peekable(); let t: usize; { t = lines.next().unwrap().parse().unwrap(); } let mut result: Vec<&str> = Vec::new(); for _ in 0..t { let mut b ...
medium
0310
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>How many hours do we have until New Year at <var>M</var> o'clock (24-hour notation) on <var>30</var>th, December?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤M...
int solution() { int m = 0; scanf("%d", &m); printf("%d\n", 48 - m); return 0; }
fn solution() { let stdin = io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).expect(""); let m: i32 = buf.trim().parse().expect(""); println!("{}", 24 + 24 - m); }
medium
0311
There is a new attraction in Singapore Zoo: The Infinite Zoo.The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled $$$1,2,3,\ldots$$$. There is a directed edge from vertex $$$u$$$ to vertex $$$u+v$$$ if and only if $$$u\&amp;v=v$$$, where $$$\&amp;$$$ denotes the bitwise AND operati...
int solution() { int q; scanf("%d", &q); while (q--) { int i; int j; int yes; scanf("%d%d", &i, &j); yes = 0; if (i <= j) { yes = 1; while (j != 0) { if (i == 0 || (i & -i) > (j & -j)) { yes = 0; break; } i &= i - 1, j &= j - 1; ...
fn solution() -> std::io::Result<()> { use std::io::Read; let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf)?; let mut itr = buf.split_whitespace(); use std::io::Write; let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); let q = scan!...
medium
0312
<h1>Problem A: AOJ50M</h1> <h2>Problem</h2> <p> AliceとBobが50m走で勝負しています。<br> しかし、この世界においてはAOJのレートが高い方が偉いので、AOJのレートが高い方が勝ちます。<br> どちらか一方でもAOJのレートが無い場合は比べようが無いので、仕方なく50m走のタイムで勝負します。この場合はタイムが短い方を勝ちとします。<br> AOJのレートが同じ場合は引き分け、50m走のタイムで勝負しなければならない場合はタイムが同じなら引き分けです。<br> </p> <h2>Input</h2> <p>入力は以下の形式で与えられる。</p> <p...
int solution(void) { const char *A = "Alice"; const char *B = "Bob"; const char *D = "Draw"; int T1; int T2; int R1; int R2; if (scanf("%d%d%d%d", &T1, &T2, &R1, &R2) != 4) { return 1; } if (R1 < 0 || R2 < 0) { puts(T1 < T2 ? A : (T1 > T2 ? B : D)); } else { puts(R1 > R2 ? A : (R1 < R2...
fn solution() { let mut stdin = io::stdin(); let mut buf = String::new(); let _ = stdin.read_to_string(&mut buf); let inputs: Vec<i16> = buf.split_whitespace().map(|s| s.parse().unwrap()).collect(); let answer = if inputs[2] >= 0 && inputs[3] >= 0 { inputs[2].cmp(&inputs[3]) } else { ...
easy
0313
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>N</var> ID cards, and there are <var>M</var> gates.</p> <p>We can pass the <var>i</var>-th gate if we have one of the following ID cards: the <var>L_i</var>-th, <var>(L_i+1)</var>-th, ..., ...
int solution() { int n = 0; int m = 0; scanf("%d %d\n", &n, &m); int l = 1; int r = n; int buf1 = 0; int buf2 = 0; for (int i = 0; i < m; i++) { scanf("%d %d", &buf1, &buf2); if (buf1 > l) { l = buf1; } if (buf2 < r) { r = buf2; } if (r < l) { printf("0\n"); ...
fn solution() { let mut nm = String::new(); stdin().read_line(&mut nm).unwrap(); let n: i32 = nm.split_whitespace().collect::<Vec<&str>>()[0] .parse() .unwrap(); let m: i32 = nm.split_whitespace().collect::<Vec<&str>>()[1] .parse() .unwrap(); let mut tl = 0; let ...
medium
0314
On a strip of land of length $$$n$$$ there are $$$k$$$ air conditioners: the $$$i$$$-th air conditioner is placed in cell $$$a_i$$$ ($$$1 \le a_i \le n$$$). Two or more air conditioners cannot be placed in the same cell (i.e. all $$$a_i$$$ are distinct).Each air conditioner is characterized by one parameter: temperatur...
int solution() { int q; scanf("%d", &q); while (q--) { long long int n; long long int k; scanf("%lld%lld", &n, &k); long long int ans[n]; long long int temps[k][2]; for (long long int i = 0; i < k; i++) { scanf("%lld", &temps[i][0]); } for (long long int i = 0; i < k; i++) {...
fn solution() { let std_in = stdin(); let in_lock = std_in.lock(); let input = BufReader::new(in_lock); let std_out = stdout(); let out_lock = std_out.lock(); let mut output = BufWriter::new(out_lock); let mut lines = input.lines().map(|r| r.unwrap()); let s = lines.next().unwrap(); ...
hard
0315
Polycarp doesn't like integers that are divisible by $$$3$$$ or end with the digit $$$3$$$ in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.Polycarp starts to write out the positive (greater than $$$0$$$) integers which he likes: $$$1, 2, 4, 5, 7, 8, 10, 11, 14, 16, \dot...
int solution() { int t = 0; int i = 0; scanf("%d", &t); getchar(); while (i < t) { int k = 0; scanf("%d", &k); getchar(); int flag = 0; int a = 0; while (flag < k) { a++; if (a % 3 != 0 && a % 10 != 3) { flag++; } } printf("%d\n\n", a); i++; } ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let t = s.trim().parse::<i32>().unwrap(); let mut arr: Vec<i32> = Vec::new(); for i in 1..2000 { if i % 3 != 0 && i % 10 != 3 { arr.push(i); } } for _ in 0..t { s = Strin...
easy
0316
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A <em>tetromino</em> is a figure formed by joining four squares edge to edge. We will refer to the following seven kinds of tetromino as I-, O-, T-, J-, L-, S- and Z-tetrominos, respectively:</p> <div s...
int solution() { int i; long long a[7]; long long ans; for (i = 0; i < 7; i++) { scanf("%lld", &(a[i])); } if (a[0] % 2 + a[3] % 2 + a[4] % 2 >= 2 && a[0] > 0 && a[3] > 0 && a[4] > 0) { ans = 3; a[0]--; a[3]--; a[4]--; } else { ans = 0; } ans += a[0] / 2 * 2 + a[1] + a[3] / 2 *...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let I: usize = itr.next().unwrap().parse().unwrap(); let O: usize = itr.next().unwrap().parse().unwrap(); let _: usize = itr.next().unwrap().parse().unwrap(); ...
hard
0317
Phoenix has a string $$$s$$$ consisting of lowercase Latin letters. He wants to distribute all the letters of his string into $$$k$$$ non-empty strings $$$a_1, a_2, \dots, a_k$$$ such that every letter of $$$s$$$ goes to exactly one of the strings $$$a_i$$$. The strings $$$a_i$$$ do not need to be substrings of $$$s$$$...
int solution() { int t; scanf("%d", &t); int n; int k; int i; char s[100005]; int cnt[30]; int c; for (; t > 0; t--) { scanf("%d %d", &n, &k); scanf("%s", s); for (i = 0; i < 30; i++) { cnt[i] = 0; } for (i = 0; s[i] != '\0'; i++) { cnt[s[i] - 'a']++; } i = 0; ...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); for _ in 0..t { let xs: Vec<usize> = lines .next() .unwrap() .unwrap() .split(' ') .map(|x| x...
medium
0318
Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid.Each test case consists of an integer $$$n$$$ and two arrays $$$a$$$ and $$$b$$$, of size $$$n$$$. If after some (possibly zero) operations described below, array $$$a$$$ can be tr...
int solution() { int t; scanf("%d", &t); int n; int i; int j; int a[502]; int b[502]; int u[502]; int f; int g; for (; t > 0; t--) { scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i < n; i++) { scanf("%d", &b[i]); } for (i = 0; i < ...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); for _ in 0..t { let n: usize = lines.next().unwrap().unwrap().parse().unwrap(); let xs: Vec<usize> = lines .next() .unwra...
medium
0319
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>The <em>ABC number</em> of a string <var>T</var> is the number of triples of integers <var>(i, j, k)</var> that satisfy all of the following conditions:</p> <ul> <li><var>1 ≤ i &lt; j &lt; k ≤ |T|</var>...
int solution(void) { int mod = 1000000007; char s[100020]; scanf("%s", s); long long ans[strlen(s) + 1][4]; ans[strlen(s)][3] = 1, ans[strlen(s)][2] = 0, ans[strlen(s)][1] = 0, ans[strlen(s)][0] = 0; for (int i = strlen(s) - 1; i >= 0; i--) { if (s[i] == '?') { ans[i][3] = (ans[i + 1][3] * 3) % ...
fn solution() { let mut s_str: String = String::new(); io::stdin().read_line(&mut s_str).expect("!!"); let s: Vec<char> = s_str.trim().chars().collect(); let N = s.len(); let mut dp: Vec<Vec<i64>> = vec![vec![0i64; 4]; N]; let abc = ['A', 'B', 'C']; for i in (0..N).rev() { for j in...
easy
0320
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi, who is <var>A</var> years old, is riding a Ferris wheel.</p> <p>It costs <var>B</var> yen (<var>B</var> is an even number) to ride the Ferris wheel if you are <var>13</var> years old or older...
int solution(void) { int a = 0; int b = 0; scanf("%d", &a); scanf("%d", &b); if (a <= 12 && a >= 6) { b = b / 2; } else if (a <= 5) { b = 0; } printf("%d", b); return 0; }
fn solution() { let mut ab = String::new(); stdin().read_line(&mut ab).unwrap(); let ab: Vec<isize> = ab.split_whitespace().flat_map(str::parse).collect(); let (a, b) = (ab[0], ab[1]); println!( "{}", match a { 0..=5 => 0, 6..=12 => b / 2, _ => b, ...
medium
0321
You are given three integers $$$a$$$, $$$b$$$ and $$$c$$$.Find two positive integers $$$x$$$ and $$$y$$$ ($$$x &gt; 0$$$, $$$y &gt; 0$$$) such that: the decimal representation of $$$x$$$ without leading zeroes consists of $$$a$$$ digits; the decimal representation of $$$y$$$ without leading zeroes consists of $$$b$$...
int solution() { int t; scanf("%d", &t); while (t-- > 0) { int a; int b; int c; scanf("%d %d %d", &a, &b, &c); long long int x = 1; long long int y = 1; long long int z = 1; for (int i = 0; i < a - 1; i++) { x = x * 10; } for (int i = 0; i < b - 1; i++) { y = y ...
fn solution() { let mut line = String::new(); stdin().read_line(&mut line).unwrap(); let t: i64 = line.trim().parse().unwrap(); for _ in 0..t { let mut line = String::new(); stdin().read_line(&mut line).unwrap(); let words: Vec<i64> = line .split_whitespace() ...
hard
0322
Shubham has an array $$$a$$$ of size $$$n$$$, and wants to select exactly $$$x$$$ elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.Tell him whether he can do so.
int solution() { int t = 1; scanf("%d", &t); while (t--) { int n; int x; int k; int ev = 0; int od = 0; int fl = 0; scanf("%d %d", &n, &x); for (int i = 0; i < n; i++) { scanf("%d", &k); if (k & 1) { od += 1; } else { ev += 1; } } ...
fn solution() { let mut buf = String::new(); stdin().read_line(&mut buf).unwrap(); let buf = buf.trim().parse::<u8>().unwrap(); for _ in 0..buf { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); let v: Vec<_> = s .trim() .split(' ') ...
hard
0323
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $$$s = s_1 s_2 \dots s_{n}$$$ of length $$$n$$$ where each letter is either R, S or P.While initializing, the bot is choosing a starting index $$$pos$$$ ($$$1 \le pos \le n$$$), an...
int solution() { int n = 0; scanf("%d", &n); char c = 'A'; char temp = '0'; scanf("%c", &temp); for (int i = 0; i < n; i++) { int p = 0; int r = 0; int s = 0; int x = 0; do { scanf("%c", &c); if (c == 'R') { p++; x++; } else if (c == 'P') { s++; ...
fn solution() { let stdin = io::stdin(); let mut input = stdin.lock(); let mut line = String::new(); input.read_line(&mut line).unwrap(); let mut count: i16 = line.trim().parse().unwrap(); while count > 0 { line.clear(); input.read_line(&mut line).unwrap(); let (mut rocks...
medium
0324
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits beg...
int solution() { int l[100000] = {0}; int r[100000] = {0}; int n = 0; scanf("%d", &n); int i = 0; scanf("%d %d", &l[0], &r[0]); int min = 0; int max = 0; int suml = l[0]; int sumr = r[0]; for (i = 1; i < n; i++) { scanf("%d %d", &l[i], &r[i]); suml += l[i]; sumr += r[i]; if (l[min...
fn solution() { let mut input_str = String::new(); io::stdin().read_line(&mut input_str).unwrap(); let col_count: u32 = input_str.trim().parse().unwrap(); let mut max_left_gap = 0; let mut max_right_gap = 0; let mut max_left_gap_index = 0; let mut max_right_gap_index = 0; let mut sum_le...
medium
0325
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given positive integers <var>A</var> and <var>B</var>.</p> <p>If <var>A</var> is a divisor of <var>B</var>, print <var>A + B</var>; otherwise, print <var>B - A</var>.</p> </section> </div> <div ...
int solution() { int N[2]; for (int i = 0; i <= 1; i++) { scanf("%d", &N[i]); } if (N[1] % N[0] == 0) { printf("%d", N[1] + N[0]); } else { printf("%d", N[1] - N[0]); } return 0; }
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).ok(); let vs: Vec<i64> = s .split_whitespace() .map(|token| token.parse().ok().unwrap()) .collect(); let a = vs[0]; let b = vs[1]; if b % a == 0 { println!("{}", a + b); } else { prin...
easy
0326
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is an integer <var>N</var>. Find the number of positive integers less than or equal to <var>N</var> that have an odd number of digits (in base ten without leading zeros).</p> </section> </div> <di...
int solution(void) { int N = 0; int len = 0; int count = 0; int i = 0; scanf("%d", &N); char str[10]; for (i = 1; i <= N; i++) { sprintf(str, "%d", i); len = strlen(str); if (len % 2 == 1) { count++; } } printf("%d", count); return 0; }
fn solution() { let stdin = io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).ok(); let num: usize = buf.trim().parse().unwrap(); let mut keta = 0; for i in 1..(num + 1) { if i.to_string().len() == 1 || i.to_string().len() == 3 || i.to_string().len() == 5 { ...
hard
0327
Let's call a positive integer $$$n$$$ ordinary if in the decimal notation all its digits are the same. For example, $$$1$$$, $$$2$$$ and $$$99$$$ are ordinary numbers, but $$$719$$$ and $$$2021$$$ are not ordinary numbers.For a given number $$$n$$$, find the number of ordinary numbers among the numbers from $$$1$$$ to ...
int solution() { int t = 0; scanf("%d", &t); while (t--) { int n = 0; scanf("%d", &n); int count = 0; int m = n; for (int i = 0; m >= 1; i++) { m = m / 10; count++; } int n_i = 0; int n1 = 0; for (int i = 0; i < count; i++) { n1 += pow(10, i); } n_i ...
fn solution() { let mut t = String::new(); io::stdin().read_line(&mut t).expect("Failed"); let mut t: i32 = t.trim().parse().expect("Failed"); while t != 0 { let mut n = String::new(); io::stdin().read_line(&mut n).expect("Failed"); let n: i128 = n.trim().parse().expect("Failed"...
easy
0328
<span class="lang-en"> <p>Score: <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>There is a farm whose length and width are <var>A</var> yard and <var>B</var> yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, ...
int solution(void) { int a = 0; int b = 0; scanf("%d %d", &a, &b); printf("%d", (a - 1) * (b - 1)); }
fn solution() { let stdin = io::stdin(); let mut s = String::new(); stdin.read_line(&mut s).ok(); s.pop(); let mut ab: Vec<i32> = Vec::new(); let it = s.split_whitespace(); for arg in it { ab.push(i32::from_str(arg).expect("e")); } let a = ab[0]; let b = ab[1]; printl...
medium
0329
Consider a tunnel on a one-way road. During a particular day, $$$n$$$ cars numbered from $$$1$$$ to $$$n$$$ entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds.A traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted ...
int solution() { int n; scanf("%d", &n); int input[n]; int output[n]; int index[n + 1]; for (int i = 0; i < n; ++i) { scanf("%d", &input[i]); index[input[i]] = i; } for (int i = 0; i < n; ++i) { scanf("%d", &output[i]); } int find = 0; int finedcars = 0; int i = 0; while (i < n && ...
fn solution() -> io::Result<()> { let stdin = io::stdin(); let mut stdin = stdin.lock(); let mut buf = String::new(); stdin.read_line(&mut buf)?; let n: usize = buf.trim().parse().unwrap(); buf.clear(); stdin.read_line(&mut buf)?; let mut a: Vec<usize> = buf .split_whitespace(...
hard
0330
You are given an array $$$a_1, a_2, \dots , a_n$$$, which is sorted in non-decreasing order ($$$a_i \le a_{i + 1})$$$. Find three indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$1 \le i &lt; j &lt; k \le n$$$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to...
int solution() { int t = 0; scanf("%d\n", &t); while (t-- > 0) { int n = 0; scanf("%d\n", &n); unsigned int a = 0; unsigned int b = 0; unsigned int c = 0; scanf("%u", &a); scanf("%u", &b); a += b; bool found = false; for (int i = 3; i <= n; ++i) { scanf("%u", &c); ...
fn solution() { let t: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().parse().unwrap() }; for _ in 0..t { let _n: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(...
medium
0331
<span class="lang-en"> <p>Score : <var>66</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p> Summer vacation ended at last and the second semester has begun. You, a Kyoto University student, came to university and heard a rumor that somebody will barricade the entrance of your classroom. ...
int solution() { int n; int a; int b; int t; int c = 0; scanf("%d %d %d", &n, &a, &b); while (n--) { scanf("%d", &t); if (a <= t && t < b) { continue; } c++; } printf("%d\n", c); return 0; }
fn solution() { let mut s = String::new(); use std::io::Read; std::io::stdin().read_to_string(&mut s).unwrap(); let mut s = s.split_whitespace(); let n: usize = s.next().unwrap().parse().unwrap(); let a: i32 = s.next().unwrap().parse().unwrap(); let b: i32 = s.next().unwrap().parse().unwrap(...
medium
0332
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> balls in the <var>xy</var>-plane. The coordinates of the <var>i</var>-th of them is <var>(x_i, i)</var>. Thus, we have one ball on each of the <var>N</var> lines <var>y = 1</var>,...
int solution(void) { int N; int K = 0; int x = 0; int ans = 0; int tmp = 0; scanf("%d", &N); scanf("%d", &K); for (int i = 0; i < N; i++) { scanf("%d", &x); tmp = K - x; ans += 2 * ((x > tmp) ? tmp : x); } printf("%d\n", ans); return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let _n: i32 = s.trim().parse().ok().unwrap(); let mut t = String::new(); std::io::stdin().read_line(&mut t).ok(); let k: i32 = t.trim().parse().ok().unwrap(); let mut u = String::new(); std::io::stdin().r...
medium
0333
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let <var>f(A, B)</var> be the exclusive OR of <var>A, A+1, ..., B</var>. Find <var>f(A, B)</var>.</p> <p><details> <summary style="display: list-item; outline: none;">What is exclusive OR?</summary></de...
int solution() { long a; long b; scanf("%ld %ld", &a, &b); long ans = 0; if (a & 1 && b & 1) { ans = (b - a) / 2 % 2 ^ a; } else if (a & 1 && (b & 1) == 0) { ans = (b - a - 1) / 2 % 2 ^ a ^ b; } else if ((a & 1) == 0 && b & 1) { ans = (b - a + 1) / 2 % 2; } else { ans = (b - a) / 2 % 2 ^...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let a: u64 = itr.next().unwrap().parse().unwrap(); let b: u64 = itr.next().unwrap().parse().unwrap(); let arem: u64 = a % 4; let adiv: u64 = a / 4; let br...
hard
0334
<h1>B: 直角三角形</h1> <h2>問題</h2> <p> 直角三角形の斜辺でない $2$ 辺の長さ $A$ , $B$ が与えられる。 長さ $A$ の辺は $x$ 軸と重なっており、長さ $B$ の辺は $y$ 軸と重なっている。 </p> <p> 次の操作を行う。 </p> <p> <ol> <li>三角形を $x$ 軸周りに回転させる。</li> <li>操作 $1$ を行ってできた図形を $y$ 軸周りに回転させる。</li> </ol> </p> <p> 操作 $2$ を行ってできた図形の体積を求めよ。 </p> <h2>制約</h2> <ul> ...
int solution() { int r; scanf("%*d%d", &r); printf("%.9f\n", 4 * 3.141592653589 / 3 * r * r * r); }
fn solution() { input!(a: f64, b: f64); println!("{}", b * b * b * consts::PI * 4.0 / 3.0); }
medium
0335
<H1>Counting Characters</H1><br> <p> Write a program which counts and reports the number of each alphabetical letter. Ignore the case of characters. </p> <H2>Input</H2> <p> A sentence in English is given in several lines. </p> <H2>Output</H2> <p> Prints the number of alphabetical letters in the following fo...
int solution() { int str[26] = {0}; int i = 0; char word[1205]; while (scanf("%c", &word[i++]) != EOF) { ; } for (int j = 0; j < i; j++) { if (word[j] >= 'A' && word[j] <= 'Z') { word[j] += 32; } if (word[j] >= 'a' && word[j] <= 'z') { str[word[j] - 'a']++; } } for (i = 0...
fn solution() { let mut buffer = String::new(); let stdin = io::stdin(); stdin.lock().read_to_string(&mut buffer).ok(); let s = buffer.trim().to_lowercase(); let alphabets = (b'a'..b'z' + 1).map(|c| c as char); let nums = alphabets.map(|a| (a, s.chars().filter(|&c| c == a).count())); for n...
medium
0336
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given strings <var>S</var> and <var>T</var> consisting of lowercase English letters.</p> <p>You can perform the following operation on <var>S</var> any number of times:</p> <p>Operation: Choose ...
int solution(void) { char S[200001]; char T[200001]; memset(&S, '\0', sizeof(S)); memset(&T, '\0', sizeof(T)); scanf("%s", S); scanf("%s", T); int size = strlen(S); char alpha[26]; memset(&alpha, '\0', sizeof(alpha)); int flag = 0; for (int i = 0; i < size; i++) { if (alpha[T[i] - 'a'] == '\...
fn solution() { let mut stdin = String::new(); std::io::Read::read_to_string(&mut std::io::stdin(), &mut stdin).unwrap(); let mut stdin = stdin.split_whitespace(); let mut get = || stdin.next().unwrap(); let s = get().as_bytes(); let t = get().as_bytes(); if s.len() != t.len() { pr...
medium
0337
<span class="lang-en"> <p>Score: <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>There are three positive integers <var>A</var>, <var>B</var> and <var>C</var> written on a blackboard. E869120 performs the following operation <var>K</var> times:</p> <ul> <li>Choose one integer writte...
int solution() { int a = 0; int b = 0; int c = 0; int k = 0; int total = 0; scanf("%d %d %d", &a, &b, &c); scanf("%d", &k); for (int i = 0; i < k; i++) { if (a > b) { if (a > c) { a = 2 * a; } else { c = 2 * c; } } else { if (b > c) { b = 2 *...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut buf = buf.split_whitespace(); let mut a: u32 = buf.next().unwrap().parse().unwrap(); let mut b: u32 = buf.next().unwrap().parse().unwrap(); let mut c: u32 = buf.next().unwrap().parse().unwrap(); l...
easy
0338
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular (shortly, RBS) if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are RBS and ")(" and ...
int solution() { int n; int i; scanf("%d ", &n); char bracket_sequence[n]; int opening_bracket[n]; int closing_bracket[n]; for (i = 0; i < n; i++) { scanf("%c", &bracket_sequence[i]); opening_bracket[i] = closing_bracket[i] = 0; } int value = 0; for (i = 0; i < n; i++) { if (bracket_sequ...
fn solution() { let mut buffer = String::new(); let stdin = io::stdin(); let mut handle = stdin.lock(); handle.read_to_string(&mut buffer).expect("stdin"); let arr: Vec<_> = buffer.split_whitespace().collect(); let n: usize = arr.get(0).expect("n").parse::<usize>().expect("n"); let brack...
easy
0339
Arkady's morning seemed to be straight of his nightmare. He overslept through the whole morning and, still half-asleep, got into the tram that arrived the first. Some time after, leaving the tram, he realized that he was not sure about the line number of the tram he was in.During his ride, Arkady woke up several times ...
int solution(void) { int n; scanf("%d", &n); int a[n][105]; int b[n]; for (int i = 0; i < n; i++) { scanf("%d", &b[i]); for (int j = 0; j < b[i]; j++) { scanf("%d", &a[i][j]); } } int c = 0; int ans = 0; int flag = 0; for (int i = 0; i < b[0]; i++) { for (int j = 1; j < n; j+...
fn solution() { let mut stdin = String::new(); std::io::Read::read_to_string(&mut std::io::stdin(), &mut stdin).unwrap(); let mut stdin = stdin.split_whitespace(); let mut get = || stdin.next().unwrap(); let n = get!(); let mut xs = vec![0; 101]; for _ in 0..n { let r = get!(); ...
hard
0340
<H1>Range</H1> <p> Write a program which reads three integers <var>a</var>, <var>b</var> and <var>c</var>, and prints "Yes" if <var>a &lt; b &lt; c</var>, otherwise "No". </p> <H2>Input</H2> <p> Three integers <var>a</var>, <var>b</var> and <var>c</var> separated by a single space are given in a line. </p> <H2...
int solution() { int a = 0; int b = 0; int c = 0; printf(""); scanf("%d %d %d", &a, &b, &c); while (1) { if (a >= 0 && a <= 100 && b >= 0 && b <= 100 && c >= 0 && c <= 100) { break; } printf(""); scanf("%d %d", &a, &b); } if (a < b && b < c) { printf("Yes\n"); } else { ...
fn solution() { let scan = std::io::stdin(); let mut line = String::new(); let _ = scan.read_line(&mut line); let vec: Vec<&str> = line.split_whitespace().collect(); let a: i32 = vec[0].parse().unwrap_or(0); let b: i32 = vec[1].parse().unwrap_or(0); let c: i32 = vec[2].parse().unwrap_or(0...
medium
0341
You are a coach of a group consisting of $$$n$$$ students. The $$$i$$$-th student has programming skill $$$a_i$$$. All students have distinct programming skills. You want to divide them into teams in such a way that: No two students $$$i$$$ and $$$j$$$ such that $$$|a_i - a_j| = 1$$$ belong to the same team (i.e. skil...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int a[n]; int ans = 1; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (abs(a[j] - a[k]) == 1) { ans = 2; ...
fn solution() { let reader = io::stdin(); let mut val = String::new(); io::stdin() .read_line(&mut val) .expect("Не удалось прочитать строку"); let val: u32 = val.trim().parse().expect("Пожалуйста, введите число!"); for _ in 0..val { let mut val = String::new(); i...
medium
0342
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi is standing on a two-dimensional plane, facing north. Find the minimum positive integer <var>K</var> such that Takahashi will be at the starting position again after he does the following acti...
int solution(void) { int x = 0; int k = 0; int sumx = 0; scanf("%d", &x); do { sumx += x; k++; } while (sumx % 360); printf("%d", k); return 0; }
fn solution() { let mut buf = String::new(); std::io::stdin() .read_line(&mut buf) .expect("failed to read"); let x: u32 = buf.trim().parse().unwrap(); let mut i: u32 = 1; loop { if (360 * i).is_multiple_of(x) { println!("{}", 360 * i / x); break; ...
medium
0343
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given are two strings <var>S</var> and <var>T</var>.</p> <p>Let us change some of the characters in <var>S</var> so that <var>T</var> will be a substring of <var>S</var>.</p> <p>At least how many charac...
int solution(void) { char s[1001] = {'\0'}; char t[1001] = {'\0'}; int l_s = 0; int l_t = 0; int max_match = 0; scanf("%s", s); scanf("%s", t); while (s[l_s] != '\0') { l_s++; } while (t[l_t] != '\0') { l_t++; } for (int i = 0; i < l_s - l_t + 1; i++) { int match = 0; for (int j ...
fn solution() -> Result<()> { let mut s = String::new(); let ls = io::stdin().read_line(&mut s)? - 1; let mut t = String::new(); let lt = io::stdin().read_line(&mut t)? - 1; let mut m = 1001; for i in 0..=(ls - lt) { let mut res = 0; for (x, y) in t.chars().zip(s[i..i + lt].char...
medium
0344
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given <var>N</var> positive integers <var>a_1, a_2, ..., a_N</var>.</p> <p>For a non-negative integer <var>m</var>, let <var>f(m) = (m\ mod\ a_1) + (m\ mod\ a_2) + ... + (m\ mod\ a_N)</var>.</p>...
int solution() { int N = 0; scanf("%d", &N); long sum = 0; for (int i = 0; i < N; i++) { int j = 0; scanf("%d", &j); sum += j - 1; } printf("%ld\n", sum); return 0; }
fn solution() { use std::io::Read; let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let n: usize = iter.next().unwrap().parse().unwrap(); let a: Vec<u64> = (&mut iter).take(n).map(|x| x.parse().unwrap()).collect(); prin...
hard
0345
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.This abbreviation is made like th...
int solution() { char a[10000][10000]; int n = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", &a[i][10000]); } for (int i = 0; i <= n; i++) { int m = strlen(a[i]); if (m > 10) { printf("%c%d%c\n", a[i][0], (m - 2), a[i][m - 1]); } else { printf("%s\n", a[i]); ...
fn solution() { let mut input = String::new(); io::stdin().read_line(&mut input).expect(""); let mut n = input.trim().parse::<i32>().unwrap(); while n > 0 { let mut word = String::new(); io::stdin().read_line(&mut word).expect(""); if word.len() > 12 { println!( ...
hard
0346
Did you know you can download more RAM? There is a shop with $$$n$$$ different pieces of software that increase your RAM. The $$$i$$$-th RAM increasing software takes $$$a_i$$$ GB of memory to run (temporarily, once the program is done running, you get the RAM back), and gives you an additional $$$b_i$$$ GB of RAM (per...
int solution() { int n = 0; scanf("%d", &n); while (n--) { int m = 0; int ram = 0; int a[1001] = {0}; int b[1001] = {0}; int c = 0; scanf("%d%d", &m, &ram); for (int i = 1; i <= m; ++i) { scanf("%d", &a[i]); } for (int i = 1; i <= m; ++i) { scanf("%d", &c); b[...
fn solution() { let mut input = String::new(); stdin().read_to_string(&mut input).unwrap(); let mut input = input.split_ascii_whitespace().flat_map(str::parse); let t = input.next().unwrap(); let mut output = String::new(); for _ in 0..t { let n = input.next().unwrap(); let mut k...
hard
0347
There is a special offer in Vasya's favourite supermarket: if the customer buys $$$a$$$ chocolate bars, he or she may take $$$b$$$ additional bars for free. This special offer can be used any number of times.Vasya currently has $$$s$$$ roubles, and he wants to get as many chocolate bars for free. Each chocolate bar cos...
int solution() { int t; long long int price; long long int offer; long long int mon; long long int free; long long int div; long long int ctr = 0; scanf("%d", &t); while (t--) { scanf("%lld%lld%lld%lld", &mon, &offer, &free, &price); ctr = 0; div = (mon / (offer * price)); ctr += (div ...
fn solution() -> io::Result<()> { let mut buf = String::new(); stdin().read_line(&mut buf)?; let times = buf.trim().parse::<u64>().unwrap(); for _ in 0..times { buf.clear(); stdin().read_line(&mut buf)?; let arr = Vec::from_iter(buf.split_whitespace()); let mut b = arr[0...
medium
0348
Binary insertion sort
void solution(int *arr, int size) { for (int i = 1; i < size; i++) { int key = arr[i]; int low = 0; int high = i - 1; int index; while (low <= high) { int mid = low + ((high - low) / 2); if (arr[mid] == key) { index = mid + 1; break; } if (arr[mid] < key) ...
fn solution<T: Ord + Clone>(arr: &mut [T]) { let len = arr.len(); for i in 1..len { let key = arr[i].clone(); let mut low = 0; let mut high = i; while low < high { let mid = low + (high - low) / 2; if arr[mid] < key { low = mid + 1; ...
easy
0349
You are given two integers $$$l$$$ and $$$r$$$, where $$$l &lt; r$$$. We will add $$$1$$$ to $$$l$$$ until the result is equal to $$$r$$$. Thus, there will be exactly $$$r-l$$$ additions performed. For each such addition, let's look at the number of digits that will be changed after it.For example: if $$$l=909$$$, th...
int solution() { int t; scanf("%d", &t); int l[100000]; int r[100000]; for (int i = 0; i < t; i++) { scanf("%d%d", &l[i], &r[i]); } for (int i = 0; i < t; i++) { int ans = 0; while (l[i] != 0 || r[i] != 0) { ans += r[i] - l[i]; l[i] /= 10; r[i] /= 10; } printf("%d\n",...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let mut nv: Vec<usize> = Vec::new(); let t = s.trim().parse().unwrap(); for _aerfresdf in 0..t { s = String::new(); io::stdin().read_line(&mut s).unwrap(); let v = s.split_whitespace(); ...
easy
0350
Bob is playing with $$$6$$$-sided dice. A net of such standard cube is shown below.He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.For examp...
int solution() { int n; scanf("%d", &n); long long int a[n]; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } for (int j = 0; j < n; j++) { long long int x = (a[j] / 14); if (0 < a[j] && a[j] < 7) { printf("NO\n"); continue; } if (((14 * x) - 13) <= a[j] && a[j] <= (((1...
fn solution() { let reader = io::stdin(); let _lucky_number_count = reader .lock() .lines() .next() .unwrap() .unwrap() .parse::<i32>() .unwrap(); let numbers: Vec<u64> = reader .lock() .lines() .next() .unwrap() ...
easy
0351
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>At an arcade, Takahashi is playing a game called <em>RPS Battle</em>, which is played as follows:</p> <ul> <li>The player plays <var>N</var> rounds of Rock Paper Scissors against the machine. (See Notes...
int solution() { int N; int K; int R; int S; int P; char T[100000]; char *ptr; int point; scanf("%d %d", &N, &K); scanf("%d %d %d", &R, &S, &P); scanf("%s", T); ptr = T; for (int i = 0; i < N; i++) { switch (*ptr) { case 'r': if (i >= K && *(ptr - K) == 'r') { T[i] = '...
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let n: usize = iter.next().unwrap().parse().unwrap(); let k: usize = iter.next().unwrap().parse().unwrap(); let mut buf = String::new(); std::io::stdin().read_l...
medium
0352
This is the easy version of the problem. The difference is that in this version the array can not contain zeros. You can make hacks only if both versions of the problem are solved.You are given an array $$$[a_1, a_2, \ldots a_n]$$$ consisting of integers $$$-1$$$ and $$$1$$$. You have to build a partition of this array...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int sum = 0; scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } if (n % 2 != 0) { printf("-1\n"); continue; } for (int i = 0; i < n; i += 2) { if (a[i] == a[i + 1])...
fn solution() -> std::io::Result<()> { let mut buf = String::new(); let sin = std::io::stdin(); sin.read_line(&mut buf)?; let mut t = buf.trim().parse::<i32>().unwrap(); let sout = std::io::stdout().lock(); let mut sout = std::io::BufWriter::new(sout); let mut res = Vec::new(); while t ...
easy
0353
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>As a New Year's gift, Dolphin received a string <var>s</var> of length <var>19</var>.<br/> The string <var>s</var> has the following format: <code>[five lowercase English letters],[seven lowercase Engli...
int solution() { char *v1 = malloc(5 * sizeof(char)); char *v2 = malloc(7 * sizeof(char)); char *v3 = malloc(5 * sizeof(char)); scanf("%5[^,],%7[^,],%5[^,]", v1, v2, v3); printf("%s %s %s\n", v1, v2, v3); free(v1); free(v2); free(v3); return 0; }
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).ok(); println!("{}", s.replace(",", " ")); }
hard
0354
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the mi...
int solution() { long t; scanf("%ld", &t); while (t--) { long n; long ans = 0; long con = 1; scanf("\n%ld", &n); char str[n]; scanf("%s", str); short f = 0; for (long a = 1; a < n; a++) { if (str[a] == str[a - 1]) { con++; } else { if (con % 2 == 1 && f ...
fn solution() { let stdin = std::io::stdin(); let mut inputs = String::new(); stdin.lock().read_to_string(&mut inputs).ok(); let mut scanner = inputs.split_whitespace(); let case = scanner.next().unwrap().parse::<usize>().unwrap(); for _ in 0..case { let _ = scanner.next(); le...
medium
0355
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke lives at position <var>x</var> on a number line. On this line, there are two stores <var>A</var> and <var>B</var>, respectively at position <var>a</var> and <var>b</var>, that offer food for deliv...
int solution(void) { int x = 0; int a = 0; int b = 0; int disA = 0; int disB = 0; scanf("%d %d %d", &x, &a, &b); disA = x - a; if (disA < 0) { disA *= -1; } disB = x - b; if (disB < 0) { disB *= -1; } if (disA < disB) { printf("A\n"); } else { printf("B\n"); } return 0;...
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let v: Vec<i32> = s .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect(); let x = v[0]; let a = v[1]; let b = v[2]; if (a - x).abs() < (b - x).abs() { println!("A"...
medium
0356
According to a new ISO standard, a flag of every country should have a chequered field n × m, each square should be of one of 10 colours, and the flag should be «striped»: each horizontal row of the flag should contain squares of the same colour, and the colours of adjacent horizontal rows should be different. Berland'...
int solution() { int i = 0; int j = 0; int k = 0; int m = 0; int n = 0; int aux = 0; int var = 0; scanf("%d %d", &n, &m); char arr[n][m]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf(" %c", &arr[i][j]); } } for (j = 0; j < m; j++) { for (k = 1; k < m; k++) { ...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let mut line_words = line.split_whitespace(); let n: u8 = line_words.next().unwrap().parse().unwrap(); let mut last_color = '*'; let mut res = true; for _ in 0..n { line.clear(); io::st...
hard
0357
Polycarp has spent the entire day preparing problems for you. Now he has to sleep for at least $$$a$$$ minutes to feel refreshed.Polycarp can only wake up by hearing the sound of his alarm. So he has just fallen asleep and his first alarm goes off in $$$b$$$ minutes.Every time Polycarp wakes up, he decides if he wants ...
int solution(void) { int t; int i; scanf("%d", &t); long long a[t]; long long b[t]; long long c[t]; long long d[t]; long long r[t]; for (i = 0; i < t; ++i) { scanf("%lld%lld%lld%lld", &a[i], &b[i], &c[i], &d[i]); } for (i = 0; i < t; ++i) { if (a[i] <= b[i]) { r[i] = b[i]; } else...
fn solution() { let mut t = String::new(); io::stdin().read_line(&mut t).unwrap(); let t: u16 = t.trim().parse().unwrap(); let mut output: Vec<i64> = Vec::new(); for _ in 0..t { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); let mut iter = input....
easy
0358
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a rectangular parallelepiped of size <var>A×B×C</var>, built with blocks of size <var>1×1×1</var>. Snuke will paint each of the <var>A×B×C</var> blocks either red or blue, so that:</p> <ul> <li>...
int solution(void) { long long int red = 0; long long int blue = 0; long long int high = 0; long long int lengh = 0; long long int deeper = 0; scanf("%lld %lld %lld", &high, &lengh, &deeper); if (high <= lengh) { if (lengh <= deeper) { red = high * lengh * (deeper / 2 + deeper % 2); blue...
fn solution() { let (A, B, C): (i64, i64, i64) = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ...
hard
0359
Emuskald needs a fence around his farm, but he is too lazy to build it himself. So he purchased a fence-building robot.He wants the fence to be a regular polygon. The robot builds the fence along a single path, but it can only make fence corners at a single angle a.Will the robot be able to build the fence Emuskald wan...
int solution() { int n; scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if ((360 % (180 - a[i])) == 0) { printf("YES\n"); } else { printf("NO\n"); } } }
fn solution() { let mut t = String::new(); io::stdin().read_line(&mut t).expect("read error"); let t: usize = t.trim().parse().expect("parse error"); for _i in 0..t { let mut a = String::new(); io::stdin().read_line(&mut a).expect("read error"); let a: usize = a.trim().parse...
easy
0360
Madoka's father just reached $$$1$$$ million subscribers on Mathub! So the website decided to send him a personalized award — The Mathhub's Bit Button! The Bit Button is a rectangular table with $$$n$$$ rows and $$$m$$$ columns with $$$0$$$ or $$$1$$$ in each cell. After exploring the table Madoka found out that: A sub...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int m; scanf("%d %d", &n, &m); int a[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%1d", &a[i][j]); } } int f = 1; for (int i = 0; f && i < n - 1; ++i) { for (int j =...
fn solution() { let stdin = io::stdin(); let mut stdin = stdin.lock(); let stdout = io::stdout(); let stdout_handle = stdout.lock(); let mut out_buffer = BufWriter::with_capacity(65536, stdout_handle); let mut line = String::new(); stdin.read_line(&mut line).unwrap(); let test_cases: us...
medium
0361
You have $$$n$$$ gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The $$$i$$$-th gift consists of $$$a_i$$$ candies and $$$b_i$$$ oranges.During one move, you can choose some gift $$$1 \le i \le n$$$ and do one of the follow...
int solution() { int t; scanf("%d", &t); while (t--) { long long int count = 0; int n; scanf("%d", &n); long long int a[n]; long long int b[n]; scanf("%lld", &a[0]); long long int min_a = a[0]; for (int i = 1; i < n; i++) { scanf("%lld", &a[i]); if (min_a > a[i]) { ...
fn solution() { let t: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().parse().unwrap() }; for _ in 0..t { let n: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap()...
medium
0362
Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light...
int solution() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); int n; int k; scanf("%d %d", &n, &k); int a[n]; int b[n]; int i = 0; int j; int t; for (i = 0; i < n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j...
fn solution() { let input = File::open("input.txt").unwrap(); let mut output = File::create("output.txt").unwrap(); let mut iter = io::BufReader::new(input).lines(); let tmp: Vec<usize> = iter .next() .unwrap() .ok() .unwrap() .split_whitespace() .map(|x...
hard
0363
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> cards. The <var>i</var>-th card has an integer <var>A_i</var> written on it. For any two cards, the integers on those cards are different.</p> <p>Using these cards, Takahashi and ...
int solution() { int i; int N; int Q; int A[100001]; scanf("%d %d", &N, &Q); for (i = 1; i <= N; i++) { scanf("%d", &(A[i])); } int q; int l[2]; int r[2]; int m; int X; int L; int R; int M; long long sum[2][100002] = {}; for (i = N, sum[0][N + 1] = 0; i > N / 2; i--) { sum[0][...
fn solution() { input! { n: usize, q: usize, a: [usize; n], xs: [usize; q] } let mut ac: Vec<usize> = vec![0; n + 1]; for i in 0..n { ac[i + 1] = ac[i] + a[i]; } let mut eo: Vec<usize> = vec![0; n]; eo[0] = a[0]; eo[1] = a[1]; for i in 2..n { ...
easy
0364
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You have a pot and <var>N</var> ingredients. Each ingredient has a real number parameter called <em>value</em>, and the value of the <var>i</var>-th ingredient <var>(1 \leq i \leq N)</var> is <var>v_i</...
int solution(void) { int n; scanf("%d", &n); double v[n]; for (int i = 0; i < n; i++) { scanf("%lf", &v[i]); } for (int j = 0; j < n; j++) { double temp1 = 10000; double temp2 = 10000; int temp1pos = 0; int temp2pos = 0; for (int i = 0; i < n; i++) { if (v[i] < temp1 && v[i]...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let mut v: Vec<f64> = (0..n) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); v.sort...
hard
0365
Vasiliy lives at point (a, b) of the coordinate plane. He is hurrying up to work so he wants to get out of his house as soon as possible. New app suggested n available Beru-taxi nearby. The i-th taxi is located at point (xi, yi) and moves with a speed vi. Consider that each of n drivers will move directly to Vasiliy an...
int solution() { int hx = 0; int hy = 0; int count = 0; int taxi[1002][5]; double time[1002]; scanf("%d %d\n%d\n", &hx, &hy, &count); for (int i = 0; i < count; i++) { scanf("%d %d %d\n", &taxi[i][0], &taxi[i][1], &taxi[i][2]); } for (int i = 0; i < count; i++) { int dx = 0; int dy = 0; ...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let line = lines.next(); let temp: Vec<f32> = line .unwrap() .unwrap() .split_whitespace() .map(|x| x.parse::<f32>().unwrap()) .collect(); let x = temp[0]; let y = temp[1]; ...
medium
0366
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS: You are given two integers $$$d, m$$$, find the number of arrays $$$a$$$, satisfying the following constraints: The length of $$$a$$$ is $$$n$$$, $$$n \ge 1$$$ $$$1 \le a_1 &lt; a_2 &lt; \dots &lt; a_n \le d$$$...
int solution() { int t; scanf("%d", &t); long long int d; long long int m; long long int v; long long int digit; long long int pow[40]; long long int i; pow[0] = 1; for (i = 1; i < 40; i++) { pow[i] = pow[i - 1] * 2; } long long int ans; for (; t > 0; t--) { scanf("%lld %lld", &d, &m);...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); for _ in 0..t { let xs: Vec<u64> = lines .next() .unwrap() .unwrap() .split(' ') .map(|x| x.p...
easy
0367
Andryusha is an orderly boy and likes to keep things in their place.Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks ...
int solution(void) { int n = 0; int i = 0; int sock = 0; int size = 0; int max = 0; int cnt[100001] = { 0, }; scanf("%d", &n); for (i = 0; i < n * 2; i++) { scanf("%d", &sock); if (cnt[sock]) { cnt[sock]--; size--; } else { cnt[sock]++; size++; } if...
fn solution() { let stdin = std::io::stdin(); let mut lines = stdin.lock().lines(); let ct = str::parse(&lines.next().unwrap().unwrap()).unwrap(); let mut seen = Vec::new(); seen.resize(ct, false); let mut table = 0; let mut max = 0; for s in lines .next() .unwrap() ...
medium
0368
Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the oppo...
int solution(void) { int cnt = 0; int i = 0; int magnets[100000]; int groups = 1; scanf("%d", &cnt); for (i = 0; i < cnt; i++) { scanf("%d", &magnets[i]); if (i > 0 && magnets[i] != magnets[i - 1]) { groups++; } } printf("%d\n", groups); return 0; }
fn solution() { let mut n = String::new(); stdin().read_line(&mut n).unwrap(); let mut n: i64 = n.trim().parse().unwrap(); let mut total = 0; let mut last: String = "".to_string(); while n > 0 { let mut x = String::new(); stdin().read_line(&mut x).unwrap(); let x = x.tr...
hard
0369
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer and his friend TopCoDeer is playing a game. The game consists of <var>N</var> turns. In each turn, each player plays one of the two <em>gestures</em>, <em>Rock</em> and <em>Paper</em>,...
int solution() { int n = 0; int p = 0; char t = '\0'; while (1) { scanf("%c", &t); if (t == '\n') { break; } n++; if (t == 'p') { p++; } } printf("%d\n", (n / 2) - p); return 0; }
fn solution() { let mut st = String::new(); stdin().read_line(&mut st).unwrap(); let s: Vec<char> = st.trim().chars().collect(); let p: usize = s.iter().filter(|x| x.to_string() == "p").count(); let n = s.len(); println!("{}", n / 2 - p); }
easy
0370
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be ...
int solution() { int n = 0; int a[100 * 1000] = {0}; int c = 0; scanf("%d", &n); c = n - 1; for (int i = 0; i < n; i++) { int t = 0; scanf("%d ", &t); a[t - 1] = 1; while (a[c] != 0 && c > -1) { printf("%d ", c + 1); c -= 1; } printf("\n"); } return 0; }
fn solution() { use std::io; let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let n = buf.trim().parse::<u32>().unwrap(); buf.clear(); io::stdin().read_line(&mut buf).unwrap(); let ns = buf .split_whitespace() .map(|str| str.trim().parse::<u32>().unwrap(...
medium
0371
<h3>Selection of Participants of an Experiment</h3> <p> Dr. Tsukuba has devised a new method of programming training. In order to evaluate the effectiveness of this method, he plans to carry out a control experiment. Having two students as the participants of the experiment, one of them will be trained under the conve...
int solution(void) { int N = 0; int ans[60000] = {0}; int count = 0; while (1) { int sa = 10000000; int a[60000] = {0}; scanf("%d", &N); if (N == 0) { break; } for (int i = 0; i < N; i++) { scanf("%d", &a[i]); } while (1) { int sw = 0; int temp = 0; ...
fn solution() { loop { let mut buf1 = String::new(); io::stdin().read_line(&mut buf1).ok(); let n: usize = buf1.trim().parse().unwrap(); if n == 0 { return; } let mut buf2 = String::new(); io::stdin().read_line(&mut buf2).ok(); let mut a: ...
medium
0372
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A string <var>S</var> of an odd length is said to be a <em>strong palindrome</em> if and only if all of the following conditions are satisfied:</p> <ul> <li><var>S</var> is a palindrome.</li> <li>Let <v...
int solution() { char s[99]; scanf("%s", s); int n = strlen(s); for (int i = 0; i < n / 2; i++) { if (s[i] == s[n - i - 1] && s[i] == s[((n - 1) / 2) - i - 1] && s[n - i - 1] == s[((n + 3) / 2) + i - 1]) { continue; } printf("No"); return 0; } printf("Yes"); return 0; }
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let s = itr.next().unwrap(); let n = s.len(); let t = s[0..n / 2].to_string(); let u = s[n.div_ceil(2)..].to_string(); let rs = s.chars().rev().collect::...
medium
0373
Nezzar's favorite digit among $$$1,\ldots,9$$$ is $$$d$$$. He calls a positive integer lucky if $$$d$$$ occurs at least once in its decimal representation. Given $$$q$$$ integers $$$a_1,a_2,\ldots,a_q$$$, for each $$$1 \le i \le q$$$ Nezzar would like to know if $$$a_i$$$ can be equal to a sum of several (one or more) ...
int solution() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int m; int l; scanf("%d%d", &m, &l); int a[m]; for (int j = 0; j < m; j++) { scanf("%d", &a[j]); } for (int j = 0; j < m; j++) { int c = 0; while (a[j] > 0) { if (a[j] % l == 0 || a[j] >= (...
fn solution() { let mut n = String::new(); io::stdin().read_line(&mut n).unwrap(); let n: Vec<i64> = n.split_whitespace().map(|x| x.parse().unwrap()).collect(); let n: i64 = n[0]; for _ in 0..n { let mut line_1 = String::new(); let mut line_2 = String::new(); io::stdin().rea...
medium
0374
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has a calculator. It has a display and two buttons.</p> <p>Initially, the display shows an integer <var>x</var>. Snuke wants to change this value into another integer <var>y</var>, by pressing the...
int solution() { int x; int y; scanf(" %d %d", &x, &y); int z = 0; int x1 = abs(x); int y1 = abs(y); z += abs(x1 - y1); if ((x1 <= y1 && ((x < 0 && y > 0) || (x > 0 && y < 0))) || (x1 >= y1 && ((x < 0 && y > 0) || (x > 0 && y < 0))) || (x == 0 && y < 0) || (x > 0 && y == 0)) { z++; } e...
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).ok(); let v: Vec<i64> = buf.split_whitespace().map(|x| x.parse().unwrap()).collect(); let (x, y) = (v[0], v[1]); let ans: i64 = if x * y < 0 || x * y == 0 && x > y { (x + y).abs() + 1 } else if x <= y { ...
medium
0375
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are going to hold a competition of one-to-one game called AtCoder Janken. <em>(Janken is the Japanese name for Rock-paper-scissors.)</em> <var>N</var> players will participate in this competition, a...
int solution(void) { long n; long m; scanf("%ld %ld", &n, &m); if (m % 2 == 1) { for (long i = 1; i <= m / 2; i++) { printf("%ld %ld\n", i, m + 1 - i); } for (long i = 1; i <= (m + 1) / 2; i++) { printf("%ld %ld\n", m + i, ((m + 1) * 2) - i); } } else { for (long i = 1; i <= m...
fn solution() { input! { n: usize, m: usize } if n % 2 == 1 { for i in (1..=m).rev() { println!("{} {}", i, n + 1 - i); } return; } let mut c = 0; for i in 1..n / 2 { if c == m || i >= n / 2 - i { break; } pr...
easy
0376
You are given a set of all integers from $$$l$$$ to $$$r$$$ inclusive, $$$l &lt; r$$$, $$$(r - l + 1) \le 3 \cdot 10^5$$$ and $$$(r - l)$$$ is always odd.You want to split these numbers into exactly $$$\frac{r - l + 1}{2}$$$ pairs in such a way that for each pair $$$(i, j)$$$ the greatest common divisor of $$$i$$$ and ...
int solution() { long long t = 1; while (t--) { long long l; long long r; scanf("%lld%lld", &l, &r); printf("YES\n"); for (long long i = 0; i <= (r - l) / 2; i++) { printf("%lld %lld\n", l + (i * 2) + 1, l + (i * 2)); } } return 0; }
fn solution() -> io::Result<()> { let mut s = String::new(); io::stdin().read_to_string(&mut s); let range: Vec<i64> = s .split_whitespace() .filter(|&s| !s.is_empty()) .map(|s| s.parse::<i64>().unwrap()) .collect(); let start = range[0]; let end = range[1] + 1; p...
hard
0377
You are given $$$n$$$ of integers $$$a_1, a_2, \ldots, a_n$$$. Process $$$q$$$ queries of two types: query of the form "0 $$$x_j$$$": add the value $$$x_j$$$ to all even elements of the array $$$a$$$, query of the form "1 $$$x_j$$$": add the value $$$x_j$$$ to all odd elements of the array $$$a$$$.Note that when proces...
int solution(void) { int t; scanf("%d", &t); while (t--) { static int n; static int q; static int cnt; static int type; static int x; static long long ans; cnt = 0; ans = 0; scanf("%d%d", &n, &q); for (int i = 0; i < n; ++i) { scanf("%d", &x); cnt += x & 1; ...
fn solution() { let stdin = std::io::stdin(); let stdin_lock = stdin.lock(); let mut line_iter = stdin_lock.lines(); let t = line_iter .next() .unwrap() .unwrap() .as_str() .parse::<usize>() .unwrap(); for _ in 0..t { let n_q_str = line_iter.ne...
hard
0378
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $$$2n$$$ jars of strawberry and blueberry jam.All the $$$2n$$$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, h...
int solution(void) { int t; scanf("%d", &t); while (t--) { long long int i; long long int k = 0; long long int dif = 0; long long int n; long long int a[200005] = {0}; long long int l[200005]; long long int r[200005]; long long int s = 0; long long int bb = 0; long long int...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut entries = buf.split_whitespace(); let q: usize = entries.next().unwrap().parse().unwrap(); for _ in 0..q { let n: usize = entries.next().unwrap().parse().unwrap(); let mut sum = 0...
hard
0379
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given are three integers <var>A_1</var>, <var>A_2</var>, and <var>A_3</var>.</p> <p>If <var>A_1+A_2+A_3</var> is greater than or equal to <var>22</var>, print <code>bust</code>; otherwise, print <code>w...
int solution(void) { int a = 0; int b = 0; int c = 0; scanf("%d %d %d", &a, &b, &c); if (a + b + c >= 22) { printf("bust"); } else { printf("win"); } return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).expect("read_line error"); let mut parts = s.split_whitespace().map(|s| s.parse::<i32>()); if let (Some(Ok(a)), Some(Ok(b)), Some(Ok(c))) = (parts.next(), parts.next(), parts.next()) { if a + b + c >= 22 { ...
easy
0380
You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$. You want to split it into exactly $$$k$$$ non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int k; scanf("%d %d", &n, &k); int a[n]; int b[n]; int count = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] % 2 == 1) { b[count] = i; count++; } } if (count >= k && ...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let stdout = io::stdout(); let mut out = io::BufWriter::with_capacity(2 * 1024 * 1024, stdout.lock()); let q = input!(i32); for _ in 0..q { let (n, k) = input!(usize, usize); let a: Vec<_> = input!()...
easy
0381
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a positive integer <var>N</var>. How many tuples <var>(A,B,C)</var> of positive integers satisfy <var>A \times B + C = N</var>?</p> </section> </div> <div class="part"> <section> <h3>Constraint...
int solution(void) { int n; int cnt = 1; int ans = 0; int flag = 0; scanf("%d", &n); for (int z = 1; z <= n - 1; z++) { flag = 0; cnt = 0; for (int i = 1; i <= sqrt(z); i++) { if (z % i == 0) { cnt++; if (z == i * i) { flag++; } } } cnt *=...
fn solution() { let mut s = String::new(); use std::io::Read; std::io::stdin().read_to_string(&mut s).unwrap(); let mut s = s.split_whitespace(); let n: i64 = s.next().unwrap().parse().unwrap(); let mut d = vec![1; n as usize + 1]; for i in 2..=n { for j in (i..=n).step_by(i as usize...
hard
0382
Let's say you are standing on the $$$XY$$$-plane at point $$$(0, 0)$$$ and you want to reach point $$$(n, n)$$$.You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $$$x$$$ coordinate, or up, i. e. vertically and in the direction that increase your $$$y$$$ coo...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int k; scanf("%d", &n); long cost[n]; for (int i = 0; i < n; i++) { scanf("%ld", &cost[i]); } long long minSum; long long minOdd = cost[0]; long long minEve = cost[1]; long long sumOdd = cost[0]; long ...
fn solution() { use std::io::Read; let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut itr = buf.split_whitespace(); use std::io::Write; let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); let t: usize = scan!(usize);...
medium
0383
<h1>Mode Value</h1> <p> Your task is to write a program which reads a sequence of integers and prints mode values of the sequence. The mode value is the element which occurs most frequently. </p> <H2>Input</H2> <p> A sequence of integers <var>a<sub>i</sub></var> (1 &le; <var>a<sub>i</sub></var> &le; 100). The numbe...
int solution(void) { int i = 1; int j = 0; int data[100]; int max = 0; int count[100]; for (j = 0; j < 100; j++) { data[j] = 0; count[j + 1] = 0; } while (scanf("%d", &data[i]) != EOF) { for (j = 1; j <= 100; j++) { if (j == data[i]) { count[j]++; } } i++; } ...
fn solution() { let ns = { let mut vec = Vec::new(); let mut s = String::new(); loop { s.clear(); match std::io::stdin().read_line(&mut s) { Ok(0) => break, Ok(_) => vec.push(s.trim_end().parse::<i32>().unwrap()), Err(e)...
medium
0384
Pupils decided to go to amusement park. Some of them were with parents. In total, n people came to the park and they all want to get to the most extreme attraction and roll on it exactly once.Tickets for group of x people are sold on the attraction, there should be at least one adult in each group (it is possible that ...
int solution() { long long n; long long c1; long long c2; long long i; char s[200010]; scanf("%lld%lld%lld", &n, &c1, &c2); scanf("%s", s); long long len = strlen(s); long long sum = 1e18; long long s1 = 0; for (i = 0; i < len; i++) { if (s[i] == '1') { s1++; } } long long a; l...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let tokens: Vec<&str> = line.split_whitespace().collect(); let n: i32 = tokens[0].parse().unwrap(); let c1: i64 = tokens[1].parse().unwrap(); let c2: i64 = tokens[2].parse().unwrap(); let mut s = String...
medium
0385
You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?No...
int solution() { int t = 0; int n = 0; int sw = 0; int h[100] = {0}; long long int sum = 0; scanf("%d", &t); while (t-- > 0) { sum = 0; sw = 1; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &h[i]); } for (int i = 0; i < n; i++) { sum += h[i]; if (s...
fn solution() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut sc = cf_scanner::Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); let tc: usize = sc.next(); for _ in 0..tc { let n: usize = sc.next(); let mut arr: Vec<i64> = (0..n).map(|_| sc.ne...
hard
0386
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock. The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn so...
int solution() { int no; int moves = 0; char str[1000]; char nstr[1000]; scanf("%d", &no); scanf("%s", str); scanf("%s", nstr); for (int i = 0; i < no; i++) { int no1 = str[i] - '0'; int no2 = nstr[i] - '0'; if (no1 - no2 > 0) { if (no1 - no2 > 10 - no1 + no2) { moves = moves +...
fn solution() { let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); let number_disk: usize = input.trim().parse().unwrap(); let mut original_state = String::new(); std::io::stdin().read_line(&mut original_state).unwrap(); let mut final_state = String::new(); std:...
medium
0387
A string is called square if it is some string written twice in a row. For example, the strings "aa", "abcabc", "abab" and "baabaa" are square. But the strings "aaa", "abaaab" and "abcdabc" are not square.For a given string $$$s$$$ determine if it is square.
int solution(void) { int n; scanf("%d", &n); char *str = malloc(sizeof(char) * 101); for (int i = 0; i < n; i++) { scanf("%s", str); int length = strlen(str); if (length % 2 == 1) { printf("NO\n"); continue; } char *tmp = malloc(sizeof(char) * (length / 2) + 1); char *tmp2 ...
fn solution() { std::io::stdin().lines().skip(1).flatten().for_each(|s| { match s.split_at(s.len().wrapping_div(2)) { (a, b) if a == b => println!("YES"), _ => println!("NO"), } }); }
medium
0388
When registering in a social network, users are allowed to create their own convenient login to make it easier to share contacts, print it on business cards, etc.Login is an arbitrary sequence of lower and uppercase latin letters, digits and underline symbols («_»). However, in order to decrease the number of frauds an...
int solution() { int n; int i = 0; int j; int log = 1; char str1[51]; char str2[51]; scanf(" %s", str1); for (j = 0; str1[j]; j++) { if (str1[j] >= 'A' && str1[j] <= 'Z') { str1[j] += 32; } if (str1[j] == 'O' || str1[j] == 'o') { str1[j] = '0'; } if (str1[j] == 'i' || str...
fn solution() { let mut s: String = String::new(); stdin().read_line(&mut s); s = s.to_lowercase(); s = s.replace("1", "!"); s = s.replace("0", "o"); s = s.replace("i", "!"); s = s.replace("l", "!"); let mut n: String = String::new(); stdin().read_line(&mut n).unwrap(); let n: i3...
medium
0389
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given are integers <var>a,b,c</var> and <var>d</var>. If <var>x</var> and <var>y</var> are integers and <var>a \leq x \leq b</var> and <var>c\leq y \leq d</var> hold, what is the maximum possible value ...
int solution() { long int a[10]; long int b[10]; for (int i = 0; i < 4; ++i) { scanf("%ld", &a[i]); } b[0] = a[0] * a[2]; b[1] = a[0] * a[3]; b[2] = a[1] * a[2]; b[3] = a[1] * a[3]; long int max = LONG_MIN; for (long int i = 0; i < 4; ++i) { if (max < b[i]) { max = b[i]; } } ...
fn solution() { let mut s = String::new(); use std::io::Read; std::io::stdin().read_to_string(&mut s).unwrap(); let mut s = s.split_whitespace(); let a: i64 = s.next().unwrap().parse().unwrap(); let b: i64 = s.next().unwrap().parse().unwrap(); let c: i64 = s.next().unwrap().parse().unwrap();...
medium
0390
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a long seat of width <var>X</var> centimeters. There are many people who wants to sit here. A person sitting on the seat will always occupy an interval of length <var>Y</var> centimeters.</p> <p...
int solution(void) { int max; int x; int y; int ans = 0; scanf("%d %d %d", &max, &x, &y); while (-1) { if (max == (ans * x) + ((ans + 1) * y)) { break; } if (max < (ans * x) + ((ans + 1) * y)) { ans--; break; } ans++; } printf("%d\n", ans); return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let vec: Vec<&str> = s.trim().split(' ').collect(); let x: i32 = vec[0].parse().unwrap(); let y: i32 = vec[1].parse().unwrap(); let z: i32 = vec[2].parse().unwrap(); if x % (y + z) == 0 || x % (y + z) < z...
hard
0391
<H1>問題 3</H1> <br/> <p>  入力ファイルの1行目に正整数 n (n≧3)が書いてあり, つづく n 行に異なる正整数 a<sub>1</sub>, ..., a<sub>n</sub> が 1つずつ書いてある. a<sub>1</sub>, ..., a<sub>n</sub> から異なる2個を選んで作られる 順列を(数として見て)小さい順に並べたとき, 3番目に来るものを出力せよ. </p> <p>  ただし, 例えば,a<sub>1</sub> = 1,a<sub>4</sub> = 11 のような場合も, a<sub>1</sub>a<sub>4</sub> と a<sub>4</sub>a<sub>...
int solution(void) { int n; int i; int j; int a; int exist[10001] = {0}; int smalla[10]; int smalla_num; int result[3] = {1000000000, 1000000000, 1000000000}; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a); exist[a] = 1; } smalla_num = 0; for (i = 0; i <= 10000 && smalla_nu...
fn solution() { let stdin = stdin(); let mut lines = stdin.lock().lines(); let n = lines.next().unwrap().unwrap().parse::<usize>().unwrap(); let mut array = lines .take(n) .map(|s| s.unwrap().parse::<u32>().unwrap()) .collect::<Vec<_>>(); array.sort(); let mut vec = Vec...
hard
0392
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Raccoon is fighting with a monster.</p> <p>The <em>health</em> of the monster is <var>H</var>.</p> <p>Raccoon can use <var>N</var> kinds of special moves. Using the <var>i</var>-th move decreases the mo...
int solution() { int h = 0; int n = 0; int sum = 0; scanf("%d %d", &h, &n); int dat[n]; for (int i = 0; i < n; i++) { scanf("%d", &dat[i]); sum += dat[i]; } if (sum >= h) { printf("Yes"); } else { printf("No"); } return 0; }
fn solution() { let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer).unwrap(); let mut lines = buffer.lines(); let a = lines.next().unwrap(); let b = lines.next().unwrap(); let a2 = a .split_whitespace() .map(|s| s.parse::<i32>().unwrap()) .collect::...
medium
0393
Tanya wants to go on a journey across the cities of Berland. There are $$$n$$$ cities situated along the main railroad line of Berland, and these cities are numbered from $$$1$$$ to $$$n$$$. Tanya plans her journey as follows. First of all, she will choose some city $$$c_1$$$ to start her journey. She will visit it, an...
int solution() { int n; scanf("%d", &n); long long int max = 0; int b[n]; int cut[n]; for (int i = 0; i < n; i++) { scanf("%d", b + i); cut[i] = b[i] - i - 1; } if (n == 1) { printf("%d", b[0]); return 0; } int min = cut[0]; int amax = cut[0]; for (int i = 0; i < n; i++) { if...
fn solution() { use std::io::{self, BufRead}; let mut cities_input = String::new(); stdin() .read_line(&mut cities_input) .expect("Failed to parse"); let reader = io::stdin(); let mut sums = HashMap::<i64, i64>::new(); let beauties_cities: Vec<i64> = reader .lock() ...
hard
0394
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$...
int solution() { long t; long i; long j; long o; long u; long n; long k; long a[300000]; double sl[30000]; scanf("%ld", &t); for (i = 0; i < t; i++) { scanf("%ld%ld", &n, &k); o = 0; for (j = 0; j < n; j++) { scanf("%ld", &a[j]); } for (j = 0; j < n; j++) { if (a[j]...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); for _ in 0..t { let xs: Vec<u64> = lines .next() .unwrap() .unwrap() .split(' ') .map(|x| x.p...
medium
0395
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Find the number of <em>palindromic numbers</em> among the integers between <var>A</var> and <var>B</var> (inclusive). Here, a palindromic number is a positive integer whose string representation in base...
int solution() { int A = 0; int B = 0; int number = 0; int answer = 0; int D[5]; scanf("%d %d", &A, &B); for (int i = A; i <= B; ++i) { number = i; for (int j = 4; j >= 0; --j) { D[j] = number % 10; number /= 10; } if (D[0] == D[4] && D[1] == D[3]) { ++answer; } ...
fn solution() { let mut buf: String = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let a = iter.next().unwrap().parse::<i64>().unwrap(); let b = iter.next().unwrap().parse::<i64>().unwrap(); let mut ans = 0; for i in a..b + 1 { let m...
hard
0396
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N+1</var> towns. The <var>i</var>-th town is being attacked by <var>A_i</var> monsters.</p> <p>We have <var>N</var> heroes. The <var>i</var>-th hero can defeat monsters attacking the <var...
int solution(void) { int N = 0; long A[200000] = {0}; long B[200000] = {0}; long sum = 0; long count = 0; scanf("%d", &N); for (int i = 0; i < N + 1; i++) { scanf("%ld", &A[i]); sum += A[i]; } for (int i = 0; i < N; i++) { scanf("%ld", &B[i]); if (A[i] < B[i]) { int temp = A[i]...
fn solution() { let min = |a, b| if a < b { a } else { b }; let mut n = String::new(); stdin().read_line(&mut n).unwrap(); let n: usize = n.trim().parse().unwrap(); let mut ais = String::new(); stdin().read_line(&mut ais).unwrap(); let mut ais: Vec<usize> = ais.split_whitespace().flat_map(...
medium
0397
Marcin is a coach in his university. There are $$$n$$$ students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other.Let's focus on the students. They are indexed with integers from $$$1$$$ to $$$n$$$. Each of them can be described with ...
int solution() { long int n; long int i; long int j; scanf("%ld", &n); long long int a[n]; long long int b[n]; long long int c[n]; long long int ch[n]; long long int skill = 0; long long int flag = 0; for (i = 0; i < n; i++) { c[i] = 0; ch[i] = -1; scanf("%lld", &a[i]); } for (i ...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let n: i32 = iter.next().unwrap().parse().unwrap(); let mut A = vec![]; let mut B = vec![]; let mut counter: BTreeMap<i64, Vec<i32>> = BTreeMap::new(); ...
medium
0398
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a chocolate bar partitioned into <var>H</var> horizontal rows and <var>W</var> vertical columns of squares.</p> <p>The square <var>(i, j)</var> at the <var>i</var>-th row from the top and the <v...
int solution() { int H; int W; int K; char S[10][1000]; char s[1002]; int pattern; scanf("%d %d %d", &H, &W, &K); for (int i = 0; i < H; i++) { scanf("%s", s); for (int j = 0; j < W; j++) { S[i][j] = s[j]; } } pattern = 1; for (int i = 0; i < H - 1; i++) { pattern *= 2; ...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let h: usize = itr.next().unwrap().parse().unwrap(); let w: usize = itr.next().unwrap().parse().unwrap(); let k: usize = itr.next().unwrap().parse().unwrap(); ...
easy
0399
You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a terna...
int solution(void) { int n; int i; int j; scanf("%d", &n); int a[n]; int h[3] = {0}; int c[3] = {0}; for (i = 0; i < n; i++) { scanf("%1d", &a[i]); h[a[i]]++; } for (i = 0; i < n; i++) { if (h[a[i]] > n / 3) { c[a[i]]++; for (j = 0; j < 3; j++) { if (a[i] != j && h[j...
fn solution() { let n: usize = { let mut read_buf = String::new(); io::stdin().read_line(&mut read_buf).unwrap(); read_buf.trim().parse().unwrap() }; let numbers = { let mut read_buf = String::new(); io::stdin().read_line(&mut read_buf).unwrap(); read_buf ...
medium
0400
You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its...
int solution() { int t; scanf("%d", &t); for (int k = 0; k < t; k++) { char s1[27]; char s2[51]; scanf("%s", s1); scanf("%s", s2); int j = 0; int k = -1; int sum = 0; for (int i = 0; s1[i] != 0; i++) { if (s2[j] == s1[i]) { if (k != -1) { sum = sum + abs(k -...
fn solution() { let mut keys = String::new(); let mut input = String::new(); io::stdin().read_line(&mut input).expect(""); let n: i32 = input.trim().parse().expect(""); for _ in 0..n { keys.clear(); input.clear(); io::stdin().read_line(&mut keys).expect(""); io::stdin...
hard