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
0801
There are n Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (x, y) on this plane. Han Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (x0, y0). In one shot it can can de...
int solution() { int n; int x1; int y1; int count = 0; int i; int j; scanf("%d %d %d", &n, &x1, &y1); int x[n]; int y[n]; for (i = 0; i < n; i++) { scanf("%d %d", &x[i], &y[i]); } for (i = 0; i < n; i++) { if (n == 1) { count++; break; } for (j = i + 1; j < n; j++) {...
fn solution() { let (n, c0) = { let mut line = String::new(); stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse::<usize>().unwrap(), ( iter.next().unwrap().parse::<isize>().unwrap(), ...
hard
0802
You are given $$$n$$$ rectangles, each of height $$$1$$$. Each rectangle's width is a power of $$$2$$$ (i. e. it can be represented as $$$2^x$$$ for some non-negative integer $$$x$$$). You are also given a two-dimensional box of width $$$W$$$. Note that $$$W$$$ may or may not be a power of $$$2$$$. Moreover, $$$W$$$ is...
int solution() { int x; int y; int z; int w; int i; int j; int k; int a; int b; int c; int n; int m; int t; int mat[25]; int res; scanf("%d", &t); while (t--) { scanf("%d %d", &n, &w); memset(mat, 0, sizeof(mat)); res = 0; for (x = 0; x < n; x++) { scanf("%d", &a...
fn solution() { use std::io::Read; let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut itr = buf.split_whitespace(); use std::io::Write; let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); let t: usize = scan!(usize);...
easy
0803
You are given a grid with $$$n$$$ rows and $$$m$$$ columns. Rows and columns are numbered from $$$1$$$ to $$$n$$$, and from $$$1$$$ to $$$m$$$. The intersection of the $$$a$$$-th row and $$$b$$$-th column is denoted by $$$(a, b)$$$. Initially, you are standing in the top left corner $$$(1, 1)$$$. Your goal is to reach ...
int solution() { int n; int m; int g = 0; while (scanf("%d", &g) != EOF) { while (g--) { scanf("%d%d", &n, &m); if (n > m) { int t = n; n = m; m = t; } if (n == 1) { if (m > 2) { printf("-1\n"); } else { printf("%d\n", m - 1...
fn solution() { let mut buffer = String::new(); stdin().read_line(&mut buffer).expect("Failed"); let entries = buffer.trim().parse::<i64>().unwrap(); for _ in 0..entries { let mut accumulator = 0; buffer.clear(); stdin().read_line(&mut buffer).expect("Failed"); let inputs...
medium
0804
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> dishes, numbered <var>1, 2, \ldots, N</var>. Initially, for each <var>i</var> (<var>1 \leq i \leq N</var>), Dish <var>i</var> has <var>a_i</var> (<var>1 \leq a_i \leq 3</var>) pie...
int solution(void) { int i; int j; int k; int N; int a[310]; int one = 0; int two = 0; int three = 0; double dp[310][310][310]; scanf("%d", &N); for (i = 1; i <= N; i++) { scanf("%d", &a[i]); switch (a[i]) { case 1: one++; break; case 2: two++; break; ca...
fn solution() { input! { n: usize, a: [usize; n], } let cc3 = a.iter().filter(|&v| *v == 3).count(); let cc2 = a.iter().filter(|&v| *v == 2).count(); let cc1 = a.iter().filter(|&v| *v == 1).count(); let mut dp = vec![vec![vec![0.0; n + 10]; n + 10]; n + 10]; for c3 in 0..n...
medium
0805
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> children, numbered <var>1, 2, ..., N</var>.</p> <p>Snuke has decided to distribute <var>x</var> sweets among them. He needs to give out all the <var>x</var> sweets, but some of th...
int solution() { int n = 0; int x = 0; scanf("%d %d", &n, &x); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n; i++) { for (int j = n - 1; j > i; j--) { if (a[j] < a[j - 1]) { int temp = a[j]; a[j] = a[j - 1]; a[j - 1] = temp; ...
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 mut x = it.next().unwrap().parse::<i32>().unwrap(); let mut a = (0..n) .map(|_| it.next().unwrap().p...
medium
0806
Tokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i \ne j$$$; $$$1 \leq i,j \leq n$$$). If $$$a_i = a_j$$$, change one of them to $$$0$$$. Otherwise change both of them to $$$\min(a_i, a_j)$$$. Tokitsukaze wants to know the minimum number of...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int zeros = 0; scanf("%d", &n); int arr[n]; int binary[101]; for (int i = 0; i < 101; ++i) { binary[i] = 0; } for (int i = 0; i < n; ++i) { scanf("%d", &arr[i]); binary[arr[i]]++; if (arr[i] == 0)...
fn solution() { let stdin = std::io::stdin(); let mut inputs = String::new(); stdin.lock().read_to_string(&mut inputs).ok(); let mut scanner = inputs .split_whitespace() .map(|x| x.parse::<usize>().unwrap()); let case = scanner.next().unwrap(); for _ in 0..case { let n...
medium
0807
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A biscuit making machine produces <var>B</var> biscuits at the following moments: <var>A</var> seconds, <var>2A</var> seconds, <var>3A</var> seconds and each subsequent multiple of <var>A</var> seconds ...
int solution() { int time = 0; int biscuit = 0; int t_limit = 0; int i = 0; int b_cnt = 0; scanf("%d%d%d", &time, &biscuit, &t_limit); for (i = 1; i * time < t_limit + 0.5; i++) { b_cnt += biscuit; } printf("%d", b_cnt); return 0; }
fn solution() { let stdin = io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).ok(); let line: Vec<i32> = buf.split_whitespace().map(|s| s.parse().unwrap()).collect(); let A = line[0]; let B = line[1]; let T = line[2]; println!("{}", T / A * B); }
hard
0808
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a grid with <var>A</var> horizontal rows and <var>B</var> vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:</p> <ul> <li>Assume th...
int solution() { long long int a; long long int b; long long int c; long long int d; long long int i; long long int j; long long int x[3100][3100]; long long int P = 998244353; scanf("%lld %lld %lld %lld", &a, &b, &c, &d); x[a][b] = 1; for (i = a; i <= c - 1; i++) { x[i + 1][b] = (x[i][b] * b...
fn solution() { const MOD: usize = 998244353; let [a, b, c, d]: [usize; 4] = { 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()...
medium
0809
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Takahashi has a deposit of <var>100</var> yen (the currency of Japan) in AtCoder Bank.</p> <p>The bank pays an annual interest rate of <var>1</var> % compounded annually. (A fraction of less than one y...
int solution(void) { long X = 0; long i = 100; int count = 0; scanf("%ld", &X); while (i < X) { i *= 1.01; count++; } printf("%d\n", count); return 0; }
fn solution() { let stdin = io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).ok(); buf.pop(); let X = i64::from_str(&buf).unwrap(); let mut money = 100; let mut cnt = 1; loop { money = money + (money / 100); if money >= X { println!("{}", cn...
medium
0810
You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is...
int solution() { int n; int m; char mat[105][105]; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", mat[i]); } int cnt = 0; int del[105] = {0}; int les[105] = {0}; for (int i = 0; i < m; i++) { int flag = 0; for (int j = 1; j < n; j++) { if (les[j] == 0 && mat[j][...
fn solution() { let (n, m) = { let mut line = String::new(); stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse::<usize>().unwrap(), iter.next().unwrap().parse::<usize>().unwrap(), ) }; ...
medium
0811
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A programming competition site <em>AtCode</em> provides algorithmic problems. Each problem is allocated a score based on its difficulty. Currently, for each integer <var>i</var> between <var>1</var> and...
int solution() { int d; int g; scanf("%d%d", &d, &g); int p[d]; int c[d]; int ans = 1001; for (int i = 0; i < d; i++) { scanf("%d%d", p + i, c + i); } for (int i = 0; i < (1 << d); i++) { int point = 0; int cnt = 0; for (int j = 0; j < d; j++) { if ((i & (1 << j)) != 0) { ...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let d: usize = itr.next().unwrap().parse().unwrap(); let g: usize = itr.next().unwrap().parse().unwrap(); let mut p: Vec<usize> = vec![0; d]; let mut c: Vec<us...
easy
0812
Qpwoeirut has taken up architecture and ambitiously decided to remodel his city.Qpwoeirut's city can be described as a row of $$$n$$$ buildings, the $$$i$$$-th ($$$1 \le i \le n$$$) of which is $$$h_i$$$ floors high. You can assume that the height of every floor in this problem is equal. Therefore, building $$$i$$$ is ...
int solution() { int t = 0; int n = 0; long long h[100000] = {}; int res = 0; long long ans1[100000] = {}; long long ans2[100000] = {}; res = scanf("%d", &t); while (t > 0) { long long ans = 0LL; res = scanf("%d", &n); for (int i = 0; i < n; i++) { res = scanf("%lld", h + i); }...
fn solution() { conio!(cin, cout); let t = scanf!(cin, usize); for _ in 1..=t { let n = scanf!(cin, usize); let h = scanf!(cin, [i64; n]); let mut dp = vec![(0, 0); n + 1]; for i in 2..n { if dp[i - 1].0 == dp[i - 2].0 + 1 { dp[i].0 = dp[i - 1].0...
easy
0813
Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside.The text is a string $$$s$$$ of lowercase Latin letters. She considers a string $$$t$$$ as hidden in string $$$s$$$ if $$$t$$$ exists as a subsequence of $$$s$$$ whose i...
int solution() { char s[100001]; scanf("%s", s); int n = strlen(s); long long int a[27][26] = {0}; for (int i = 0; i < n; i++) { for (int o = 0; o < 26; o++) { a[o][s[i] - 'a'] += a[26][o]; } a[26][s[i] - 'a']++; } long long int m = 0; for (int i = 0; i < 27; i++) { for (int o = 0;...
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().collect::<Vec<_>>(); let mut c1 = [0u64; 26]; let mut c2 = [[0u64; 26]; 26]; ...
medium
0814
You are given a decimal representation of an integer $$$x$$$ without leading zeros.You have to perform the following reduction on it exactly once: take two neighboring digits in $$$x$$$ and replace them with their sum without leading zeros (if the sum is $$$0$$$, it's represented as a single $$$0$$$).For example, if $$...
int solution() { int n = 0; scanf("%d", &n); for (int n1 = 0; n1 < n; n1++) { char *s = malloc(200010); scanf("\n%s", s); int a = 0; for (int s1 = 1; s1 <= 200001; s1++) { if (s[s1] == '\0') { break; } if ((int)s[s1 - 1] + (int)(s[s1]) - 96 >= 10) { a = s1; ...
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 cs: Vec<char> = lines.next().unwrap().unwrap().chars().collect(); let l = cs.len(); let mut d = None; for i i...
medium
0815
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has a string <var>S</var> of length <var>N</var> consisting of digits from <code>0</code> through <code>9</code>.</p> <p>He loves the prime number <var>P</var>. He wants to know how many non-e...
int solution() { int n; int p; scanf("%d%d", &n, &p); char s[n + 5]; scanf("%s", s); long ans = 0; if (10 % p == 0) { for (int i = 0; i < n; i++) { if ((s[i] - '0') % p == 0) { ans += i + 1; } } } else { long dig = 1; long r[p]; long t = 0; for (int i = 0; i <...
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 p: usize = itr.next().unwrap().parse().unwrap(); let mut s: Vec<usize> = itr .next() .unwrap() ...
easy
0816
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an undirected connected weighted graph with <var>N</var> vertices and <var>M</var> edges that contains neither self-loops nor double edges.<br/> The <var>i</var>-th <var>(1≤i≤M)</var> edge...
int solution(void) { int n; int m; scanf("%d %d", &n, &m); int pass[101][101] = {0}; int x[m]; int y[m]; int z[m]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { if (i != j) { pass[i][j] = -1; } } } for (int i = 0; i < m; i++) { scanf("%d %d %d", &x...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let m: usize = itr.next().unwrap().parse().unwrap(); let mut g = vec![vec![1 << 30; n]; n]; let mut edges ...
medium
0817
Pay attention to the non-standard memory limit in this problem.In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solutio...
int solution() { int t; scanf("%d", &t); while (t--) { int a[80005][3]; int b[80005]; int n; int sum = 0; for (int i = 0; i < 8000; i++) { for (int j = 0; j < 3; j++) { a[i][j] = 0; } } scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i][0]); ...
fn solution() -> io::Result<()> { let mut input = String::new(); io::stdin().read_line(&mut input)?; let n = input.trim().parse().unwrap(); for _ in 0..n { input.clear(); io::stdin().read_line(&mut input)?; input.clear(); io::stdin().read_line(&mut input)?; let ...
hard
0818
Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the larges...
int solution() { int t = 0; scanf("%d", &t); while (t--) { int n = 0; int a = 0; int b = 0; int c = 0; int d = 0; int e = 0; scanf("%d%d%d", &n, &a, &b); if (a + b <= n) { printf("1 "); } else { c = a + b; d = n - 1; e = c - d; if (e >= 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 stuff = { let mut buf = String::new(); stdin().read_line(&mut buf).expect("Faile...
medium
0819
You are given an array $$$a_1, a_2, \dots, a_n$$$, which is sorted in non-descending order. You decided to perform the following steps to create array $$$b_1, b_2, \dots, b_n$$$: Create an array $$$d$$$ consisting of $$$n$$$ arbitrary non-negative integers. Set $$$b_i = a_i + d_i$$$ for each $$$b_i$$$. Sort the arr...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); if (n == 1) { long long int n1; long long int n2; scanf("%lld %lld", &n1, &n2); printf("%lld\n%lld\n", n2 - n1, n2 - n1); continue; } long long int a[n]; long long int b[n]; lon...
fn solution() { let t: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() }; for _tests in 0..t { let n: usize = { let mut line: String = String::new(); std::io::stdin().read_li...
easy
0820
You have a card deck of $$$n$$$ cards, numbered from top to bottom, i. e. the top card has index $$$1$$$ and bottom card — index $$$n$$$. Each card has its color: the $$$i$$$-th card has color $$$a_i$$$.You should process $$$q$$$ queries. The $$$j$$$-th query is described by integer $$$t_j$$$. For each query you should...
int solution() { int n; int q; scanf("%d %d", &n, &q); int sol[q]; int arrn[n]; int arrq[q]; for (int i = 0; i < n; i++) { scanf("%d", &arrn[i]); } for (int i = 0; i < q; i++) { scanf("%d", &arrq[i]); } for (int i = 0; i < q; i++) { int f = arrq[i]; for (int j = 0; j < n; j++) { ...
fn solution() { let mut line = String::new(); stdin().read_line(&mut line).unwrap(); let words: Vec<i64> = line .split_whitespace() .map(|x| x.parse().unwrap()) .collect(); let n = words[0]; let _q = words[1]; let mut line = String::new(); stdin().read_line(&mut li...
medium
0821
The sequence of $$$m$$$ integers is called the permutation if it contains all integers from $$$1$$$ to $$$m$$$ exactly once. The number $$$m$$$ is called the length of the permutation.Dreamoon has two permutations $$$p_1$$$ and $$$p_2$$$ of non-zero lengths $$$l_1$$$ and $$$l_2$$$.Now Dreamoon concatenates these two pe...
int solution() { long long int t; scanf("%lld", &t); do { long long int n; scanf("%lld", &n); long long int a[n]; long long int i; long long int st = 0; long long int freq2[n]; for (int i = 0; i < n; ++i) { freq2[i] = 0; } for (i = 0; i < n; ++i) { scanf("%lld", &a...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); for _ in 0..t { let n: usize = lines.next().unwrap().unwrap().parse().unwrap(); let xs: Vec<usize> = lines .next() .unwra...
hard
0822
Alice guesses the strings that Bob made for her.At first, Bob came up with the secret string $$$a$$$ consisting of lowercase English letters. The string $$$a$$$ has a length of $$$2$$$ or more characters. Then, from string $$$a$$$ he builds a new string $$$b$$$ and offers Alice the string $$$b$$$ so that she can guess ...
int solution() { int t; scanf("%i", &t); getchar(); while (t--) { char str[101]; scanf("%s", str); int len = strlen(str); for (int i = 0; i < len; i++) { if (i == 0 || i % 2 == 1) { printf("%c", str[i]); } } printf("\n"); } return 0; }
fn solution() { let stdin = io::stdin(); let mut iterator = stdin.lock().lines(); let mut line: String; let mut original: Vec<String> = Vec::new(); line = iterator.next().unwrap().unwrap(); let t: i32 = line.parse().unwrap(); for _ in 0..t { original.clear(); line = iterat...
easy
0823
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has two positive integers <var>A</var> and <var>B</var>.</p> <p>It is known that <var>A</var> plus <var>B</var> equals <var>N</var>. Find the minimum possible value of "the sum of the digits o...
int solution(void) { char n[100000]; scanf("%s", n); int ans = 0; for (int i = 0; n[i] != '\0'; i++) { ans += (int)n[i] - 48; } if (ans == 1) { ans = 10; } printf("%d\n", ans); return 0; }
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let mut m = n; let mut ans = 0; while m > 0 { ans += m % 10; m /= 10; } let mut ...
medium
0824
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>We have a tree with <var>N</var> vertices numbered <var>1</var> to <var>N</var>. The <var>i</var>-th edge in this tree connects Vertex <var>a_i</var> and Vertex <var>b_i</var>.<br/> Consider painting e...
int solution() { int i; int u; int w; int N; int M; int s[21]; int t[21]; int adj[51][51] = {}; scanf("%d", &N); for (i = 1; i < N; i++) { scanf("%d %d", &u, &w); adj[u][w] = i; adj[w][u] = i; } scanf("%d", &M); for (i = 0; i < M; i++) { scanf("%d %d", &(s[i]), &(t[i])); } ...
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
0825
Given three distinct integers $$$a$$$, $$$b$$$, and $$$c$$$, find the medium number between all of them.The medium number is the number that is neither the minimum nor the maximum of the given three numbers. For example, the median of $$$5,2,6$$$ is $$$5$$$, since the minimum is $$$2$$$ and the maximum is $$$6$$$.
int solution() { int a = 0; int b = 0; int c = 0; int m = 0; scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); if ((a < b && b < c) || (c < b && b < a)) { printf("%d\n", b); } if ((a < c && c < b) || (b < c && c < a)) { printf("%d\n", c); } if ((...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).expect(""); let t: i32 = line.trim().parse().expect(""); for _ in 0..t { let reader = io::stdin(); let mut numbers: Vec<i32> = reader .lock() .lines() .next() .u...
hard
0826
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values.
int solution() { int t; scanf("%d", &t); while (t--) { int n; int a[10010]; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } int place = -1; for (int i = 1; i <= n; i++) { if (a[i] % 2 == 0) { place = i; break; } } if (pla...
fn solution() { let mut quantity = String::new(); stdin().read_line(&mut quantity).unwrap(); let quantity = quantity.trim().parse::<u32>().unwrap(); for _i in 0..quantity { let mut arr = String::new(); stdin().read_line(&mut arr).unwrap(); arr = String::new(); stdin().rea...
medium
0827
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a string <var>S</var> of length <var>N</var> consisting of <code>R</code>, <code>G</code>, and <code>B</code>.</p> <p>Find the number of triples <var>(i,~j,~k)~(1 \leq i &lt; j &lt; k \leq N)</v...
int solution() { int n; char s[4000]; long long int sum = 0; scanf("%d", &n); scanf("%s", s); long long int rnum = 0; long long int gnum = 0; long long int bnum = 0; for (int i = 0; i < n; i++) { if (s[i] == 'R') { rnum++; continue; } if (s[i] == 'G') { gnum++; cont...
fn solution() { let n: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().parse().unwrap() }; let s: Vec<usize> = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end() ...
easy
0828
Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself. To make everything right, Petya is going to move at most one bracket from its...
int solution() { int n; int bra = 0; int count = 0; int check = 0; scanf("%d", &n); char a[n]; for (int i = 0; i < n; i++) { scanf(" %c", &a[i]); } for (int i = 0; i < n; i++) { if (a[i] == '(') { count++; } else if (a[i] == ')') { count--; } if (count < 0) { che...
fn solution() { let mut _buffer = String::new(); io::stdin() .read_line(&mut _buffer) .expect("failed to read input"); let mut buffer = String::new(); io::stdin() .read_line(&mut buffer) .expect("failed to read input"); let vec = buffer .trim() .chars(...
easy
0829
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a string <var>X</var>, which has an even number of characters. Half the characters are <code>S</code>, and the other half are <code>T</code>.</p> <p>Takahashi, who hates the string <code>ST</cod...
int solution() { int s_cnt = 0; int ans = 0; while (1) { char c = getchar(); if (c == 'S') { s_cnt++; ans++; } else if (c == 'T' && s_cnt > 0) { s_cnt--; ans--; } else if (c == 'T') { ans++; } else { break; } } printf("%d\n", ans); return 0; }
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).ok(); let v: Vec<char> = buf.trim().chars().collect(); let mut cnt = 0; let mut del = 0; for &x in v.iter() { if x == 'S' { cnt += 1; } else if cnt > 0 { cnt -= 1; ...
easy
0830
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a <var>2 \times N</var> grid. We will denote the square at the <var>i</var>-th row and <var>j</var>-th column (<var>1 \leq i \leq 2</var>, <var>1 \leq j \leq N</var>) as <var>(i, j)</var>.</p> <...
int solution(void) { int N = 0; int count = 0; int max = 0; scanf("%d", &N); int A[N][2]; for (int i = 0; i < N; i++) { scanf("%d", &A[i][0]); } for (int i = 0; i < N; i++) { scanf("%d", &A[i][1]); } for (int i = 0; i < N; i++) { count = 0; for (int k = 0; k <= i; k++) { count ...
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 squares: Vec<Vec<u32>> = vec![ (0..n) .map(|_| iter.next().unwrap().parse().unwrap())...
medium
0831
Your favorite shop sells $$$n$$$ Kinder Surprise chocolate eggs. You know that exactly $$$s$$$ stickers and exactly $$$t$$$ toys are placed in $$$n$$$ eggs in total.Each Kinder Surprise can be one of three types: it can contain a single sticker and no toy; it can contain a single toy and no sticker; it can contain b...
int solution() { int T; scanf("%d", &T); while (T--) { long long int n; long long int s; long long int t; scanf("%lld %lld %lld", &n, &s, &t); if (n == s && n == t) { printf("1\n"); } else { if (s >= t) { printf("%lld\n", n - t + 1); } else { printf("...
fn solution() -> io::Result<()> { let mut line = String::new(); let stdin = io::stdin(); stdin.lock().read_line(&mut line)?; let trimmed_line = line.trim(); let number_of_queries: u8 = trimmed_line.parse().unwrap(); let queries: Vec<_> = stdin .lock() .lines() .filter_...
medium
0832
Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pieces of sausage lay on the crust, and he does not really like the crust.The pizza is a circle of radius r and center at the origin. Pizza consists of the main part — circle of radius r - d with center at the origin, and ...
int solution() { int r; int d; scanf("%d%d", &r, &d); int n; scanf("%d", &n); int a = 0; int p[n][3]; for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { scanf("%d", &p[i][j]); } } for (int k = 0; k < n; k++) { float dis = sqrt((p[k][0] * p[k][0]) + (p[k][1] * p[k][1])); ...
fn solution() { let (r, d) = { let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); let mut it = input.split_whitespace().map(|k| k.parse::<f64>().unwrap()); (it.next().unwrap(), it.next().unwrap()) }; let n = { let mut input = String::new(...
medium
0833
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and th...
int solution() { int n; char values[200005]; scanf("%d", &n); scanf("%s", values); int one = 0; int zero = 0; for (int i = 0; i < n; i++) { if (values[i] == '0') { zero++; } else { one++; } } if (one < zero) { printf("%d\n", n - (2 * one)); } else { printf("%d\n", n ...
fn solution() { let mut text = String::new(); io::stdin().read_to_string(&mut text).unwrap(); let mut iter = text.split_whitespace(); let _n = iter.next().unwrap(); let s = iter.next().unwrap(); let mut count = 0i32; for c in s.chars() { if c == '1' { count += 1; ...
hard
0834
A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of ea...
int solution() { int n; int i = 0; int x = -1000; scanf("%d", &n); for (i = 0; i < n && x != 1; i++) { scanf("%d", &x); } if (x == 1) { printf("-1"); } else { printf("1"); } return 0; }
fn solution() { let inputstatus = 1; let mut buf = String::new(); let filename = "inputrust.txt"; if inputstatus == 0 { let mut f = File::open(filename).expect("file not found"); f.read_to_string(&mut buf) .expect("something went wrong reading the file"); } else { ...
medium
0835
After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers...
int solution() { long long int n; scanf("%lld", &n); long long int arr[n + 1]; long long int brr[n + 1]; long long int ais[n + 1]; long long int bis[n + 1]; long long int dist[n + 1]; for (long long int i = 1; i <= n; i++) { scanf("%lld", &arr[i]); ais[arr[i]] = i; } for (long long int i =...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let n: usize = lines.next().unwrap().unwrap().parse().unwrap(); let xs: Vec<usize> = lines .next() .unwrap() .unwrap() .split(' ') .map(|x| x.parse().unwrap()) .collect(); l...
hard
0836
Lee is going to fashionably decorate his house for a party, using some regular convex polygons...Lee thinks a regular $$$n$$$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $$$OX$$$-axis and at least one of its edges is parallel to the...
int solution() { int n; scanf("%d\n", &n); int p[n]; for (int i = 0; i < n; i++) { scanf("%d\n", &p[i]); if (((p[i]) % 4) == 0) { printf("YES\n"); } else { printf("NO\n"); } } }
fn solution() { let mut t = String::new(); io::stdin().read_line(&mut t).expect(""); let t: u32 = t.trim().parse().expect(""); for _ in 0..t { let mut n = String::new(); io::stdin().read_line(&mut n).expect(""); let n: u64 = n.trim().parse().expect(""); if n.is_multiple_o...
medium
0837
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a <var>4</var>-character string <var>S</var> consisting of uppercase English letters. Determine if <var>S</var> consists of exactly two kinds of characters which both appear twice in <var>...
int solution(void) { char S[5]; scanf("%s", S); char a = S[0]; char b = S[1]; char c = S[2]; char d = S[3]; bool ok = false; if (a == b && c == d && a != c) { ok = true; } if (a == c && b == d && a != d) { ok = true; } if (a == d && b == c && a != b) { ok = true; } if (ok) { ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).ok(); s = s.trim_end().to_owned(); let mut s_vec: Vec<char> = s.chars().collect(); s_vec.sort(); if s_vec[0] == s_vec[1] && s_vec[1] != s_vec[2] && s_vec[2] == s_vec[3] { println!("Yes"); } else { printl...
easy
0838
You have a list of numbers from $$$1$$$ to $$$n$$$ written from left to right on the blackboard.You perform an algorithm consisting of several steps (steps are $$$1$$$-indexed). On the $$$i$$$-th step you wipe the $$$i$$$-th number (considering only remaining numbers). You wipe the whole number (not one digit). When t...
int solution() { int T; scanf("%d", &T); int x[T]; int n; for (int i = 0; i < T; ++i) { scanf("%d %d", &n, &x[i]); } for (int i = 0; i < T; ++i) { printf("%d\n", 2 * x[i]); } return 0; }
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) ...
medium
0839
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer has three cards, one red, one green and one blue.<br/> An integer between <var>1</var> and <var>9</var> (inclusive) is written on each card: <var>r</var> on the red card, <var>g</var> on the gr...
int solution() { int r = 0; int g = 0; int b = 0; int number = 0; scanf("%d %d %d", &r, &g, &b); number = 100 * r + 10 * g + b; if (number % 4 == 0) { printf("YES"); } else { printf("NO"); } return 0; }
fn solution() { use std::io; let mut input = String::new(); let _ = io::stdin().read_line(&mut input); let inputs = input .split_whitespace() .map(|x| x.parse::<isize>().unwrap()) .collect::<Vec<isize>>(); let num = inputs[1] * 10 + inputs[2]; if num % 4 == 0 { pr...
easy
0840
<H1>Finding Missing Cards</H1> <p> Taro is going to play a card game. However, now he has only <var>n</var> cards, even though there should be 52 cards (he has no Jokers). </p> <p> The 52 cards include 13 ranks of each of the four suits: spade, heart, club and diamond. </p> <H2>Input</H2> <p> In the first line,...
int solution(void) { int repeatNumber = 0; scanf("%d", &repeatNumber); int spade[13] = {0}; int heart[13] = {0}; int chulab[13] = {0}; int dia[13] = {0}; char judge1 = 'A'; int judge2 = 0; for (int i = 0; i < repeatNumber; i++) { scanf(" %c %d", &judge1, &judge2); if (judge1 == 'S') { ...
fn solution() { let stdin = io::stdin(); let mut buf = String::new(); let design = ['S', 'H', 'C', 'D']; let mut trump = HashMap::new(); for d in &design { trump.insert(d, [false; 13]); } let _ = stdin.read_line(&mut buf); let n = i64::from_str(buf.trim()).unwrap(); for _...
medium
0841
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In Republic of Atcoder, there are <var>N</var> prefectures, and a total of <var>M</var> cities that belong to those prefectures.</p> <p>City <var>i</var> is established in year <var>Y_i</var> and belongs...
int solution() { long long N; long long M; long long i; scanf("%lld%lld", &N, &M); long long P[M]; long long Y[M]; long long C[M]; long long A[M]; long long count[N + 1]; for (i = 0; i < M; i++) { scanf("%lld%lld", &P[i], &Y[i]); C[i] = i; } for (i = 0; i <= N; i++) { count[i] = 0; ...
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 c = vec![Vec::new(); n]; for i in 0..m { ...
medium
0842
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> consisting of <code>0</code> and <code>1</code>. Find the maximum integer <var>K</var> not greater than <var>|S|</var> such that we can turn all the characters of <va...
int solution() { char S[100000]; scanf("%s", S); int length = strlen(S); int K = strlen(S); for (int i = 0; i < length - 1; i++) { if (S[i + 1] != S[i]) { if (2 * (i + 1) < length && length - (i + 1) < K) { K = length - (i + 1); } else if (i + 1 < K) { K = i + 1; } ...
fn solution() { let stdin = stdin(); let mut lock = stdin.lock(); let mut str1 = String::new(); lock.read_line(&mut str1).unwrap(); let vec: Vec<_> = str1.split_whitespace().next().unwrap().bytes().collect(); let mut sanjo = vec.len(); for (i, j) in vec.iter().enumerate() { if i == 0...
hard
0843
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> towns in Snuke Kingdom, conveniently numbered <var>1</var> through <var>N</var>. Town <var>1</var> is the capital.</p> <p>Each town in the kingdom has a <em>Teleporter</em>, a fac...
int solution() { int i; int N; int K; int a[100001]; scanf("%d %d", &N, &K); for (i = 1; i <= N; i++) { scanf("%d", &(a[i])); } int ans = 0; if (a[1] != 1) { a[1] = 1; ans++; } list *adj[100001]; list e[100001]; for (i = 2; i <= N; i++) { e[i].v = i; e[i].next = adj[a[i]]...
fn solution() { input!(n: usize, k: usize, a: [usize1; n]); let mut ans = 0; let mut a = a; if a[0] != 0 { ans += 1; a[0] = 0; } let mut tree = vec![vec![]; n]; for child in 0..n { let parent = a[child]; tree[parent].push(child); } let mut dist_from_l...
medium
0844
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Three poles stand evenly spaced along a line. Their heights are <var>a</var>, <var>b</var> and <var>c</var> meters, from left to right. We will call the arrangement of the poles <em>beautiful</em> if th...
int solution() { int a[10]; scanf("%d%d%d", &a[0], &a[1], &a[2]); if (a[1] - a[0] == a[2] - a[1]) { printf("YES"); } else { printf("NO"); } }
fn solution() { let stdin = io::stdin(); let mut s = String::new(); stdin.read_line(&mut s).ok(); s.pop(); let mut abc: Vec<i32> = Vec::new(); let it = s.split_whitespace(); for arg in it { abc.push(i32::from_str(arg).expect("e")); } let a = abc[0]; let b = abc[1]; le...
medium
0845
You are policeman and you are playing a game with Slavik. The game is turn-based and each turn consists of two phases. During the first phase you make your move and during the second phase Slavik makes his move.There are $$$n$$$ doors, the $$$i$$$-th door initially has durability equal to $$$a_i$$$.During your move you...
int solution() { int n; int x; int y; scanf("%d%d%d", &n, &x, &y); int d[n]; int good = 0; for (int i = 0; i < n; i++) { scanf("%d", &d[i]); if (d[i] <= x) { good++; } } if (x > y) { printf("%d", n); } else { printf("%d", (good + 1) / 2); } }
fn solution() { let (n, a, b): (usize, u32, u32) = { let mut read_buf = String::new(); io::stdin().read_line(&mut read_buf).unwrap(); let mut read_buf = read_buf.split_whitespace(); ( read_buf.next().unwrap().parse().unwrap(), read_buf.next().unwrap().parse()....
medium
0846
<H1>Toggling Cases</H1><br> <p> Write a program which converts uppercase/lowercase letters to lowercase/uppercase for a given string. </p> <H2>Input</H2> <p> A string is given in a line. </p> <H2>Output</H2> <p> Print the converted string in a line. Note that you do not need to convert any characters other ...
int solution(void) { char s[1201]; int i = 0; scanf("%[^\n]", s); while (s[i] != '\0' && s[i] != '\0') { if (s[i] > 96 && s[i] < 123) { s[i] = s[i] - 32; } else if (s[i] > 64 && s[i] < 91) { s[i] = s[i] + 32; } i++; } puts(s); return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); for c in s.trim().chars() { let n = c as u8; print!( "{}", if (65..=90).contains(&n) { (n + 32) as char } else if (97..=122).contains(&n) { ...
medium
0847
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>It is November <var>18</var> now in Japan. By the way, <var>11</var> and <var>18</var> are adjacent Lucas numbers.</p> <p>You are given an integer <var>N</var>. Find the <var>N</var>-th Lucas number.</p...
int solution(void) { int n; unsigned long long a = -1; unsigned long long b = 2; scanf("%d", &n); while (n-- > 0) { b = a + b; a = b - a; } printf("%llu\n", b); return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let n: usize = s.trim().parse().ok().unwrap(); let mut v: Vec<i64> = Vec::new(); v.push(2); v.push(1); for i in 2..(n + 1) { let x = v[i - 1] + v[i - 2]; v.push(x); } println!("{}", v...
medium
0848
You are given an integer $$$n$$$. You have to change the minimum number of digits in it in such a way that the resulting number does not have any leading zeroes and is divisible by $$$7$$$.If there are multiple ways to do it, print any of them. If the given number is already divisible by $$$7$$$, leave it unchanged.
int solution() { int t = 0; int n = 0; scanf("%d", &t); for (int i = 0; i < t; i++) { scanf("%d", &n); if (n % 7 == 0) { printf("%d\n", n); } else { for (int j = 0; j <= 9; j++) { n /= 10; n = n * 10 + j; if (n % 7 == 0) { printf("%d\n", n); br...
fn solution() { let mut buf = String::new(); let handle = io::stdin(); let _ = handle.read_line(&mut buf); let ntc: usize = buf.trim().parse().unwrap(); for _ in 1..=ntc { buf.clear(); let _ = handle.read_line(&mut buf); let num: usize = buf.trim().parse().unwrap(); l...
medium
0849
Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid a...
int solution() { int n; int a; static int cnt[3]; scanf("%d", &n); while (n-- > 0) { scanf("%d", &a); cnt[a % 3]++; } printf("%d\n", (cnt[0] / 2) + (cnt[1] < cnt[2] ? cnt[1] : cnt[2])); return 0; }
fn solution() -> io::Result<()> { let mut buf = String::new(); io::stdin().read_line(&mut buf)?; buf.clear(); io::stdin().read_line(&mut buf)?; let a: Vec<usize> = buf .split_ascii_whitespace() .map(|x| x.parse().unwrap()) .collect(); let mut rem = [0; 3]; for i in a...
easy
0850
The elections in which three candidates participated have recently ended. The first candidate received $$$a$$$ votes, the second one received $$$b$$$ votes, the third one received $$$c$$$ votes. For each candidate, solve the following problem: how many votes should be added to this candidate so that he wins the electio...
int solution() { int t; scanf("%d", &t); while (t--) { long long a[3]; long long max = 0; long long c = 0; for (int i = 0; i < 3; i++) { scanf("%lld", &a[i]); if (max < a[i]) { max = a[i]; } } for (int i = 0; i < 3; i++) { if (max == a[i]) { c += 1; ...
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 v = l .unwrap() .split(' ') .map(|w| w.parse::<u64>().unwrap()) .collect::<Vec<_>>(); let (m, c) = v[1..].iter().fo...
hard
0851
Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples.For example, triples...
int solution() { long long n = 0; scanf("%lli", &n); long long a = -1; long long b = -1; if (n > 2) { if (n % 2 == 0) { long long m = n / 2; a = m * m - 1; b = m * m + 1; } else { long long k = (n - 1) / 2; long long m = 1 + k; b = 2 * k * m; a = m * m + k *...
fn solution() { let stdin = std::io::stdin(); let mut s = String::new(); let _lines = stdin.lock().read_line(&mut s).unwrap(); let a = s.trim().parse::<u64>().unwrap(); if a <= 2 { println!("-1"); return; } if (a & 0x01u64) != 0 { println!("{} {}", ((a * a) - 1) / 2, ...
easy
0852
A sequence $$$a = [a_1, a_2, \ldots, a_l]$$$ of length $$$l$$$ has an ascent if there exists a pair of indices $$$(i, j)$$$ such that $$$1 \le i &lt; j \le l$$$ and $$$a_i &lt; a_j$$$. For example, the sequence $$$[0, 2, 0, 2, 0]$$$ has an ascent because of the pair $$$(1, 4)$$$, but the sequence $$$[4, 3, 3, 3, 1]$$$ ...
int solution() { long long int n; scanf("%lld", &n); long long int l; long long int sl[100005]; sl[0] = 0; long long int i; long long int j; long long int s[100005]; for (i = 0; i < n; i++) { scanf("%lld", &l); for (j = 0; j < l; j++) { scanf("%lld", &s[sl[i] + j]); } sl[i + 1] =...
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
0853
Vasya has recently got a job as a cashier at a local store. His day at work is $$$L$$$ minutes long. Vasya has already memorized $$$n$$$ regular customers, the $$$i$$$-th of which comes after $$$t_{i}$$$ minutes after the beginning of the day, and his service consumes $$$l_{i}$$$ minutes. It is guaranteed that no custo...
int solution() { int n; int L; int a; scanf("%d %d %d", &n, &L, &a); int t[n]; int l[n]; for (int i = 0; i < n; ++i) { scanf("%d %d", &t[i], &l[i]); } int time_between = 0; int sessions = 0; for (int i = 0; i < n; ++i) { if (i == 0) { time_between = t[i] - 0; } else { time...
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<_> = buffer .split_whitespace() .map(|x| x.parse::<u32>().unwrap()) .collect(); let n = arr[0] as usize; ...
medium
0854
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers, and an array $$$b$$$, with length $$$n$$$. Initially $$$b_i=0$$$ for each $$$1 \leq i \leq n$$$.In one move you can choose an integer $$$i$$$ ($$$1 \leq i \leq n$$$), and add $$$a_i$$$ to $$$b_i$$$ or subtract $$$a_i$$$ from $$$b_i$$$. What is the ...
int solution(void) { int n; scanf("%d", &n); long long int a[n]; for (int i = 0; i < n; i++) { scanf("%lld", a + i); } long long int zeropos; long long int minmov = LLONG_MAX; for (zeropos = 0; zeropos < n; zeropos++) { long long int movcount = 0; long long b[n]; b[zeropos] = 0; for ...
fn solution() { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); let n: usize = input.trim().parse().unwrap(); input = String::new(); io::stdin().read_line(&mut input).unwrap(); let arr: Vec<i64> = input .trim() .split(' ') .map(|x| x.parse().unw...
medium
0855
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer <var>N</var>. Build an undirected graph with <var>N</var> vertices with indices <var>1</var> to <var>N</var> that satisfies the following two conditions:</p> <ul> <li>The graph ...
int solution() { int n; scanf("%d", &n); if (n & 1) { printf("%d\n", (n - 1) * (n - 1) / 2); for (int i = 2; i <= n / 2; ++i) { for (int j = 1; j < i; ++j) { printf("%d %d\n", j, i); printf("%d %d\n", n - j, i); printf("%d %d\n", j, n - i); printf("%d %d\n", n - j, n ...
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 ng = if n % 2 == 0 { n + 1 } else { n }; let mut res = Vec::new(); for i in 1..n { for j in i + ...
medium
0856
Let's call a positive integer composite if it has at least one divisor other than $$$1$$$ and itself. For example: the following numbers are composite: $$$1024$$$, $$$4$$$, $$$6$$$, $$$9$$$; the following numbers are not composite: $$$13$$$, $$$1$$$, $$$2$$$, $$$3$$$, $$$37$$$. You are given a positive integer $$$n$$...
int solution() { int n = 0; scanf("%d", &n); if (n % 2 == 0) { printf("%d %d", n + 4, 4); } else { printf("%d %d", n + 9, 9); } return 0; }
fn solution() { let mut s = String::from(""); io::stdin().read_line(&mut s).expect(""); let n: i32 = s.trim().parse().expect(""); println!("{} {}", 9 * n, 8 * n); }
medium
0857
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer <var>N</var> that has exactly four digits in base ten. How many times does <code>2</code> occur in the base-ten representation of <var>N</var>?</p> </section> </div> <div class=...
int solution(void) { int n = 0; int ans = 0; scanf("%d", &n); for (int i = 1; i <= 4; i++) { if (n % 10 == 2) { ans += 1; } n /= 10; } printf("%d", ans); return 0; }
fn solution() { let mut buf = String::new(); let handle = std::io::stdin(); handle.read_line(&mut buf).unwrap(); let mut count = 0; for c in buf.chars() { if c == '2' { count += 1; } } println!("{}", count); }
medium
0858
Cycle sort
void solution(int *arr, int n) { int writes = 0; for (int cycle_start = 0; cycle_start <= n - 2; cycle_start++) { int item = arr[cycle_start]; int pos = cycle_start; for (int i = cycle_start + 1; i < n; i++) { if (arr[i] < item) { pos++; } } if (pos == cycle_start) { ...
fn solution(arr: &mut [i32]) { for cycle_start in 0..arr.len() { let mut item = arr[cycle_start]; let mut pos = cycle_start; for i in arr.iter().skip(cycle_start + 1) { if *i < item { pos += 1; } } if pos == cycle_start { co...
easy
0859
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> gems. The value of the <var>i</var>-th gem is <var>V_i</var>.</p> <p>You will choose some of these gems, possibly all or none, and get them.</p> <p>However, you need to pay a cost...
int solution() { int total = 0; int t = 0; int i = 0; int N = 0; int V[50]; int C[50]; for (i = 0; i < 50; i++) { V[i] = 0; C[i] = 0; } scanf("%d", &N); for (i = 0; i < N; i++) { scanf("%d", &V[i]); } for (i = 0; i < N; i++) { scanf("%d", &C[i]); } for (i = 0; i < N; i++...
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let n: usize = buf.trim().parse().unwrap(); let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let vs = buf .split_whitespace() .map(|x| x.parse::<i32>().unwrap()...
medium
0860
You are playing another computer game, and now you have to slay $$$n$$$ monsters. These monsters are standing in a circle, numbered clockwise from $$$1$$$ to $$$n$$$. Initially, the $$$i$$$-th monster has $$$a_i$$$ health.You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the h...
int solution() { int t; scanf("%d", &t); long long int res[t]; for (int i = 0; i < t; i++) { int n; scanf("%d", &n); long long int health[n]; long long int damage[n]; long long int bullets = 0; for (int j = 0; j < n; j++) { scanf("%lld %lld", &health[j], &damage[j]); } for...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); let mut s = String::new(); for _ in 0..t { let n: usize = lines.next().unwrap().unwrap().parse().unwrap(); let mut ls: Vec<u64> = vec![0; n];...
hard
0861
You are given a grid, consisting of $$$2$$$ rows and $$$n$$$ columns. Each cell of this grid should be colored either black or white.Two cells are considered neighbours if they have a common border and share the same color. Two cells $$$A$$$ and $$$B$$$ belong to the same component if they are neighbours, or if there i...
int solution(int argc, char **argv) { int n; int k; scanf("%d %d", &n, &k); int64_t **same = (int64_t **)calloc(n, sizeof(int64_t *)); int64_t **opposite = (int64_t **)calloc(n, sizeof(int64_t *)); for (int i = 0; i < n; i++) { same[i] = (int64_t *)calloc(k, sizeof(int64_t)); opposite[i] = (int64_...
fn solution() { let mut buffer = String::new(); let stdin = io::stdin(); let mut handle = stdin.lock(); handle.read_to_string(&mut buffer).expect("stdin"); let arr: Vec<_> = buffer.split_whitespace().collect(); let n = arr[0].parse::<u32>().unwrap(); let k = arr[1].parse::<u32>().unwrap()...
medium
0862
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given integers <var>N</var> and <var>M</var>.</p> <p>Consider a sequence <var>a</var> of length <var>N</var> consisting of positive integers such that <var>a_1 + a_2 + ... + a_N</var> = <var>M</...
int solution() { int n; int m; scanf("%d%d", &n, &m); if (n == 1) { printf("%d", m); return 0; } for (int i = m / 2; i; i--) { if (!(m % i) && m / i >= n) { printf("%d", i); return 0; } } return 0; }
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 m: usize = iter.next().unwrap().parse().unwrap(); let mut ans = 1; let mut gcd = 1; while ...
medium
0863
<H1>Capitalize</H1> <p> Write a program which replace all the lower-case letters of a given text with the corresponding captital letters. </p> <H2>Input</H2> <p> A text including lower-case letters, periods, and space is given in a line. The number of characters in the text is less than or equal to 200. </p> <H2>O...
int solution() { char str[10001]; int i = 0; fgets(str, 10001, stdin); while (*(str + i) != '\0') { if (*(str + i) >= 'a' && *(str + i) <= 'z') { *(str + i) += -'a' + 'A'; } i++; } *(str + i - 1) = '\0'; puts(str); return 0; }
fn solution() { let mut buf = String::new(); stdin().read_line(&mut buf).unwrap(); let chars: Vec<char> = buf .trim() .chars() .map(|c| c.to_uppercase().next().unwrap()) .collect(); println!("{}", chars.iter().collect::<String>()); }
hard
0864
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Decades have passed since the beginning of AtCoder Beginner Contest.</p> <p>The contests are labeled as <code>ABC001</code>, <code>ABC002</code>, <var>...</var> from the first round, but after the <var>...
int solution() { int N = 0; scanf("%d", &N); printf((N < 1000) ? "ABC" : "ABD"); }
fn solution() { let mut n = String::new(); io::stdin().read_line(&mut n).expect("Failed to read line"); let n: u32 = n.trim().parse().expect("Failed to parse"); if n >= 1000 { println!("ABD"); } else { println!("ABC"); } }
easy
0865
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference i...
int solution() { long long num; long long i = 0; scanf("%lld", &num); long long arr[num]; long long max = 0; long long min = 1000000000; for (i = 0; i < num; ++i) { scanf("%lld", &arr[i]); if (arr[i] > max) { max = arr[i]; } if (arr[i] < min) { min = arr[i]; } } long lo...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let n: u32 = buf.trim().parse().unwrap(); let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut s = buf.split_whitespace(); let mut b = Vec::new(); for _ in 0..n { let ...
medium
0866
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integers <var>K</var> and <var>S</var>.<br/> Three variable <var>X, Y</var> and <var>Z</var> takes integer values satisfying <var>0≤X,Y,Z≤K</var>.<br/> How many different assignments o...
int solution() { int *k = malloc(sizeof(int)); int *s = malloc(sizeof(int)); int resp = 0; scanf("%d%d", k, s); for (int x = 0; x <= *k; ++x) { for (int y = 0; y <= *k; ++y) { int z = (*s) - x - y; if (*k >= z && z >= 0) { ++resp; } } } printf("%d\n", resp); free(k); ...
fn solution() { let mut input = String::new(); let _ = std::io::stdin().read_line(&mut input).unwrap(); let input = input .split_whitespace() .map(|x| x.parse::<u32>().unwrap()) .collect::<Vec<_>>(); let k = input[0] + 1; let s = input[1]; let mut count = 0; for x i...
easy
0867
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A robot is put at the origin in a two-dimensional plane. Initially, the robot is facing in the positive <var>x</var>-axis direction.</p> <p>This robot will be given an instruction sequence <var>s</var>....
int solution() { char s[8003]; scanf("%s", s); int x; int y; scanf("%d %d", &x, &y); int i; int j; int n = strlen(s); int v[2][2][20000]; for (i = 0; i < 20000; i++) { for (j = 0; j < 2; j++) { v[0][j][i] = 0; } } v[0][0][10000] = v[1][0][10000] = 1; int k = 0; j = 0; int a; ...
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 op = s .next() .unwrap() .split("T") .map(|a| a.len()) .collect::<Vec<_>>(); let x: i32 = s.next().unwrap(...
medium
0868
Madoka decided to participate in an underground sports programming competition. And there was exactly one task in it:A square table of size $$$n \times n$$$, where $$$n$$$ is a multiple of $$$k$$$, is called good if only the characters '.' and 'X' are written in it, as well as in any subtable of size $$$1 \times k$$$ o...
int solution() { int t; scanf("%d", &t); int n[t]; int k[t]; int r[t]; int c[t]; int i = 0; for (; i < t; i++) { scanf("%d", &n[i]); scanf("%d", &k[i]); scanf("%d", &r[i]); scanf("%d", &c[i]); } for (i = 0; i < t; i++) { r[i]--; c[i]--; int row = 0; int col = 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, k, r, c): (usize, usize, usize, usize) = (input.token(), input.t...
easy
0869
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a positive integer <var>N</var>. Find the minimum positive integer divisible by both <var>2</var> and <var>N</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><...
int solution(void) { int N = 0; scanf("%d", &N); if (N % 2 == 0) { printf("%d\n", N); return 0; } printf("%d\n", 2 * N); return 0; return 0; }
fn solution() { let mut ln = String::new(); io::stdin().read_line(&mut ln).ok(); let n: i32 = ln.trim().parse().unwrap(); println!("{}", if n % 2 == 0 { n } else { n * 2 }); }
easy
0870
You like the card board game "Set". Each card contains $$$k$$$ features, each of which is equal to a value from the set $$$\{0, 1, 2\}$$$. The deck contains all possible variants of cards, that is, there are $$$3^k$$$ different cards in total.A feature for three cards is called good if it is the same for these cards or...
int solution() { int x; int y; int z; int w; int i; int j; int k; int a; int b; int n; int m; int t; int arr[1000][20]; long long c; long long cnt[1000]; long long d; long long nn[1000]; do { scanf("%d %d", &n, &k); memset(cnt, 0, sizeof(cnt)); for (x = 0; x < n; x++) {...
fn solution() { input! { n: usize, k: usize, c: [[i8; k]; n], } let mut map = BTreeMap::new(); for i in 0..n { map.insert(&c[i], 0i64); } for i in 0..n { for j in 0..i { let mut good = vec![0; k]; for k in 0..k { goo...
easy
0871
You are given an array of positive integers $$$a_1, a_2, \ldots, a_n$$$.Make the product of all the numbers in the array (that is, $$$a_1 \cdot a_2 \cdot \ldots \cdot a_n$$$) divisible by $$$2^n$$$.You can perform the following operation as many times as you like: select an arbitrary index $$$i$$$ ($$$1 \leq i \leq n$...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int max = 0; int a; int ava; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a); while (a != 0) { if (a % 2 == 0) { max++; a /= 2; } else { break;...
fn solution() { let stdin = std::io::stdin(); let stdin_lock = stdin.lock(); let mut line_iter = stdin_lock.lines(); let t = line_iter .next() .unwrap() .unwrap() .as_str() .parse::<usize>() .unwrap(); for _ in 0..t { let n = line_iter.next().u...
medium
0872
You are given the array $$$a$$$ consisting of $$$n$$$ positive (greater than zero) integers.In one move, you can choose two indices $$$i$$$ and $$$j$$$ ($$$i \ne j$$$) such that the absolute difference between $$$a_i$$$ and $$$a_j$$$ is no more than one ($$$|a_i - a_j| \le 1$$$) and remove the smallest of these two ele...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int a[n]; int min = 100; int max = 0; int ct[101]; memset(ct, 0, 101 * sizeof(int)); for (int i = 0; i < n; ++i) { int aa; scanf("%d", &aa); a[i] = aa; ct[aa] = 1; if (min >...
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)....
medium
0873
For an array $$$[b_1, b_2, \ldots, b_m]$$$ define its number of inversions as the number of pairs $$$(i, j)$$$ of integers such that $$$1 \le i &lt; j \le m$$$ and $$$b_i&gt;b_j$$$. Let's call array $$$b$$$ odd if its number of inversions is odd. For example, array $$$[4, 2, 7]$$$ is odd, as its number of inversions is...
int solution(void) { int testcase = 0; scanf("%i", &testcase); for (int i = 0; i < testcase; i++) { int n = 0; scanf("%i", &n); int p[n]; for (int i = 0; i < n; i++) { scanf("%i", &p[i]); } int count = 0; int i = 0; while (i < n) { if (p[i] > p[i + 1]) { count+...
fn solution() { let mut inputt = String::new(); io::stdin().read_line(&mut inputt).expect("input faild"); let t: i32 = inputt.trim().parse().expect("not a nubmer"); for _ in 0..t as usize { let mut _inputn = String::new(); io::stdin().read_line(&mut _inputn).expect("input faild"); ...
medium
0874
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is buying a bicycle. The bicycle of his choice does not come with a bell, so he has to buy one separately.</p> <p>He has very high awareness of safety, and decides to buy two bells, one for each h...
int solution() { int price[3]; scanf("%d %d %d", &price[0], &price[1], &price[2]); if (price[0] >= price[1] && price[0] >= price[2]) { printf("%d\n", price[1] + price[2]); } else if (price[1] >= price[0] && price[1] >= price[2]) { printf("%d\n", price[0] + price[2]); } else { printf("%d\n", price[...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).ok(); let v: Vec<u16> = line .split_whitespace() .map(|n| n.parse().unwrap()) .collect(); let a = v[0]; let b = v[1]; let c = v[2]; println!("{}", cmp::min(cmp::min(a + b, b + c), a + c)); ...
medium
0875
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Compute <var>A \times B</var>, truncate its fractional part, and print the result as an integer.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0 \leq A \leq 10^{15...
int solution(void) { long double A = 0.0; long double B = 0.0; scanf("%Lf %Lf", &A, &B); B = A * B; B = floorl(B); printf("%0.0Lf\n", B); return 0; }
fn solution() { let (A, B) = { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let mut splitted = s.split_whitespace(); ( splitted.next().unwrap().to_string(), splitted.next().unwrap().to_string(), ) }; let mut aup = 0;...
hard
0876
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Nagase is a top student in high school. One day, she's analyzing some properties of special sets of positive integers. </p> <p>She thinks that a set <var>S = \{a_{1}, a_{2}, ..., a_{N}\}</var> of <stro...
int solution(void) { int N; scanf("%d", &N); if (N == 3) { printf("2 5 63"); return 0; } if (N == 4) { printf("2 5 20 63"); return 0; } int po = N % 8; if (po == 1) { printf("30000 "); } if (po == 2) { printf("30000 29994 "); } if (po == 3) { printf("30000 29998 29996...
fn solution() { let N: i64 = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() }; let m = std::cmp::min(5000, 2 * ((N - 2) / 2)); let res = if N == 3 { vec![2, 3, 25] } else { (0..m) ...
medium
0877
Consider a conveyor belt represented using a grid consisting of $$$n$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row from the top and the $$$j$$$-th column from the left is labelled $$$(i,j)$$$. Every cell, except $$$(n,m)$$$, has a direction R (Right) or D (Down) assigned to it. If the cell $$$(i,j)$$$ is ...
int solution() { int n = 0; int i = 0; scanf("%d", &n); for (; i < n; i++) { int n = 0; int m = 0; scanf("%d %d", &n, &m); int j = 0; int k = 0; int resp = 0; for (; j < n; j++) { for (k = 0; k < m; k++) { char a = 0; scanf(" %c", &a); if (a == 'D' && ...
fn solution() { let t: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().parse().unwrap() }; for _ in 0..t { let (n, m): (usize, usize) = { let mut buf = String::new(); std::io::stdin().read_line(&mut...
medium
0878
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $$$1$$$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $$$3$$$ steps, and the second conta...
int solution() { int c = 1; int n = 0; int i = 0; scanf("%d\n", &n); int a[n]; a[0] = 0; for (i = 1; i <= n; i++) { scanf("%d ", &a[i]); if (a[i] <= a[i - 1]) { c++; } } printf("%d\n", c); for (i = 1; i <= n; i++) { scanf("%d ", &a[i]); if (a[i] <= a[i - 1]) { prin...
fn solution() { let stdin = io::stdin(); let mut in_lines = stdin.lock().lines(); let _l: u32 = in_lines.next().unwrap().unwrap().parse().unwrap(); let elems: Vec<u32> = in_lines .next() .unwrap() .unwrap() .split(' ') .map(|s| s.parse::<u32>().unwrap()) ....
hard
0879
While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem.Given two strings a and b, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of one...
int solution() { const unsigned int n = 1 + 1e5; char Str1[n]; char Str2[n]; scanf("%s%s", Str1, Str2); unsigned int i = 0; unsigned int s1 = 0; unsigned int s2 = 0; while (Str1[s1] != '\0') { s1++; } while (Str2[s2] != '\0') { s2++; } while (Str1[i] == Str2[i] && Str1[i] != '\0' && Str2...
fn solution() { use std::io; let mut str1 = String::new(); let mut str2 = String::new(); io::stdin().read_line(&mut str1).unwrap(); io::stdin().read_line(&mut str2).unwrap(); let bytes1 = str1.trim().as_bytes(); let bytes2 = str2.trim().as_bytes(); if bytes1 == bytes2 { println!(...
medium
0880
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a string <var>S</var> of length <var>N</var>.</p> <p>Find the maximum length of a non-empty string that occurs twice or more in <var>S</var> as contiguous substrings without overlapping.</p> <p...
int solution() { int n; int ans = 0; scanf("%d\n", &n); char s[5005]; int a[n][n]; scanf("%s", s); for (int i = 0; i < n; i++) { a[i][i] = n - i; } for (int i = 0; i < n; i++) { int j = i + 1; int k = 0; while (j < n) { while (j + k < n && s[i + k] == s[j + k]) { k++; ...
fn solution() { let mut stdin = io::stdin(); let mut lines_str = String::new(); let _ = stdin.read_to_string(&mut lines_str); let lines_vec = lines_str.lines().collect::<Vec<&str>>(); let n: usize = lines_vec[0].trim().parse().unwrap(); let s_vec = lines_vec[1].trim().chars().collect::<Vec<char>...
medium
0881
Lee was cleaning his house for the party when he found a messy string under the carpets. Now he'd like to make it clean accurately and in a stylish way...The string $$$s$$$ he found is a binary string of length $$$n$$$ (i. e. string consists only of 0-s and 1-s).In one move he can choose two consecutive characters $$$s...
int solution() { int N; scanf("%d", &N); char *out[N]; for (int k = 0; k < N; k++) { int n; scanf("%d", &n); out[k] = (char *)malloc((n + 1) * sizeof(char)); scanf("%s", out[k]); int i; for (i = 0; i < n - 1 && out[k][i] <= out[k][i + 1]; i++) { ; } if (i != n - 1) { ...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); for _ in 0..t { let n: usize = lines.next().unwrap().unwrap().parse().unwrap(); let cs: Vec<char> = lines.next().unwrap().unwrap().chars().collec...
medium
0882
It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya's friend presented her an integer array $$$a$$$. Several hours after starting her journey home Nastya remembered about the present. To ...
int solution() { static int i; static int j; static int number[5000100]; static int sum; static int n; static int store[5000100] = {0}; static int pos1[5000100]; static int pos2[5000100]; static int flag = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &number[i]); } for (i = 0;...
fn solution() { let mut qntnumb = String::new(); io::stdin().read_line(&mut qntnumb).unwrap(); let _qntnumb: u32 = qntnumb.trim().parse().unwrap(); let mut valoresdosnumeros = String::new(); io::stdin().read_line(&mut valoresdosnumeros).unwrap(); let vectornumb = valoresdosnumeros .spl...
medium
0883
Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house.There are exac...
int solution() { int size = 0; scanf("%d", &size); int l_door = 0; int r_door = 0; int arr[size]; for (int i = 0; i < size; i++) { scanf("%d", &arr[i]); if (arr[i] == 0) { l_door++; } else { r_door++; } } for (int i = 0; i < size; i++) { if (arr[i] == 0) { l_door--;...
fn solution() { let mut input = String::new(); std::io::stdin() .read_line(&mut input) .expect("input: read line failed"); let amount = input .trim() .parse::<usize>() .expect("amount: parse to usize failed"); input.clear(); std::io::stdin() .read_line...
hard
0884
You are walking through a parkway near your house. The parkway has $$$n+1$$$ benches in a row numbered from $$$1$$$ to $$$n+1$$$ from left to right. The distance between the bench $$$i$$$ and $$$i+1$$$ is $$$a_i$$$ meters.Initially, you have $$$m$$$ units of energy. To walk $$$1$$$ meter of distance, you spend $$$1$$$ ...
int solution() { int t; int n; int m; scanf("%d", &t); for (int i = 0; i < t; i++) { int c = 0; scanf("%d %d", &n, &m); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); c = c + a[i]; } int e = m - c; if (e < 0) { printf("%d\n", -1 * e); } else { ...
fn solution() { let mut t = String::new(); io::stdin().read_line(&mut t).unwrap(); let t = t.trim().parse::<i32>().unwrap(); for _i in 0..t { let mut line = String::new(); let mut benches = String::new(); io::stdin().read_line(&mut line).unwrap(); io::stdin().read_line...
hard
0885
There are $$$n$$$ candies in a row, they are numbered from left to right from $$$1$$$ to $$$n$$$. The size of the $$$i$$$-th candy is $$$a_i$$$.Alice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob — from right to left. The game ends if all the candies are ea...
int solution() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int a[1000] = {0}; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int l = 0; int r = n - 1; int suml = 0; int sumr = 0; int cnt = 0; int ansl = 0; int ansr = 0; while (l <=...
fn solution() -> io::Result<()> { let mut input = String::new(); io::stdin().read_line(&mut input)?; let n = input.trim().parse().unwrap(); for _ in 0..n { input.clear(); io::stdin().read_line(&mut input)?; input.clear(); io::stdin().read_line(&mut input)?; let ...
hard
0886
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Our world is one-dimensional, and ruled by two empires called Empire A and Empire B.</p> <p>The capital of Empire A is located at coordinate <var>X</var>, and that of Empire B is located at coordinate <...
int solution(void) { int N = 0; int M = 0; int X = 0; int Y = 0; scanf("%d", &N); scanf("%d", &M); scanf("%d", &X); scanf("%d", &Y); int x[100]; int y[100]; int i = 0; for (i = 0; i < N; ++i) { scanf("%d", &x[i]); } for (i = 0; i < M; ++i) { scanf("%d", &y[i]); } int x_max = X; ...
fn solution() { let stdin = std::io::stdin(); let mut reader = std::io::BufReader::new(stdin.lock()); let mut v: Vec<Vec<i32>> = Vec::new(); for _ in 0..3 { let mut s = String::new(); let _ = reader.read_line(&mut s); let tmp = s.split_whitespace().map(|x| x.parse().unwrap()).c...
easy
0887
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Alice and Bob are playing <em>One Card Poker</em>.<br/> One Card Poker is a two-player game using playing cards. </p> <p>Each card in this game shows an integer between <code>1</code> and <code>13</cod...
int solution(void) { int a = 0; int b = 0; scanf("%d", &a); scanf("%d", &b); if (a == 1) { a = 14; } if (b == 1) { b = 14; } if (a < b) { printf("Bob"); } else if (a > b) { printf("Alice"); } else { printf("Draw"); } return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let v: Vec<i32> = s .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect(); let a = v[0]; let b = v[1]; if a == b { println!("Draw"); } else if a == 1 { print...
easy
0888
<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> squares arranged in a row from left to right.</p> <p>The height of the <var>i</var>-th square from the left is <var>H_i</var>.</p> <p>You will land on a square of your choice, the...
int solution() { int n; scanf("%d", &n); int H[n]; int count = 0; int max = 0; for (int i = 0; i < n; i++) { scanf("%d", &H[i]); if (i != 0 && H[i - 1] >= H[i]) { count++; } else { if (max < count) { max = count; } count = 0; } } if (max < count) { ...
fn solution() { let mut buft = String::new(); io::stdin().read_line(&mut buft).unwrap(); let n: usize = buft.trim().parse().unwrap(); buft.clear(); io::stdin().read_line(&mut buft).unwrap(); let hs: Vec<i32> = buft .split_whitespace() .map(|x| x.parse::<i32>().unwrap()) ....
medium
0889
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given are a positive integer <var>N</var> and a string <var>S</var> of length <var>N</var> consisting of lowercase English letters.</p> <p>Determine whether the string is a concatenation of two copies o...
int solution() { char S[101] = {0}; int N = 0; scanf("%d", &N); scanf("%s", S); if (N % 2 != 0) { printf("No\n"); } else { int ret = 0; ret = strncmp(&S[0], &S[(N / 2)], N / 2); if (ret != 0) { printf("No\n"); } else { printf("Yes\n"); } } return 0; }
fn solution() { let stdin = io::stdin(); let mut vec = Vec::new(); for line in stdin.lock().lines() { vec.push(line.unwrap()) } let n: usize = vec[0].parse().unwrap(); let s = &vec[1]; if !n.is_multiple_of(2) { println!("No"); return; } let m = n / 2; ...
medium
0890
<H1>Circle</H1> <p> Write a program which calculates the area and circumference of a circle for given radius <var>r</var>. </p> <H2>Input</H2> <p> A real number <var>r</var> is given. </p> <H2>Output</H2> <p> Print the area and circumference of the circle in a line. Put a single space between them. The output shou...
int solution(void) { double r = 0; scanf("%lf", &r); printf("%f %f\n", 3.141592653589 * r * r, 2 * r * 3.141592653589); return 0; }
fn solution() { let scan = std::io::stdin(); let mut line = String::new(); let _ = scan.read_line(&mut line); let vec: Vec<&str> = line.split_whitespace().collect(); let r: f64 = vec[0].parse().unwrap_or(0.0); println!( "{} {}", r * r * std::f64::consts::PI, r * 2_f64 * s...
medium
0891
Recently Vova found $$$n$$$ candy wrappers. He remembers that he bought $$$x$$$ candies during the first day, $$$2x$$$ candies during the second day, $$$4x$$$ candies during the third day, $$$\dots$$$, $$$2^{k-1} x$$$ candies during the $$$k$$$-th day. But there is an issue: Vova remembers neither $$$x$$$ nor $$$k$$$ b...
int solution() { int T; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); int s = 1; int p = 1; while (n > 0) { p = p * 2; s = s + p; if (n % s == 0) { printf("%d\n", n / s); break; } } } return 0; }
fn solution() { let mut stdin = BufReader::new(io::stdin()); let mut stdout = BufWriter::new(io::stdout()); let mut buffer = String::new(); stdin.read_line(&mut buffer).unwrap(); let t = buffer.trim().parse().unwrap(); for _ in 0..t { let mut buffer = String::new(); stdin.read_l...
medium
0892
For the given integer $$$n$$$ ($$$n &gt; 2$$$) let's write down all the strings of length $$$n$$$ which contain $$$n-2$$$ letters 'a' and two letters 'b' in lexicographical (alphabetical) order.Recall that the string $$$s$$$ of length $$$n$$$ is lexicographically less than string $$$t$$$ of length $$$n$$$, if there exi...
int solution() { int t = 0; scanf("%d", &t); for (int i = 1; i <= t; i++) { int n; int k; scanf("%d%d", &n, &k); long long int j = 1; while (k > (j * (j + 1)) / 2) { j++; } long long int c = k - ((j * (j - 1)) / 2); if ((n - 1 - j) > 0) { for (int k = 1; k <= n - 1 - j;...
fn solution() { let stdin = io::stdin(); let mut lock = stdin.lock(); let mut line = String::new(); lock.read_line(&mut line).unwrap(); let t = line.trim().parse().unwrap(); for _ in 0..t { let mut line = String::new(); lock.read_line(&mut line).unwrap(); let mut line = ...
easy
0893
Polycarp had an array $$$a$$$ of $$$3$$$ positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array $$$b$$$ of $$$7$$$ integers.For example, if $$$a = \{1, 4, 3\}$$$, then Polycarp wrote out $$$1$$$, $$$4$$$, $$$3$$$, $$$1 + 4 = 5$$$, $$$1...
int solution() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int a[7]; scanf("%d%d%d%d%d%d%d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7]); printf("%d %d %d\n", a[0], a[1], a[6] - a[1] - a[0]); } return 0; }
fn solution() { let t: u16 = { let mut buffer = String::new(); io::stdin().read_line(&mut buffer).unwrap(); buffer.trim().parse::<u16>().unwrap() }; for _ in 0..t { let a: Vec<u32> = { let mut buffer = String::new(); io::stdin().read_line(&mut buffer)...
easy
0894
Counting sort
int solution() { int i, n, l = 0; printf("Enter size of array = "); scanf("%d", &n); int *a = (int *)malloc(n * sizeof(int)); printf("Enter %d elements in array :\n", n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] > l) l = a[i]; } int *b = (int *)malloc((l + 1) * sizeof(int)...
fn solution<T>(arr: &mut [T], maxval: usize) where T: Into<u64> + From<u8> + AddAssign + Copy, { let mut occurrences: Vec<usize> = vec![0; maxval + 1]; for &value in arr.iter() { occurrences[value.into() as usize] += 1; } let mut index = 0; let mut current = T::from(0); for &count...
hard
0895
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You have <var>N</var> bamboos. The lengths (in centimeters) of these are <var>l_1, l_2, ..., l_N</var>, respectively.</p> <p>Your objective is to use some of these bamboos (possibly all) to obtain three...
int solution() { int n; int i; int j; int a; int b; int c; int min = 10000; int max = 1; scanf("%d%d%d%d", &n, &a, &b, &c); int l[n + 1]; for (i = 0; i < n; i++) { scanf("%d", &l[i]); max *= 4; } for (i = 1; i < max; i++) { int anum = 0; int bnum = 0; int cnum = 0; int ...
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 a: i32 = iter.next().unwrap().parse().unwrap(); let b: i32 = iter.next().unwrap().parse().unwrap(); ...
easy
0896
Alice and Bob are playing a game. They have an array of positive integers $$$a$$$ of size $$$n$$$.Before starting the game, Alice chooses an integer $$$k \ge 0$$$. The game lasts for $$$k$$$ stages, the stages are numbered from $$$1$$$ to $$$k$$$. During the $$$i$$$-th stage, Alice must remove an element from the array...
int solution() { int a[101]; int b; int t; int n; scanf("%d", &t); while (t--) { memset(a, 0, sizeof(a)); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &b); a[b]++; } int ans = 0; for (int k = 1; k <= n; k++) { int now = 0; int pd = 0; for ...
fn solution() { let mut buffer = String::new(); io::stdin().read_line(&mut buffer).unwrap(); let t = buffer.trim().parse::<u16>().unwrap(); for _ in 0..t { buffer = String::new(); io::stdin().read_line(&mut buffer).unwrap(); let n = buffer.trim().parse::<usize>().unwrap(); ...
hard
0897
Pak Chanek has a prime number$$$^\dagger$$$ $$$n$$$. Find a prime number $$$m$$$ such that $$$n + m$$$ is not prime.$$$^\dagger$$$ A prime number is a number with exactly $$$2$$$ factors. The first few prime numbers are $$$2,3,5,7,11,13,\ldots$$$. In particular, $$$1$$$ is not a prime number.
int solution() { int n = 0; int t; scanf("%d", &t); while (t--) { scanf("%d", &n); printf("%d\n", n); } return 0; }
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let t = s.trim().parse::<i32>().unwrap(); let mut writer = io::BufWriter::new(io::stdout()); for _ in 0..t { s.clear(); io::stdin().read_line(&mut s).unwrap(); let n = s.trim().parse::<i32>()....
hard
0898
<H1>Sorting Three Numbers</H1> <p> Write a program which reads three integers, and prints them in ascending order. </p> <H2>Input</H2> <p> Three integers separated by a single space are given in a line. </p> <H2>Output</H2> <p> Print the given integers in ascending order in a line. Put a single space betwee...
int solution() { int a = 0; int b = 0; int c = 0; scanf("%d %d %d", &a, &b, &c); if (a <= b && b <= c) { printf("%d %d %d\n", a, b, c); } else if (a <= c && c <= b) { printf("%d %d %d\n", a, c, b); } else if (b <= c && c <= a) { printf("%d %d %d\n", b, c, a); } else if (b <= a && a <= c) ...
fn solution() { let scan = std::io::stdin(); let mut line = String::new(); let _ = scan.read_line(&mut line); let vec: Vec<&str> = line.split_whitespace().collect(); let a: u32 = vec[0].parse().unwrap_or(0); let b: u32 = vec[1].parse().unwrap_or(0); let c: u32 = vec[2].parse().unwrap_or(0)...
medium
0899
Everyone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital was surrounded by n hills, forming a circle. On each hill there was a watchman, who watched the neighbourhood day and night.In case of any danger the watchman could make a fire on the hill. One watchman could ...
int solution() { long num; long max = 0; long pos = 0; long i; long j; unsigned long long count; long l[1000000]; long r[1000000]; long arr[1000000]; long h[1000000]; long c[1000000]; scanf("%ld", &num); for (i = 0; i < num; ++i) { c[i] = 0; scanf("%ld", &h[i]); if (h[i] > max) { ...
fn solution() { let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); let mut input = input.split_whitespace(); let n = read!(usize); let mut hs = read!([u64; n]); let max = *hs.iter().max().unwrap(); let i = hs.iter().position(|&h| h == max).unwrap(); let xs...
medium
0900
You are given an array $$$a$$$ consisting of $$$n$$$ integers. You want to distribute these $$$n$$$ integers into two groups $$$s_1$$$ and $$$s_2$$$ (groups can be empty) so that the following conditions are satisfied: For each $$$i$$$ $$$(1 \leq i \leq n)$$$, $$$a_i$$$ goes into exactly one group. The value $$$|sum(s...
int solution() { int case_num = 0; scanf("%d", &case_num); for (int i = 0; i < case_num; ++i) { int arr_len = 0; scanf("%d", &arr_len); long long int arr[100000] = {0}; long long int sum = 0; for (int j = 0; j < arr_len; ++j) { scanf("%lld", &arr[j]); sum += arr[j]; } sum >...
fn solution() { let mut buf = BufWriter::new(io::stdout().lock()); io::stdin() .lines() .skip(2) .step_by(2) .flatten() .for_each(|s| { let ans = s .split_ascii_whitespace() .flat_map(str::parse::<isize>) .sum::<...
hard