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
0501
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Given is a tree <var>G</var> with <var>N</var> vertices. The vertices are numbered <var>1</var> through <var>N</var>, and the <var>i</var>-th edge connects Vertex <var>a_i</var> and Vertex <var>b_i</va...
int solution() { int i; int N; int a[100001]; int b[100001]; scanf("%d", &N); for (i = 1; i <= N - 1; i++) { scanf("%d %d", &(a[i]), &(b[i])); } edge e[100001]; list *inc[100001] = {}; list l[200001]; for (i = 1; i <= N - 1; i++) { e[i].end[0] = a[i]; e[i].end[1] = b[i]; e[i].colo...
fn solution() { let N: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() }; let (a, b): (Vec<usize>, Vec<usize>) = { let (mut a, mut b) = (vec![], vec![]); for _ in 0..(N - 1) { let...
medium
0502
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Alice, Bob and Charlie are playing <em>Card Game for Three</em>, as below:</p> <ul> <li>At first, each of the three players has a deck consisting of some number of cards. Each card has a letter <code>a<...
int solution() { int a = 0; int b = 0; int c = 0; char ary_a[128]; char ary_b[128]; char ary_c[128]; char turn = 'a'; fgets(ary_a, sizeof(ary_a), stdin); fgets(ary_b, sizeof(ary_b), stdin); fgets(ary_c, sizeof(ary_c), stdin); while (1) { if (turn == 'a') { turn = ary_a[a]; if (i...
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).ok(); let a = String::from(s.trim()); let mut s = String::new(); stdin().read_line(&mut s).ok(); let b = String::from(s.trim()); let mut s = String::new(); stdin().read_line(&mut s).ok(); let c = String::from(s.trim...
easy
0503
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has one biscuit and zero Japanese yen (the currency) in his pocket. He will perform the following operations exactly <var>K</var> times in total, in the order he likes:</p> <ul> <li>Hit his pocket...
int solution(void) { long K = 0; long A = 0; long B = 0; scanf("%ld %ld %ld", &K, &A, &B); if (K <= A) { printf("%ld\n", K + 1); } else { if (B - A <= 2) { printf("%ld\n", K + 1); } else { long count = 0; long hit = 0; count = A; hit = A - 1; K -= hit; i...
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).ok(); let vs: Vec<u64> = s .split_whitespace() .map(|token| token.parse().ok().unwrap()) .collect(); let k = vs[0]; let a = vs[1]; let b = vs[2]; if b <= a + 2 { println!("{}", k + 1); ...
medium
0504
You are given $$$n$$$ words of equal length $$$m$$$, consisting of lowercase Latin alphabet letters. The $$$i$$$-th word is denoted $$$s_i$$$.In one move you can choose any position in any single word and change the letter at that position to the previous or next letter in alphabetical order. For example: you can chan...
int solution() { int test_cases = 0; scanf("%d", &test_cases); while (test_cases--) { int num_strings = 0; scanf("%d", &num_strings); int string_len = 0; scanf("%d", &string_len); char array[num_strings][string_len]; for (int i = 0; i < num_strings; i++) { scanf("%s", array[i]); ...
fn solution() { let mut input = String::new(); stdin().read_to_string(&mut input).ok(); let mut output = String::new(); let mut input = input.split_ascii_whitespace(); let t = input.next().unwrap().parse::<usize>().unwrap(); for _ in 0..t { let n = input.next().unwrap().parse::<usize>()....
hard
0505
Alice and Bob are playing a game with strings. There will be $$$t$$$ rounds in the game. In each round, there will be a string $$$s$$$ consisting of lowercase English letters. Alice moves first and both the players take alternate turns. Alice is allowed to remove any substring of even length (possibly empty) and Bob is...
int solution() { int t; scanf("%d", &t); while (t--) { char str[200001]; scanf("%s", str); int l = strlen(str); if (l == 1) { printf("Bob %d\n", (str[0] - 96)); continue; } int sum = 0; for (int i = 0; i < l; i++) { sum += (str[i] - 96); } if (!(l & 1)) { ...
fn solution() { let alphabet = "abcdefghijklmnopqrstuvwxyz"; let mut t = String::new(); stdin().read_line(&mut t); let t = t.trim().parse::<usize>().unwrap(); for _ in 0..t { let mut buf = String::new(); stdin().read_line(&mut buf); buf = buf.trim().to_string(); if...
easy
0506
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In some other world, today is Christmas.</p> <p>Mr. Takaha decides to make a multi-dimensional burger in his party. A <em>level-<var>L</var> burger</em> (<var>L</var> is an integer greater than or equal...
int solution(void) { long N; long X; long sou[51]; long patty[51]; long ans = 0; scanf("%ld %ld", &N, &X); sou[0] = 1; patty[0] = 1; for (int i = 1; i <= N; i++) { sou[i] = sou[i - 1] * 2 + 3; patty[i] = patty[i - 1] * 2 + 1; } int flag = 0; int i = 0; for (i = N; i >= 0 && flag == 0...
fn solution() { input! { n: usize, x: usize } let mut v: Vec<(usize, usize)> = vec![(1, 1)]; for i in 0..n { let (iall, ipat) = v[i]; let all = 3 + 2 * iall; let pat = 1 + 2 * ipat; v.push((all, pat)); } let mut sum = 0; let mut queue: Vec<(us...
medium
0507
You might have remembered Theatre square from the problem 1A. Now it's finally getting repaved.The square still has a rectangular shape of $$$n \times m$$$ meters. However, the picture is about to get more complicated now. Let $$$a_{i,j}$$$ be the $$$j$$$-th square in the $$$i$$$-th row of the pavement.You are given th...
int solution() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int n; int m; int x; int y; char *a = NULL; long long cost = 0; scanf("%d%d%d%d", &n, &m, &x, &y); a = (char *)calloc(m + 4, sizeof(char)); for (int j = 0; j < n; j++) { scanf("%s", a); for (int ...
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
0508
You have $$$n$$$ rectangular wooden blocks, which are numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th block is $$$1$$$ unit high and $$$\lceil \frac{i}{2} \rceil$$$ units long.Here, $$$\lceil \frac{x}{2} \rceil$$$ denotes the result of division of $$$x$$$ by $$$2$$$, rounded up. For example, $$$\lceil \frac{4}{2} \rce...
int solution() { long long t = 0; long long n = 0; scanf("%lld", &t); while (t--) { scanf("%lld", &n); printf("%lld \n", (long long)(n + 1) / 2); } return 0; }
fn solution() { let mut buf = BufWriter::new(io::stdout().lock()); std::io::stdin() .lines() .skip(1) .flatten() .flat_map(|s| s.parse::<i32>()) .for_each(|n| { writeln!(buf, "{}", n.wrapping_add(1).wrapping_shr(1)).unwrap_or_default(); }); }
hard
0509
You are given an array $$$a$$$ of $$$n$$$ positive integers. Determine if, by rearranging the elements, you can make the array strictly increasing. In other words, determine if it is possible to rearrange the elements such that $$$a_1 &lt; a_2 &lt; \dots &lt; a_n$$$ holds.
int solution() { int ccount = 0; int num2 = 0; scanf("%d", &ccount); for (int i = 0; i < ccount; ++i) { scanf("%d", &num2); int arr[num2]; for (int j = 0; j < num2; ++j) { scanf("%d", &arr[j]); } for (int i = 0; i < num2 - 1; i++) { for (int j = 0; j < num2 - 1 - i; j++) { ...
fn solution() { std::io::stdin() .lines() .skip(2) .step_by(2) .flatten() .for_each(|a| { let mut hs = std::collections::HashSet::with_capacity(100_000); if a.split_ascii_whitespace() .try_fold((), |_a, s| hs.insert(s).then_some(())) ...
hard
0510
The USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of $$$n$$$ haybale piles on the farm. The $$$i$$$-th pile contains $$$a_i$$$ haybales. However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in ...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int d; int j = 1; scanf("%d %d", &n, &d); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } while (1) { if (n == 1) { break; } if (a[j] > 0 && d >= j && j < n) { a[0] ...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let t: u32 = line.trim().parse().unwrap(); for _ in 0..t { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let mut pieces = line.split_whitespace(); let n: u3...
medium
0511
You are given an integer $$$n$$$. Check if $$$n$$$ has an odd divisor, greater than one (does there exist such a number $$$x$$$ ($$$x &gt; 1$$$) that $$$n$$$ is divisible by $$$x$$$ and $$$x$$$ is odd).For example, if $$$n=6$$$, then there is $$$x=3$$$. If $$$n=4$$$, then such a number does not exist.
int solution() { long long int n = 0; long long int x = 0; long long int fl = 0; scanf("%lld", &n); for (long long int t = 0; t < n; t++) { scanf("%lld", &x); fl = 0; while (x % 2 == 0) { x /= 2; } if (x % 2 == 1 && x != 1) { fl = 1; } if (fl == 0) { printf("NO\n"...
fn solution() { let mut buf = BufWriter::new(io::stdout().lock()); io::stdin() .lines() .skip(1) .flatten() .flat_map(|s| s.parse::<isize>()) .for_each(|n| { let ans = if n.wrapping_neg() & n == n { "NO" } else { "YE...
hard
0512
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In some other world, today is Christmas Eve.</p> <p>There are <var>N</var> trees planted in Mr. Takaha's garden. The height of the <var>i</var>-th tree <var>(1 \leq i \leq N)</var> is <var>h_i</var> met...
int solution(void) { int a[100000]; int min; int n; int i; int b; int k; int x; int tmp; int j; int count; int answer; scanf("%d %d", &n, &k); for (i = 0; i < n; i++) { scanf("%d", &a[i]); b = b + a[i]; } int increment; int temp; int array_size = n; increment = 4; while (...
fn solution() { let mut stdin = io::stdin(); let mut buf = String::new(); let _ = stdin.read_to_string(&mut buf); let mut iter = buf.split_whitespace(); let n: usize = iter.next().unwrap().parse().unwrap(); let k: usize = iter.next().unwrap().parse().unwrap(); let mut h: Vec<u32> = iter.map(...
medium
0513
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a right triangle <var>ABC</var> with <var>∠ABC=90°</var>.</p> <p>Given the lengths of the three sides, <var>|AB|,|BC|</var> and <var>|CA|</var>, find the area of the right triangle <var>ABC</va...
int solution() { int ab = 0; int bc = 0; int ca = 0; scanf("%d %d %d", &ab, &bc, &ca); printf("%d\n", (ab * bc) / 2); return 0; }
fn solution() { let mut l = String::new(); stdin().read_line(&mut l).unwrap(); let l: Vec<isize> = l.split_whitespace().take(2).flat_map(str::parse).collect(); println!("{}", l[0] * l[1] / 2); }
medium
0514
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> cubes stacked vertically on a desk.</p> <p>You are given a string <var>S</var> of length <var>N</var>. The color of the <var>i</var>-th cube from the bottom is red if the <var>i</...
int solution(void) { int i = 0; int cnt1 = 0; int cnt0 = 0; char s[1000000]; scanf("%s", s); while (s[i] != '\0') { if (s[i] == '1') { cnt1++; } else if (s[i] == '0') { cnt0++; } i++; } if (cnt1 >= cnt0 && cnt1 > 0 && cnt0 > 0) { printf("%d\n", 2 * cnt0); } else if (c...
fn solution() { let mut input = String::new(); let mut stack: Vec<char> = Vec::new(); std::io::stdin().read_line(&mut input).ok(); let s = input.trim(); for c in s.chars() { if c != '1' && c != '0' { continue; } match stack.last() { None => stack.push...
medium
0515
<h2>Entrance Examination</h2> <p> The International Competitive Programming College (ICPC) is famous for its research on competitive programming. Applicants to the college are required to take its entrance examination. </p> <p> The successful applicants of the examination are chosen as follows. </p> <ul> <li>The sco...
int solution(void) { int num = 1; int a = 1; int b = 1; while (1) { scanf("%d %d %d", &num, &a, &b); if (num == 0 && a == 0 && b == 0) { break; } int i; int point[num]; for (i = 1; i <= num; i++) { scanf("%d", &point[i]); } int sa = 0; int max = 0; int ba = 0;...
fn solution() { loop { let mut buf1 = String::new(); io::stdin().read_line(&mut buf1).ok(); let mut iter = buf1.split_whitespace().map(|n| usize::from_str(n).unwrap()); let (m, nmin, nmax) = ( iter.next().unwrap(), iter.next().unwrap(), iter.next()...
hard
0516
Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and ...
int solution() { int q; scanf("%d", &q); unsigned long long int a[q][3]; unsigned long long int m[q]; for (int i = 0; i < q; i++) { scanf("%llu %llu %llu", &a[i][0], &a[i][1], &a[i][2]); m[i] = 0; if (a[i][0] == 1) { m[i] = a[i][1]; } else { if (a[i][2] >= (2 * a[i][1])) { ...
fn solution() -> Result<(), Box<dyn std::error::Error>> { let mut buf = String::new(); stdin().read_line(&mut buf)?; let queries: i64 = buf.trim().parse()?; buf.clear(); for _ in 0..queries { { stdin().read_line(&mut buf)?; let mut tmp = buf.split_whitespace().map...
hard
0517
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><em>Kode Festival</em> is an anual contest where the hardest stone in the world is determined. (Kode is a Japanese word for "hardness".)</p> <p>This year, <var>2^N</var> stones participated. The hardnes...
int solution(void) { int N; int st[300000]; int i; int j; int k; int l; int m; int n; scanf("%d", &N); j = 1; k = 1; m = 2; for (i = 1; i <= N; i++) { j = j * 2; } for (i = 0; i < j; i++) { scanf("%d", &st[i]); } for (l = 1; l <= N; l++) { for (i = 0; i < j; i = i + m) { ...
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 mut a: Vec<i32> = s.take(1 << n).map(|a| a.parse().unwrap()).collect(); while a.len() > 1 {...
medium
0518
Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts a...
int solution() { int n = 0; int odd_flag = 0; int num = 0; while (scanf("%d", &n) != EOF) { while (n > 0) { n--; scanf("%d", &num); if (num % 2 == 1) { odd_flag = 1; } } if (odd_flag == 1) { printf("First\n"); } else { printf("Second\n"); } ...
fn solution() { let mut line = String::new(); let stdin = io::stdin(); stdin.lock().read_line(&mut line).unwrap(); line.clear(); stdin.lock().read_line(&mut line).unwrap(); if line .split_whitespace() .fold(false, |acc, s| acc | ((s.parse::<i32>().unwrap() & 1) != 0)) { ...
easy
0519
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are three houses on a number line: House <var>1</var>, <var>2</var> and <var>3</var>, with coordinates <var>A</var>, <var>B</var> and <var>C</var>, respectively. Print <code>Yes</code> if we pass ...
int solution(void) { int a = 3; int b = 8; int c = 5; scanf("%d%d%d", &a, &b, &c); if ((c > a && c < b) || (a > c && b < c)) { printf("Yes"); } else { printf("No"); } return 0; }
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).expect("Faled to read line"); let numbers: Vec<i32> = s .split(' ') .map(|s| s.trim()) .filter(|s| !s.is_empty()) .map(|s| s.parse().unwrap()) .collect(); let a = numbers[0]; let b = n...
easy
0520
There is an infinite set generated as follows: $$$1$$$ is in this set. If $$$x$$$ is in this set, $$$x \cdot a$$$ and $$$x+b$$$ both are in this set. For example, when $$$a=3$$$ and $$$b=6$$$, the five smallest elements of the set are: $$$1$$$, $$$3$$$ ($$$1$$$ is in this set, so $$$1\cdot a=3$$$ is in this set), ...
int solution() { long long t; scanf("%lld\n", &t); while (t--) { long long n; long long a; long long b; scanf("%lld %lld %lld\n", &n, &a, &b); long long m = n % b; if (a == 1) { if ((n - 1) % b == 0) { puts("Yes"); } else { puts("No"); } continue; ...
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(); ...
easy
0521
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Given is a permutation <var>P_1, \ldots, P_N</var> of <var>1, \ldots, N</var>. Find the number of integers <var>i</var> <var>(1 \leq i \leq N)</var> that satisfy the following condition: </p> <ul> <li...
int solution(void) { int n; scanf("%d", &n); int array[n]; int j = 1; int count = 0; for (int i = 1; i <= n; i++) { scanf("%d", &array[i]); while (j <= i) { if (j == i && array[i] <= array[j]) { count++; break; } if (array[i] <= array[j] && j < i) { j++; ...
fn solution() { let mut buf = String::new(); let stdin = std::io::stdin(); stdin.lock().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let mut n = iter.next().unwrap().parse::<usize>().unwrap(); let p_vec = iter .map(|e| e.parse::<usize>().unwrap()) .co...
medium
0522
You have a stripe of checkered paper of length $$$n$$$. Each cell is either white or black.What is the minimum number of cells that must be recolored from white to black in order to have a segment of $$$k$$$ consecutive black cells on the stripe?If the input data is such that a segment of $$$k$$$ consecutive black cell...
int solution() { int t; scanf("%d", &t); while (t > 0) { int n; int k; scanf("%d %d", &n, &k); char string[n + 1]; scanf("%s", string); int left = 0; int white = 0; int min_whites = n; for (int right = 0; right < n; right++) { if (string[right] == 'W') { ++white...
fn solution() { let mut input = String::new(); io::stdin().read_line(&mut input).expect("Err"); for _i in 0..input.trim().to_string().parse::<u16>().unwrap() { input = String::new(); io::stdin().read_line(&mut input).expect("Err"); let mut data: Vec<u32> = vec![]; for i in i...
hard
0523
You are given an array $$$a$$$ of length $$$n$$$. The array is called 3SUM-closed if for all distinct indices $$$i$$$, $$$j$$$, $$$k$$$, the sum $$$a_i + a_j + a_k$$$ is an element of the array. More formally, $$$a$$$ is 3SUM-closed if for all integers $$$1 \leq i &lt; j &lt; k \leq n$$$, there exists some integer $$$1...
int solution() { int t; scanf("%d", &t); long long arr[200001]; while (t--) { int n; scanf("%d", &n); int pos = 0; int neg = 0; int zeroes = 0; for (int i = 0; i < n; i++) { scanf("%lld", &arr[i]); if (arr[i] > 0) { pos++; } else if (arr[i] < 0) { neg++;...
fn solution() { let mut content = String::new(); io::stdin() .read_to_string(&mut content) .expect("Unable to read from stdin"); let mut lines = content.lines(); let num_tests: usize = lines.next().unwrap().parse().unwrap(); 'test: for _ in 0..num_tests { let _n: usize = lin...
medium
0524
This problem differs from the next one only in the presence of the constraint on the equal length of all numbers $$$a_1, a_2, \dots, a_n$$$. Actually, this problem is a subtask of the problem D2 from the same contest and the solution of D2 solves this subtask too.A team of SIS students is going to make a trip on a subm...
int solution() { long long int n; long long int i; long long int cnt = 0; long long int count = 0; long long int flag = 0; long long int t = 0; long long int finalans = 0; scanf("%d", &n); long long int a[n]; long long int r[100] = {0}; long long int d[100] = {0}; for (i = 0; i < n; i++) { s...
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 md = 998244353; let mut ans = 0; for _ in 0..n { let m...
easy
0525
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a rectangle in the <var>xy</var>-plane, with its lower left corner at <var>(0, 0)</var> and its upper right corner at <var>(W, H)</var>. Each of its sides is parallel to the <var>x</var>-axis o...
int solution() { int x[101]; int y[101]; int a[101]; scanf("%d%d%d", &x[0], &y[0], &a[0]); int xs = 0; int ys = 0; int xf = x[0]; int yf = y[0]; int f = 0; for (int i = 1; i <= a[0]; i++) { scanf("%d%d%d", &x[i], &y[i], &a[i]); if (a[i] == 1 && xs < x[i]) { xs = x[i]; } else if ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).ok(); let whn: Vec<usize> = s.trim().split(' ').map(|x| x.parse().unwrap()).collect(); let mut x1 = 0; let mut x2 = whn[0]; let mut y1 = 0; let mut y2 = whn[1]; for _ in 0..whn[2] { let mut s = String::ne...
easy
0526
There are $$$n$$$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it.Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $$$x$$$ eats a slime with a value $$$y$$$, the eaten slime di...
int solution() { int n; scanf("%d", &n); int en; if (n == 1) { scanf("%d", &en); printf("%d\n", en); } else { long long sum = 0; int p; int ne; p = ne = 0; int mi = 1000000000; while (n--) { scanf("%d", &en); if (en > 0) { p = 1; if (en < mi) { ...
fn solution() { let mut buffer = String::new(); { let stdin = io::stdin(); let mut handle = stdin.lock(); handle.read_to_string(&mut buffer).unwrap(); } let arr = buffer .split_whitespace() .map(|x| x.parse::<i64>().expect("convert to number")) .collect::...
medium
0527
You are given a grid with $$$n$$$ rows and $$$m$$$ columns. We denote the square on the $$$i$$$-th ($$$1\le i\le n$$$) row and $$$j$$$-th ($$$1\le j\le m$$$) column by $$$(i, j)$$$ and the number there by $$$a_{ij}$$$. All numbers are equal to $$$1$$$ or to $$$-1$$$. You start from the square $$$(1, 1)$$$ and can move ...
int solution() { int t = 0; int n = 0; int m = 0; int a[1000][1000] = {}; int res = 0; int dp[1000][1000][2] = {}; res = scanf("%d", &t); while (t > 0) { res = scanf("%d", &n); res = scanf("%d", &m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { res = scanf("%...
fn solution() { let mut buf = String::new(); let stdin = io::stdin(); let (mut n, mut m): (usize, usize); let mut grid: Vec<Vec<i32>> = vec![]; let mut max_sums: Vec<Vec<i32>> = vec![]; let mut min_sums: Vec<Vec<i32>> = vec![]; stdin.read_line(&mut buf).expect("bruh"); let tests: i32 =...
medium
0528
Polycarp likes numbers that are divisible by 3.He has a huge number $$$s$$$. Polycarp wants to cut from it the maximum number of numbers that are divisible by $$$3$$$. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after $$$m$$$ such cuts, there will be $$$m+1$$...
int solution() { char a[200004]; int n; int i; scanf("%s", a); n = strlen(a); int sum[200005]; int ans = 0; for (i = 0; i < n; i++) { sum[i] = (a[i] - '0') % 3; } for (i = 0; i < n; i++) { if (sum[i] == 0) { ans++; } else { if (sum[i] == 1) { if (i + 1 < n && sum[i +...
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 mut p1 = 0; let mut p2 = 0; let mut ans = 0; for...
medium
0529
You are given a string $$$s$$$ consisting only of lowercase Latin letters.You can rearrange all letters of this string as you wish. Your task is to obtain a good string by rearranging the letters of the given string or report that it is impossible to do it.Let's call a string good if it is not a palindrome. Palindrome ...
int solution() { int t; scanf("%d", &t); while (t--) { char str[1001]; scanf("%s", str); int len = strlen(str); if (len == 1) { puts("-1"); } else { int cnt[26]; memset(cnt, 0, sizeof(cnt)); for (int i = 0; str[i]; ++i) { ++cnt[str[i] - 'a']; } for (...
fn solution() { let mut reader = BufReader::new(io::stdin()); let mut Tstr = String::new(); reader.read_line(&mut Tstr); let T = Tstr.trim().parse::<i32>().unwrap(); for _i in 0..T { let mut Qstr = String::new(); reader.read_line(&mut Qstr); Qstr = Qstr.trim().to_string(); ...
medium
0530
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players' scores in a game, which p...
int solution(void) { int score[200000] = {0}; long N = 0; long K = 0; long Q = 0; scanf("%ld %ld %ld", &N, &K, &Q); for (long i = 0; i < Q; i++) { int A = 0; scanf("%d", &A); score[A - 1] += 1; } for (long i = 0; i < N; i++) { if (K - Q + score[i] <= 0) { printf("No\n"); } else...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let nkq = lines.next().unwrap().unwrap(); let mut nkq = nkq.split_whitespace(); let n: usize = nkq.next().unwrap().parse().unwrap(); let k: i32 = nkq.next().unwrap().parse().unwrap(); let q: i32 = nkq.next().unwra...
easy
0531
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size...
int solution() { int test_cases; scanf("%d", &test_cases); while (test_cases--) { int n = 0; scanf("%d", &n); printf("%d\n", (n / 2) + 1); } }
fn solution() { let stdin = std::io::stdin(); let mut handle = stdin.lock(); let mut buf = String::new(); handle.read_line(&mut buf).unwrap(); let n: usize = buf.trim().parse().unwrap(); for _q in 0..n { buf.clear(); handle.read_line(&mut buf).unwrap(); let m: usize = buf...
medium
0532
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has received an undirected graph with <var>N</var> vertices, numbered <var>1</var>, <var>2</var>, ..., <var>N</var>. The edges in this graph are represented by <var>(u_i, v_i)</var>. There are...
int solution() { int i; int N; int M; int u; int w; list *adj[100001] = {}; list e[400001]; scanf("%d %d", &N, &M); for (i = 0; i < M; i++) { scanf("%d %d", &u, &w); e[i * 2].v = w; e[(i * 2) + 1].v = u; e[i * 2].next = adj[u]; e[(i * 2) + 1].next = adj[w]; adj[u] = &(e[i * 2])...
fn solution() { input!(n: usize, m: usize, uv: [(usize1, usize1); m]); let mut graph = vec![vec![]; n]; for &(u, v) in uv.iter() { graph[u].push(v); graph[v].push(u); } let mut q = VecDeque::new(); let mut vis = vec![0; n]; let mut components = vec![]; for i in 0..n { ...
medium
0533
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In the Kingdom of AtCoder, people use a language called Taknese, which uses lowercase English letters.</p> <p>In Taknese, the plural form of a noun is spelled based on the following rules:</p> <ul> <li>...
int solution() { int i = -1; char a[10000]; do { i++; scanf("%s", &a[i]); } while (a[i]); if (a[i - 1] == 's') { a[i] = 'e'; a[i + 1] = 's'; } else { a[i] = 's'; } printf("%s\n", a); return 0; }
fn solution() { let mut word = String::new(); stdin().read_line(&mut word).unwrap(); if word.trim().ends_with("s") { println!("{}es", word.trim()); } else { println!("{}s", word.trim()); } }
medium
0534
Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. One day Kana gets interested in a new adventure game called Dragon Quest. In this game, her quest is to beat a dragon. The dragon has a hit point of $$$x...
int solution() { int k; scanf("%d", &k); int b[k]; int a[k]; int c[k]; for (int i = 0; i < k; i++) { int sum = 0; int flag = 0; scanf("%d", &a[i]); scanf("%d", &b[i]); scanf("%d", &c[i]); if (c[i] == 0) { printf("NO\n"); continue; } if (c[i] == 1 && a[i] > 10) {...
fn solution() { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let test: i64 = str.trim().parse().unwrap(); for _ in 0..test { str.clear(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); let mut health: i64...
medium
0535
This is a simplified version of the problem B2. Perhaps you should read the problem B2 before you start solving B1.Paul and Mary have a favorite string $$$s$$$ which consists of lowercase letters of the Latin alphabet. They want to paint it using pieces of chalk of two colors: red and green. Let's call a coloring of a ...
int solution() { int t; scanf("%d", &t); while (t--) { char s[53]; int a[27] = {0}; int r = 0; int g = 0; int sum = 0; scanf("%s", s); for (int i = 0; s[i] != '\0'; i++) { a[s[i] - 'a']++; sum++; } for (int i = 0; i < 26; i++) { if (a[i] == 1 && r == g && r +...
fn solution() { let (i, o) = (io::stdin(), io::stdout()); let mut o = bw::new(o.lock()); for l in i.lock().lines().skip(1) { let mut h = std::collections::HashMap::new(); for c in l.unwrap().chars() { h.entry(c).and_modify(|e| *e = 2).or_insert(1); } writeln!(o, "...
hard
0536
There is a $$$n \times m$$$ grid. You are standing at cell $$$(1, 1)$$$ and your goal is to finish at cell $$$(n, m)$$$.You can move to the neighboring cells to the right or down. In other words, suppose you are standing at cell $$$(x, y)$$$. You can: move right to the cell $$$(x, y + 1)$$$ — it costs $$$x$$$ burles;...
int solution() { int t; scanf("%d", &t); char *yes_str = "YES\n"; char *no_str = "NO\n"; char *yesOrNo_pStr[t]; for (int i = 0; i < t; i++) { int n; int m; int k; scanf("%d%d%d", &n, &m, &k); yesOrNo_pStr[i] = no_str; if (n * m - 1 == k) { yesOrNo_pStr[i] = yes_str; } ...
fn solution() { let (i, o) = (io::stdin(), io::stdout()); let mut o = bw::new(o.lock()); for l in i.lock().lines().skip(1) { if let [n, m, k] = l .unwrap() .split(' ') .map(|w| w.parse::<u32>().unwrap()) .collect::<Vec<_>>()[..] { w...
medium
0537
<span class="lang-en"> <p>Score: <var>400</var> points</p> <div class="part"> <section> <h3>Problem statement</h3> <p>We have an <var>H \times W</var> grid whose squares are painted black or white. The square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left is denoted as <var>(i, j)<...
int solution(void) { int h; int w; int black = 0; scanf("%d%d", &h, &w); char s[h][w + 2]; int cost[h * w][h * w]; for (int i = 0; i < h; i++) { scanf("%s", s[i]); } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == '#') { black++; } } } for...
fn solution() { use std::io::Read; let mut input = String::new(); std::io::stdin().read_to_string(&mut input).unwrap(); let mut iter = input.split_whitespace(); let h: usize = iter.next().unwrap().parse().unwrap(); let w: usize = iter.next().unwrap().parse().unwrap(); use std::iter::FromIter...
medium
0538
Eshag has an array $$$a$$$ consisting of $$$n$$$ integers.Eshag can perform the following operation any number of times: choose some subsequence of $$$a$$$ and delete every element from it which is strictly larger than $$$AVG$$$, where $$$AVG$$$ is the average of the numbers in the chosen subsequence.For example, if $$...
int solution() { int t = 0; scanf("%d", &t); for (int i = 1; i <= t; i++) { int n = 0; scanf("%d", &n); int a[n + 1]; for (int j = 0; j < n; j++) { scanf("%d", &a[j]); } a[n] = a[n - 1]; int flag = 1; for (int j = 0; j < n; j++) { if (a[j] != a[j + 1]) { flag =...
fn solution() { let (i, o) = (io::stdin(), io::stdout()); let mut o = bw::new(o.lock()); for l in i.lock().lines().skip(2).step_by(2) { let (l, c, _) = l .unwrap() .split(' ') .map(|w| w.parse::<u32>().unwrap()) .fold((0, 0, u32::MAX), |(l, c, m), x| {...
hard
0539
One day, Ahmed_Hossam went to Hemose and said "Let's solve a gym contest!". Hemose didn't want to do that, as he was playing Valorant, so he came up with a problem and told it to Ahmed to distract him. Sadly, Ahmed can't solve it... Could you help him?There is an Agent in Valorant, and he has $$$n$$$ weapons. The $$$i$...
int solution() { long long int t; scanf("%lld", &t); while (t--) { long long int n; long long int h; long long int b = 0; long long int c = 0; scanf("%lld %lld", &n, &h); long long int a[n]; for (long long int i = 0; i < n; i++) { scanf("%lld", &a[i]); if (a[i] > c) { ...
fn solution() { let stdin = stdin(); let mut string = String::new(); stdin.read_line(&mut string).unwrap(); for _ in 0..string.trim().parse::<usize>().unwrap() { string.clear(); stdin.read_line(&mut string).unwrap(); let enemy = string .split_ascii_whitespace() ...
hard
0540
You are given a binary string of length $$$n$$$ (i. e. a string consisting of $$$n$$$ characters '0' and '1').In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than $$$k$$$ moves? It is possi...
int solution() { int t; scanf("%d", &t); while (t--) { long long int n; long long int k; long long int c = 0; scanf("%lld%lld", &n, &k); char s[1000000] = {'\0'}; int f = 0; scanf("%s", s); for (long long int i = 0; i < n; i++) { if (s[i] == '0' && f == 0) { if (k - i...
fn solution() { let stdin = stdin(); let stdout = stdout(); let mut input_line = String::new(); stdin.read_line(&mut input_line).unwrap(); let q = input_line.trim().parse::<i64>().unwrap(); let mut b_string = String::new(); for _ in 0..q { input_line.clear(); stdin.read_line...
hard
0541
A non-empty digit string is diverse if the number of occurrences of each character in it doesn't exceed the number of distinct characters in it.For example: string "7" is diverse because 7 appears in it $$$1$$$ time and the number of distinct characters in it is $$$1$$$; string "77" is not diverse because 7 appears ...
int solution() { int i; int j; int t; int n; int l; int d = 0; int aux; int total; char s[100001]; scanf("%d", &t); while (t--) { scanf("%d\n%s", &n, s); total = 0; for (l = 2; l <= 100 && l <= n; l++) { for (i = 0; i < n - (l - 1); i++) { int dc[10] = {0}; ...
fn solution() { let mut buf = BufWriter::new(io::stdout().lock()); io::stdin() .lines() .skip(2) .step_by(2) .flatten() .for_each(|s| { let t = s.as_bytes(); let mut ans = 0usize; for i in 0..s.len() { let mut a = [0usiz...
hard
0542
Another Codeforces Round has just finished! It has gathered $$$n$$$ participants, and according to the results, the expected rating change of participant $$$i$$$ is $$$a_i$$$. These rating changes are perfectly balanced — their sum is equal to $$$0$$$.Unfortunately, due to minor technical glitches, the round is declare...
int solution() { int i = 0; scanf("%d", &i); int ar[i]; int ar2[i]; int l = 0; while (l < i) { scanf("%d", &ar[l]); l++; } while (l >= 0) { ar2[l] = ar[l]; ar[l] = ar[l] / 2; l--; } l = 0; while (l < i) { int k = ar2[l] % 2; int j = 0; int m = 0; while (j < i) {...
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(); let mut a = vec![0; n]; for i in 0..n { buf.clear(); stdin.read_line(&mut buf)?; ...
medium
0543
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>N</var> logs of lengths <var>A_1,A_2,\cdots A_N</var>.</p> <p>We can cut these logs at most <var>K</var> times in total. When a log of length <var>L</var> is cut at a point whose distance f...
int solution(void) { long n; long k; scanf("%ld %ld", &n, &k); long a[n]; for (long i = 0; i < n; i++) { scanf("%ld", &a[i]); } long ng = 0; long ok = 1000000000; long center; long cut; while (ng + 1 < ok) { center = (ng + ok) / 2; cut = 0; for (long i = 0; i < n; i++) { cut...
fn solution() { let (n, k): (usize, i64) = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) }; ...
easy
0544
You are given two integers $$$a$$$ and $$$b$$$.In one move, you can choose some integer $$$k$$$ from $$$1$$$ to $$$10$$$ and add it to $$$a$$$ or subtract it from $$$a$$$. In other words, you choose an integer $$$k \in [1; 10]$$$ and perform $$$a := a + k$$$ or $$$a := a - k$$$. You may use different values of $$$k$$$ ...
int solution() { int t; int a; int b; scanf("%d", &t); for (int i = 0; i < t; i++) { int diff = 0; int quo = 0; int rem = 0; int moves = 0; scanf("%d %d", &a, &b); diff = abs(a - b); quo = diff / 10; rem = diff % 10; if (diff != 0 && rem != 0) { moves = quo + 1; ...
fn solution() { let mut st = String::new(); io::stdin().read_line(&mut st).expect("1"); let t: usize = st.trim().parse().expect("2"); let mut ans = Vec::with_capacity(t); for _ in 0..t { let mut ab = String::new(); io::stdin().read_line(&mut ab).expect("3"); let mut c = ab.sp...
medium
0545
<span class="lang-en"> <p>(Please read problem A first. The maximum score you can get by solving this problem B is 1, which will have almost no effect on your ranking.)</p> <div class="part"> <section> <h3>Beginner's Guide</h3><p>Let's first write a program to calculate the score from a pair of input and output. You ca...
int solution(void) { int d; scanf("%d", &d); long c[26]; for (int i = 0; i < 26; i++) { scanf("%ld", &c[i]); } long s[d][26]; for (int i = 0; i < d; i++) { for (int j = 0; j < 26; j++) { scanf("%ld", &s[i][j]); } } int t[d]; for (int i = 0; i < d; i++) { scanf("%d", &t[i]); ...
fn solution() { let d: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().parse().unwrap() }; let c: Vec<i64> = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let iter = buf.split...
easy
0546
Boboniu likes bit operations. He wants to play a game with you.Boboniu gives you two sequences of non-negative integers $$$a_1,a_2,\ldots,a_n$$$ and $$$b_1,b_2,\ldots,b_m$$$.For each $$$i$$$ ($$$1\le i\le n$$$), you're asked to choose a $$$j$$$ ($$$1\le j\le m$$$) and let $$$c_i=a_i\&amp; b_j$$$, where $$$\&amp;$$$ den...
int solution() { int n; int m; int i; int j; int k; int ans = 0; scanf("%d%d", &n, &m); int a[n]; int b[m]; int c[n]; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i < m; i++) { scanf("%d", &b[i]); } int size = pow(2, 9) - 1; int arr[size]; int arr_index = 0; fo...
fn solution() { let (n, m): (usize, usize) = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) }; ...
medium
0547
Let's call an array $$$a$$$ consisting of $$$n$$$ positive (greater than $$$0$$$) integers beautiful if the following condition is held for every $$$i$$$ from $$$1$$$ to $$$n$$$: either $$$a_i = 1$$$, or at least one of the numbers $$$a_i - 1$$$ and $$$a_i - 2$$$ exists in the array as well.For example: the array $$$...
int solution() { int t; scanf("%d", &t); int output[t]; for (int i = 0; i < t; i++) { int s; scanf("%d", &s); int j = 1; while (s >= j * j) { j++; } output[i] = j - 1; if (s - 2 * j + 3 && s - j * j + 2 * j - 1) { output[i] += 1; } } for (int i = 0; i < t; i++) { ...
fn solution() { let (i, o) = (io::stdin(), io::stdout()); let mut o = bw::new(o.lock()); for l in i.lock().lines().skip(1) { writeln!(o, "{}", l.unwrap().parse::<f32>().unwrap().sqrt().ceil()).ok(); } }
hard
0548
<span class="lang-en"> <p>Score: <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>New AtCoder City has an infinite grid of streets, as follows:</p> <ul> <li>At the center of the city stands a clock tower. Let <var>(0, 0)</var> be the coordinates of this point.</li> <li>A straight str...
int solution() { int i; int N; int X[16]; int Y[16]; int P[16]; scanf("%d", &N); for (i = 0; i < N; i++) { scanf("%d %d %d", &(X[i]), &(Y[i]), &(P[i])); } int j; int k; int dist[2][32768][16]; int num[32768] = {}; const int bit[16] = {1, 2, 4, 8, 16, 32, 64, 128, ...
fn solution() { let stdin = std::io::stdin(); let stdout = std::io::stdout(); let mut reader = std::io::BufReader::new(stdin.lock()); let mut writer = std::io::BufWriter::new(stdout.lock()); let n: usize = { let mut buf = String::new(); reader.read_line(&mut buf).unwrap(); b...
medium
0549
Merge sort
void solution(int *a, int n) { int curr_size, left_start; int *b = (int *)malloc(n * sizeof(int)); for (curr_size = 1; curr_size <= n - 1; curr_size = 2 * curr_size) { for (left_start = 0; left_start < n - 1; left_start += 2 * curr_size) { int l = left_start; int mid = (left_start + curr_size - 1...
fn solution<T: Copy + Ord>(arr: &mut [T]) { if arr.len() <= 1 { return; } let len = arr.len(); let mut sub_array_size = 1; while sub_array_size < len { let mut start_index = 0; while len - start_index > sub_array_size { let end_idx = if start_index + 2 * sub_ar...
medium
0550
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the p...
int solution(void) { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int v[n]; printf("%d\n", n); for (int i = 1; i <= n; i++) { v[i - 1] = i; printf("%d ", i); } printf("\n"); for (int i = n - 1; i > 0; i--) { int temp = v[i]; v[i] = v[i - 1];...
fn solution() { let out = &mut BufWriter::new(stdout()); let mut input = String::new(); stdin().read_line(&mut input).expect("Failed read"); let t = input.trim().parse::<i64>().unwrap(); for _ in 0..t { input = String::new(); stdin().read_line(&mut input).expect("Failed read"); ...
easy
0551
Let's define $$$S(x)$$$ to be the sum of digits of number $$$x$$$ written in decimal system. For example, $$$S(5) = 5$$$, $$$S(10) = 1$$$, $$$S(322) = 7$$$.We will call an integer $$$x$$$ interesting if $$$S(x + 1) &lt; S(x)$$$. In each test you will be given one integer $$$n$$$. Your task is to calculate the number of...
int solution(void) { int t = 0; scanf("%d", &t); while (t--) { int n = 0; scanf("%d", &n); if (n >= 10) { if (n % 10 == 9) { printf("%i \n", (n + 1) / 10); } else { printf("%i \n", (n / 10)); } } else if (n == 9) { printf("1 \n"); } else if (n < 9) { ...
fn solution() { let mut t = String::new(); std::io::stdin().read_line(&mut t).expect(""); let mut t: i32 = t.trim().parse().expect(""); while t > 0 { let mut n = String::new(); std::io::stdin().read_line(&mut n).expect(""); let n: u64 = n.trim().parse().expect(""); print...
medium
0552
A little boy Gerald entered a clothes shop and found out something very unpleasant: not all clothes turns out to match. For example, Gerald noticed that he looks rather ridiculous in a smoking suit and a baseball cap.Overall the shop sells n clothing items, and exactly m pairs of clothing items match. Each item has its...
int solution() { char triples[101][101]; int i; int j; int k; int l; int m; int n; int cost[101]; int aux; int min = INT_MAX; scanf("%d %d", &n, &m); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { triples[i][j] = 0; } } for (i = 1; i <= n; i++) { scanf("%d", &cost...
fn solution() { let stdin: io::Stdin = io::stdin(); let mut lines: io::Lines<io::StdinLock> = stdin.lock().lines(); let first_line = lines.next().unwrap().unwrap(); let flv: Vec<_> = first_line .trim() .split(' ') .map(|n| n.parse::<u32>().unwrap()) .collect(); let...
medium
0553
You are given an integer $$$n$$$.You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times: Replace $$$n$$$ with $$$\frac{n}{2}$$$ if $$$n$$$ is divisible by $$$2$$$; Replace $$$n$$$ with $$$\frac{2n}{3}$$$ if $$$n$$$ is divisible by $$$3$$$; Replace $$$n$$$ with...
int solution() { int count = 0; int amount; long long int number[1001]; scanf("%d", &amount); for (long long int i = 0; i < amount; i++) { scanf("%lld", &number[i]); } for (long long int i = 0; i < amount; i++) { while (1) { if (number[i] % 5 == 0) { number[i] = (4 * number[i]) /...
fn solution() { let mut t = String::new(); io::stdin().read_line(&mut t).unwrap(); let mut t: u32 = t.trim().parse().unwrap(); while t != 0 { t -= 1; let mut q = String::new(); io::stdin().read_line(&mut q).unwrap(); let mut q: u64 = q.trim().parse::<u64>().unwrap(); ...
medium
0554
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence of length <var>N</var>. The <var>i</var>-th term in the sequence is <var>a_i</var>. In one operation, you can select a term and either increment or decrement it by one....
int solution(void) { int n; scanf("%d", &n); long long a[n]; long long sum = 0; long long c = 0; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } long long b[n]; for (int i = 0; i < n; i++) { b[i] = a[i]; } if (b[0] <= 0) { c = 1 - b[0]; b[0] = 1; } sum = b[0]; f...
fn solution() { let mut s: String = String::new(); stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let mut a: Vec<i64> = (0..n) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); for i in 0..n -...
hard
0555
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You have a digit sequence <var>S</var> of length <var>4</var>. You are wondering which of the following formats <var>S</var> is in:</p> <ul> <li>YYMM format: the last two digits of the year and the two-...
int solution() { char s[4]; scanf("%s", s); int f = (10 * (s[0] - '0')) + s[1] - '0'; int b = (10 * (s[2] - '0')) + s[3] - '0'; if (f >= 1 && f <= 12 && b >= 1 && b <= 12) { printf("AMBIGUOUS\n"); } else if (f >= 0 && f < 100 && b >= 1 && b <= 12) { printf("YYMM\n"); } else if (f >= 1 && f <= 12 ...
fn solution() { let mut buf = String::new(); let _ = std::io::stdin().read_line(&mut buf); let (b, a) = buf.trim().split_at(2); let b = b.parse::<u32>().unwrap(); let a = a.parse::<u32>().unwrap(); let b = !(1..=12).contains(&b); let a = !(1..=12).contains(&a); if b && a { printl...
easy
0556
<H1>Digit Number</H1> <p> Write a program which computes the digit number of sum of two integers <var>a</var> and <var>b</var>. </p> <H2>Input</H2> <p> There are several test cases. Each test case consists of two non-negative integers <var>a</var> and <i>b</i> which are separeted by a space in a line. The input term...
int solution(void) { int a = 0; int b = 0; int i = 0; int sum = 0; while (scanf("%d %d", &a, &b) != EOF) { sum = a + b; for (i = 1; i < 15; i++) { if (sum < 10) { printf("%d\n", i); break; } sum = sum / 10; } } return 0; }
fn solution() { let mut input; let mut total: u64; loop { input = String::new(); if let Ok(0) = std::io::stdin().read_line(&mut input) { break; } total = 0; for i in input.split_whitespace() { total += i.parse::<u64>().expect(""); } ...
hard
0557
You have an array of integers (initially empty).You have to perform $$$q$$$ queries. Each query is of one of two types: "$$$1$$$ $$$x$$$" — add the element $$$x$$$ to the end of the array; "$$$2$$$ $$$x$$$ $$$y$$$" — replace all occurrences of $$$x$$$ in the array with $$$y$$$. Find the resulting array after perform...
int solution() { int n; scanf("%d", &n); int *a = malloc(3000000); int *b = malloc(3000000); int *c = malloc(3000000); int *d = malloc(3000000); int *p = malloc(3000000); for (int n1 = 0; n1 < n; n1++) { scanf("%d", p + n1); if (p[n1] == 1) { scanf("%d", a + n1); } else { scanf("...
fn solution() { let mut cin = String::new(); std::io::stdin().read_to_string(&mut cin).unwrap(); let mut cin = cin.split_ascii_whitespace().flat_map(str::parse::<_>); cin![q]; let mut p = 0; let mut ps = vec![HashSet::new(); 1 << 19]; for _ in 1..=q { cin![op x]; if op == 1...
hard
0558
Santa has $$$n$$$ candies and he wants to gift them to $$$k$$$ kids. He wants to divide as many candies as possible between all $$$k$$$ kids. Santa can't divide one candy into parts but he is allowed to not use some candies at all.Suppose the kid who recieves the minimum number of candies has $$$a$$$ candies and the ki...
int solution() { int p; scanf("%d", &p); int a[p][3]; for (int i = 0; i < p; i++) { int n; int k; scanf("%d", &n); scanf("%d", &k); a[i][0] = n; a[i][1] = k; if (n % k <= k / 2) { a[i][2] = n; } else { a[i][2] = n - n % k + k / 2; } printf("%d \n", a[i][2]); ...
fn solution() { let stdin = io::stdin(); let mut input = stdin.lock(); let mut line = String::new(); input.read_line(&mut line).unwrap(); let mut queries: u16 = line.trim().parse().unwrap(); while queries > 0 { line.clear(); input.read_line(&mut line).unwrap(); let mut nu...
hard
0559
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> of length <var>N</var> consisting of lowercase English letters. We will cut this string at one position into two strings <var>X</var> and <var>Y</var>. Here, we would...
int solution(void) { int N; scanf("%d", &N); char S[N]; scanf("%s", S); char alp[2]['z' + 1]; for (int i = 'a'; i <= 'z'; i++) { alp[0][i] = -1; } for (int i = 'a'; i <= 'z'; i++) { alp[1][i] = -1; } for (int i = 0; i < N; i++) { if (alp[0][S[i]] == -1) { alp[0][S[i]] = i; } e...
fn solution() { let mut is = String::new(); stdin().read_line(&mut is).ok(); is.clear(); stdin().read_line(&mut is).ok(); let s = is.trim(); let mut ans = 0; for i in 1..s.len() { let (x, y) = s.split_at(i); let mut x_cnt = [false; 26]; let mut y_cnt = [false; 26]; ...
hard
0560
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a string <var>S</var> consisting of lowercase English letters. Find the maximum positive integer <var>K</var> that satisfies the following condition:</p> <ul> <li>There exists a partition of <v...
int solution() { bool is2 = false; char s[200000]; scanf("%s", s); int len = strlen(s); int k = len; for (int i = 0; i < len - 1; i++) { if (!is2 && s[i] == s[i + 1]) { is2 = true; i++; k--; } else { is2 = false; } } printf("%d\n", k); return 0; }
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 mut dp = vec![vec![0; 2]; s.len() + 3]; dp[0][0] = 1; ...
hard
0561
The police department of your city has just started its journey. Initially, they don’t have any manpower. So, they started hiring new recruits in groups.Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.If there is no police officer ...
int solution() { int n = 0; int i = 0; int ans = 0; int b = 0; scanf("%d", &n); int a[n + 1]; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i < n; i++) { b += a[i]; if (b < 0) { ans++; b = 0; } } printf("%d", ans); return 0; }
fn solution() { let _ = { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().to_string() }; let v: Vec<i32> = { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim() .split(...
hard
0562
Pig is visiting a friend.Pig's house is located at point 0, and his friend's house is located at point m on an axis.Pig can use teleports to move along the axis.To use a teleport, Pig should come to a certain point (where the teleport is located) and choose where to move: for each teleport there is the rightmost point ...
int solution() { int n = 0; int max = 0; int x = 0; scanf("%d", &n); scanf("%d", &x); int a[n]; int b[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); scanf("%d", &b[i]); } bool continuous = true; max = b[0]; for (int i = 1; i < n; i++) { if (a[i] > max) { continuous = fal...
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 g = get!(); let mut x = 0; for _ in 0..n { let ai = ge...
medium
0563
Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips:Vladik is at initial train station, and now n people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code ai is known ...
int solution() { int n; int low[5003]; int high[5003]; int ar[5003]; int dp[5003]; int ver_no[5003]; int i; int j; int k; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", ar + i); } for (i = 0; i < 5003; i++) { low[i] = -1; high[i] = -1; ver_no[i] = -1; } for (i = 0; ...
fn solution() { let n = { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); input.trim().parse::<usize>().unwrap() }; let a: Vec<usize> = { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); input ....
medium
0564
This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \le n \le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exa...
int solution(void) { long long r1 = 0; long long r2 = 0; for (int b = 2; b <= 26; ++b) { printf("\? %d %d\n", 1, b); fflush(stdout); scanf("%lli", &r1); if (r1 == -1) { printf("! %d\n", b - 1); fflush(stdout); return 0; } printf("\? %d %d\n", b, 1); fflush(stdout); ...
fn solution() -> io::Result<()> { let _input = String::new(); let out = io::stdout(); let mut out = BufWriter::new(out.lock()); let mut write_and_flush = |line: &str| { writeln!(out, "{}", line).unwrap(); out.flush().unwrap(); }; let is_finished = |line: &str| line.eq("0"); ...
medium
0565
<H1>Switching Railroad Cars</H1> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_switchingRailroadCars"> </center> <br> <p> This figure shows railway tracks for reshuffling cars. The rail tracks end in the bottom and the top-left rail track is used for the entrace and the top-right rail track...
int solution() { int t[10]; int ts = 0; while (scanf("%d", &t[ts]) != EOF) { if (t[ts]) { ts++; } else { printf("%d\n", t[--ts]); } } return 0; }
fn solution() { let mut stack = vec![]; let input = BufReader::new(stdin()); for line in input.lines() { let n = line.unwrap().trim().parse::<u32>().unwrap(); if n == 0 { println!("{}", stack.last().unwrap()); let last_idx = stack.len() - 1; stack.remove(l...
medium
0566
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>We have an undirected graph <var>G</var> with <var>N</var> vertices numbered <var>1</var> to <var>N</var> and <var>N</var> edges as follows:</p> <ul> <li>For each <var>i=1,2,...,N-1</var>, there is an ...
int solution() { int n; int key1; int key2; int i; int j; scanf("%d%d%d", &n, &key1, &key2); int a[n + 1][n + 1]; int ans[n + 1]; for (i = 1; i <= n - 1; i++) { ans[i] = 0; } for (i = 1; i <= n; i++) { for (j = 1; j < i; j++) { if (i >= key2 && j <= key1) { a[i][j] = i - j ...
fn solution() { let (n, x, y) = { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let s: Vec<usize> = s.split_whitespace().map(|x| x.parse().unwrap()).collect(); (s[0], s[1], s[2]) }; let mut k = vec![0; n]; let f = |i, j| if i < j { j - i } else {...
hard
0567
You are given an array $$$a$$$ consisting of $$$n$$$ integers.Let's call a pair of indices $$$i$$$, $$$j$$$ good if $$$1 \le i &lt; j \le n$$$ and $$$\gcd(a_i, 2a_j) &gt; 1$$$ (where $$$\gcd(x, y)$$$ is the greatest common divisor of $$$x$$$ and $$$y$$$).Find the maximum number of good index pairs if you can reorder th...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int i; int j; int count = 0; int rem = 0; scanf("%d", &n); int ara[n]; int temp; int n1; int n2; for (i = 0; i < n; i++) { scanf("%d", &ara[i]); } for (i = 0; i < n - 1; i++) { for (j = 0...
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(); ...
medium
0568
Given a set of integers (it can contain equal elements).You have to split it into two subsets $$$A$$$ and $$$B$$$ (both of them can contain equal elements or be empty). You have to maximize the value of $$$mex(A)+mex(B)$$$.Here $$$mex$$$ of a set denotes the smallest non-negative integer that doesn't exist in the set. ...
int solution() { int t; scanf("%d", &t); while (t--) { int m; scanf("%d", &m); int arr[m]; int farr[101]; for (int i = 0; i < 101; i++) { farr[i] = 0; } for (int i = 0; i < m; i++) { scanf("%d", &arr[i]); farr[arr[i]]++; } int xx = 101; int yy = 101; ...
fn solution() { let mut input = String::new(); stdin().read_line(&mut input).expect("error"); let iterations = input.trim().parse::<u32>().expect("error"); for _ in 0..iterations { input.clear(); stdin().read_line(&mut input).expect("error"); input.clear(); stdin().read_l...
hard
0569
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer has found two positive integers, <var>a</var> and <var>b</var>. Determine whether the concatenation of <var>a</var> and <var>b</var> in this order is a square number.</p> </section> </...
int solution(void) { int a = 0; int b = 0; int c = 1; int d = 0; scanf("%d %d", &a, &b); if (b < 10) { d = a * 10 + b; } else { if (b < 100) { d = a * 100 + b; } else { d = a * 1000 + b; } } while (c < 400) { if ((c * c) == d) { puts("Yes"); 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 mut n = String::new(); for v in &vec { n += v.as_ref(); } let p = n.parse::<i32>().unwrap(); for i in 0..10000 { let s = i * i; ...
medium
0570
The only difference between the easy and hard versions is that the given string $$$s$$$ in the easy version is initially a palindrome, this condition is not always true for the hard version.A palindrome is a string that reads the same left to right and right to left. For example, "101101" is a palindrome, while "0101" ...
int solution() { char s[1005]; int t; int n; int flag = 0; int sum = 0; scanf("%d", &t); while (t--) { flag = 0, sum = 0; scanf("%d", &n); scanf("%s", s); for (int i = 0; i < n; i++) { if (s[i] != s[n - 1 - i]) { flag = 1; } if (s[i] == '0') { sum++; ...
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 t = lines.next().unwrap().pa...
easy
0571
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits ca...
int solution() { long long int x[1000][4]; long long int n; long long int i; scanf("%lld", &n); for (i = 0; i < n; i++) { scanf("%lld %lld %lld %lld", &x[i][0], &x[i][1], &x[i][2], &x[i][3]); } for (i = 0; i < n; i++) { if ((x[i][1] - x[i][0]) % (x[i][2] + x[i][3]) == 0) { printf("%lld\n"...
fn solution() { let t = { let mut buf = String::new(); stdin().read_line(&mut buf).expect("Failed to read line"); buf.trim().parse::<i32>().unwrap() }; for _ in 0..t { let (a, b, x, y) = { let mut buf = String::new(); stdin().read_line(&mut buf).expect...
medium
0572
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Dolphin resides in two-dimensional Cartesian plane, with the positive <var>x</var>-axis pointing right and the positive <var>y</var>-axis pointing up.<br/> Currently, he is located at the point <var>(sx...
int solution() { int sx = 0; int sy = 0; int tx = 0; int ty = 0; int i = 0; scanf("%d %d %d %d", &sx, &sy, &tx, &ty); for (i = 0; i < ty - sy; i++) { printf("U"); } for (i = 0; i < tx - sx; i++) { printf("R"); } for (i = 0; i < ty - sy; i++) { printf("D"); } for (i = 0; i < tx - sx...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let sx: i64 = itr.next().unwrap().parse().unwrap(); let sy: i64 = itr.next().unwrap().parse().unwrap(); let tx: i64 = itr.next().unwrap().parse().unwrap(); let...
easy
0573
<H1>問題 4: 部活のスケジュール表 (Schedule) </H1> <br/> <h2>問題</h2> <p> IOI 高校のプログラミング部には,J 君と O 君と I 君の 3 人の部員がいる. プログラミング部では,部活のスケジュールを組もうとしている. </p> <p> 今,N 日間の活動のスケジュールを決めようとしている. 各活動日のスケジュールとして考えられるものは,各部員については活動に参加するかどうかの 2 通りがあり,部としては全部で 8 通りある. 部室の鍵はただ 1 つだけであり,最初は J 君が持っている. 各活動日には,その日の活動に参加する部員のうちのいずれか 1 人が鍵を持っている必要があり...
int solution(void) { int dp[1000][8]; int n; int i; int j; int k; int ans = 0; char res[1001]; scanf("%d", &n); scanf("%s", res); for (i = 0; i < n; i++) { switch (res[i]) { case 'J': res[i] = 4; break; case 'O': res[i] = 2; break; case 'I': res[i] = 1; ...
fn solution() { let input = { let mut buf = vec![]; stdin().read_to_end(&mut buf); unsafe { String::from_utf8_unchecked(buf) } }; let mut lines = input.split('\n'); let iter = lines.nth(1).unwrap().bytes(); let mut comb = [1u64, 0, 0, 0, 0, 0, 0]; for ch in iter { ...
medium
0574
You are given a set of $$$2n+1$$$ integer points on a Cartesian plane. Points are numbered from $$$0$$$ to $$$2n$$$ inclusive. Let $$$P_i$$$ be the $$$i$$$-th point. The $$$x$$$-coordinate of the point $$$P_i$$$ equals $$$i$$$. The $$$y$$$-coordinate of the point $$$P_i$$$ equals zero (initially). Thus, initially $$$P_...
int solution() { long long n; long long k; while (scanf("%lld%lld", &n, &k) != EOF) { long long t; t = k / n; if (t * n != k) { printf("%lld\n", t + 1); } else { printf("%lld\n", t); } } return 0; }
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() .map(|x| x.parse::<i64>().unwrap()) .collect(); let n = arr[0]; ...
medium
0575
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are two rectangles. The lengths of the vertical sides of the first rectangle are <var>A</var>, and the lengths of the horizontal sides of the first rectangle are <var>B</var>. The lengths of the v...
int solution(void) { int A = 0; int B = 0; int C = 0; int D = 0; int s1 = 0; int s2 = 0; scanf("%d%d%d%d", &A, &B, &C, &D); s1 = A * B; s2 = C * D; if (s1 == s2) { printf("%d\n", s1); } if (s1 > s2) { printf("%d\n", s1); } else if (s1 < s2) { printf("%d\n", s2); } return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let v: Vec<usize> = s.split_whitespace().map(|x| x.parse().unwrap()).collect(); println!("{}", std::cmp::max(v[0] * v[1], v[2] * v[3])); }
medium
0576
I, Fischl, Prinzessin der Verurteilung, descend upon this land by the call of fate an — Oh, you are also a traveler from another world? Very well, I grant you permission to travel with me.It is no surprise Fischl speaks with a strange choice of words. However, this time, not even Oz, her raven friend, can interpret her...
int solution() { int t; int n; scanf("%d", &t); while (t--) { scanf("%d", &n); int b[30]; int flag = 0; memset(b, 0, sizeof(b)); char a[1005]; char tem; for (int i = 0; i < n;) { tem = getchar(); if (tem >= 'a' && tem <= 'z') { a[i] = tem; i++; b[t...
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(); ...
medium
0577
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You drew lottery <var>N</var> times. In the <var>i</var>-th draw, you got an item of the kind represented by a string <var>S_i</var>.</p> <p>How many kinds of items did you get?</p> </section> </div> <d...
int solution(void) { int n; int i; int s; int c; int changed; int j; struct cell head; struct cell *p; for (i = 0; i < 30; i++) { head.next[i] = NULL; } head.e = 0; s = 0; scanf("%d", &n); getchar(); for (i = 0; i < n; i++) { p = &head; changed = 0; while ((c = getchar(...
fn solution() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: i32 = n.trim().parse().unwrap(); let mut m = std::collections::HashMap::<String, bool>::new(); for _ in 0..n { let mut t = String::new(); std::io::stdin().read_line(&mut t).ok(); m.ins...
hard
0578
You have integer $$$n$$$. Calculate how many ways are there to fully cover belt-like area of $$$4n-2$$$ triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. $$$2$$$ coverings are different if some $$$2$$$ triangles are covered by the sa...
int solution(void) { int t; scanf("%d", &t); int n[t]; for (int i = 0; i < t; i++) { scanf("%d", &n[i]); } for (int i = 0; i < t; i++) { printf("%d\n", n[i]); } return 0; }
fn solution() { let mut i = 0u32; let mut t = String::new(); io::stdin().read_line(&mut t).expect("Input Failed"); let t: u32 = t.trim().parse().expect("Not a number"); loop { if i == t { break; } else { let mut n = String::new(); io::stdin().read_...
hard
0579
You are given two segments $$$[l_1; r_1]$$$ and $$$[l_2; r_2]$$$ on the $$$x$$$-axis. It is guaranteed that $$$l_1 &lt; r_1$$$ and $$$l_2 &lt; r_2$$$. Segments may intersect, overlap or even coincide with each other. The example of two segments on the $$$x$$$-axis. Your problem is to find two integers $$$a$$$ and $$...
int solution() { int q; int a[505]; int b[505]; int c[505]; int d[505]; scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]); } for (int i = 0; i < q; i++) { if (a[i] != c[i]) { printf("%d %d\n", a[i], c[i]); } else { printf("%d %d\n", a[...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let n: u32 = line.trim().parse().unwrap(); for _ in 0..n { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let v: Vec<u32> = line.trim().split(' ').map(|x| x.parse(...
medium
0580
According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open.You are given a jacket with n buttons. Determine if it is fastened...
int solution(void) { char c = 0; int n = 0; int i = 0; int cnt = 0; char arr[1000] = { 0, }; scanf("%d", &n); for (i = 0; i < n; i++) { scanf(" %c", &arr[i]); if (arr[i] == '1') { cnt++; } } if (n == 1) { if (cnt == 0) { c = 'N'; } else { c = 'Y'; } ...
fn solution() { let mut buttons = String::new(); let mut arr = String::new(); stdin().read_line(&mut buttons).unwrap(); stdin().read_line(&mut arr).unwrap(); let buttons: u16 = buttons.trim().parse().unwrap(); let arr: Vec<u8> = arr .split_whitespace() .map(|e| if e == "1" { 1 ...
medium
0581
Polycarp is working on a new project called "Polychat". Following modern tendencies in IT, he decided, that this project should contain chat as well. To achieve this goal, Polycarp has spent several hours in front of his laptop and implemented a chat server that can process three types of commands: Include a person to...
int solution() { char status[100]; int quant = 0; int qntBytes = 0; int i = 0; while (scanf("%[^\n] ", status) != EOF) { if (status[0] == '+') { quant++; } else if (status[0] == '-') { quant--; } else { for (i = 0; status[i] != ':'; i++) { } qntBytes += quant * (st...
fn solution() { let reader = BufReader::new(io::stdin()); let mut cnt = 0; let mut length = 0; for line in reader.lines().flatten() { let bs = line.as_bytes(); match bs[0] { b'+' => { cnt += 1; } b'-' => { cnt -= 1; ...
easy
0582
At the store, the salespeople want to make all prices round. In this problem, a number that is a power of $$$10$$$ is called a round number. For example, the numbers $$$10^0 = 1$$$, $$$10^1 = 10$$$, $$$10^2 = 100$$$ are round numbers, but $$$20$$$, $$$110$$$ and $$$256$$$ are not round numbers. So, if an item is worth ...
int solution() { long long int n; long long int t; scanf("%lld", &t); for (int i = 0; i < t; i++) { scanf("%lld", &n); if (n >= 1 && n < 10) { printf("%lld\n", n - 1); } else if (n >= 10 && n < 100) { printf("%lld\n", n - 10); } else if (n >= 100 && n < 1000) { printf("%lld\n",...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).expect("Error!"); let x: i32 = line.trim().parse().expect("Error!"); for _ in 0..x { let mut price = String::new(); io::stdin().read_line(&mut price).expect("Error!"); let _price: i32 = price.trim()....
hard
0583
You are given two huge binary integer numbers $$$a$$$ and $$$b$$$ of lengths $$$n$$$ and $$$m$$$ respectively. You will repeat the following process: if $$$b &gt; 0$$$, then add to the answer the value $$$a~ \&amp;~ b$$$ and divide $$$b$$$ by $$$2$$$ rounding down (i.e. remove the last digit of $$$b$$$), and repeat the...
int solution(void) { int n; int m; int i; int j; int bs[200001]; int dif; char a[200000]; char b[200000]; long long int ans = 0; long long int p2 = 1; scanf("%d %d", &n, &m); scanf("%s", &a); scanf("%s", &b); bs[0] = 1; for (i = 1; i < m; i++) { bs[i] = bs[i - 1]; if (b[i] == '1'...
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 mut str1 = arr[2].parse::<String>().unwrap(); let mut str2 = arr[3].parse::...
medium
0584
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an integer <var>a</var> as input, print the value <var>a + a^2 + a^3</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq a \leq 10</var></li> <li><va...
int solution(void) { int a = 0; scanf("%d", &a); printf("%d", a + (a * a) + (a * a * a)); return 0; }
fn solution() { let mut guess = String::new(); io::stdin() .read_line(&mut guess) .expect("Failed to read line"); let guess: f64 = guess.trim().parse().expect("Please type a number!"); println!("{}", guess + guess * guess + guess * guess * guess); }
medium
0585
You are given an integer $$$n$$$. You have to construct a permutation of size $$$n$$$.A permutation is an array where each integer from $$$1$$$ to $$$s$$$ (where $$$s$$$ is the size of permutation) occurs exactly once. For example, $$$[2, 1, 4, 3]$$$ is a permutation of size $$$4$$$; $$$[1, 2, 4, 5, 3]$$$ is a permutat...
int solution() { int t = 0; scanf("%d", &t); while (t) { int n = 0; scanf("%d", &n); int arr[100]; int tmp1 = 0; int tmp2 = n - 1; for (int i = 0; i < n; i++) { if ((i + 1) % 2 != 0) { arr[tmp1] = i + 1; tmp1++; } if ((i + 1) % 2 == 0) { arr[tmp2] ...
fn solution() { let test_cases = { let mut s = String::new(); io::stdin().read_line(&mut s).expect("read failed"); s.trim().parse::<i32>().unwrap() }; for _ in 0..test_cases { let n = { let mut s = String::new(); io::stdin().read_line(&mut s).expect("...
hard
0586
A telephone number is a sequence of exactly $$$11$$$ digits such that its first digit is 8.Vasya and Petya are playing a game. Initially they have a string $$$s$$$ of length $$$n$$$ ($$$n$$$ is odd) consisting of digits. Vasya makes the first move, then players alternate turns. In one move the player must choose a char...
int solution() { int n = 0; char s[100000]; scanf("%d", &n); scanf("%s", s); int moves = (n - 11) / 2; int eights[100000]; int eights_counter = 0; for (int i = 0; i < n - 10; i++) { if (s[i] == '8') { eights[eights_counter++] = n - i - 11; } } if (eights_counter <= moves) { printf...
fn solution() { let mut input = "".split_ascii_whitespace(); let mut read = || loop { if let Some(word) = input.next() { break word; } input = { let mut input = "".to_owned(); io::stdin().read_line(&mut input).unwrap(); if input.is_empty() ...
easy
0587
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In a flower bed, there are <var>N</var> flowers, numbered <var>1,2,......,N</var>. Initially, the heights of all flowers are <var>0</var>. You are given a sequence <var>h=\{h_1,h_2,h_3,......\}</var> as...
int solution() { int N; scanf("%d", &N); int h[102]; int table[102] = {0}; for (int i = 0; i < N; i++) { scanf("%d", h + i); } int ans = 0; int f = 0; int changed = 0; do { changed = 0; for (int j = 0; j < N; j++) { if (f == 0 && table[j] < h[j]) { f = 1; table[j]++...
fn solution() { let text = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = text.split_whitespace(); let _ = iter.next(); let mut sum = 0i32; let mut prev = 0i32; for height in iter.map(|word...
hard
0588
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi had a pair of two positive integers not exceeding <var>N</var>, <var>(a,b)</var>, which he has forgotten. He remembers that the remainder of <var>a</var> divided by <var>b</var> was greater th...
int solution(void) { unsigned long long n; unsigned long long k; unsigned long long i; unsigned long long cnt; scanf("%llu %llu", &n, &k); cnt = 0; if (k == 0) { printf("%llu\n", n * n); return 0; } for (i = 1; i <= n; i++) { if (i > k) { cnt += (i - k) * (n - n % i) / i; cn...
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: u64 = itr.next().unwrap().parse().unwrap(); let k: u64 = itr.next().unwrap().parse().unwrap(); let mut ans: u64 = 0; for b in k + 1..n + 1 { le...
medium
0589
You went to the store, selling $$$n$$$ types of chocolates. There are $$$a_i$$$ chocolates of type $$$i$$$ in stock.You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy $$$x_i$$$ chocolates of type $$$i$$$ (clearly, $$$0 \le x_i \...
int solution() { int n; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } long long sum = 0; for (int i = n - 1; i >= 0; i--) { sum += arr[i]; if (arr[i] == 1) { break; } if ((i - 1) >= 0 && arr[i - 1] >= arr[i]) { arr[i - 1] = arr[i] - 1; ...
fn solution() { let mut buf = String::new(); stdin().read_to_string(&mut buf).unwrap(); let mut tok = buf.split_whitespace(); let mut get = || tok.next().unwrap(); let n = get!(usize); let mut xs = vec![]; for _ in 0..n { xs.push(get!()); } xs.reverse(); let mut ans ...
medium
0590
It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, one week in Berland lasts k days!When Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept ...
int solution() { int k; int n; scanf("%d %d", &n, &k); int weeks = (n - k) + 1; int a[n]; double soma = 0.0; double interm_soma = 0.0; int start_counting = 0; int teste = 0; for (int i = 0; i < n; i++) { if (i + 1 == k) { start_counting = 1; } scanf("%d", &a[i]); interm_soma +=...
fn solution() { let mut fline = String::new(); let mut nums = String::new(); io::stdin() .read_line(&mut fline) .expect("Can't input first line"); io::stdin() .read_line(&mut nums) .expect("can't input second line"); nums = nums.trim().parse().expect("Can't parse"); ...
medium
0591
Alice and Bob are playing a game on a matrix, consisting of $$$2$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row in the $$$j$$$-th column contains $$$a_{i, j}$$$ coins in it.Initially, both Alice and Bob are standing in a cell $$$(1, 1)$$$. They are going to perform a sequence of moves to reach a cell $$$(2...
int solution() { int t; scanf("%d", &t); while (t--) { int m; scanf("%d", &m); int arr[2][m]; int sumu = 0; int sum1 = 0; int min = 1000000000; int max = 0; for (int i = 0; i < m; i++) { scanf("%d", &arr[0][i]); sumu += arr[0][i]; } for (int i = 0; i < m; i++) {...
fn solution() { let mut line = String::new(); let stdin = io::stdin(); let mut stdin = stdin.lock(); stdin.read_line(&mut line).unwrap(); let t: usize = line.trim().parse().unwrap(); let stdout = io::stdout(); let handle = stdout.lock(); let mut buffer = BufWriter::with_capacity(65536, ...
medium
0592
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have <var>N</var> katana (swords), Katana <var>1</var>, Katana <var>2</var>, <var>…</var>, Katana <var>N</var>, and ...
int solution() { int s[200000][5]; int n; int i; int j; int HP; int max; int katana; int exdamage = 0; int exkatana = 0; int result = 0; int exmax; int memo; int k; int l; int s2[100001]; scanf("%d", &n); scanf("%d", &HP); for (i = 0; i < n; i++) { for (j = 0; j <= 1; j++) { ...
fn solution() { let (n, mut h) = { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); s = s.trim_end().to_owned(); let mut ws = s.split_whitespace(); let n: i32 = ws.next().unwrap().parse().unwrap(); let h: i32 = ws.next().unwrap().parse().unwrap(...
hard
0593
This is an interactive problem.Initially, there is an array $$$a = [1, 2, \ldots, n]$$$, where $$$n$$$ is an odd positive integer. The jury has selected $$$\frac{n-1}{2}$$$ disjoint pairs of elements, and then the elements in those pairs are swapped. For example, if $$$a=[1,2,3,4,5]$$$, and the pairs $$$1 \leftrightarr...
int solution() { int i; int T; int n; int s; int d; int c; int temp; int count; scanf("%d", &T); while (T--) { scanf("%d", &n); s = 1; d = n; while (s < d) { c = s + d >> 1; printf("? %d %d\n", s, c); fflush(stdout); count = 0; for (i = s; i <= c; i++) {...
fn solution() { let num_tests: usize = io::stdin() .lock() .lines() .next() .unwrap() .unwrap() .parse() .unwrap(); for _ in 0..num_tests { let n: i32 = io::stdin() .lock() .lines() .next() .unwrap() ...
medium
0594
You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ ...
int solution() { long long int a; long long int b; long long int k; long long int i; scanf("%lld %lld %lld", &a, &b, &k); if (b == 1) { if (k > 0) { printf("No\n"); } else { printf("Yes\n"); printf("1"); for (i = 1; i <= a; i++) { printf("0"); } printf("\n...
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 (a, b, k) ...
easy
0595
<H1>Triangle</H1><br> <p> For given two sides of a triangle <i>a</i> and <i>b</i> and the angle <i>C</i> between them, calculate the following properties: </p> <ul> <li><var>S</var>: Area of the triangle</li> <li><var>L</var>: The length of the circumference of the triangle</li> <li><var>h</var>: The height o...
int solution() { double a; double b; double c; double PI = acos(-1); scanf("%lf %lf %lf", &a, &b, &c); c /= 180.0; c *= PI; printf("%lf\n%lf\n%lf\n", a * b * 0.5 * sin(c), a + b + sqrt((a * a) + (b * b) - (2.0 * a * b * cos(c))), a * b * sin(c) / a); return 0; }
fn solution() { let stdin = std::io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).unwrap(); let mut ite = buf.split_whitespace().map(|x| x.parse::<f64>().unwrap()); let a = ite.next().unwrap(); let b = ite.next().unwrap(); let c = ite.next().unwrap(); let rad = c.to...
easy
0596
Nezzar has $$$n$$$ balls, numbered with integers $$$1, 2, \ldots, n$$$. Numbers $$$a_1, a_2, \ldots, a_n$$$ are written on them, respectively. Numbers on those balls form a non-decreasing sequence, which means that $$$a_i \leq a_{i+1}$$$ for all $$$1 \leq i &lt; n$$$.Nezzar wants to color the balls using the minimum nu...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int arr[n]; int repeat = 0; int max = 0; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); if (arr[i] == arr[i - 1]) { repeat++; } else { if (repeat > max) { ...
fn solution() { let (i, o) = (io::stdin(), io::stdout()); let mut o = bw::new(o.lock()); for l in i.lock().lines().skip(2).step_by(2) { let mut a = [0; 101]; l.unwrap() .split(' ') .for_each(|w| a[w.parse::<usize>().unwrap()] += 1); writeln!(o, "{}", a.iter()....
hard
0597
There are $$$n$$$ students standing in a circle in some order. The index of the $$$i$$$-th student is $$$p_i$$$. It is guaranteed that all indices of students are distinct integers from $$$1$$$ to $$$n$$$ (i. e. they form a permutation).Students want to start a round dance. A clockwise round dance can be started if the...
int solution() { int t; scanf(" %d", &t); while (t--) { int n; scanf(" %d", &n); int nums[n]; for (int i = 0; i < n; ++i) { scanf(" %d", &nums[i]); } int first = nums[0]; int po = 1; for (int i = 1; i < n && po; ++i) { if (nums[i] - 1 != first && (nums[i] != 1 || first ...
fn solution() -> Result<(), Box<dyn std::error::Error>> { 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 = next!(u32); for _ in 0..q { let n = next!(u32); ...
hard
0598
<!-- begin en only --> <h3><U>Space Coconut Crab</U></h3> <!-- end en only --> <!-- begin ja only --> <h3><U>宇宙ヤシガニ</U></h3> <!-- end ja only --> <!-- begin en only --> <p> English text is not available in this practice contest. </p> <!-- end en only --> <!-- begin ja only --> <p> ケン・マリンブルーは,宇宙ヤシガニを求めて全銀河を旅するスペースハンタ...
int solution(void) { int e; int x = 0; int y = 0; int z = 0; int m = 1000000; while (1) { m = 1000000; scanf("%d", &e); if (e == 0) { break; } for (z = 0; z < 101; z++) { for (y = 0; y < 1001; y++) { x = e - z * z * z - y * y; if (x < 0) { continue;...
fn solution() { loop { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let mut iter = s.split_whitespace(); let e: i64 = iter.next().unwrap().parse().unwrap(); if e == 0 { break; } let mut min_ = 1e8 as i64; let _f...
medium
0599
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a rectangle in a coordinate plane. The coordinates of the four vertices are <var>(0,0)</var>, <var>(W,0)</var>, <var>(W,H)</var>, and <var>(0,H)</var>. You are given a point <var>(x,y)</var> wh...
int solution(void) { double w = 0; double h = 0; double x = 0; double y = 0; double max; scanf("%lf %lf %lf %lf", &w, &h, &x, &y); max = (w * h) / 2; if (w == x * 2 && h == y * 2) { printf("%lf %d", max, 1); } else { printf("%lf %d", max, 0); } return 0; }
fn solution() { let (w, h, x, y) = { let mut inputline = String::new(); std::io::stdin().read_line(&mut inputline).ok(); let mut inputline = inputline.split_whitespace(); ( inputline.next().unwrap().parse::<usize>().ok().unwrap(), inputline.next().unwrap().par...
easy
0600
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition.There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ posit...
int solution(void) { int n = 0; int m = 0; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { printf("%d", i % 2); } printf("\n"); return 0; }
fn solution() { let mut buffer = String::new(); io::stdin().read_line(&mut buffer).expect("err"); let n: i32 = buffer.split_whitespace().collect::<Vec<&str>>()[0] .parse() .unwrap(); for i in 1..n { print!("{}", { if i % 2 == 0 { '1' } else...
medium