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
1501
You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in th...
int solution() { int cases; scanf("%d", &cases); while (cases--) { int arrLen; scanf("%d", &arrLen); int array[arrLen]; for (int index = 0; index < arrLen; index++) { scanf("%d ", array + index); } if (arrLen % 2 == 0) { for (int index = 0; index < arrLen; index += 2) { ...
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().flat_map(str::parse); let t = input.next().unwrap(); for _ in 0..t { let n = input.next().unwrap(); let a: Vec<_>...
medium
1502
<H1>Factorial</H1> <p> Write a program which reads an integer <var>n</var> and prints the factorial of <var>n</var>. You can assume that <var>n</var> &le; 20. </p> <H2>Input</H2> <p> An integer <var>n</var> (1 &le; <var>n</var> &le; 20) in a line. </p> <H2>Output</H2> <p> Print the factorial of <var>n</var> in a l...
int solution(void) { long long int f = 1; int a = 0; scanf("%d", &a); while (a) { f *= a; a--; } printf("%lld\n", f); return 0; }
fn solution() { let mut buf = String::new(); stdin().read_line(&mut buf).unwrap(); let n = buf.trim().parse::<u64>().unwrap(); let mut ans = 1; for i in 2..n + 1 { ans *= i; } println!("{}", ans); }
medium
1503
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A railroad running from west to east in Atcoder Kingdom is now complete.</p> <p>There are <var>N</var> stations on the railroad, numbered <var>1</var> through <var>N</var> from west to east.</p> <p>Tomo...
int solution() { int n; scanf("%d", &n); int c[n - 1]; int s[n - 1]; int f[n - 1]; for (int i = 0; i < n - 1; i++) { scanf("%d %d %d", &c[i], &s[i], &f[i]); } for (int i = 0; i < n - 1; i++) { int t = 0; for (int j = i; j < n - 1; j++) { t = (((t - 1) / f[j] + 1) * f[j] < s[j]) ? s[...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).ok(); let mut it = buf.split_whitespace(); let n = it.next().unwrap().parse::<usize>().unwrap() - 1; let mut t = Vec::new(); for _ in 0..n { let c = it.next().unwrap().parse::<i64>().unwrap(); ...
medium
1504
<H1>Sum of Numbers</H1><br> <p> Write a program which reads an integer and prints sum of its digits. </p> <H2>Input</H2> <p> The input consists of multiple datasets. For each dataset, an integer <var>x</var> is given in a line. The number of digits in <var>x</var> does not exceed 1000. </p> <p> The input ends ...
int solution(void) { char str[1001] = {0}; int i = 0; int sum = 0; while (1) { scanf("%c", &str[i]); if (str[i] == '\n') { if (i == 1 && str[0] == '0') { break; } printf("%d\n", sum); sum = 0; i = 0; } else { sum += str[i] - 0x30; i++; } } ...
fn solution() { loop { let mut b = String::new(); let s = std::io::stdin(); s.read_line(&mut b).unwrap(); if b.trim() == "0" { break; } println!( "{}", b.trim() .chars() .map(|c| c.to_digit(10).unwrap...
medium
1505
You are given two integers $$$n$$$ and $$$k$$$.You should create an array of $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$ such that the sum $$$(a_1 + a_2 + \dots + a_n)$$$ is divisible by $$$k$$$ and maximum element in $$$a$$$ is minimum possible.What is the minimum possible maximum element in $$$a$$$?
int solution() { int t; scanf("%d", &t); long long output[t]; for (int i = 0; i < t; i++) { long long n; long long k; scanf("%lld %lld", &n, &k); long long mult = k; while (mult / n == 0) { if (n % k) { mult = (n / k) * k + k; } else { mult = (n / k) * k; } ...
fn solution() { let stdin = io::stdin(); let mut input = stdin.lock(); let mut buf = String::new(); input.read_line(&mut buf).unwrap(); let t = buf.trim().parse().unwrap(); for _ in 0..t { buf.clear(); input.read_line(&mut buf).unwrap(); let mut words = buf.trim().split...
medium
1506
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a sequence of length <var>N</var>, <var>a = (a_1, a_2, ..., a_N)</var>. Each <var>a_i</var> is a positive integer.</p> <p>Snuke's objective is to permute the element in <var>a</var> so that the ...
int solution(void) { int n = 0; long long s[150000] = {0}; int s4 = 0; int s2 = 0; int i = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lld", &s[i]); } for (i = 0; i < n; i++) { if (s[i] % 4 == 0) { s4++; } else if (s[i] % 2 == 0) { s2++; } } if (s4 * 2 + s2 ==...
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let a: Vec<usize> = s.split_whitespace().map(|x| x.parse().unwrap()).collect(); let mut x: isize = 0; let mut y: isize = 0; let...
medium
1507
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has decided to play with a six-sided die. Each of its six sides shows an integer <var>1</var> through <var>6</var>, and two numbers on opposite sides always add up to <var>7</var>.</p> <p>Snuke wi...
int solution(void) { long long int x; scanf("%lld", &x); if (1 <= x && x <= 6) { printf("1"); } else if (x <= 10) { printf("2"); } else if (x % 11 == 0) { printf("%lld", 2 * x / 11); } else if (1 <= x % 11 && x % 11 <= 6) { printf("%lld", (2 * (x / 11)) + 1); } else if (7 <= x % 11 && x % ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).ok(); let x: u64 = s.trim().parse().unwrap(); let r = x / 11; let m = x % 11; println!( "{}", if m == 0 { r * 2 } else if m > 6 { r * 2 + 2 } else { r *...
easy
1508
A rectangle with its opposite corners in $$$(0, 0)$$$ and $$$(w, h)$$$ and sides parallel to the axes is drawn on a plane.You are given a list of lattice points such that each point lies on a side of a rectangle but not in its corner. Also, there are at least two points on every side of a rectangle.Your task is to choo...
int solution() { int n_case = 0; scanf("%d", &n_case); long long w = 0; long long h = 0; long long w_up[2 * 100000]; long long k = 0; long long min = 0; long long max = 0; long long a[4]; long long final = 0; for (int ii = 0; ii < n_case; ++ii) { scanf("%lld", &w); scanf("%lld", &h); s...
fn solution() { let mut buf = String::new(); let stdin = io::stdin(); let r = stdin.read_line(&mut buf); match r { Ok(_) => {} _ => panic!("could not read from stdin"), } let n: u128 = buf.trim().parse().unwrap(); for _i in 0..n { let mut buf = String::new(); ...
medium
1509
David was given a red checkered rectangle of size $$$n \times m$$$. But he doesn't like it. So David cuts the original or any other rectangle piece obtained during the cutting into two new pieces along the grid lines. He can do this operation as many times as he wants.As a result, he will get a set of rectangles. Recta...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int m; scanf("%d %d", &n, &m); if (n == 2 && m == 2) { printf("2\n"); continue; } if (n % 3 == 0 || m % 3 == 0) { printf("%d\n", (m * n) / 3); continue; } else if (m % 3 == 1) { printf("%d\n",...
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] = l .unwrap() .split(' ') .map(|w| w.parse::<u32>().unwrap()) .collect::<Vec<_>>()[..] { writ...
medium
1510
Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)!He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help!The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths ...
int solution() { int n; int a[100000]; int b[100000]; int s[100000]; int k; int root; int n_r; scanf("%d", &n); for (int i = 0; i < n; i++) { s[i] = 0; } for (int i = 0; i < n - 1; i++) { scanf("%d%d", &a[i], &b[i]); s[a[i] - 1] += 1; s[b[i] - 1] += 1; } k = 0; for (int i = ...
fn solution() { let stdin = io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf); let n: i32 = buf.trim().parse::<i32>().unwrap(); let mut edges: Vec<Vec<i32>> = Vec::new(); for _idx in 0..n { let mut buf = String::new(); stdin.read_line(&mut buf); let ...
medium
1511
You are given an array $$$a$$$ consisting of $$$n$$$ integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from $$$a$$$ to make it a good array. Recall that the prefix of the array $$$a=[a_1, a_2, \dots, a_n]$$$ is a subarray consisting several first elements: the prefix ...
int solution() { int t = 0; scanf("%d", &t); int i = 0; for (; i < t; i++) { int n = 0; scanf("%d", &n); int v[200000] = {0}; int j = 0; for (; j < n; j++) { scanf("%d", &v[j]); } int suf = 0; int aux = 0; for (j = 0; j < n; j++) { if (aux == 0 && v[n - j - 1] > v...
fn solution() { let t: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().parse().unwrap() }; 'outer: for _ in 0..t { let n: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf)....
hard
1512
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given are integers <var>A</var>, <var>B</var>, and <var>N</var>.</p> <p>Find the maximum possible value of <var>floor(Ax/B) - A × floor(x/B)</var> for a non-negative integer <var>x</var> not greater tha...
int solution() { long int x = 0; double A; double B; double N; scanf("%lf %lf %lf", &A, &B, &N); if (N >= B - 1) { x = A * ((B - 1) / B); printf("%ld", x); } else { x = A * (N / B); printf("%ld", x); } return 0; }
fn solution() { let mut line = String::new(); std::io::stdin().read_to_string(&mut line); let mut iter = line.split_whitespace(); let a: i64 = iter.next().unwrap().parse().unwrap(); let b: i64 = iter.next().unwrap().parse().unwrap(); let n: i64 = iter.next().unwrap().parse().unwrap(); if b <...
easy
1513
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We say that a odd number <var>N</var> is <em>similar to 2017</em> when both <var>N</var> and <var>(N+1)/2</var> are prime.</p> <p>You are given <var>Q</var> queries.</p> <p>In the <var>i</var>-th query,...
int solution(void) { long q; scanf("%ld", &q); long l[q]; long r[q]; for (long i = 0; i < q; i++) { scanf("%ld %ld", &l[i], &r[i]); } int prime[200001]; prime[0] = 0; prime[1] = 0; for (long i = 2; i < 200001; i++) { prime[i] = 1; } for (long i = 2; i < 200001; i++) { if (prime[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 q: usize = itr.next().unwrap().parse().unwrap(); let mut out = Vec::new(); let mut is_prime: Vec<bool> = vec![true; 100010]; let mut cumsum: Vec<u64> = ve...
easy
1514
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi wants to print a document with <var>N</var> pages double-sided, where two pages of data can be printed on one sheet of paper.</p> <p>At least how many sheets of paper does he need?</p> </secti...
int solution() { int N = 0; scanf("%d", &N); if (N % 2 == 0 && (N >= 1 && N <= 100)) { printf("%d", N / 2); } else if (N % 2 == 1 && (N >= 1 && N <= 100)) { printf("%d", (N / 2) + 1); } }
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).expect("error"); let n: i32 = match buf.trim().parse() { Ok(num) => num, Err(_) => panic!("error"), }; println!("{}", n / 2 + n % 2); }
easy
1515
Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!).Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps: Replace each o...
int solution() { int n; int a[1005] = {0}; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n - 1; i++) { if (a[i] & 1) { printf("%d ", a[i]); } else { printf("%d ", a[i] - 1); } } if (a[n - 1] & 1) { printf("%d\n", a[n - 1]); } e...
fn solution() { { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); } let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let nums: Vec<u32> = buf .split_whitespace() .map(|elem| elem.parse().unwrap()) .collec...
medium
1516
Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly $$$1$$$ problem, during the second day — exactly $$$2$$$ problems, during the third day — exactly $$$3$$$ problems, and so on. During the $$$k$$$-th day he should solve $$$k$$$ problems.Polycarp ...
int solution() { int n; scanf("%d", &n); int a[n]; int i; int b[200001] = {0}; for (i = 0; i < n; i++) { scanf("%d", &a[i]); b[a[i]]++; } int ans = 1; for (i = 1; i < 200001; i++) { if (i == ans && b[i] > 0) { ans++; } else if (i > ans && b[i] > 0) { if (ans + b[i] > i) { ...
fn solution() { let mut input = String::new(); std::io::stdin() .read_line(&mut input) .expect("INPUT::read line failed"); let contests = input .trim() .parse::<i32>() .expect("CONTESTS::parse to i32 failed"); input.clear(); std::io::stdin() .read_lin...
hard
1517
Madoka finally found the administrator password for her computer. Her father is a well-known popularizer of mathematics, so the password is the answer to the following problem.Find the maximum decimal number without zeroes and with no equal digits in a row, such that the sum of its digits is $$$n$$$.Madoka is too tired...
int solution() { char result[1000] = {0}; int t = 0; int n = 0; int bd = 0; scanf("%d", &t); for (int i = 0; i < t; i++) { memset(result, 0, 1000); bd = 0; scanf("%d", &n); if (n == 1) { printf("1"); printf("\n"); } else if (n == 2) { printf("2"); printf("\n"); ...
fn solution() { let mut events = String::new(); io::stdin() .read_line(&mut events) .expect("Failed to read input"); let events: i64 = events.trim().parse().expect("Invalid input"); let mut count = 0; while count < events { let mut number = String::new(); io::stdin()...
medium
1518
Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. I...
int solution() { long int n; scanf("%ld", &n); long int arrI[n]; long int arrM[300000] = {0}; int i; int s; int m; int b; for (i = 0; i < n; i++) { arrI[i] = 0; } int j; int k; s = 0; m = 0; b = 0; long long int sumS = 0; long long int sumB = 0; long long int x; for (i = 0; i <...
fn solution() { let mut v: Vec<i32> = BufReader::new(io::stdin()) .lines() .nth(1) .unwrap() .unwrap() .split_whitespace() .map(|s| s.parse::<i32>().unwrap()) .collect(); v.sort(); let mut res = 0u64; for (i, itm) in v.iter().enumerate() { ...
hard
1519
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> people standing in a queue from west to east.</p> <p>Given is a string <var>S</var> of length <var>N</var> representing the directions of the people. The <var>i</var>-th person fr...
int solution(void) { int n; int k; char s[199999]; int ans = 0; int tmp = 0; scanf("%d %d", &n, &k); scanf("%s", s); for (int i = 0; i < n; i++) { if (s[i] == 'L') { if (i >= 1) { if (s[i - 1] == 'L') { tmp += 1; } } } else { if (i < n) { ...
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 k: usize = s.next().unwrap().parse().unwrap(); let mut s: Vec<_> = s.next().unwrap().chars(...
hard
1520
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Find the number of integers between <var>1</var> and <var>N</var> (inclusive) that contains exactly <var>K</var> non-zero digits when written in base ten.</p> </section> </div> <div class="part"> <secti...
int solution(void) { char n[200]; scanf("%s", n); int length = strlen(n); int k; scanf("%d", &k); long dp0[length][4]; long dp1[length][4]; dp0[0][0] = 1; dp0[0][1] = n[0] - '0' - 1; dp0[0][2] = 0; dp0[0][3] = 0; dp1[0][0] = 0; dp1[0][1] = 1; dp1[0][2] = 0; dp1[0][3] = 0; for (int i = 1...
fn solution() { input! { N: chars, K: usize, } let mut dp = [[[0; 2]; 4]; 101]; dp[0][0][0] = 1; for (i, &c) in N.iter().enumerate() { let d = c as i32 - '0' as i32; for j in 0..(K + 1) { for k in 0..2 { for di in 0..10 { ...
medium
1521
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In AtCoder City, there are three stations numbered <var>1</var>, <var>2</var>, and <var>3</var>.</p> <p>Each of these stations is operated by one of the two railway companies, A and B. A string <var>S</...
int solution(void) { char s[3]; scanf("%c %c %c", &s[0], &s[1], &s[2]); if ((s[0] == 'A' && s[1] == 'A' && s[2] == 'A') || (s[0] == 'B' && s[1] == 'B' && s[2] == 'B')) { printf("No\n"); } else { printf("Yes\n"); } return 0; }
fn solution() { let mut input = String::new(); if io::stdin().read_line(&mut input).is_ok() { let s = input.trim(); println!( "{}", if s == "AAA" || s == "BBB" { "No" } else { "Yes" } ); } }
medium
1522
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>We have a canvas divided into a grid with <var>H</var> rows and <var>W</var> columns. The square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left is represented as <v...
int solution() { char canvas[52][52]; int h = 0; int w = 0; int loop_h = 0; int loop_w = 0; int flag = 0; char strin[52]; memset(canvas, 0x00, sizeof(canvas)); scanf("%d%d", &h, &w); while ('\n' != getchar()) { ; } for (loop_h = 0; loop_h < h; loop_h++) { memset(strin, 0x00, sizeof(st...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let y: usize = iter.next().unwrap().parse().unwrap(); let x: usize = iter.next().unwrap().parse().unwrap(); let squares: Vec<Vec<char>> = (0..y) .map(...
medium
1523
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an undirected unweighted graph with <var>N</var> vertices and <var>M</var> edges that contains neither self-loops nor double edges.<br/> Here, a <em>self-loop</em> is an edge where <var>a_...
int solution() { int n; int m; int i; int ab[30][2]; int ans = 0; int ttyk[10][10] = {0}; int j; scanf("%d%d", &n, &m); for (i = 0; i < m; i++) { scanf("%d%d", &ab[i][0], &ab[i][1]); ttyk[ab[i][0]][ab[i][1]] = 1; ttyk[ab[i][1]][ab[i][0]] = 1; } int dfs[100000][10] = {0}; int st = 0; ...
fn solution() { let mut stdin = io::stdin(); let mut lines = String::new(); let _ = stdin.read_to_string(&mut lines); let vec = lines.lines().collect::<Vec<&str>>(); let firstline = vec[0].split(' ').collect::<Vec<&str>>(); let n: usize = firstline[0].trim().parse().unwrap(); let m: usize = ...
hard
1524
There are $$$n$$$ workers and $$$m$$$ tasks. The workers are numbered from $$$1$$$ to $$$n$$$. Each task $$$i$$$ has a value $$$a_i$$$ — the index of worker who is proficient in this task.Every task should have a worker assigned to it. If a worker is proficient in the task, they complete it in $$$1$$$ hour. Otherwise, ...
int solution() { int testcases; int n; int m; long long int extra; long long int need; int low; int high; int mid; scanf("%d", &testcases); int a[200500]; int temp; while (testcases--) { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { a[i] = 0; } for (int i = 0; i < m;...
fn solution() { input! { name = reader, tests: usize } for _ in 0..tests { input! { use reader, n: usize, m: usize, a: [usize1; m] } let mut cnt = vec![0i64; n]; for a in a { cnt[a] += 1; } ...
medium
1525
A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is...
int solution() { int T = 1; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = n - 1; i >= 0; i--) { printf("%d ", a[i]); } printf("\n"); } return 0; }
fn solution() { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); let t: u32 = input.trim().parse().unwrap(); input.clear(); let _ans: &mut Vec<u32>; let mut q: Vec<String>; for _i in 0..t { io::stdin().read_line(&mut input).unwrap(); input.clear(); ...
medium
1526
There are $$$n$$$ boxers, the weight of the $$$i$$$-th boxer is $$$a_i$$$. Each of them can change the weight by no more than $$$1$$$ before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.It is necessary to choose the largest boxing team in...
int solution() { int n; int m; int i; int j; int k; int arr[200009] = {}; int brr[150009] = {}; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &m); arr[m]++; } k = 0; for (i = 1; i <= 150001; i++) { if (arr[i] >= 1 && i > 1 && brr[i - 1] == 0) { k++; brr[i - 1] = ...
fn solution() { let mut v: Vec<i32> = BufReader::new(io::stdin()) .lines() .nth(1) .unwrap() .unwrap() .split_whitespace() .map(|s| s.parse::<i32>().unwrap()) .collect(); v.sort(); let mut max = 0u32; let mut cnt = 0u32; for mut w in v { ...
hard
1527
The Squareland national forest is divided into equal $$$1 \times 1$$$ square plots aligned with north-south and east-west directions. Each plot can be uniquely described by integer Cartesian coordinates $$$(x, y)$$$ of its south-west corner.Three friends, Alice, Bob, and Charlie are going to buy three distinct plots of...
int solution(int argc, char const *argv[]) { int x1; int x2; int x3; int y1; int y2; int y3; scanf("%d%d", &x1, &y1); scanf("%d%d", &x2, &y2); scanf("%d%d", &x3, &y3); int minx = 2000; int miny = 2000; int maxx = -1; int maxy = -1; if (x1 < minx) { minx = x1; } if (x2 < minx) { m...
fn solution() { let mut visited = vec![vec![0; 1005]; 1005]; let mut v = Vec::new(); for _ in 0..3 { let mut s = String::new(); stdin().read_line(&mut s).ok(); let mut iter = s.split_whitespace(); let t: (usize, usize) = ( iter.next().unwrap().parse().unwrap(), ...
hard
1528
Madoka is a very strange girl, and therefore she suddenly wondered how many pairs of integers $$$(a, b)$$$ exist, where $$$1 \leq a, b \leq n$$$, for which $$$\frac{\operatorname{lcm}(a, b)}{\operatorname{gcd}(a, b)} \leq 3$$$.In this problem, $$$\operatorname{gcd}(a, b)$$$ denotes the greatest common divisor of the nu...
int solution() { int licz; scanf("%d", &licz); int wczyt[licz]; int i = 0; for (i = 0; i < licz; i++) { scanf("%d", &wczyt[i]); } for (i = 0; i < licz; i++) { printf("%d\n", wczyt[i] + ((wczyt[i] / 2) * 2) + ((wczyt[i] / 3) * 2)); } return 0; }
fn solution() { let std_in = stdin(); let mut input = Scanner::new(std_in.lock()); let std_out = stdout(); let mut output = BufWriter::new(std_out.lock()); let t: usize = input.token(); for _ in 0..t { let n: usize = input.token(); let result = n + 2 * (n / 2 + n / 3); ...
medium
1529
Arkady is playing Battleship. The rules of this game aren't really important.There is a field of $$$n \times n$$$ cells. There should be exactly one $$$k$$$-decker on the field, i. e. a ship that is $$$k$$$ cells long oriented either horizontally or vertically. However, Arkady doesn't know where it is located. For each...
int solution() { int n; int k; int i; int j; int bi = 0; int bj = 0; int bv = 0; char map[101][101]; scanf("%d%d", &n, &k); for (i = 0; i < n; i++) { scanf("%s", map[i]); } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (map[i][j] == '#') { continue; } i...
fn solution() { use std::io; use std::io::prelude::*; let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); let mut it = input.split_whitespace(); let n: usize = it.next().unwrap().parse().unwrap(); let k: usize = it.next().unwrap().parse().unwrap(); let mut ...
medium
1530
Vasya has a grid with $$$2$$$ rows and $$$n$$$ columns. He colours each cell red, green, or blue.Vasya is colourblind and can't distinguish green from blue. Determine if Vasya will consider the two rows of the grid to be coloured the same.
int solution() { int n = 0; int m = 0; scanf("%d", &m); getchar(); for (int v = 0; v < m; v++) { scanf("%d", &n); getchar(); char s1[9999] = "0"; char s2[9999] = "0"; for (int i = 0; i < n; i++) { scanf("%c", &s1[i]); if (s1[i] == 'B') { s1[i] = 'G'; } } ...
fn solution() { let mut inp = String::new(); std::io::stdin().read_line(&mut inp).unwrap(); let t: i32 = inp.trim().parse().unwrap(); for _ in 0..t { inp = String::new(); std::io::stdin().read_line(&mut inp).unwrap(); let mut l1 = String::new(); std::io::stdin().read_l...
easy
1531
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are parking at a parking lot. You can choose from the following two fee plans:</p> <ul> <li>Plan <var>1</var>: The fee will be <var>A×T</var> yen (the currency of Japan) when you park for <var>T</va...
int solution(void) { int a = 0; int b = 0; int n = 0; scanf("%d %d %d", &n, &a, &b); if (a * n <= b) { printf("%d\n", a * n); } else { printf("%d\n", b); } return 0; }
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).ok(); let input: Vec<i32> = buf.split_whitespace().map(|c| c.parse().unwrap()).collect(); let n = input[0]; let a = input[1]; let b = input[2]; println!("{}", std::cmp::min(n * a, b)); }
medium
1532
You are given a following process. There is a platform with $$$n$$$ columns. $$$1 \times 1$$$ squares are appearing one after another in some columns on this platform. If there are no squares in the column, a square will occupy the bottom row. Otherwise a square will appear at the top of the highest square of this colu...
int solution() { int len = 0; int num = 0; scanf("%d %d", &len, &num); int arr[1000] = {0}; int count = 0; int stop = 0; for (int i = 0; i < num; i++) { scanf("%d", &arr[i]); } int platform[1000] = {0}; for (int i = 0; i < num; i++) { for (int j = 1; j < len + 1; j++) { if (arr[i] == j...
fn solution() { let mut input = String::new(); io::stdin().read_line(&mut input); let i = input.clone(); let mut iter1 = i.split_whitespace().map(|x| x.parse::<usize>().unwrap()); let squares = iter1.next().unwrap(); input = String::new(); io::stdin().read_line(&mut input); let iter = i...
hard
1533
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is always an integer in Takahashi's mind.</p> <p>Initially, the integer in Takahashi's mind is <var>0</var>. Takahashi is now going to eat four symbols, each of which is <code>+</code> or <code>-<...
int solution(void) { char S[3]; scanf("%c%c%c%c", &S[0], &S[1], &S[2], &S[3]); int x = 0; for (int i = 0; i < 4; i++) { if (S[i] == '+') { x++; } else if (S[i] == '-') { x--; } } printf("%d\n ", x); }
fn solution() { let stdin = io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).ok(); let mut cnt: isize = 0; for c in buf.chars() { if c == '+' { cnt += 1; } else if c == '-' { cnt -= 1; } } println!("{}", cnt); }
easy
1534
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi's house has only one socket.</p> <p>Takahashi wants to extend it with some number of power strips, each with <var>A</var> sockets, into <var>B</var> or more empty sockets.</p> <p>One power str...
int solution(void) { int mouth = 0; int want = 0; int ans = 0; int ima = 1; int flag = 0; scanf("%d", &mouth); scanf("%d", &want); while (flag == 0) { if (ima < want) { ima = ima - 1; ima = ima + mouth; ans++; } else { flag = 1; } } printf("%d", ans); return ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).ok(); let v: Vec<i32> = s .split_whitespace() .map(|n| n.parse().ok().unwrap()) .collect(); let a = v[0]; let b = v[1]; let _s = a - 1; let _t = b - 1; let mut r: i32 = 0; let mut o: i32 ...
medium
1535
<span class="lang-en lang-child hidden-lang"> <div id="task-statement"> Max Score: <var>350</var> Points <br/> <section> <h3>Problem Statement</h3> There are <var>N</var> buildings along the line. The <var>i</var>-th building from the left is colored in color <var>i</var>, and its height is currently <var>a_i<...
int solution(void) { int n; int k; scanf("%d%d", &n, &k); int a[15]; int i; int j; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } long long int ans = 999999999999999; int bit; int a_temp[15]; int can_see = 0; int maxh = 0; long long int cost; for (bit = 0; bit < (1 << n); ++bit) { ...
fn solution() { input! { n: usize, k: usize, a: [usize; n] } let mut ans = usize::max_value(); for i in 0..1 << n { let i: usize = i; let q = i.count_ones() as usize; if q < k { continue; } let mut c = 0; let mut h = a[0]; f...
medium
1536
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><style> #nck { width: 30px; height: auto; } </style> <p>We have a grid of <var>H</var> rows and <var>W</var> columns. Initially, there is a stone in the top left cell. Shik is trying to m...
int solution(void) { int H; int W; scanf("%d%d", &H, &W); char a[H][W + 1]; for (int i = 0; i < H; i++) { scanf("%s", a[i]); } int s[H][W]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (a[i][j] == '#') { s[i][j] = 1; } else { s[i][j] = 0; } ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).ok(); let hw: Vec<_> = s.trim().split(' ').map(|x| x.parse().unwrap()).collect(); let mut xs = vec![vec![false; hw[1]]; hw[0]]; let mut res = true; 'outer: for i in 0..hw[0] { let mut s = String::new(); ...
medium
1537
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Find <var>\displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}</var>.</p> <p>Here <var>\gcd(a,b,c)</var> denotes the greatest common divisor of <var>a</var>, <var>b</var>, and <var>c</v...
int solution(void) { int k; long long int ans = 0; int min = 200; scanf("%d", &k); for (int i = 1; i <= k; i++) { for (int j = 1; j <= k; j++) { for (int l = 1; l <= k; l++) { min = fmin(i, j); min = fmin(min, l); for (int m = min; m > 0; m--) { if (i % m == 0 && j...
fn solution() { let result: Vec<i32> = vec![ 1, 9, 30, 76, 141, 267, 400, 624, 885, 1249, 1590, 2208, 2689, 3411, 4248, 5248, 6081, 7485, 8530, 10248, 11889, 13687, 15228, 17988, 20053, 22569, 25242, 28588, 31053, 35463, 38284, 42540, 46581, 50893, 55362, 61824, 65857, 71247, 76884, 84388, 8...
hard
1538
Polycarp got an array of integers $$$a[1 \dots n]$$$ as a gift. Now he wants to perform a certain number of operations (possibly zero) so that all elements of the array become the same (that is, to become $$$a_1=a_2=\dots=a_n$$$). In one operation, he can take some indices in the array and increase the elements of the ...
int solution() { int n = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { int a = 0; scanf("%d", &a); int max = 0; int min = 1000000000; for (int j = 0; j < a; j++) { int temp = 0; scanf("%d", &temp); if (temp > max) { max = temp; } if (temp < min) { ...
fn solution() { let mut buf = BufWriter::new(io::stdout().lock()); io::stdin() .lines() .skip(2) .step_by(2) .flatten() .for_each(|s| { let (max, min) = s .split_ascii_whitespace() .flat_map(str::parse::<usize>) ...
hard
1539
You are given a permutation $$$a_1, a_2, \ldots, a_n$$$ of size $$$n$$$, where each integer from $$$1$$$ to $$$n$$$ appears exactly once.You can do the following operation any number of times (possibly, zero): Choose any three indices $$$i, j, k$$$ ($$$1 \le i &lt; j &lt; k \le n$$$). If $$$a_i &gt; a_k$$$, replace $...
int solution() { int t = 0; int a = 0; int b[100] = {0}; scanf("%d", &t); while (t != 0) { scanf("%d", &a); for (int i = 0; i < a; i++) { scanf("%d ", &b[i]); } if (b[0] == 1) { printf("\nYes\n"); } else { printf("\nNo\n"); } t--; } return 0; }
fn solution() { let mut buf = BufWriter::new(io::stdout().lock()); io::stdin() .lines() .skip(2) .step_by(2) .flatten() .for_each(|s| { writeln!(buf, "{}", if s.starts_with("1 ") { "Yes" } else { "No" }).unwrap_or_default(); }); }
medium
1540
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>K</var> items placed on a grid of squares with <var>R</var> rows and <var>C</var> columns. Let <var>(i, j)</var> denote the square at the <var>i</var>-th row (<var>1 \leq i \leq R</var>) ...
int solution() { int R; int C; int K; scanf("%d %d %d", &R, &C, &K); int i; int j; int r[200005]; int c[200005]; long long int v[200005]; for (i = 0; i < K; i++) { scanf("%d %d %lld", &r[i], &c[i], &v[i]); } int ind[3003][3003]; for (i = 0; i < R; i++) { for (j = 0; j < C; j++) { ...
fn solution() { let mut buf = String::new(); std::io::stdin() .read_to_string(&mut buf) .expect("fail to read"); let mut iter = buf.split_whitespace(); let h: usize = iter.next().unwrap().parse().unwrap(); let w: usize = iter.next().unwrap().parse().unwrap(); let k: usize = iter...
medium
1541
Ashish has a string $$$s$$$ of length $$$n$$$ containing only characters 'a', 'b' and 'c'.He wants to find the length of the smallest substring, which satisfies the following conditions: Length of the substring is at least $$$2$$$ 'a' occurs strictly more times in this substring than 'b' 'a' occurs strictly more ti...
int solution() { int t; scanf("%d", &t); int n; for (int m = 0; m < t; m++) { scanf("%d", &n); char str[n]; scanf("%s", str); int b[n]; int c[n]; int store[n]; int k = 0; int cb = 0; int ca = 0; int cc = 0; for (int i = 0; str[i] != '\0'; i++) { if (str[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(); ...
medium
1542
Phoenix has collected $$$n$$$ pieces of gold, and he wants to weigh them together so he can feel rich. The $$$i$$$-th piece of gold has weight $$$w_i$$$. All weights are distinct. He will put his $$$n$$$ pieces of gold on a weight scale, one piece at a time. The scale has an unusual defect: if the total weight on it is...
int solution() { int t = 0; scanf("%d", &t); while (t--) { int n = 0; int x = 0; scanf("%d", &n); scanf("%d", &x); int w[n]; for (int i = 0; i < n; i++) { scanf("%d", &w[i]); } int sum = 0; int flag = 0; for (int i = 0; i < n; i++) { sum += w[i]; if (sum >...
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).expect("Failed read"); let t: i32 = buf.trim().parse().expect("Failed parse"); buf = String::new(); for _ in 0..t { std::io::stdin().read_line(&mut buf).expect("Failed read"); let x: i32 = buf.split_w...
medium
1543
<span class="lang-en"> <p>Score: <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>The number <var>105</var> is quite special - it is odd but still it has eight divisors. Now, your task is this: how many odd numbers with exactly eight positive divisors are there between <var>1</var> a...
int solution() { int n = 0; int qntdiv = 0; int divi = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { if (i & 1) { for (int j = 1; j <= i; ++j) { if (i % j == 0) { ++qntdiv; if (qntdiv == 8) { ++divi; } } } qntdiv = 0; }...
fn solution() { let reader = io::stdin(); let mut reader = BufReader::new(reader.lock()); let mut s = String::new(); let _ = reader.read_line(&mut s); let n: u32 = s.trim().parse().unwrap(); let mut result = 0; for i in 1..n + 1 { if i % 2 == 0 { continue; } ...
easy
1544
<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> people, conveniently numbered <var>1</var> through <var>N</var>. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, ...
int solution() { int n; scanf("%d", &n); int a[n]; int fil[500001] = {0}; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); fil[a[i]]++; if (fil[a[i]] > 2) { printf("0"); return 0; } } if (n % 2 == 1 && fil[0] > 1) { printf("0"); return 0; } long long ans = 1; for...
fn solution() { input! { N: usize, A: [usize; N], } let mut ans = 1; let modulo = 1_000_000_007; let mut A = A; A.sort(); if N % 2 == 0 { for i in 0..N / 2 { if A[i * 2] == i * 2 + 1 && A[i * 2 + 1] == i * 2 + 1 { ans *= 2; ...
medium
1545
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.</p> <p>First, Takahashi created a tree with <var>N</var> vertices numbered <var>1</var> through <...
int solution(void) { int N; int M; scanf("%d %d", &N, &M); int a[M]; int b[M]; for (int i = 0; i < M; i++) { scanf("%d %d", &a[i], &b[i]); } int C[N + 1]; for (int i = 1; i <= N; i++) { C[i] = 0; } for (int i = 0; i < M; i++) { C[a[i]]++; C[b[i]]++; } for (int i = 1; i <= N; i...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).ok(); let mut it = buf.split_whitespace(); let n = it.next().unwrap().parse::<usize>().unwrap(); let m = it.next().unwrap().parse::<usize>().unwrap(); let mut cnt = vec![0; n]; for _ in 0..2 * m { ...
easy
1546
Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up $$$q$$$ questions about this song. Each question is about a subsegment of the song starting from the $$$l$$$-th letter to the $$$r$$$-th letter. Vasya considers a substring made up from c...
int solution() { int n; int q; scanf("%d %d", &n, &q); char *song = malloc(sizeof(char) * (n + 1)); scanf("%s", song); int count[n + 2]; char alphabet[27] = "abcdefghijklmnopqrstuvwxyz"; count[0] = 0; for (int i = 0; i < n; i++) { const char *find = strchr(alphabet, song[i]); count[i + 1] = co...
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
1547
One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".Nikita wants to make the...
int solution() { char s1[5001]; scanf("%s", s1); int ca[5001]; int cb[5001]; memset(ca, 0, sizeof(ca)); memset(cb, 0, sizeof(cb)); int i; for (i = 0; s1[i]; i++) { ca[i + 1] = ca[i]; cb[i + 1] = cb[i]; if (s1[i] == 'a') { ca[i + 1]++; } else { cb[i + 1]++; } } int len...
fn solution() { let mut buf = String::new(); stdin().read_line(&mut buf); let bs: Vec<i32> = once(0) .chain(buf.chars().scan(0, |acc, c| match c { 'a' => Some(*acc), 'b' => { *acc += 1; Some(*acc) } _ => None, })...
medium
1548
<span class="lang-en"> <p>Score: <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>There is a cave.</p> <p>The cave has <var>N</var> rooms and <var>M</var> passages. The rooms are numbered <var>1</var> to <var>N</var>, and the passages are numbered <var>1</var> to <var>M</var>. Passag...
int solution() { int N; int M; scanf("%d %d", &N, &M); int *node[N + 1]; int n_edge[N + 1]; for (int i = 0; i <= N; i++) { node[i] = NULL; n_edge[i] = 0; } for (int m = 1; m <= M; m++) { int i; int j; scanf("%d %d", &i, &j); n_edge[i]++; n_edge[j]++; node[i] = (int *)real...
fn solution() { input! { n: usize, m: usize, v: [(usize1, usize1); m] } let mut g = vec![vec![]; n]; for &(a, b) in &v { g[a].push(b); g[b].push(a); } let mut q = VecDeque::new(); let mut ans = vec![n; n]; ans[0] = 0; q.push_back(0); while ...
medium
1549
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of $$$n$$$ students doing yet another trick. Let's assume that all these students are numbered from $$$1$$$ to $$$n$$$. The teacher came to student $$$a$$$ and put a hole in hi...
int solution() { int n; scanf("%d", &n); int A[n]; int vis[n]; for (int i = 1; i <= n; i++) { scanf("%d", &A[i]); vis[i] = 0; } for (int i = 1; i <= n; i++) { int j = i; while (1) { if (vis[j] == 0) { vis[j] = 1; j = A[j]; } else { printf("%d\t", j); ...
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: Vec<i32> = buffer .split_whitespace() .map(|x| x.parse::<i32>().unwrap()) .collect(); let n = *arr.get(0).unwrap()...
medium
1550
You are given two sequences $$$a_1, a_2, \dots, a_n$$$ and $$$b_1, b_2, \dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$...
int solution() { int t; int a[3]; int b[3]; scanf("%d", &t); while (t--) { int sum = 0; scanf("%d%d%d", &a[0], &a[1], &a[2]); scanf("%d%d%d", &b[0], &b[1], &b[2]); if (a[2] >= b[1]) { sum += b[1] * 2; a[2] -= b[1]; b[1] = 0; if (b[2] >= a[0]) { b[2] -= a[0]; ...
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 (_x1, y1, z1): (i64, i64, i64) = { let mut buf = String::new(); std::io::stdin().read_l...
medium
1551
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Mountaineers Mr. Takahashi and Mr. Aoki recently trekked across a certain famous mountain range. The mountain range consists of <var>N</var> mountains, extending from west to east in a straight line as ...
int solution() { int i; int N; int T[100001]; int A[100002]; scanf("%d", &N); for (i = 1; i <= N; i++) { scanf("%d", &(T[i])); } for (i = 1; i <= N; i++) { scanf("%d", &(A[i])); } long long ans = 1; for (i = 1, T[0] = 0, A[N + 1] = 0; i <= N; i++) { if (T[i] < T[i - 1] || A[i] < A[i +...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).ok(); let n: usize = s.trim().parse().unwrap(); let mut s = String::new(); io::stdin().read_line(&mut s).ok(); let xs: Vec<u64> = s.trim().split(' ').map(|x| x.parse().unwrap()).collect(); let mut s = String::new(); ...
hard
1552
<h1>路線バスの時刻表</h1> <p> バスマニアの健次郎君は、A市内のバスをよく利用しています。ある日ふと、健次郎君の家の前のバス停から出発するすべてのバスを写真に収めることを思い立ちました。このバス停には飯盛山行きと鶴ケ城行きの2つのバス路線が通ります。各路線の時刻表は手に入れましたが、1つの時刻表としてまとめた方がバス停で写真が撮りやすくなります。 </p> <p> 健次郎君を助けるために、2つの路線の時刻表を、0時0分を基準として出発時刻が早い順に1つの時刻表としてまとめるプログラムを作成してください。 </p> <h2>入力</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> <var>N</...
int solution(void) { int bus[24][60] = {0}; int j; int i; int n; int m; int l; int f = 0; scanf("%d", &n); for (j = 1; j <= n; j++) { scanf("%d %d", &m, &l); bus[m][l] = 1; } scanf("%d", &n); for (j = 1; j <= n; j++) { scanf("%d %d", &m, &l); bus[m][l] = 1; } for (j = 0; j < ...
fn solution() { let input = { let mut buf = vec![]; stdin().read_to_end(&mut buf); unsafe { String::from_utf8_unchecked(buf) } }; let lines = input.split('\n'); let mut table = vec![]; for line in lines.take(2) { let mut iter = line.split(' ').map(|s| s.parse::<u32>...
hard
1553
The flag of Berland is such rectangular field n × m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color...
int solution() { int n; int m; int i; int j; int flag; scanf("%d%d", &n, &m); char arr[n][m]; int c[3] = {0}; getchar(); int hor = 1; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { arr[i][j] = getchar(); if (arr[i][j] == 'R') { c[0]++; } else if (arr[i][j] == 'G'...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut nums: Vec<u32> = buf .split_whitespace() .map(|c| c.parse::<u32>().unwrap()) .collect(); let width = nums.pop().unwrap(); let height = nums.pop().unwrap(); let mut buf = String...
medium
1554
Alice has a birthday today, so she invited home her best friend Bob. Now Bob needs to find a way to commute to the Alice's home.In the city in which Alice and Bob live, the first metro line is being built. This metro line contains $$$n$$$ stations numbered from $$$1$$$ to $$$n$$$. Bob lives near the station with number...
int solution() { int n; int s; int a[1001]; int b[1001]; scanf("%d %d", &n, &s); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } for (int i = 1; i <= n; ++i) { scanf("%d", &b[i]); } if (a[1] && a[s]) { puts("YES"); } else { for (int i = 1; i <= n; ++i) { if (a[1] && a[i]...
fn solution() { let reader = io::stdin(); let n_s_array: Vec<u32> = reader .lock() .lines() .next() .unwrap() .unwrap() .split(' ') .map(|s| s.trim()) .filter(|s| !s.is_empty()) .map(|s| s.parse().unwrap()) .collect(); let a_a...
medium
1555
<span class="lang-en"> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>N</var> boxes, numbered <var>1</var> through <var>N</var>. At first, box <var>1</var> contains one red ball, and each of the other boxes contains one white ball.</p> <p>Snuke will perform the following <var>M</var> operations,...
int solution(void) { int n; int m; scanf("%d %d", &n, &m); int x[m]; int y[m]; for (int i = 0; i < m; i++) { scanf("%d %d", &x[i], &y[i]); } int box[n]; for (int i = 0; i < n; i++) { box[i] = 1; } int red[n]; red[0] = 1; for (int i = 1; i < n; i++) { red[i] = 0; } for (int i =...
fn solution() { let mut stdin = io::stdin(); let mut lines = String::new(); let _ = stdin.read_to_string(&mut lines); let vec = lines.lines().collect::<Vec<&str>>(); let vec2 = vec[0].split(' ').collect::<Vec<&str>>(); let n: usize = vec2[0].trim().parse().unwrap(); let m: usize = vec2[1].tr...
medium
1556
<span class="lang-en"> <p>Score: <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>In <var>2020</var>, AtCoder Inc. with an annual sales of more than one billion yen (the currency of Japan) has started a business in programming education.<br/> One day, there was an exam where a one-y...
int solution() { int number = 0; int A = 0; int B = 0; scanf("%d", &number); if (number == 1) { printf("Hello World"); } else { scanf("%d%d", &A, &B); printf("%d", A + B); } return 0; }
fn solution() { let scan = std::io::stdin(); let mut line = String::new(); let _ = scan.read_line(&mut line); line = line.trim_end().to_owned(); if line == "1" { println!("Hello World"); } else if line == "2" { let mut a_s = String::new(); let _ = scan.read_line(&mut a_s...
easy
1557
Everyone knows that agents in Valorant decide, who will play as attackers, and who will play as defenders. To do that Raze and Breach decided to play $$$t$$$ matches of a digit game...In each of $$$t$$$ matches of the digit game, a positive integer is generated. It consists of $$$n$$$ digits. The digits of this integer...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); char s[n]; scanf("%s", s); if (n & 1) { int ans = 0; for (int i = 0; i < n; i += 2) { int x = s[i] - '0'; if (x & 1) { ans = 1; break; } } ans ? pr...
fn solution() { let stdin = stdin(); let mut lines = { stdin.lock().lines().map(|l| l.unwrap()) }; let t: usize = lines.next().unwrap().parse().unwrap(); let mut ds: Vec<usize> = Vec::new(); let mut ns: Vec<Vec<u8>> = Vec::new(); for i in 0..t { ds.push(lines.next().unwrap().parse().unw...
medium
1558
There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team.The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams o...
int solution() { int re[3]; int N = 0; re[1] = re[2] = 0; scanf("%d", &N); while (N--) { int tmp = 0; scanf("%d", &tmp); ++re[tmp]; } int result = re[1]; result = result > re[2] ? re[2] : result; result = result + (re[1] - result) / 3; printf("%d\n", result); return 0; }
fn solution() { let mut input = String::new(); use std::io::prelude::*; std::io::stdin().read_to_string(&mut input).unwrap(); let mut it = input .split_whitespace() .map(|x| x.parse::<usize>().unwrap()); let n = it.next().unwrap(); let mut d = [0; 2]; for i in it.take(n) {...
easy
1559
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>AtCoder Mart sells <var>1000000</var> of each of the six items below:</p> <ul> <li>Riceballs, priced at <var>100</var> yen (the currency of Japan) each</li> <li>Sandwiches, priced at <var>101</var> yen ...
int solution() { int X = 0; int i = 0; int j = 0; int ans = 0; ; scanf("%d", &X); i = X / 100; j = X % 100; if (i * 5 >= j) { ans = 1; } else { ans = 0; } printf("%d\n", ans); return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let x: u32 = s.trim().parse().ok().unwrap(); let y: u32 = x / 100; let z: u32 = (x % 100).div_ceil(5); println!("{}", if y >= z { 1 } else { 0 }); }
easy
1560
Polycarp is playing a new computer game. This game has $$$n$$$ stones in a row. The stone on the position $$$i$$$ has integer power $$$a_i$$$. The powers of all stones are distinct.Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int power[n]; for (int i = 0; i < n; i++) { scanf("%d", &power[i]); } int min = power[0]; int max = power[0]; int m_l = 0; int M_l = 0; for (int i = 1; i < n; i++) { if (power[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
1561
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc.<br/> The pyramid had <em>center coordinates</em> <var>(C_X, C_Y)</var> and <e...
int solution(void) { int n; scanf("%d", &n); int p[n][3]; for (int i = 0; i < n; i++) { int x; int y; int h; scanf("%d %d %d", &x, &y, &h); p[i][0] = x; p[i][1] = y; p[i][2] = h; } for (int i = 0; i <= 100; i++) { for (int j = 0; j <= 100; j++) { int ans_h = -1; ...
fn solution() { 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 mut x = vec![0; n]; let mut y = vec![0; n]; let mut h = vec![0; n]; for i in 0..n { ...
easy
1562
Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly gr...
int solution(void) { int n = 0; int p = 0; int d = 0; int o = 0; int counter1 = 0; int sum = 0; scanf("%d", &n); scanf("%d", &p); scanf("%d", &d); for (int i = 0; i < n; i++) { scanf("%d", &o); if (o <= p) { sum += o; } if ((sum > d)) { counter1++; sum = 0; }...
fn solution() { use std::io; let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let b; let d; { let mut ns = buf.split(" ").map(|str| str.trim().parse::<u32>().unwrap()); ns.next(); b = ns.next().unwrap(); d = ns.next().unwrap(); } buf....
hard
1563
And where the are the phone numbers?You are given a string s consisting of lowercase English letters and an integer k. Find the lexicographically smallest string t of length k, such that its set of letters is a subset of the set of letters of s and s is lexicographically smaller than t.It's guaranteed that the answer e...
int solution() { int a; int b; scanf("%d %d\n", &a, &b); char c[a]; char v[27]; char min = 'z'; char max = 'a'; for (int i = 0; i < 27; i++) { v[i] = 0; } for (int i = 0; i < a; i++) { scanf("%c", &c[i]); if (c[i] < min) { min = c[i]; } if (c[i] > max) { max = c[i]...
fn solution() { let mut input = String::new(); use std::io; use std::io::prelude::*; io::stdin().read_to_string(&mut input).unwrap(); let mut it = input.split_whitespace(); let n: usize = it.next().unwrap().parse().unwrap(); let k: usize = it.next().unwrap().parse().unwrap(); let s = ...
hard
1564
A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from $$$1$$$ to $$$9$$$ (inclusive) are round.For example, the following number...
int solution() { int n = 0; scanf("%d", &n); for (int c = 0; c < n; c++) { int a = 0; scanf("%d", &a); int re[100] = {0}; int sum = 0; int i = 0; for (i = 0; a > 10; i++, a /= 10) { int tmp = 1; for (int j = 0; j < i; j++) { tmp *= 10; } if (a % 10 != 0) { ...
fn solution() { let stdin = stdin(); let lockin = stdin.lock(); let mut r = BufReader::new(lockin); let stdout = stdout(); let lockout = stdout.lock(); let mut w = BufWriter::new(lockout); let mut i = String::new(); r.read_line(&mut i).expect(""); for _ in 0..i.trim().parse::<u16>...
hard
1565
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi made <var>N</var> problems for competitive programming. The problems are numbered <var>1</var> to <var>N</var>, and the difficulty of Problem <var>i</var> is represented as an integer <var>d_i...
int solution(void) { int n; int d[100001]; int a[100001] = {0}; int x = 0; int i; int j; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &d[i]); a[d[i]]++; } for (i = 1; i < 100001; i++) { if (a[i] > 0) { x += a[i]; } if (x >= n / 2) { if (x > n / 2) { ...
fn solution() { let mut input = String::new(); io::stdin().read_line(&mut input).ok(); let n = input.trim().parse().unwrap(); let mut input = String::new(); io::stdin().read_line(&mut input).ok(); let mut v: Vec<usize> = Vec::new(); for z in input.split_whitespace() { v.push(z.pars...
hard
1566
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given a lowercase English letter <var>c</var>, determine whether it is a vowel. Here, there are five vowels in the English alphabet: <code>a</code>, <code>e</code>, <code>i</code>, <code>o</code> and <c...
int solution(void) { char str = 0; scanf("%c", &str); if (str == 'a' || str == 'i' || str == 'u' || str == 'e' || str == 'o') { printf("vowel"); } else { printf("consonant"); } return 0; }
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).ok(); let s = s.trim(); let set: HashSet<&str> = ["a", "e", "i", "o", "u"].iter().cloned().collect(); println!( "{}", if set.contains(s) { "vowel" } else { "consonant" } ...
easy
1567
<h2>B: 双子 (Twins)</h2> <p>とある双子は、自分たちのどちらが兄でどちらが弟かがあまり知られていないことに腹を立てた。</p> <p>"ani" と入力されたら "square1001"、"otouto" と入力されたら "e869120" と出力するプログラムを作りなさい。</p> <h3>入力</h3> <p>入力として "ani" または "otouto" という文字列のどちらかが与えられます。</p> <h3>出力</h3> <p>"e869120" または "square1001" を、問題文の通りに出力してください。最後の改行を忘れないようにしましょう。</p> <h3>入力例1</h3> ...
int solution(void) { char nyuuryoku[128]; if (scanf("%127s", nyuuryoku) != 1) { return 1; } if (strcmp(nyuuryoku, "ani") == 0) { puts("square1001"); } else if (strcmp(nyuuryoku, "otouto") == 0) { puts("e869120"); } else { puts("YOU ARE AN IDIOT!!!!!"); } return 0; }
fn solution() { input! { s : String, } if s == "ani" { println!("square1001") } else { println!("e869120") } }
medium
1568
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a string <var>S</var> consisting of digits <code>1</code>, <code>2</code>, <var>...</var>, <code>9</code>. Lunlun, the Dachshund, will take out three consecutive digits from <var>S</var>, treat...
int solution(void) { char k[20]; int dif = 1000; scanf("%s", k); for (int i = 0; k[i + 2] != '\0'; i++) { if (dif > abs((100 * (k[i] - '0') + 10 * (k[i + 1] - '0') + (k[i + 2] - '0')) - 753)) { dif = abs((100 * (k[i] - '0') + 10 * (k[i + 1] - '0') + (k[i + 2] - '0'))...
fn solution() { let mut s = String::new(); let t = 753; stdin().read_line(&mut s).unwrap(); let s: Vec<_> = s.trim().chars().flat_map(|v| v.to_digit(10)).collect(); let mut result = 753; for ((&a, &b), &c) in s.iter().zip(s.iter().skip(1)).zip(s.iter().skip(2)) { let mut v = a * 100 + b ...
medium
1569
Phoenix has $$$n$$$ coins with weights $$$2^1, 2^2, \dots, 2^n$$$. He knows that $$$n$$$ is even.He wants to split the coins into two piles such that each pile has exactly $$$\frac{n}{2}$$$ coins and the difference of weights between the two piles is minimized. Formally, let $$$a$$$ denote the sum of weights in the fir...
int solution() { int t; int n; int p = 0; scanf("%d", &t); while (t--) { scanf("%d", &n); p = pow(2, (n / 2) + 1) - 2; printf("%d\n", p); } return 0; }
fn solution() { let mut input_str = String::new(); io::stdin().read_line(&mut input_str).expect("err"); let t = input_str.trim().parse::<u32>().unwrap(); for _ in 0..t { input_str.clear(); io::stdin().read_line(&mut input_str).expect("err"); let n = input_str.trim().parse::<u32>(...
medium
1570
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a image with a height of <var>H</var> pixels and a width of <var>W</var> pixels. Each pixel is represented by a lowercase English letter. The pixel at the <var>i</var>-th row from the top ...
int solution() { char a[100][100]; int n = 0; int m = 0; int i = 0; scanf("%d%d", &n, &m); for (int j = 0; j < n; j++) { scanf("%s", a[j]); } for (i = 0; i < m + 2; i++) { printf("#"); } printf("\n"); for (i = 0; i < n; i++) { printf("#"); for (int j = 0; j < m; j++) { print...
fn solution() { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); let split: Vec<&str> = input.as_str().split_whitespace().collect(); let h: i64 = split[0].parse().unwrap(); let w: i64 = split[1].parse().unwrap(); for _n in 0..w + 2 { print!("#"); } pr...
medium
1571
<H1>問題 2:  投票 (Vote) </H1> <br/> <h2>問題</h2> <p> 20XX年に東京で世界的なスポーツ大会が開かれることになった.プログラミングコンテストはスポーツとして世界で楽しまれており,競技として採用される可能性がある.採用される競技を決める審査委員会について調べたところ,次のようなことが分かった. </p> <ul> <li> 審査委員会のために,候補となる N 個の競技を面白い方から順番に並べたリストが作成された.リストの上から i 番目には i 番目に面白い競技が書かれている.それを競技 i とする.さらに競技 i の開催に必要な費用 A<sub>i</sub> が書かれている. </l...
int solution(void) { int n = 0; int m = 0; int i = 0; int j = 0; int a[1001] = {0}; int b[1001] = {0}; int hyou[1001] = {0}; int sum = 0; int k = 0; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (j = 1; j <= m; j++) { scanf("%d", &b[j]); } for (j = 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 (n, m) = { let line = lines.next().unwrap(); let mut iter = line.split(' ').map(|s| s.parse::<...
medium
1572
<H1>Distance</H1><br> <p> Write a program which calculates the distance between two points P1(x1, y1) and P2(x2, y2). </p> <H2>Input</H2> <p> Four real numbers x1, y1, x2 and y2 are given in a line. </p> <H2>Output</H2> <p> Print the distance in real number. The output should not contain an absolute error ...
int solution() { double x1 = 0; double x2 = 0; double y1 = 0; double y2 = 0; double x = 0; double y = 0; scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2); x = (x2 - x1) * (x2 - x1); y = (y2 - y1) * (y2 - y1); printf("%lf\n", sqrt(x + y)); return (0); }
fn solution() { let handle = std::io::stdin(); let mut s = String::new(); handle.lock().read_line(&mut s).unwrap(); let points: Vec<f64> = s.split_whitespace().map(|a| a.parse().unwrap()).collect(); let (ax, ay, bx, by) = (points[0], points[1], points[2], points[3]); println!("{}", ((ax - bx).po...
medium
1573
Little Dormi received a histogram with $$$n$$$ bars of height $$$a_1, a_2, \ldots, a_n$$$ for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking.To modify the histogram, Little Dormi is able to perform the following operat...
int solution() { long long int testcases; scanf("%lld", &testcases); while (testcases--) { long long int n; scanf("%lld", &n); long long int arrrr[n + 2]; arrrr[0] = 0; arrrr[n + 1] = 0; for (int i = 1; i <= n; i++) { scanf("%lld", &arrrr[i]); } long long int count = 0; f...
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
1574
Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink "Beecola", which can be bought in n different shops in the city. It's known that the price of one bottle in the shop i is equal to xi coins.Vasiliy plans to buy his favorite drink for ...
int solution(void) { int n; int b[1000001] = {0}; int tmp; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &tmp), b[tmp]++; } for (int i = 1; i < 100001; i++) { b[i] += b[i - 1]; } scanf("%d", &n); while (n--) { scanf("%d", &tmp), printf("%d\n", b[tmp <= 100000 ? tmp : 100000]...
fn solution() { let mut n = String::new(); stdin().read_line(&mut n).unwrap(); let n: i32 = n.trim().parse().unwrap(); let mut line = String::new(); stdin().read_line(&mut line).unwrap(); let mut x: Vec<i32> = line .split_whitespace() .map(|x| x.parse().unwrap()) .colle...
hard
1575
Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.You are to determine the minimum possible nu...
int solution() { char a[101][101]; int n; int m; scanf("%d %d", &n, &m); int minx = m; int miny = n; int maxx = 0; int maxy = 0; int count = 0; for (int i = 0; i < n; i++) { scanf("%s", a[i]); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == 'B') { ...
fn solution() { let (n, m) = { let mut input = String::new(); stdin().read_line(&mut input).unwrap(); let mut it = input .split_whitespace() .map(|k| k.parse::<usize>().unwrap()); (it.next().unwrap(), it.next().unwrap()) }; let br = BufReader::new(std...
medium
1576
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has <var>N</var> sticks that are distinguishable from each other. The length of the <var>i</var>-th stick is <var>L_i</var>.</p> <p>He is going to form a triangle using three of these sticks. ...
int solution(void) { int N; int L[2000]; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &L[i]); } int count = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < i; j++) { for (int k = 0; k < j; k++) { if (L[i] < L[j] + L[k] && L[j] < L[k] + L[i] && L[k] < L[i] + L[j]) {...
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 mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut l_s = buf .sp...
hard
1577
Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negative integer x.You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and is maximum possible. If there are multiple such numbers find the smallest of them.
int solution(void) { int n = 0; long long l = 0; long long r = 0; scanf("%d", &n); while (n--) { scanf("%lli %lli", &l, &r); while ((l | (l + 1)) <= r) { l = l | (l + 1); } printf("%lli\n", l); } return 0; }
fn solution() { let mut buffer = String::new(); io::stdin() .read_line(&mut buffer) .expect("failed to read input"); let n: u16 = buffer.trim().parse().expect("invalid input"); for _ in 0..n { let mut input = String::new(); io::stdin() .read_line(&mut input) ...
medium
1578
PizzaForces is Petya's favorite pizzeria. PizzaForces makes and sells pizzas of three sizes: small pizzas consist of $$$6$$$ slices, medium ones consist of $$$8$$$ slices, and large pizzas consist of $$$10$$$ slices each. Baking them takes $$$15$$$, $$$20$$$ and $$$25$$$ minutes, respectively.Petya's birthday is today,...
int solution() { int t = 0; scanf("%d", &t); while (t--) { long long int n = 0; scanf("%lld", &n); if (n <= 6) { printf("15\n"); continue; } if (n % 2 == 0) { printf("%lld\n", (n / 2) * 5); } else { printf("%lld\n", ((n / 2) + 1) * 5); } } return 0; }
fn solution() { let mut inPut = String::new(); io::stdin() .read_line(&mut inPut) .expect("Failed to read line"); let mut inPutInt: i32 = inPut.trim().parse().unwrap(); inPut.clear(); let mut out: i128 = 0; while inPutInt != 0 { io::stdin() .read_line(&mut in...
easy
1579
Bubble sort
void solution(int *arr, int size) { int i, j; for (i = 0; i < size - 1; i++) { bool swapped = false; for (j = 0; j < size - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp; temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; swapped = true; } ...
fn solution<T: Ord>(arr: &mut [T]) { if arr.is_empty() { return; } let mut sorted = false; let mut n = arr.len(); while !sorted { sorted = true; for i in 0..n - 1 { if arr[i] > arr[i + 1] { arr.swap(i, i + 1); sorted = false; ...
easy
1580
Boyer moore search
void solution(char *str, char *pattern) { int n = strlen(str); int m = strlen(pattern); if (m == 0 || n == 0 || m > n) return; int shift = 0; int bad_char[256]; for (int i = 0; i < 256; i++) bad_char[i] = -1; for (int i = 0; i < m; i++) bad_char[(unsigned char)pattern[i]] = i; while (s...
fn solution(text: &str, pat: &str) -> Vec<usize> { let mut positions = Vec::new(); let text_len = text.len() as isize; let pat_len = pat.len() as isize; if text_len == 0 || pat_len == 0 || pat_len > text_len { return positions; } let text: Vec<char> = text.chars().collect(); let p...
easy
1581
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A string is called a KEYENCE string when it can be changed to <code>keyence</code> by removing its contiguous substring (possibly empty) only once.</p> <p>Given a string <var>S</var> consisting of lower...
int solution(void) { char s[100]; scanf("%s", s); int l = strlen(s); if ((s[l - 7] == 'k' && s[l - 6] == 'e' && s[l - 5] == 'y' && s[l - 4] == 'e' && s[l - 3] == 'n' && s[l - 2] == 'c' && s[l - 1] == 'e') || (s[0] == 'k' && s[l - 6] == 'e' && s[l - 5] == 'y' && s[l - 4] == 'e' && s[l...
fn solution() { let mut buf = String::new(); let handle = std::io::stdin(); handle.read_line(&mut buf).unwrap(); let s: String = buf.trim().to_string(); let k: String = "keyence".to_string(); let _k_index: Vec<usize> = vec![]; let mut left = 0; let mut right = s.len() - k.len(); let...
hard
1582
You are given a permutation $$$p_1, p_2, \dots, p_n$$$. Recall that sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once.Find three indices $$$i$$$, $$$j$$$ and $$$k$$$ such that: $$$1 \le i &lt; j &lt; k \le n$$$; $$$p_i &lt; p_j$$$ and $$$p_j &gt; p_...
int solution() { int n = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { int flag = 0; int size = 0; scanf("%d", &size); int ind[size]; for (int j = 0; j < size; j++) { scanf("%d", &ind[j]); } for (int j = 1; j < size - 1; j++) { if (ind[j] > ind[j + 1] && ind[j] > ind[j...
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 { input.read_line(&mut line).unwrap(); line.clear(); input.read_lin...
medium
1583
You are given a string $$$s$$$. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may er...
int solution() { int t; scanf("%d", &t); while (t--) { char str[101] = {0}; int res = 0; int flag = 0; scanf("%s", str); for (int i = 0; str[i] != '\0'; i++) { if (str[i] == '1' && str[i + 1] == '0') { flag = 1; while (str[++i] == '0') { res++; } ...
fn solution() -> io::Result<()> { let mut input = String::new(); io::stdin().read_to_string(&mut input)?; let input = input.trim(); for line in input.lines().skip(1) { let mut erased_0 = 0; let mut flag = false; let mut prev_digit = '0'; let line = line.trim_matches('0')...
medium
1584
A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).Killjoy's account is already infected and has a rating equal to $$$x$$$. Its rating is constant. There are $$$n$$$ accounts except he...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int x; scanf("%d", &n); scanf("%d", &x); int a[n]; int count = 0; int sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] == x) { count++; } } for (int i = 0; i < n; i++)...
fn solution() { let stdin = std::io::stdin(); let mut handle = stdin.lock(); let mut buf = String::new(); handle.read_line(&mut buf).unwrap(); let tests: i64 = buf.trim().parse().unwrap(); for _test in 0..tests { buf.clear(); handle.read_line(&mut buf).unwrap(); let mut...
hard
1585
<H1>通学経路</H1> <h2>問題</h2> <p> 太郎君の住んでいるJOI市は,南北方向にまっすぐに伸びる a 本の道路と,東西方向にまっすぐに伸びる b 本の道路により,碁盤の目の形に区分けされている. </p> <p> 南北方向の a 本の道路には,西から順に 1, 2, ... , a の番号が付けられている.また,東西方向の b 本の道路には,南から順に 1, 2, ... , b の番号が付けられている.西から i 番目の南北方向の道路と,南から j 番目の東西方向の道路が交わる交差点を (i, j) で表す. </p> <p> 太郎君は,交差点 (1, 1) の近くに住んでおり,交差点 (a, b) の近く...
int solution() { int w; int h; int i; int j; int n; while (scanf("%d %d", &w, &h), w || h) { int f[20][20]; int d[20][20] = {0}; for (i = 0; i < 400; i++) { f[i / 20][i % 20] = 1; } scanf("%d", &n); while (n--) { scanf("%d %d", &i, &j); f[j][i] = 0; } d[1][0...
fn solution() { let mut stdin = io::stdin(); let mut buf = String::new(); stdin.read_to_string(&mut buf); let mut iter = buf.split_whitespace(); loop { let a: usize = iter.next().unwrap().parse().unwrap(); let b: usize = iter.next().unwrap().parse().unwrap(); if (a, b) == (0...
hard
1586
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are taking a computer-based examination. The examination consists of <var>N</var> questions, and the score allocated to the <var>i</var>-th question is <var>s_i</var>. Your answer to each question w...
int solution() { int a; int b = 0; int c = 100000; scanf("%d", &a); int d[a]; for (int i = 0; i < a; i++) { scanf("%d", &d[i]); b += d[i]; if (d[i] % 10 != 0 && d[i] < c) { c = d[i]; } } if (b % 10 != 0) { printf("%d", b); } else { b -= c; if (b < 0) { b = 0; ...
fn solution() { let N: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() }; let s: Vec<usize> = (0..N) .map(|_| { let mut line: String = String::new(); std::io::stdin().read_lin...
medium
1587
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi lives in another world. There are slimes (creatures) of <var>10000</var> colors in this world. Let us call these colors Color <var>1, 2, ..., 10000</var>.</p> <p>Takahashi has <var>N</var> sli...
int solution() { int n = 0; scanf("%d\n", &n); int a[n]; int i = 0; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } int buf = 0; int count = 0; int ans = 0; for (i = 0; i < n; i++) { if (buf == a[i]) { count++; } else { buf = a[i]; ans = ans + count / 2; count = ...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut buf_it = buf.split_whitespace(); let N = buf_it.next().unwrap().parse::<usize>().unwrap(); let mut A = (0..N) .map(|_| buf_it.next().unwrap().parse::<isize>().unwrap()) .collect::...
medium
1588
Given an array $$$a$$$ of length $$$n$$$, tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements.
int solution() { int t = 0; scanf("%d", &t); while (t--) { int n = 0; scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int flag = 0; for (int i = 0; i < n; i++) { int temp = sqrt(a[i]); if (a[i] != temp * temp) { printf("Yes\n"); ...
fn solution() { let mut input = String::new(); stdin().read_to_string(&mut input).unwrap(); let mut it = input .split_whitespace() .map(|s| s.parse::<usize>().unwrap()); let t = it.next().unwrap(); let mut output = String::new(); for _ in 0..t { let n = it.next().unwr...
medium
1589
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi is a user of a site that hosts programming contests.<br/> When a user competes in a contest, the <em>rating</em> of the user (not necessarily an integer) changes according to the <em>performan...
int solution() { int R = 0; int G = 0; int ans = 0; scanf("%d\n%d", &R, &G); ans = (2 * G) - R; printf("%d", ans); return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let a: i32 = s.trim().parse().ok().unwrap(); let mut t = String::new(); std::io::stdin().read_line(&mut t).ok(); let b: i32 = t.trim().parse().ok().unwrap(); println!("{}", (2 * b - a)); }
medium
1590
You are given an array of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$You can apply the following operation an arbitrary number of times: select an index $$$i$$$ ($$$1 \le i \le n$$$) and replace the value of the element $$$a_i$$$ with the value $$$a_i + (a_i \bmod 10)$$$, where $$$a_i \bmod 10$$$ is the remainder of ...
int solution() { int t; scanf("%d\n", &t); while (t--) { int n; scanf("%d\n", &n); int a[n]; int max = -1; int m = -1; int count = 0; for (int i = 0; i < n; i++) { scanf("%d\n", &a[i]); if (a[i] % 2 != 0) { a[i] += a[i] % 10; } if (max < a[i]) { ...
fn solution() { let mut tln = String::new(); std::io::stdin().read_line(&mut tln).unwrap(); let tcn = tln.trim_end().parse::<i32>().unwrap(); for _k in 0..tcn { let mut sln = String::new(); std::io::stdin().read_line(&mut sln).unwrap(); let _n = sln.trim_end().parse::<i32>().un...
medium
1591
Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length $$$n$$$ consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, ...
int solution() { int n; int i; scanf("%d%*c", &n); long long int minimum_values[n][4]; int values[n]; char str[n]; for (i = 0; i < n; i++) { scanf("%c", &str[i]); minimum_values[i][0] = minimum_values[i][1] = minimum_values[i][2] = minimum_values[i][3] = 0; } for (i = 0; i < n; i++) { ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let n: usize = s.trim().parse().unwrap(); s = String::new(); io::stdin().read_line(&mut s).unwrap(); let chars: Vec<char> = s.trim().chars().collect(); s = String::new(); io::stdin().read_line(&mut s).un...
medium
1592
Nauuo is a girl who loves playing cards.One day she was playing cards but found that the cards were mixed with some empty ones.There are $$$n$$$ cards numbered from $$$1$$$ to $$$n$$$, and they were mixed with another $$$n$$$ empty cards. She piled up the $$$2n$$$ cards and drew $$$n$$$ of them. The $$$n$$$ cards in Na...
int solution() { int i; int j; int k = 0; int n; int x = 0; int y; int z = 0; int d = 1; int e = 0; scanf("%d", &n); int a[n + 1]; int b[n + 1]; for (i = 1; i <= n; i++) { scanf("%d", &b[i]); if (b[i] == 1) { z = 1; } } for (i = 1; i <= n; i++) { scanf("%d", &a[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 n = get!(usize); let mut zeros = 0; for _ in 0..n { let h = get!(); ...
medium
1593
Polycarp has $$$26$$$ tasks. Each task is designated by a capital letter of the Latin alphabet.The teacher asked Polycarp to solve tasks in the following way: if Polycarp began to solve some task, then he must solve it to the end, without being distracted by another task. After switching to another task, Polycarp canno...
int solution() { int t = 0; scanf("%d", &t); while (t--) { int n = 0; scanf("%d", &n); char string[n + 1]; scanf("%s", string); int flag1 = 0; { for (char c = 'A'; c <= 'Z' && flag1 == 0; c++) { int count = 0; for (int i = 0; i < n; i++) { if (string[i] == c...
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(|x| x.unwrap()); let t = lines.next().unwrap().pa...
medium
1594
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have two desks: A and B. Desk A has a vertical stack of <var>N</var> books on it, and Desk B similarly has <var>M</var> books on it.</p> <p>It takes us <var>A_i</var> minutes to read the <var>i</var>...
int solution(void) { long int desk1; long int desk2; long int limit; scanf("%ld%ld%ld", &desk1, &desk2, &limit); long int now1 = 0; long int now2 = desk2; long int sum1 = 0; long int sum2 = 0; long int max = 0; long int read1[desk1 + 1]; long int read2[desk2 + 1]; for (long int i = 0; i < des...
fn solution() { let (n, m, k): (usize, 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(), ...
hard
1595
Alice is playing with some stones.Now there are three numbered heaps of stones. The first of them contains $$$a$$$ stones, the second of them contains $$$b$$$ stones and the third of them contains $$$c$$$ stones.Each time she can do one of two operations: take one stone from the first heap and two stones from the seco...
int solution(void) { int times; int a[3]; int t = 0; scanf("%d", &times); while (times--) { scanf("%d %d %d", &a[0], &a[1], &a[2]); while (a[2] >= 2 && a[1] > 0) { t++; a[2] -= 2; a[1] -= 1; } while (a[1] > 1 && a[0] > 0) { t++; a[0]--; a[1] -= 2; } ...
fn solution() { let mut buffer = String::new(); io::stdin() .read_line(&mut buffer) .expect("failed to read input"); let t: u16 = buffer.trim().parse().expect("invalid input"); for _ in 0..t { let mut input = String::new(); io::stdin() .read_line(&mut input) ...
hard
1596
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke, who loves animals, built a zoo.</p> <p>There are <var>N</var> animals in this zoo. They are conveniently numbered <var>1</var> through <var>N</var>, and arranged in a circle. The animal numbered ...
int solution() { int n; scanf("%d", &n); char s[100005]; scanf("%s", s); int i; int j; char ans[4][100005]; ans[0][0] = ans[1][0] = 'S'; ans[2][0] = ans[3][0] = 'W'; ans[0][1] = ans[2][1] = 'S'; ans[1][1] = ans[3][1] = 'W'; for (i = 1; i < n - 1; i++) { for (j = 0; j < 4; j++) { if (an...
fn solution() { let mut line1 = String::new(); let mut line2 = String::new(); let stdin = std::io::stdin(); stdin.read_line(&mut line1).expect("Could not read line"); stdin.read_line(&mut line2).expect("Could not read line"); let p: Vec<_> = line1.trim().split(' ').collect(); let n: usize = ...
medium
1597
One night, Mark realized that there is an essay due tomorrow. He hasn't written anything yet, so Mark decided to randomly copy-paste substrings from the prompt to make the essay.More formally, the prompt is a string $$$s$$$ of initial length $$$n$$$. Mark will perform the copy-pasting operation $$$c$$$ times. Each oper...
int solution() { int t; scanf("%d", &t); int n; int c; int q; for (int m = 0; m < t; m++) { scanf("%d %d %d", &n, &c, &q); char str[n]; scanf("%s", str); long long a = strlen(str); long long arr1[c]; long long arr2[c]; long long total[c]; for (int i = 0; i < c; i++) { ...
fn solution() { input! { name = reader, tests: usize } for _ in 0..tests { input! { use reader, n: usize, c: usize, q: usize, s: bytes, cs: [(usize1, usize); c], qs: [usize1; q] } let ...
medium
1598
You are given an array $$$a_1, a_2, \dots a_n$$$. Count the number of pairs of indices $$$1 \leq i, j \leq n$$$ such that $$$a_i &lt; i &lt; a_j &lt; j$$$.
int solution() { int x; int y; int z; int i; int j; int k; int a; int b; int c; int n; int m; int t; int arr[200001]; int aa[200001]; long long ans; long long d; scanf("%d", &t); while (t--) { ans = 0; scanf("%d", &n); aa[0] = 0; for (x = 1; x <= n; x++) { scan...
fn solution() { let mut inp_t = String::new(); io::stdin() .read_line(&mut inp_t) .expect("Failed to read line"); let t: i32 = inp_t.trim().parse().expect("T is not an integer"); for _ in 0..t { let mut inp_n = String::new(); io::stdin() .read_line(&mut inp_...
hard
1599
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi wants to be a member of some web service.</p> <p>He tried to register himself with the ID <var>S</var>, which turned out to be already used by another user.</p> <p>Thus, he decides to register...
int solution(void) { char *S = (char *)malloc(128); char *T = (char *)malloc(128); scanf("%s", S); scanf("%s", T); *(T + (strlen(S))) = '\0'; if (strcmp(S, T) != 0) { puts("No"); } else { puts("Yes"); } free(S); free(T); }
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).ok(); s.pop(); let mut t = String::new(); stdin().read_line(&mut t).ok(); t.pop(); t.pop(); println!("{}", if s == t { "Yes" } else { "No" }); }
medium
1600
<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> boxes arranged in a circle. The <var>i</var>-th box contains <var>A_i</var> stones.</p> <p>Determine whether it is possible to remove all the stones from the boxes by repeatedly p...
int solution() { long long i; long long N; long long A[100001]; scanf("%lld", &N); for (i = 0; i < N; i++) { scanf("%lld", &(A[i])); } A[N] = A[0]; long long sum = 0; long long diff[100001]; for (i = 1; i <= N; i++) { sum += A[i]; diff[i] = A[i] - A[i - 1]; } if (sum % (N * (N + 1) ...
fn solution() { let mut input_line = String::new(); io::stdin().read_line(&mut input_line).unwrap(); let N = input_line.trim().parse::<usize>().unwrap(); let Ni = N as isize; let mut input_line = String::new(); io::stdin().read_line(&mut input_line).unwrap(); let data = input_line ....
easy