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
1201
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \le i \le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements i...
int solution() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { int t = 0; int n = 0; int c = 0; scanf("%d", &n); int a[n]; for (int j = 0; j < n; j++) { scanf("%d", &a[j]); t = t + a[j]; if (a[j] == 0) { c++; } } int s = c; if (c + t == 0...
fn solution() { let mut line = String::new(); io::stdin().read_line(&mut line).unwrap(); let t: u32 = line.trim().parse().unwrap(); for _ in 0..t { line = String::new(); io::stdin().read_line(&mut line).unwrap(); let _n: u32 = line.trim().parse().unwrap(); let mut a = Vec...
medium
1202
$$$n$$$ students are taking an exam. The highest possible score at this exam is $$$m$$$. Let $$$a_{i}$$$ be the score of the $$$i$$$-th student. You have access to the school database which stores the results of all students.You can change each student's score as long as the following conditions are satisfied: All sc...
int solution() { int n; scanf("%d\n", &n); for (int i = 1; i <= n; i++) { int x1; int x2; scanf("%d %d\n", &x1, &x2); int arr[x1]; for (int j = 0; j < x1; j++) { if (j != (x1 - 1)) { scanf("%d ", &arr[j]); } else { scanf("%d\n", &arr[j]); } } int sum =...
fn solution() { let mut t: String = String::new(); std::io::stdin().read_line(&mut t).expect("input"); let counter: Vec<i32> = t .split_whitespace() .map(|x| x.parse::<i32>().expect("Not an Integer!")) .collect(); let mut test_case = counter[0]; while test_case > 0 { ...
hard
1203
<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> balls in a two-dimensional plane. The <var>i</var>-th ball is at coordinates <var>(x_i, y_i)</var>.</p> <p>We will collect all of these balls, by choosing two integers <var>p</var...
int solution() { int n; scanf("%d", &n); int x[n]; int y[n]; for (int i = 0; i < n; i++) { scanf("%d %d", &x[i], &y[i]); } int max = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { int p = x[j] - x[i]; int q = y[j] - y[i]; int cnt = 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 a: Vec<(i64, i64)> = (0..n) .map(|_| { ( itr.next().unwrap().parse().unwra...
medium
1204
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate <var>A</var>, and he can travel the ...
int solution(void) { long long int a; long long int b; long long int v; long long int w; long long int t; scanf("%lld %lld %lld %lld %lld", &a, &v, &b, &w, &t); if (a < b) { if (a + v * t >= b + w * t) { printf("YES"); } else { printf("NO"); } } else { if (a + v * t * -1 <=...
fn solution() { let mut is = String::new(); stdin().read_line(&mut is).ok(); let mut itr = is.split_whitespace().map(|e| e.parse::<i64>().unwrap()); let a = itr.next().unwrap(); let v = itr.next().unwrap(); is.clear(); stdin().read_line(&mut is).ok(); let mut itr = is.split_whitespace()....
easy
1205
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has two boards, each divided into a grid with <var>N</var> rows and <var>N</var> columns. For both of these boards, the square at the <var>i</var>-th row from the top and the <var>j</var>-th colum...
int solution() { int N; scanf("%d", &N); char S[N][N]; for (int i = 0; i < N; i++) { scanf(" %s ", S[i]); } int num = 0; for (int k = 0; k < N; k++) { int symmetry = 1; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (S[(i + k) % N][j] != S[(j + k) % N][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 g: Vec<Vec<char>> = (0..n) .map(|_| itr.next().unwrap().chars().collect()) .collect(); let mut ans...
easy
1206
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are playing the following game with Joisino.</p> <ul> <li>Initially, you have a blank sheet of paper.</li> <li>Joisino announces a number. If that number is written on the sheet, erase the number fr...
int solution() { int N; scanf("%d", &N); int ans = N; int A[N]; for (int i = 0; i < N; i++) { scanf("%d", &A[i]); int j = i - 1; while (A[i] != A[j] && j >= 0) { j--; } if (j < 0) { continue; } A[j] = 0; A[i] = 0; ans -= 2; } printf("%d", ans); return 0;...
fn solution() { let stdin = io::stdin(); let mut stdin = stdin.lock(); for _ in stdin .by_ref() .bytes() .take_while(|r| r.as_ref().unwrap() != &b'\n') {} let mut set = HashSet::with_capacity(100_000); for a in stdin.lines() { let a: u32 = a.unwrap().parse().unwr...
hard
1207
You are given an array $$$a$$$ consisting of $$$n$$$ integers.In one move, you can choose two indices $$$1 \le i, j \le n$$$ such that $$$i \ne j$$$ and set $$$a_i := a_j$$$. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is th...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int count = 0; int temp = 0; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); count += arr[i]; if (arr[i] % 2 == 1) { temp++; } } if (temp != 0) { if ...
fn solution() -> Result<(), Box<dyn std::error::Error>> { let stdin = io::stdin(); let mut stdin = stdin.lock().lines(); let _ = stdin.next(); for line in stdin .map(|e| e.unwrap()) .collect::<Vec<String>>() .chunks(2) .map(|a| { let (cnt, (odd_sum, odd_number...
medium
1208
<H1>Sum of 4 Integers</H1> <p> Write a program which reads an integer <var>n</var> and identifies the number of combinations of <var>a, b, c</var> and <var>d</var> (0 &le; <var>a, b, c, d</var> &le; 9) which meet the following equality:<br> <br> <var>a + b + c + d = n</var><br> <br> For example, for <var>n</var> = 3...
int solution() { int No1 = 0; int No2 = 0; int No3 = 0; int No4 = 0; int Solns = 0; int Sum = 0; while (scanf("%d", &Sum) != EOF) { Solns = 0; for (No1 = 0; No1 < 10; No1++) { for (No2 = 0; No2 < 10; No2++) { for (No3 = 0; No3 < 10; No3++) { for (No4 = 0; No4 < 10; No4++) ...
fn solution() { let mut input; loop { input = String::new(); match std::io::stdin().read_line(&mut input) { Ok(0) => break, Err(_) => break, Ok(_) => {} }; let n = input.trim().parse::<u32>().expect("Error parse"); let mut count = 0; ...
medium
1209
You have decided to watch the best moments of some movie. There are two buttons on your player: Watch the current minute of the movie. By pressing this button, you watch the current minute of the movie and the player automatically proceeds to the next minute of the movie. Skip exactly x minutes of the movie (x is so...
int solution() { int n = 0; int x = 0; int i = 0; int current = 1; int min = 0; scanf("%d %d", &n, &x); int moments[n][2]; for (i = 0; i < n; i++) { scanf("%d %d", &moments[n][0], &moments[n][1]); min += moments[n][1] - (((moments[n][0] - current) / x) * x + current) + 1; current = moments[n...
fn solution() { let v: Vec<i32> = { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim() .split(" ") .map(|s| s.parse::<i32>().unwrap()) .collect() }; let n = v[0]; let x = v[1]; let mut v = vec![]; ...
hard
1210
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is an integer <var>x</var> that is greater than or equal to <var>0</var>, and less than or equal to <var>1</var>. Output <var>1</var> if <var>x</var> is equal to <var>0</var>, or <var>0</var> if <...
int solution() { int x = 0; scanf("%d", &x); if (x == 1) { printf("0"); } else { printf("1"); } }
fn solution() { let mut no = String::new(); io::stdin() .read_line(&mut no) .expect("failed to read line "); let no: i32 = no.trim().parse().expect("error!"); if no == 0 { println!("1"); } else if no == 1 { println!("0"); } else { println!("..."); }...
easy
1211
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given strings <var>s</var> and <var>t</var>, consisting of lowercase English letters. You will create a string <var>s'</var> by freely rearranging the characters in <var>s</var>. You will also c...
int solution() { int i = 0; int j = 0; int s_max = 0; int t_max = 0; int s_baketu[26] = {0}; int t_baketu[26] = {0}; char s[200] = {0}; char t[200] = {0}; scanf("%s", s); scanf("%s", t); for (i = 0; i < 200; i++) { if (s[i] != 0) { s_baketu[s[i] - 97]++; } } for (i = 0; i < 26;...
fn solution() { let mut stdin = String::new(); std::io::Read::read_to_string(&mut std::io::stdin(), &mut stdin).unwrap(); let mut stdin = stdin.split_whitespace(); let mut get = || stdin.next().unwrap(); let s = get().as_bytes(); let t = get().as_bytes(); let mut ss = vec![0; 256]; let...
medium
1212
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given three integers, <var>A</var>, <var>B</var> and <var>C</var>.<br/> Among them, two are the same, but the remaining one is different from the rest.<br/> For example, when <var>A=5,B=7,C=5</v...
int solution() { int A = 0; int B = 0; int C = 0; scanf("%d %d %d", &A, &B, &C); if (A == B) { printf("%d", C); } else if (B == C) { printf("%d", A); } else if (A == C) { printf("%d", B); } }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let vec: Vec<&str> = s.trim().split(' ').collect(); if vec[0] == vec[1] { println!("{}", vec[2]); } else if vec[1] == vec[2] { println!("{}", vec[0]); } else if vec[0] == vec[2] { prin...
easy
1213
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a two-dimensional grid with <var>H \times W</var> squares. There are <var>M</var> targets to destroy in this grid - the position of the <var>i</var>-th target is <var>\left(h_i, w_i \right)</var...
int solution() { int H; int W; int m; int hmax = 0; int wmax = 0; scanf("%d %d %d", &H, &W, &m); int hbomb[H + 1]; int h[m + 1]; int wbomb[W + 1]; int w[m + 1]; for (int i = 1; i <= H; i++) { hbomb[i] = 0; } for (int i = 1; i <= W; i++) { wbomb[i] = 0; } for (int i = 1; i <= m...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).ok(); let mut it = buf.split_whitespace(); let h = it.next().unwrap().parse::<usize>().unwrap(); let w = it.next().unwrap().parse::<usize>().unwrap(); let m = it.next().unwrap().parse::<usize>().unwrap(); ...
easy
1214
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Rng is going to a festival.</p> <p>The name of the festival is given to you as a string <var>S</var>, which ends with <code>FESTIVAL</code>, from input. Answer the question: "Rng is going to a festival ...
int solution() { char s[99]; scanf("%s", s); int n = strlen(s); s[n - 8] = 0; puts(s); }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); println!("{}", s.chars().take(s.len() - 9).collect::<String>()); }
easy
1215
<span class="lang-en"> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integers <var>a</var> and <var>b</var> (<var>a≤b</var>). Determine if the product of the integers <var>a</var>, <var>a+1</var>, <var>…</var>, <var>b</var> is positive, negative or zero.</p> </section> </div> <div class="p...
int solution(void) { long long int a; long long int b; scanf("%lld", &a); scanf("%lld", &b); if (a <= 0 && b >= 0) { printf("Zero\n"); return 0; } if (a < 0 && b < 0) { if ((b - a) % 2 == 0) { printf("Negative\n"); return 0; } printf("Positive\n"); return 0; } e...
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 a: i64 = vec2[0].trim().parse().unwrap(); let b: i64 = vec2[1].trim()...
easy
1216
You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to ...
int solution() { int n = 1; scanf("%d", &n); int positive = 0; int negative = 0; int zero = 0; long long int arr[n]; for (int i = 0; i < n; i++) { scanf("%lld", &arr[i]); long long int number = arr[i]; if (number > 0) { positive++; } else if (number < 0) { negative++; } ...
fn solution() { let mut input = String::new(); io::stdin() .read_line(&mut input) .expect("failed to read input"); let n = input.trim().parse::<u32>().unwrap(); let mut neg = 0; let mut zeros = 0; let mut sum = 0; let mut input = String::new(); io::stdin() .read_...
easy
1217
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given are two strings <var>S</var> and <var>T</var> consisting of lowercase English letters. Concatenate <var>T</var> and <var>S</var> in this order, without space in between, and print the resulting st...
int solution(void) { char s[101]; char t[101]; char buf[203]; fgets(buf, sizeof(buf), stdin); sscanf(buf, "%s %s", s, t); printf("%s%s\n", t, s); 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 s = iter.next().unwrap().to_owned(); let t = iter.next().unwrap().to_owned(); println!("{}", t + &s); }
medium
1218
<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> towns on a line running east-west. The towns are numbered <var>1</var> through <var>N</var>, in order from west to east. Each point on the line has a one-dimensional coordinate, a...
int solution(void) { long n; long a; long b; scanf("%ld %ld %ld", &n, &a, &b); long x[n]; for (long i = 0; i < n; i++) { scanf("%ld", &x[i]); } long min = 0; for (long i = 1; i < n; i++) { if ((x[i] - x[i - 1]) * a <= b) { min += (x[i] - x[i - 1]) * a; } else { min += b; }...
fn solution() { input! { n: usize, a: usize, b: usize, x: [usize; n], } let mut tired = 0; for i in 1..n { if (x[i] - x[i - 1]) * a <= b { tired += (x[i] - x[i - 1]) * a } else { tired += b; } } println!("{}", tired); }
easy
1219
Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length $$$k$$$ have the same sum. A subarray of an array is any sequence of consecutive elements.Phoenix currently has an array $$$a$$$ of length $$$n$$$. He wants to insert some number of integers, possibly zero, into his array such that it ...
int solution() { int t; scanf("%d", &t); while (t--) { int n; int k; scanf("%d %d", &n, &k); int A[n]; int uniq[100] = {0}; int len = 0; for (int i = 0; i < n; i++) { scanf("%d", &A[i]); if (uniq[A[i] - 1] == 0) { len++; uniq[A[i] - 1] = 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 xs: Vec<usize> = lines .next() .unwrap() .unwrap() .split(' ') .map(|x| x...
medium
1220
Let $$$s$$$ be a string of lowercase Latin letters. Its price is the sum of the indices of letters (an integer between 1 and 26) that are included in it. For example, the price of the string abca is $$$1+2+3+1=7$$$.The string $$$w$$$ and the integer $$$p$$$ are given. Remove the minimal number of letters from $$$w$$$ s...
int solution() { int t; scanf("%d", &t); for (int q = 0; q < t; q++) { int v[27] = {0}; char w[200001]; char *c = w; scanf("%s", w); while (*c) { v[*c - '`']++; v[0] = v[0] + *c - '`'; c++; } int p; scanf("%d", &p); p++; char i = 26; int num; while...
fn solution() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); let t: usize = scan.next(); for _ in 0..t { let s: String = scan.next(); let v: Vec<char> = s.chars().collect(); let ...
hard
1221
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Three people, A, B and C, are trying to communicate using transceivers. They are standing along a number line, and the coordinates of A, B and C are <var>a</var>, <var>b</var> and <var>c</var> (in meter...
int solution(void) { int a = 0; int b = 0; int c = 0; int d = 0; int dca = 0; int dba = 0; int dcb = 0; scanf("%d %d %d %d", &a, &b, &c, &d); if (c - a >= 0) { dca = c - a; } else { dca = a - c; } if (b - a >= 0) { dba = b - a; } else { dba = a - b; } if (c - b >= 0) { ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).ok(); let s = s.split_whitespace(); let mut i: Vec<isize> = Vec::new(); for z in s { i.push(z.parse().unwrap()); } let a = i[0]; let b = i[1]; let c = i[2]; let d = i[3]; let mut ans = false; ...
easy
1222
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoder Inc. holds a contest every Saturday.</p> <p>There are two types of contests called ABC and ARC, and just one of them is held at a time.</p> <p>The company holds these two types of contests alter...
int solution() { char a[3]; scanf("%s", a); if (a[0] == 'A' && a[1] == 'B' && a[2] == 'C') { printf("ARC"); } else if (a[0] == 'A' && a[1] == 'R' && a[2] == 'C') { printf("ABC"); } return 0; }
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); if s.trim() == "ABC" { println!("ARC"); } else { println!("ABC") } }
medium
1223
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi likes the sound when he buys a drink from a vending machine.</p> <p>That sound can be heard by spending <var>A</var> yen (the currency of Japan) each time.</p> <p>Takahashi has <var>B</var> ye...
int solution() { int a = 0; int b = 0; int c = 0; int son = 0; scanf("%d %d %d", &a, &b, &c); while (b >= a) { b = b - a; son++; if (son == c) { break; } } printf("%d\n", son); 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 a: usize = iter.next().unwrap().parse().unwrap(); let b: usize = iter.next().unwrap().parse().unwrap(); let c: usize = iter.next().unwrap().parse().unwrap...
hard
1224
Vasya claims that he had a paper square. He cut it into two rectangular parts using one vertical or horizontal cut. Then Vasya informed you the dimensions of these two rectangular parts. You need to check whether Vasya originally had a square. In other words, check if it is possible to make a square using two given rec...
int solution() { int n; scanf("%d", &n); int a[10000][4]; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { scanf("%d", &a[i][j]); } if ((a[i][0] == a[i][2] && a[i][1] + a[i][3] == a[i][0]) || (a[i][0] == a[i][3] && a[i][1] + a[i][2] == a[i][0]) || (a[i][1] == a[i][2]...
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)?; let a: Vec<i32> = input .trim() .split(" ") ...
medium
1225
When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the ...
int solution() { long n; long t; scanf("%ld %ld", &n, &t); long a[n]; long i = 0; long j = 0; long books = 0; long mints = 0; for (i = 0; i < n; i++) { scanf("%ld", &a[i]); mints += a[i]; if (mints <= t) { books++; } else if (mints > t) { mints -= a[j]; j++; } }...
fn solution() { let stdin = io::stdin(); let mut iterator = stdin.lock().lines(); let line: Vec<usize> = iterator .next() .unwrap() .unwrap() .split(" ") .map(|x| x.parse().expect("Not an integer!")) .collect(); let books: Vec<usize> = iterator ....
medium
1226
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>E869120 found a chest which is likely to contain treasure.<br/> However, the chest is locked. In order to open it, he needs to enter a string <var>S</var> consisting of lowercase English letters.<br/> H...
int solution(void) { char s[51]; char t[51]; scanf("%s %s", s, t); int s_length = strlen(s); int t_length = strlen(t); int flag; for (int i = s_length - 1; i >= t_length - 1; i--) { flag = 1; for (int j = 0; j < t_length; j++) { if (s[i - j] != t[t_length - 1 - j] && s[i - j] != '?') { ...
fn solution() { let mut s = String::new(); let mut t = String::new(); std::io::stdin().read_line(&mut s).ok(); std::io::stdin().read_line(&mut t).ok(); let a = s.trim_end().as_bytes(); let b = t.trim_end().as_bytes(); for i in (0..a.len()).rev() { if i + b.len() > a.len() { ...
easy
1227
Let's denote a $$$k$$$-step ladder as the following structure: exactly $$$k + 2$$$ wooden planks, of which two planks of length at least $$$k+1$$$ — the base of the ladder; $$$k$$$ planks of length at least $$$1$$$ — the steps of the ladder; Note that neither the base planks, nor the steps planks are required to be e...
int solution(void) { int T = 0; scanf("%d", &T); while (T-- > 0) { long first = 0; long second = 0; int n = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { long a = 0; scanf("%ld", &a); if (a > first) { second = first; first = a; } else if (a > secon...
fn solution() { let mut s: String = String::new(); let t: i32 = { io::stdin().read_line(&mut s).ok(); s.trim().parse().unwrap() }; s.clear(); for _t in 0..t { let n: i32 = { io::stdin().read_line(&mut s).ok(); s.trim().parse().unwrap() }; ...
medium
1228
There are $$$n$$$ piles of stones, where the $$$i$$$-th pile has $$$a_i$$$ stones. Two people play a game, where they take alternating turns removing stones.In a move, a player may remove a positive number of stones from the first non-empty pile (the pile with the minimal index, that has at least one stone). The first ...
int solution() { int t; scanf("%d", &t); for (int k = 0; k < t; ++k) { int n; scanf("%d", &n); int a[n]; int check1 = 0; int not1 = 0; int flag = 0; for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); if (a[i] == 1) { ++check1; if (flag == 0) { ++not...
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(); let mut first_...
medium
1229
There is a famous olympiad, which has more than a hundred participants. The Olympiad consists of two stages: the elimination stage, and the final stage. At least a hundred participants will advance to the final stage. The elimination stage in turn consists of two contests.A result of the elimination stage is the total ...
int solution(void) { int t = 0; scanf("%d", &t); while (t--) { int a = 0; int b = 0; int c = 0; int d = 0; scanf("%d %d %d %d", &a, &b, &c, &d); printf("%d\n", (a + b) > (c + d) ? (a + b) : (c + d)); } return 0; }
fn solution() { let mut reader = BufReader::new(stdin()); let mut get_line = || { let mut buf = String::new(); reader.read_line(&mut buf).unwrap(); buf }; let mut writer = BufWriter::new(stdout()); let t: usize = get_line().trim().parse().unwrap(); for _ in 0..t { ...
medium
1230
Bash wants to become a Pokemon master one day. Although he liked a lot of Pokemon, he has always been fascinated by Bulbasaur the most. Soon, things started getting serious and his fascination turned into an obsession. Since he is too young to go out and catch Bulbasaur, he came up with his own way of catching a Bulbas...
int solution() { char str[1000001]; scanf("%s", str); int i; int len = strlen(str); int count = 0; int B = 0; int u = 0; int b = 0; int a = 0; int s = 0; int r = 0; int l = 0; for (i = 0; i < len; i++) { if (str[i] == 'B') { B++; } else if (str[i] == 'b') { b++; } else...
fn solution() { let mut buffer = String::new(); io::stdin().read_line(&mut buffer).expect("wrong format"); let mut counter: [i32; 256] = [0; 256]; for c in buffer.as_bytes() { let i: usize = (*c).into(); counter[i] += 1; } let mut max = 1000000; for c in "Blbsr".as_bytes()...
hard
1231
Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 ≤ i ≤ n) he makes ai money. Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai. Let us remind you that the subsegment of the sequence is it...
int solution() { long long n; scanf("%lli", &n); long long x[n]; long long z = 1; long long temp = 1; for (int i = 0; i < n; i++) { scanf("%lli", &x[i]); if (i == 0) { continue; } if (i > 0 && x[i] >= x[i - 1] && i != n - 1) { z++; } else if (i > 0 && x[i] < x[i - 1]) { ...
fn solution() { let mut input = String::new(); io::stdin().read_line(&mut input).expect("read_line error"); input.clear(); io::stdin().read_line(&mut input).expect("read_line error"); let n: Vec<i32> = input .trim() .split_ascii_whitespace() .map(|num| num.parse::<i32>().e...
medium
1232
<span class="lang-en"> <p>Score: <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Takahashi the Jumbo will practice golf.</p> <p>His objective is to get a carry distance that is a multiple of <var>K</var>, while he can only make a carry distance of between <var>A</var> and <var>B</va...
int solution() { int k = 0; int a = 0; int b = 0; int i = 0; scanf("%d", &k); scanf("%d %d", &a, &b); i = 1; while (k <= a) { if (a <= k * i) { break; } i++; } if (k * i <= b) { printf("OK\n"); } else { printf("NG\n"); } return 0; }
fn solution() { let stdin = io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).ok(); buf.pop(); let K = i64::from_str(&buf).unwrap(); buf.clear(); stdin.read_line(&mut buf).ok(); let mut it = buf.split_whitespace().map(|n| i64::from_str(n).unwrap()); let (A, B) = (it...
easy
1233
Ehab loves number theory, but for some reason he hates the number $$$x$$$. Given an array $$$a$$$, find the length of its longest subarray such that the sum of its elements isn't divisible by $$$x$$$, or determine that such subarray doesn't exist.An array $$$a$$$ is a subarray of an array $$$b$$$ if $$$a$$$ can be obta...
int solution() { int t; int size; int x; scanf("%d", &t); for (int i = 0; i < t; i++) { scanf("%d %d", &size, &x); int arr[size]; int flag = 0; int sum = 0; for (int j = 0; j < size; j++) { scanf("%d", &arr[j]); if ((arr[j] % x) != 0) { flag = 1; } sum = s...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let t: usize = lines.next().unwrap().unwrap().parse().unwrap(); for _ in 0..t { let xs: Vec<usize> = lines .next() .unwrap() .unwrap() .split(' ') .map(|x| x...
medium
1234
During the quarantine, Sicromoft has more free time to create the new functions in "Celex-2021". The developers made a new function GAZ-GIZ, which infinitely fills an infinite table to the right and down from the upper left corner as follows: The cell with coordinates $$$(x, y)$$$ is at the intersection of $$$x$$$-th...
int solution(void) { int t; scanf("%d", &t); while (t--) { int x1; int y1; int x2; int y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); int x = x2 - x1; long long y = y2 - y1; long long ans = x * y; printf("%lld \n", ++ans); } }
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let t: usize = itr.next().unwrap().parse().unwrap(); let mut out = Vec::new(); for _ in 0..t { let a: u64 = itr.next().unwrap().parse().unwrap(); l...
medium
1235
Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a "+=" operation that adds the right-hand side value to the left-hand side variable. For example, performing "a += b" when a = $$$2$$$, b = $$$3$$$ changes the value of a to $$$5$$$ (the value of b does not change).In a...
int solution() { int t; scanf("%d", &t); int vet[t][3]; int count[t]; for (int i = 0; i < t; ++i) { scanf("%d %d %d", &vet[i][0], &vet[i][1], &vet[i][2]); count[i] = 0; } for (int i = 0; i < t; ++i) { while (vet[i][0] <= vet[i][2] && vet[i][1] <= vet[i][2]) { if (vet[i][0] <= vet[i][1...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).expect(""); let s: u32 = s.trim().parse().expect(""); for _ in 0..s { let mut st = String::new(); io::stdin().read_line(&mut st).expect(""); let st: Vec<&str> = st.split(" ").collect(); let mut a: u3...
hard
1236
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you...
int solution() { int a; int b; double c; double d; double e; double f; double g; double h; scanf("%d%d%lf%lf%lf%lf", &a, &b, &c, &d, &e, &f); if (a < 20) { h = (20 - a) * 60 + (0 - b); } else { h = 0; } h = c + h * d; h = h / f; if (h != (int)h) { h = ((int)h + 1); } h = e ...
fn solution() { let mut input = String::new(); use std::io::{self, prelude::*}; io::stdin().read_to_string(&mut input).unwrap(); let mut it = input.split_whitespace(); let hh: f64 = it.next().unwrap().parse().unwrap(); let mm: f64 = it.next().unwrap().parse().unwrap(); let h: f64 = it.next...
medium
1237
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence of length <var>n</var>, <var>a_1, ..., a_n</var>. Let us consider performing the following <var>n</var> operations on an empty sequence <var>b</var>.</p> <p>The <var>i<...
int solution() { int n = 0; int a[200001]; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } if (n & 1) { printf("%d", a[n]); for (int i = n - 2; i >= 1; i -= 2) { printf(" %d", a[i]); } for (int i = 2; i <= n - 1; i += 2) { printf(" %d", a[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: Vec<i64> = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); lin...
medium
1238
Vlad, like everyone else, loves to sleep very much.Every day Vlad has to do $$$n$$$ things, each at a certain time. For each of these things, he has an alarm clock set, the $$$i$$$-th of them is triggered on $$$h_i$$$ hours $$$m_i$$$ minutes every day ($$$0 \le h_i &lt; 24, 0 \le m_i &lt; 60$$$). Vlad uses the $$$24$$$...
int solution() { int test; scanf("%d", &test); while (test--) { int n; int t = 0; int x = 24 * 60; int y = x; scanf("%d", &n); int H[n + 1]; int M[n + 1]; int T[n + 1]; scanf("%d %d", &H[0], &M[0]); T[0] = H[0] * 60 + M[0]; for (int i = 1; i < n + 1; i++) { scanf(...
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 sln = sln .split_ascii_wh...
medium
1239
<h1>電子レンジ (Microwave)</h1> <h2>問題</h2> <p> JOI 君は食事の準備のため,A ℃の肉を電子レンジで B ℃まで温めようとしている. 肉は温度が 0 ℃未満のとき凍っている. また,温度が 0 ℃より高いとき凍っていない. 温度がちょうど 0 ℃のときの肉の状態は,凍っている場合と,凍っていない場合の両方があり得る. </p> <p> JOI 君は,肉の加熱にかかる時間は以下のようになると仮定して,肉を温めるのにかかる時間を見積もることにした. </p> <ul> <li>肉が凍っていて,その温度が 0 ℃より小さいとき: C 秒で 1 ℃温まる. <li>肉が凍っていて,その温度がちょ...
int solution(void) { int a = 0; int b = 0; int c = 0; int d = 0; int e = 0; int t = 0; scanf("%d%d%d%d%d", &a, &b, &c, &d, &e); if (a < 0) { t = -a * c + d + e * b; } else { t = (b - a) * e; } printf("%d\n", t); return 0; }
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').map(|s| s.parse::<i32>().unwrap()); let a = lines.next().unwrap(); let b = lines.next().unwrap(); let c = l...
easy
1240
<span class="lang-en"> <p>Score: <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the "year-month-day" order, or the "month-day" order without the year.<br/> For example, May <var>3</var>, <var>2018</var> is wri...
int solution() { int a = 0; int b = 0; scanf("%d %d", &a, &b); if (a <= b) { printf("%d", a); } else { printf("%d", a - 1); } }
fn solution() { let mut s = String::new(); stdin().read_line(&mut s).unwrap(); let v: Vec<u64> = s.trim().split(" ").map(|n| n.parse().unwrap()).collect(); if v[0] > v[1] { println!("{}", v[0] - 1); } else { println!("{}", v[0]); } }
medium
1241
You are given two arrays $$$a$$$ and $$$b$$$ of positive integers, with length $$$n$$$ and $$$m$$$ respectively. Let $$$c$$$ be an $$$n \times m$$$ matrix, where $$$c_{i,j} = a_i \cdot b_j$$$. You need to find a subrectangle of the matrix $$$c$$$ such that the sum of its elements is at most $$$x$$$, and its area (the t...
int solution() { int n; int m; int a[2010]; int b[2010]; int x; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= m; i++) { scanf("%d", &b[i]); } scanf("%d", &x); a[0] = b[0] = 0; for (int i = 1; i <= n; i++) { a[i] = a[i - 1] + a[i...
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::<u64>().unwrap()) .collect(); let n = arr[0]; let m = a...
medium
1242
Today we will be playing a red and white colouring game (no, this is not the Russian Civil War; these are just the colours of the Canadian flag).You are given an $$$n \times m$$$ grid of "R", "W", and "." characters. "R" is red, "W" is white and "." is blank. The neighbours of a cell are those that share an edge with i...
int solution() { int t; scanf("%d", &t); while (t--) { int m; int n; scanf("%d%d", &m, &n); char st[m][n]; char ch; scanf("%c", &ch); for (int i = 0; i < m; i++) { scanf("%s", st[i]); scanf("%c", &ch); } char mt1[m][n]; char mt2[m][n]; for (int i = 0; i < m...
fn solution() { let std_in = stdin(); let in_lock = std_in.lock(); let input = BufReader::new(in_lock); let std_out = stdout(); let out_lock = std_out.lock(); let mut output = BufWriter::new(out_lock); let mut lines = input.lines().map(|r| r.unwrap()); let s = lines.next().unwrap(); ...
easy
1243
You are given a string $$$a$$$, consisting of $$$n$$$ characters, $$$n$$$ is even. For each $$$i$$$ from $$$1$$$ to $$$n$$$ $$$a_i$$$ is one of 'A', 'B' or 'C'.A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct a...
int solution() { int i; int t; scanf("%d", &t); char a[51]; for (i = 0; i < t; i++) { int s = 0; int b = 0; int c = 0; int m = 50; scanf("%s", a); int j = 0; int d = 1; while (a[j] != '\0' && j < 50) { if (a[j] == a[0]) { s++; b++; c++; } ...
fn solution() { let mut input = String::new(); stdin().read_to_string(&mut input).unwrap(); let mut it = input.split_whitespace(); let _t = it.next(); for a in it { let a = a.as_bytes(); let s = a.first().unwrap(); let e = a.last().unwrap(); let ans = if e == s { ...
medium
1244
You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes.You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consist...
int solution() { int n; int m; scanf("%d %d", &n, &m); char l1[m][15]; char l2[m][15]; for (int i = 0; i < m; i++) { scanf("%s %s", l1[i], l2[i]); } char s[15]; for (int i = 1; i <= n; i++) { scanf("%s", s); for (int j = 0; j < m; j++) { if (strcmp(s, l1[j]) == 0) { if (strle...
fn solution() { let mut fl = String::new(); io::stdin().read_line(&mut fl).expect("error reading line"); let fl: Vec<&str> = fl.as_str().split(' ').collect(); let n: i32 = fl[0].trim().parse().expect("en"); let m: i32 = fl[1].trim().parse().expect("en"); let mut v1 = Vec::new(); let mut v2 =...
medium
1245
<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 living on a number line.</p> <p>The <var>i</var>-th person lives at coordinate <var>X_i</var>.</p> <p>You are going to hold a meeting that all <var>N</var> people have to a...
int solution() { int N; int sum = 0; int sum_sq = 0; int p = 0; int ans_a = 0; int ans_b = 0; scanf("%d", &N); int x[N]; for (int i = 0; i < N; i++) { scanf("%d", &x[i]); sum += x[i]; sum_sq += x[i] * x[i]; } p = sum / N; ans_a = N * p * p - 2 * sum * p + sum_sq; p++; ans_b = N *...
fn solution() { let mut buf = String::new(); io::stdin() .read_line(&mut buf) .expect("Failed to read line"); let vec_1: Vec<&str> = buf.split_whitespace().collect(); let n: i32 = vec_1[0].parse().unwrap_or(0); let mut buf = String::new(); io::stdin() .read_line(&mut buf...
hard
1246
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a string <var>s</var> of length <var>3</var> or greater. No two neighboring characters in <var>s</var> are equal.</p> <p>Takahashi and Aoki will play a game against each other. The two players ...
int solution() { char s[100002]; scanf("%s", s); int len = strlen(s); if ((s[0] == s[len - 1] || len % 2 != 0) && (s[0] != s[len - 1] || len % 2 == 0)) { printf("First\n"); } else { printf("Second\n"); } }
fn solution() { let stdin = std::io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).unwrap(); let s_vec: Vec<char> = buf.trim().chars().collect(); let parity = s_vec.len() + if s_vec.first() == s_vec.last() { 1 } else { 0 }; match parity % 2 { 1 => println!("First"), ...
easy
1247
Tired of boring office work, Denis decided to open a fast food restaurant.On the first day he made $$$a$$$ portions of dumplings, $$$b$$$ portions of cranberry juice and $$$c$$$ pancakes with condensed milk.The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a...
int solution() { int t; scanf("%d", &t); while (t--) { int s[3]; scanf("%d %d %d", &s[0], &s[1], &s[2]); int ans = 0; if (s[0] > 0) { ans++; s[0]--; } if (s[1] > 0) { ans++; s[1]--; } if (s[2] > 0) { ans++; s[2]--; } for (int i = 0; i < 2...
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines().map(|l| l.unwrap()); let t: u32 = lines.next().unwrap().trim().parse().unwrap(); for _ in 0..t { let line = lines.next().unwrap(); let mut parts = line.split_whitespace(); let a: u32 = parts.next().unwr...
hard
1248
<h2>C: 祈祷 (Pray)</h2> <p>とある双子は、コンテスト前に祈祷をすることで有名である。</p> <p>4 つの整数 $H, W, X, Y$ があって、$H \times W$ と $x + y$ が両方とも奇数だと縁起が悪いらしい。</p> <h3>入力</h3> <p>4 つの整数 $H, W, X, Y$ が空白区切りで与えられる。</p> <h3>出力</h3> <p>縁起が悪いなら "No"、そうでないなら "Yes" を出力しなさい。ただし、最後の改行を忘れないこと。</p> <h3>制約</h3> <ul> <li>$H, W, X, Y$ は $1$ 以上 $100$ 以下の整数<...
int solution() { int h; int w; int x; int y; scanf("%d%d%d%d", &h, &w, &x, &y); if ((h * w & 1) && ((x + y) & 1)) { puts("No"); } else { puts("Yes"); } return 0; }
fn solution() { input! { H:i32, W:i32, X:i32, Y:i32, } if (((H * W) % 2) == 1) & (((X + Y) % 2) == 1) { println!("No") } else { println!("Yes") } }
easy
1249
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given are positive integers <var>N</var> and <var>K</var>.</p> <p>Determine if the <var>3N</var> integers <var>K, K+1, ..., K+3N-1</var> can be partitioned into <var>N</var> triples <var>(a_1,b_1,c_1), ...
int solution() { int N; int K; scanf("%d %d", &N, &K); if (K * 2 + N * 2 - 1 >= K + (N * 5 + 1) / 2) { printf("-1\n"); fflush(stdout); return 0; } int i; for (i = 0; i < (N + 1) / 2; i++) { printf("%d %d %d\n", K + (i * 2), K + (N * 2) - 1 - i, K + (N * 5 / 2) + i); } for (i = 0; i < ...
fn solution() { let (N, K): (usize, usize) = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ...
medium
1250
<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> monsters, numbered <var>1, 2, ..., N</var>.</p> <p>Initially, the health of Monster <var>i</var> is <var>A_i</var>.</p> <p>Below, a monster with at least <var>1</var> health is ca...
int solution(void) { int N; scanf("%d", &N); int A[N]; for (int i = 0; i < N; i++) { scanf("%d", &A[i]); } while (1) { int min_iter = 0; while (A[min_iter] == 0) { min_iter++; } for (int i = 0; i < N; i++) { if (A[i] < A[min_iter] && A[i] != 0) { min_iter = i; ...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let _n: usize = iter.next().unwrap().parse().unwrap(); let ns: Vec<usize> = iter.map(|n| n.parse::<usize>().unwrap()).collect(); let mut min: usize = *ns.ite...
easy
1251
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>When you asked some guy in your class his name, he called himself <var>S</var>, where <var>S</var> is a string of length between <var>3</var> and <var>20</var> (inclusive) consisting of lowercase Englis...
int solution(void) { char a[3]; scanf("%c%c%c", &a[0], &a[1], &a[2]); printf("%c%c%c", a[0], a[1], a[2]); return 0; }
fn solution() { let mut is = String::new(); stdin().read_line(&mut is).ok(); println!("{}", &is[..3]); }
hard
1252
We call two numbers $$$x$$$ and $$$y$$$ similar if they have the same parity (the same remainder when divided by $$$2$$$), or if $$$|x-y|=1$$$. For example, in each of the pairs $$$(2, 6)$$$, $$$(4, 3)$$$, $$$(11, 7)$$$, the numbers are similar to each other, and in the pairs $$$(1, 4)$$$, $$$(3, 12)$$$, they are not.Y...
int solution() { int t = 0; scanf("%d", &t); while (t--) { int n = 0; scanf("%d", &n); int a[n]; int nc = 0; int nl = n - 1; int v = 0; for (int i = 0; i < n; i++) { scanf("%d", &v); if (v % 2 == 0) { a[nc] = v; nc++; } else { a[nl] = v; ...
fn solution() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let t: usize = itr.next().unwrap().parse().unwrap(); let mut out = Vec::new(); for _ in 0..t { let n: usize = itr.next().unwrap().parse().unwrap(); ...
easy
1253
Old timers of Summer Informatics School can remember previous camps in which each student was given a drink of his choice on the vechorka (late-evening meal). Or may be the story was more complicated?There are $$$n$$$ students living in a building, and for each of them the favorite drink $$$a_i$$$ is known. So you know...
int solution() { int n; int k; scanf("%d %d", &n, &k); int a[k]; int por = (n / 2) + (n % 2); for (int i = 0; i < k; i++) { a[i] = 0; } for (int i = 0; i < n; i++) { int tmp; scanf("%d", &tmp); a[tmp - 1]++; } for (int i = 0; i < k; i++) { while (a[i] > 1) { por--; a...
fn solution() { let mut stdin = String::new(); std::io::Read::read_to_string(&mut std::io::stdin(), &mut stdin).unwrap(); let mut stdin = stdin.split_whitespace(); let mut get = || stdin.next().unwrap(); let n = get!(); let k = get!(usize); let mut ds = vec![0; k]; for _ in 0..n { ...
medium
1254
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are four towns, numbered <var>1,2,3</var> and <var>4</var>. Also, there are three roads. The <var>i</var>-th road connects different towns <var>a_i</var> and <var>b_i</var> bidirectionally. No two...
int solution(void) { int a = 0; int b = 0; int c = 0; int d = 0; int e = 0; int f = 0; int g = 0; scanf("%d", &a); scanf("%d", &b); scanf("%d", &c); scanf("%d", &d); scanf("%d", &e); scanf("%d", &f); g = a + b + c + d + e + f; if (g == 15) { printf("YES\n"); } else { printf("NO\n...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let iter = buf.split_whitespace().map(|c| c.parse::<usize>().unwrap()); let mut counts = HashMap::new(); for c in iter { if let Some(x) = counts.get_mut(&c) { *x += 1; c...
hard
1255
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>When <var>l</var> is an odd number, the median of <var>l</var> numbers <var>a_1, a_2, ..., a_l</var> is the <var>(\frac{l+1}{2})</var>-th largest value among <var>a_1, a_2, ..., a_l</var>.</p> <p>You ar...
int solution(void) { int i; int j; int l; int r; int t = 2; int n; int p; int tmp; int s[200000]; int a[200000]; int b[200000]; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } s[0] = 0; s[1] = n - 1; while (t > 0) { t -= 2; i = l = s[t]; ...
fn solution() { let mut s: String = String::new(); stdin().read_to_string(&mut s).ok(); let mut itr = s.split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let mut x = Vec::new(); for i in 0..n { let a: usize = itr.next().unwrap().parse().unwrap(); x.push((a,...
hard
1256
$$$2^k$$$ teams participate in a playoff tournament. The tournament consists of $$$2^k - 1$$$ games. They are held as follows: first of all, the teams are split into pairs: team $$$1$$$ plays against team $$$2$$$, team $$$3$$$ plays against team $$$4$$$ (exactly in this order), and so on (so, $$$2^{k-1}$$$ games are pl...
int solution(void) { int k; scanf("%d\n", &k); int n = (1 << k) - 1; unsigned *a = malloc(sizeof(unsigned) * n); char *r = malloc(n); for (int i = 0; i < n; ++i) { r[n - i - 1] = getchar(); } for (int i = n - 1; i >= 0; --i) { if (i * 2 + 1 >= n) { a[i] = r[i] == '?' ? 2 : 1; } else { ...
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
1257
Patience sort
void solution(int *array, int length) { int i, j; int **piles = (int **)malloc(sizeof(int *) * length); for (i = 0; i < length; ++i) { piles[i] = (int *)malloc(sizeof(int) * length); } int *pileSizes = (int *)calloc(length, sizeof(int)); piles[0][0] = array[0]; int pileCount = 1; for (i = 1; i ...
fn solution<T: Ord + Copy>(arr: &mut [T]) { if arr.is_empty() { return; } let mut piles: Vec<Vec<T>> = Vec::new(); for &card in arr.iter() { let mut left = 0usize; let mut right = piles.len(); while left < right { let mid = left + (right - left) / 2; ...
easy
1258
"Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I ple...
int solution(void) { int n = 0; int i = 0; int min = -1; int max = -1; int cnt = 0; int arr[100000] = { 0, }; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); if (min == -1 || min > arr[i]) { min = arr[i]; } if (max < arr[i]) { max = arr[i]; } ...
fn solution() { use std::io; let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); buf.clear(); io::stdin().read_line(&mut buf).unwrap(); let mut ns: Vec<u32> = buf .split_whitespace() .map(|str| str.trim().parse().unwrap()) .collect(); ns.sort(); ...
medium
1259
<h1>Deque</h1> <p> For a dynamic array $A = \{a_0, a_1, ...\}$ of integers, perform a sequence of the following operations: </p> <ul> <li>push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.</li> <li>randomAccess($p$): Print element $a_p$.</li> <l...
int solution() { int q; scanf("%d", &q); int A[q * 2]; int head = q; int end = q; int order; int hob; int position; int data; while (q--) { scanf("%d", &order); switch (order) { case 0: scanf("%d%d", &hob, &data); if (hob == 0) { head--; A[head] = data; ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).ok(); let row: usize = s.trim().parse().unwrap_or(0); s.clear(); let mut array = VecDeque::with_capacity(row); for _ in 0..row { io::stdin().read_line(&mut s).ok(); let v: Vec<isize> = s .split_...
hard
1260
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x &lt; y$$$....
int solution() { int t; scanf("%d", &t); while (t--) { int n; int x; int y; scanf("%d%d%d", &n, &x, &y); int jiange = 1; int zhongjiangeshu = y - x - 1; while (1) { if ((zhongjiangeshu + 1) % jiange == 0) { if (zhongjiangeshu / jiange + 2 <= n) { break; ...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let t: i32 = s.trim().parse().unwrap(); for _i in 0..t { s.clear(); io::stdin().read_line(&mut s).unwrap(); let arr: Vec<i32> = s.split_whitespace().map(|s| s.parse().unwrap()).collect(); l...
medium
1261
You are given an array $$$a$$$ with $$$n$$$ integers. You can perform the following operation at most $$$k$$$ times: Choose two indices $$$i$$$ and $$$j$$$, in which $$$i \,\bmod\, k = j \,\bmod\, k$$$ ($$$1 \le i &lt; j \le n$$$). Swap $$$a_i$$$ and $$$a_j$$$. After performing all operations, you have to select $$$k...
int solution() { long x[1000] = {0}; int t = 0; scanf("%d", &t); int len = 0; int k = 0; for (int T = 0; T < t; T++) { scanf("%d %d", &len, &k); for (int i = 0; i < len; i++) { scanf("%ld", &x[i]); } long long sum = 0; for (int i = 0; i < k; i++) { int mid = 0; for (in...
fn solution() { let k = std::io::stdin() .lock() .lines() .next() .unwrap() .unwrap() .parse::<i64>() .unwrap(); 'joker: for _ in 0..k { let mut l = String::new(); std::io::stdin().read_line(&mut l).unwrap(); let l = l ....
hard
1262
Pinkie Pie has bought a bag of patty-cakes with different fillings! But it appeared that not all patty-cakes differ from one another with filling. In other words, the bag contains some patty-cakes with the same filling.Pinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the...
int solution() { long long int t; scanf("%lld", &t); while (t != 0) { long long int n; scanf("%lld", &n); long long int a[n]; long long int b[n]; long long int i; for (i = 0; i < n; ++i) { b[i] = 0; } for (i = 0; i < n; ++i) { scanf("%lld", &a[i]); b[a[i] - 1] +...
fn solution() { let t: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().parse().unwrap() }; for _ in 0..t { let n: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap()...
hard
1263
<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> islands lining up from west to east, connected by <var>N-1</var> bridges.</p> <p>The <var>i</var>-th bridge connects the <var>i</var>-th island from the west and the <var>(i+1)</v...
int solution(void) { int n; int m; int near = 0; int ans = 0; scanf("%d%d", &n, &m); int a[m]; int b[m]; int c[n]; for (int i = 0; i < n; i++) { c[i] = 1e9; } for (int i = 0; i < m; i++) { scanf("%d%d", &a[i], &b[i]); a[i]--; b[i]--; if (c[a[i]] > b[i]) { c[a[i]] = b[i]; ...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.trim().split(" "); let _n: usize = iter.next().unwrap().parse().unwrap(); let m: usize = iter.next().unwrap().parse().unwrap(); let mut v: Vec<(u32, u32)> = Vec::with_capacity(m); for _ ...
medium
1264
You are given a binary string $$$s$$$ (recall that a string is binary if each character is either $$$0$$$ or $$$1$$$).Let $$$f(t)$$$ be the decimal representation of integer $$$t$$$ written in binary form (possibly with leading zeroes). For example $$$f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$$$ and...
int solution() { int t; scanf("%d", &t); while (t--) { char *s = malloc(sizeof(char) * 200001); int str_len; int good_substrings = 0; int zeros = 0; scanf("%s", s); str_len = strlen(s); for (int i = 0; i < str_len; i++) { if (s[i] == '0') { zeros++; } else { ...
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 t = get!(); for _ in 0..t { let s = get().as_bytes(); let mut z = 0; ...
medium
1265
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>If there is an integer not less than <var>0</var> satisfying the following conditions, print the smallest such integer; otherwise, print <code>-1</code>.</p> <ul> <li>The integer has exactly <var>N</var...
int solution() { long n; long m; int flag = 0; scanf("%ld %ld", &n, &m); long s[m]; long c[m]; for (int i = 0; i < m; i++) { scanf("%ld %ld", &s[i], &c[i]); } long ans[3]; for (int i = 0; i < 3; i++) { ans[i] = -1; } for (int i = 0; i < m; i++) { if (ans[s[i] - 1] == -1) { 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 n: usize = s.next().unwrap().parse().unwrap(); let m: usize = s.next().unwrap().parse().unwrap(); let a: Vec<_> = (0..m) .map(|_| { ...
medium
1266
In order to write a string, Atilla needs to first learn all letters that are contained in the string.Atilla needs to write a message which can be represented as a string $$$s$$$. He asks you what is the minimum alphabet size required so that one can write this message.The alphabet of size $$$x$$$ ($$$1 \leq x \leq 26$$...
int solution() { int m = 0; int n = 0; int max = 0; char a[102]; scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &n); scanf("%s", a); max = a[0]; for (int j = 0; j < n; j++) { if (max < a[j]) { max = a[j]; } } max = max - 'a' + 1; printf("%d\n", max...
fn solution() { let mut buf = BufWriter::new(io::stdout().lock()); io::stdin() .lines() .skip(2) .step_by(2) .flatten() .for_each(|s| { if let Some(m) = s.as_bytes().iter().max() { let ans = m.wrapping_sub(b'`'); writeln!(buf, "...
hard
1267
Vasya has his favourite number $$$n$$$. He wants to split it to some non-zero digits. It means, that he wants to choose some digits $$$d_1, d_2, \ldots, d_k$$$, such that $$$1 \leq d_i \leq 9$$$ for all $$$i$$$ and $$$d_1 + d_2 + \ldots + d_k = n$$$.Vasya likes beauty in everything, so he wants to find any solution wit...
int solution() { int x; scanf("%d", &x); printf("%d\n", x); while (x--) { printf("1 "); } return 0; }
fn solution() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let number: u16 = lines.next().unwrap().unwrap().parse::<u16>().unwrap(); let mut output = String::with_capacity(2000); write!(output, "{}\n1", number).unwrap(); for _ in 1..number { write!(output, " 1").unw...
hard
1268
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In programming, hexadecimal notation is often used.</p> <p>In hexadecimal notation, besides the ten digits <var>0, 1, ..., 9</var>, the six letters <code>A</code>, <code>B</code>, <code>C</code>, <code>...
int solution(void) { char c[3]; scanf("%c %c", &c[0], &c[1]); if (c[0] > c[1]) { printf(">"); } else if (c[0] == c[1]) { printf("="); } else { printf("<"); } return 0; }
fn solution() { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).ok(); let s: Vec<&str> = buf.split_whitespace().collect(); if s[0] > s[1] { println!(">"); } else if s[0] == s[1] { println!("="); } else { println!("<"); } }
easy
1269
You are standing at the point $$$0$$$ on a coordinate line. Your goal is to reach the point $$$n$$$. In one minute, you can move by $$$2$$$ or by $$$3$$$ to the left or to the right (i. e., if your current coordinate is $$$x$$$, it can become $$$x-3$$$, $$$x-2$$$, $$$x+2$$$ or $$$x+3$$$). Note that the new coordinate c...
int solution() { int i = 0; int t = 0; int m = 0; int num[10000 + 1] = {0}; scanf("%d", &t); for (; i < t; ++i) { scanf("%d", &num[i]); } for (i = 0; i < t; ++i) { if (num[i] % 3 == 0) { m = num[i] / 3; } else if (num[i] % 3 == 2) { m = num[i] / 3 + 1; } else { m = (num...
fn solution() { let mut testcases = String::new(); io::stdin().read_line(&mut testcases).expect("Error"); let t: usize = testcases.trim().parse().expect("errr"); testcases.clear(); for _ in 0..t { let mut n = String::new(); io::stdin().read_line(&mut n).expect("Error"); let...
easy
1270
Let's call a number a binary decimal if it's a positive integer and all digits in its decimal notation are either $$$0$$$ or $$$1$$$. For example, $$$1\,010\,111$$$ is a binary decimal, while $$$10\,201$$$ and $$$787\,788$$$ are not.Given a number $$$n$$$, you are asked to represent $$$n$$$ as a sum of some (not necess...
int solution() { int t = 0; scanf("%d", &t); long int n = 0; int temp = 0; int count = 0; for (int i = 0; i < t; i++) { scanf("%ld", &n); count = 0; while (n != 0) { temp = n % 10; if (temp > count) { count = temp; } n = n / 10; } printf("%d\n", count); ...
fn solution() -> Result<(), Box<dyn Error>> { let mut input = String::new(); io::stdin().read_line(&mut input)?; let cases = input.trim().parse::<u32>()?; for _ in 0..cases { input.clear(); io::stdin().read_line(&mut input)?; let mut n = input.trim().parse::<u32>()?; let ...
easy
1271
<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> bags of biscuits. The <var>i</var>-th bag contains <var>A_i</var> biscuits.</p> <p>Takaki will select some of these bags and eat all of the biscuits inside. Here, it is also possi...
int solution(void) { int n; int p; int even = 0; int odd = 0; long long ans = 1; long long tmp1 = 1; long long tmp2 = 0; scanf("%d%d", &n, &p); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] % 2 == 0) { even++; } else { odd++; } } if (p == 0) { ...
fn solution() { let (N, P): (usize, i64) = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), )...
medium
1272
A palindrome is a string that reads the same backward as forward. For example, the strings "z", "aaa", "aba", and "abccba" are palindromes, but "codeforces" and "ab" are not. You hate palindromes because they give you déjà vu.There is a string $$$s$$$. You must insert exactly one character 'a' somewhere in $$$s$$$. If ...
int solution() { int t; scanf("%d", &t); while (t--) { int l; int i; int k; char c[300009]; char p; char *x; scanf("%s", c); l = strlen(c); p = c[0]; for (i = 0; i < ((l / 2) + (l % 2)); i++) { if (c[i] != c[l - 1 - i]) { break; } if (p != c[i]) { ...
fn solution() { let mut input = String::new(); stdin().read_to_string(&mut input).unwrap(); let mut it = input.split_whitespace(); let _t = it.next().unwrap(); for s in it { match s.chars().position(|c| c != 'a') { Some(i) => { let j = s.len() - i; ...
medium
1273
You are given a bracket sequence $$$s$$$ of length $$$n$$$, where $$$n$$$ is even (divisible by two). The string $$$s$$$ consists of $$$\frac{n}{2}$$$ opening brackets '(' and $$$\frac{n}{2}$$$ closing brackets ')'.In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end ...
int solution() { int n = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { int br = 0; int sum = 0; int moves = 0; char c = '0'; scanf("%d", &br); while (br >= 0) { br--; scanf("%c", &c); if (c == '(') { sum++; } else if (c == ')') { sum--; } ...
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 _ = lines.next().unwrap().unwrap(); let cs: Vec<char> = lines.next().unwrap().unwrap().chars().collect(); let mut x: ...
medium
1274
<span class="lang-en"> <p>Score: <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>E869120's and square1001's <var>16</var>-th birthday is coming soon.<br/> Takahashi from AtCoder Kingdom gave them a round cake cut into <var>16</var> equal fan-shaped pieces.</p> <p>E869120 and square1...
int solution() { int A = 0; int B = 0; scanf("%d %d", &A, &B); if (A <= 8 && B <= 8) { printf("Yay!\n"); } else { printf(":(\n"); } return 0; }
fn solution() { let mut args = String::new(); stdin().read_line(&mut args).expect("Failed to read line"); let args: Vec<i32> = args .split_whitespace() .filter_map(|it| it.parse().ok()) .collect(); let a = args[0]; let b = args[1]; println!("{}", if a > 8 || b > 8 { ":(" ...
easy
1275
<H1>Sorting Five Numbers</H1> <p> Write a program which reads five numbers and sorts them in descending order. </p> <H2>Input</H2> <p> Input consists of five numbers <var>a</var>, <var>b</var>, <var>c</var>, <var>d</var> and <var>e</var> (-100000 &le; <var>a</var>, <var>b</var>, <var>c</var>, <var>d</var>,<var>e</va...
int solution(void) { int number[5]; int i; int j; scanf("%d %d %d %d %d", &number[0], &number[1], &number[2], &number[3], &number[4]); for (i = 0; i < 5; i++) { for (j = 4; j > i; j--) { if (number[j] > number[j - 1]) { int temp = number[j]; number[j] = number[j - 1]; ...
fn solution() { let mut buf = String::new(); stdin().read_line(&mut buf).unwrap(); let mut values: Vec<i32> = buf .split_whitespace() .filter_map(|x| x.parse::<i32>().ok()) .collect(); values.sort(); let values: Vec<String> = values.iter().rev().map(|x| x.to_string()).collect...
hard
1276
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi, who lives on the number line, is now at coordinate <var>X</var>. He will make exactly <var>K</var> moves of distance <var>D</var> in the positive or negative direction.</p> <p>More specifical...
int solution(void) { long long int x; long long int k; long long int d; long long int q; long long int r; scanf("%lld%lld%lld", &x, &k, &d); x = llabs(x); d = llabs(d); q = x / d; r = x % d; if (k <= q) { printf("%lld", x - (d * k)); return 0; } if ((k - q) % 2 == 0) { printf("%lld...
fn solution() { let (x, k, d): (i64, i64, i64) = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), it...
easy
1277
Suppose you are performing the following algorithm. There is an array $$$v_1, v_2, \dots, v_n$$$ filled with zeroes at start. The following operation is applied to the array several times — at $$$i$$$-th step ($$$0$$$-indexed) you can: either choose position $$$pos$$$ ($$$1 \le pos \le n$$$) and increase $$$v_{pos}$$...
int solution() { int t; scanf("%d", &t); int n; int i; int j; long long int k; long long int a[33]; int b[102]; for (; t > 0; t--) { scanf("%d %lld", &n, &k); for (i = 0; i < n; i++) { scanf("%lld", &a[i]); } for (i = 0; i < 102; i++) { b[i] = 0; } for (i = 0; i < n...
fn solution() { let mut t = String::new(); stdin().read_line(&mut t).unwrap(); let t = t.trim().parse().unwrap(); for _ in 0..t { let mut line = String::new(); stdin().read_line(&mut line).unwrap(); let words: Vec<i64> = line .split_whitespace() .map(|x|...
medium
1278
Calculate the minimum number of characters you need to change in the string s, so that it contains at least k different letters, or print that it is impossible.String s consists only of lowercase Latin letters, and it is allowed to change characters only to lowercase Latin letters too.
int solution() { char str[1001]; scanf("%s", str); int i = 0; int ans = 0; int k; int cnt[26]; scanf("%d", &k); while (i < 26) { cnt[i++] = 0; } i = 0; while (str[i] != '\0') { if (cnt[str[i] - 'a'] == 0) { ans++; cnt[str[i] - 'a'] = 1; } i++; } if (i < k) { pri...
fn solution() { let mut s = String::new(); io::stdin().read_line(&mut s).expect("Something went wrong"); let s: &str = s.trim(); let mut k = String::new(); io::stdin().read_line(&mut k).expect("Something went wrong"); let k: i32 = k.trim().parse().expect("Something went wrong"); if k > s.len...
easy
1279
The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations: Operation ++ increases the value of variable x by 1. Operation -- decreases the value of variable x by 1. A stateme...
int solution() { static int i; static int n; static int x; char str[4]; scanf("%d", &n); if (n >= 1 && n <= 150) { for (i = 0; i < n; i++) { scanf("%s", str); if (str[0] == '+' && str[2] == 'X') { ++x; } if (str[0] == 'X' && str[1] == '+') { x++; } if...
fn solution() { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); let num = buf.trim().parse::<u8>().unwrap(); let mut result = 0; buf.clear(); for _ in 0..num { io::stdin().read_line(&mut buf).unwrap(); } for c in buf.chars() { if c == '+' { ...
medium
1280
Polycarp has an array $$$a$$$ consisting of $$$n$$$ integers.He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains $$$n-1$$$ elements). For each of the next moves he chooses any element with the only ...
int solution() { int n; int even = 0; scanf("%d", &n); int ara[n]; for (int i = 0; i < n; i++) { scanf("%d", &ara[i]); if (ara[i] % 2 == 0) { even++; } } int odd = n - even; if (odd == even || odd + 1 == even || even + 1 == odd) { printf("0\n"); } else if (odd > even) { int l...
fn solution() { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); let _n: usize = input.trim().parse().unwrap(); input.clear(); io::stdin().read_line(&mut input).unwrap(); let a: Vec<isize> = input .split_whitespace() .map(|x| x.parse().unwrap()) ...
medium
1281
The only difference between easy and hard versions are constraints on $$$n$$$ and $$$k$$$.You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most $$$k$$$ most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conver...
int solution() { int n; int k; scanf("%d %d", &n, &k); int a[n]; for (int i = 0; i < n; i++) { scanf(" %d", &a[i]); } int b[k]; int idx = 1; for (int i = 0; i < k; i++) { b[i] = 0; } b[0] = a[0]; for (int i = 1; i < n; i++) { int flag = 0; for (int j = 0; j < k; j++) { if (...
fn solution() { let reader = io::stdin(); let mut result = Vec::new(); let parts: Vec<u32> = reader .lock() .lines() .next() .unwrap() .unwrap() .split_whitespace() .filter(|s| !s.is_empty()) .map(|s| s.parse::<u32>().unwrap()) .colle...
hard
1282
A binary string is a string consisting only of the characters 0 and 1. You are given a binary string $$$s_1 s_2 \ldots s_n$$$. It is necessary to make this string non-decreasing in the least number of operations. In other words, each character should be not less than the previous. In one operation, you can do the follo...
int solution() { int t; scanf("%d", &t); int n; int i; while (t--) { scanf("%d\n", &n); char s[n]; int ops = 0; scanf("%c", &s[0]); for (i = 1; i < n; i++) { scanf("%c", &s[i]); if (s[i] != s[i - 1]) { ops++; } } if ((s[0] == '0') && (ops > 0)) { ops...
fn solution() { let t: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() }; for _ in 0..t { let n: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&m...
medium
1283
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>The door of Snuke's laboratory is locked with a security code.</p> <p>The security code is a <var>4</var>-digit number. We say the security code is <em>hard to enter</em> when it contains two consecuti...
int solution() { char str[5]; scanf("%s", str); if (str[0] != str[1] && str[1] != str[2] && str[2] != str[3]) { printf("Good\n"); } else { printf("Bad\n"); } return 0; }
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let vecs: Vec<char> = s.chars().collect(); let mut isNotInputtable: bool = false; for i in 0..(vecs.len() - 1) { if vecs[i] == vecs[i + 1] { isNotInputtable = true; } } if...
medium
1284
ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a &gt; c, then eve...
int solution() { int i = 0; int count = 0; int n = 0; int c = 0; int t[111111]; scanf("%d %d", &n, &c); for (i = 0; i < n; i++) { scanf("%d", &t[i]); if (i >= 1) { if (t[i] - t[i - 1] <= c) { count++; } else { count = 1; } } if (i == 0) { count++;...
fn solution() { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let mut ints = s.split_whitespace().map(|x| x.parse::<usize>().unwrap()); let n = ints.next().unwrap(); let t = ints.next().unwrap(); let mut r = String::new(); std::io::stdin().read_line(&mut r).unwrap()...
hard
1285
The King of Berland Polycarp LXXXIV has $$$n$$$ daughters. To establish his power to the neighbouring kingdoms he wants to marry his daughters to the princes of these kingdoms. As a lucky coincidence there are $$$n$$$ other kingdoms as well.So Polycarp LXXXIV has enumerated his daughters from $$$1$$$ to $$$n$$$ and the...
int solution() { long long int t; scanf("%lld", &t); do { long long int n; scanf("%lld", &n); long long int pos[n]; long long int k[n]; for (long long int i = 0; i < n; ++i) { pos[i] = -1; k[i] = -1; } long long int pr[n + 1]; long long int alfa = -5; long long in...
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 n = line.tr...
medium
1286
We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently o...
int solution(void) { int t; scanf("%d", &t); for (; t > 0; t--) { int n; long long sum = 0; int x = 0; long long y = 0; int flag2 = 0; scanf("%d", &n); long long a[n]; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); sum = sum + a[i]; } if (sum == 0) { ...
fn solution() { let mut content = String::new(); io::stdin().read_to_string(&mut content); let mut lines = content.lines(); let num_tests: i32 = lines.next().unwrap().parse().unwrap(); 'cycle: for _ in 0..num_tests { let _n: i32 = lines.next().unwrap().parse().unwrap(); let a: Vec<i...
medium
1287
Acacius is studying strings theory. Today he came with the following problem.You are given a string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in...
int solution() { int n; int t1; int i; int j; int k; int count; int flag; int curr_idx; char a[65]; char c[65]; char t[8]; scanf("%d", &t1); while (t1--) { for (i = 0; i < 64; i++) { a[i] = 'o'; c[i] = 'o'; } scanf("%d", &n); scanf("%c", &c[55]); scanf("%s", a);...
fn solution() { let t: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); buf.trim_end().parse().unwrap() }; for _ in 0..t { let n: usize = { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap()...
easy
1288
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let <var>N</var> be a positive odd number.</p> <p>There are <var>N</var> coins, numbered <var>1, 2, \ldots, N</var>. For each <var>i</var> (<var>1 \leq i \leq N</var>), when Coin <var>i</var> is tossed,...
int solution(void) { int N; scanf("%d", &N); double a[N + 1]; for (int i = 0; i < N; i++) { scanf("%lf", &a[i + 1]); } double dp[N + 1][N + 1]; for (int i = 0; i <= N; i++) { for (int j = 0; j <= N; j++) { dp[i][j] = 0; } } dp[0][0] = 1; for (int i = 1; i <= N; i++) { dp[i][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 a: Vec<f64> = (0..n) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let mut d...
medium
1289
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi is meeting up with Aoki.</p> <p>They have planned to meet at a place that is <var>D</var> meters away from Takahashi's house in <var>T</var> minutes from now.</p> <p>Takahashi will leave his h...
int solution(void) { int D = 0; int T = 0; int S = 0; scanf("%d %d %d", &D, &T, &S); if (S * T < D) { printf("No"); } else if (S * T >= D) { printf("Yes"); } }
fn solution() -> Result<()> { let mut s = String::new(); let _ls = io::stdin().read_line(&mut s)? - 1; let input: Vec<u32> = s .split_whitespace() .map(|x| x.parse::<u32>().unwrap()) .collect(); if input[1] * input[2] >= input[0] { println!("Yes"); } else { pr...
medium
1290
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has decided to give a string to his mother.</p> <p>The <em>value</em> of a string <var>T</var> is the length of the longest common subsequence of <var>T</var> and <var>T'</var>, where <var>T'<...
int solution() { int K; char S[301]; scanf("%s", S); scanf("%d", &K); int i; int l; char T[301]; for (l = 0; S[l] != 0; l++) { ; } for (i = 0, T[l] = 0; i < l; i++) { T[i] = S[l - i - 1]; } if (K >= l / 2) { printf("%d\n", l); fflush(stdout); return 0; } int j; int k;...
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).ok(); let mut it = buf.split_whitespace(); let s = it.next().unwrap().as_bytes(); let n = s.len(); let k = it.next().unwrap().parse::<usize>().unwrap(); let mut dp = vec![vec![vec![0; n]; n]; k + 1]; f...
medium
1291
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands.A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one ...
int solution(int argc, const char *argv[]) { int tests; scanf("%d", &tests); while (tests--) { int n; static int a[1005]; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); } static int stk[1005]; int top = 0; for (int i = 1; i <= n; ++i) { if (a[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 t = lines.next().unwrap().pa...
hard
1292
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a string <var>S</var> consisting of uppercase English letters. Additionally, an integer <var>N</var> will be given.</p> <p>Shift each character of <var>S</var> by <var>N</var> in alphabetical or...
int solution() { int n = 0; char s[10240]; int i = 0; scanf("%d", &n); scanf("%s", s); for (i = 0; i < 10240; i++) { if (s[i] == '\0') { break; } s[i] = (s[i] - 'A' + n) % 26 + 'A'; } printf("%s\n", s); }
fn solution() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); let n: u8 = iter.next().unwrap().parse().unwrap(); let s = iter.next().unwrap(); for c in s.bytes() { print!("{}", (((c - b'A') + n) % 26 + b'A') as cha...
medium
1293
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke built an online judge to hold a programming contest.</p> <p>When a program is submitted to the judge, the judge returns a verdict, which is a two-character string that appears in the string <var>S...
int solution(void) { char s[6]; scanf("%s", s); int ans = 0; int p = 0; while (ans == 0 && p < 6) { if (s[p] == 0x41 && s[p + 1] == 0x43) { printf("Yes"); ans = 1; } p++; } if (ans == 0) { printf("No"); } }
fn solution() { let S: String = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().chars().collect() }; let ans = if S.find("AC").is_some() { "Yes" } else { "No" }; println!("{}", ans); }
hard
1294
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.—Perfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $$$n$$$ points on the $$$X$$$-axis numbered $$$1$$...
int solution() { int t = 0; int n = 0; long long k = 0LL; long long a[200000] = {}; int res = 0; long long sum[200001] = {}; res = scanf("%d", &t); while (t > 0) { long long ans = 0LL; res = scanf("%d", &n); res = scanf("%lld", &k); for (int i = 0; i < n; i++) { res = scanf("%ll...
fn solution() { let mut content = String::new(); io::stdin().read_to_string(&mut content); let mut lines = content.lines(); let num_tests: i32 = lines.next().unwrap().parse().unwrap(); for _ in 0..num_tests { let arr: Vec<i64> = lines .next() .unwrap() .s...
medium
1295
In the snake exhibition, there are $$$n$$$ rooms (numbered $$$0$$$ to $$$n - 1$$$) arranged in a circle, with a snake in each room. The rooms are connected by $$$n$$$ conveyor belts, and the $$$i$$$-th conveyor belt connects the rooms $$$i$$$ and $$$(i+1) \bmod n$$$. In the other words, rooms $$$0$$$ and $$$1$$$, $$$1$...
int solution() { int T; scanf("%d", &T); for (int C = 0; C < T; C++) { int n; scanf("%d", &n); char s[300005]; scanf("%s", s); int ans = 0; int clkf = 0; int aclkf = 0; for (int i = 0; i < n; i++) { if (s[i] == '>') { clkf = 1; } if (s[i] == '<') { ...
fn solution() { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let test: i64 = str.trim().parse().unwrap(); for _ in 0..test { str.clear(); let _ = stdin().read_line(&mut str).unwrap(); str.clear(); let _ = stdin().read_line(&mut str).unwrap();...
easy
1296
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Fennec is fighting with <var>N</var> monsters.</p> <p>The <em>health</em> of the <var>i</var>-th monster is <var>H_i</var>.</p> <p>Fennec can do the following two actions:</p> <ul> <li>Attack: Fennec ch...
int solution() { int n; int j; int temp; int prev; int k; int data[200005]; int order[200005][2]; int i; int f = 1; int b; int start = 0; int s; int a; long m = 0; scanf("%d", &n); scanf("%d", &s); for (i = 0; i < n; i++) { scanf("%d", &data[i]); order[i][0] = -1; order[i][...
fn solution() { let (n, k): (usize, usize) = { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); let mut it = input.split_whitespace(); ( it.next().unwrap().parse().unwrap(), it.next().unwrap().parse().unwrap(), ) }; le...
hard
1297
Pak Chanek has $$$n$$$ two-dimensional slices of cheese. The $$$i$$$-th slice of cheese can be represented as a rectangle of dimensions $$$a_i \times b_i$$$. We want to arrange them on the two-dimensional plane such that: Each edge of each cheese is parallel to either the x-axis or the y-axis. The bottom edge of each...
int solution() { int t; scanf("%d", &t); while (t--) { long long int small = 0; long long int big = 0; long long int n; scanf("%lld", &n); long long int a; long long int b; while (n--) { scanf("%lld %lld", &a, &b); small += a > b ? b : a; if (a > big) { big...
fn solution() { let stdin = io::stdin(); let stdout = io::stdout(); let mut reader = Scanner::new(stdin.lock()); let mut writer = io::BufWriter::new(stdout.lock()); let tests_number = reader.token::<i64>(); for _ in 0..tests_number { let slices = reader.token::<i64>(); let mut ...
hard
1298
Ternary search
int solution(int l, int r, int key, int ar[]) { if (r >= l) { int mid1 = l + ((r - l) / 3); int mid2 = r - ((r - l) / 3); if (ar[mid1] == key) { return mid1; } if (ar[mid2] == key) { return mid2; } if (key < ar[mid1]) { return ternarySearch(l, mid1 - 1, key, ar); ...
fn solution<T: Ord>(item: &T, arr: &[T]) -> Option<usize> { if arr.is_empty() { return None; } let is_asc = arr.len() > 1 && arr[0] < arr[arr.len() - 1]; let mut left = 0; let mut right = arr.len() - 1; while left <= right { let first_mid = left + (right - left) / 3; l...
medium
1299
You have a board represented as a grid with $$$2 \times n$$$ cells.The first $$$k_1$$$ cells on the first row and first $$$k_2$$$ cells on the second row are colored in white. All other cells are colored in black.You have $$$w$$$ white dominoes ($$$2 \times 1$$$ tiles, both cells are colored in white) and $$$b$$$ black...
int solution() { int T = 0; scanf("%d", &T); for (int i = T; i--;) { int n; int k1; int k2; scanf("%d %d %d", &n, &k1, &k2); int w; int b; scanf("%d %d", &w, &b); if (w == 0 && b == 0) { printf("YES\n"); continue; } if (w * 2 <= (k1 + k2) && b * 2 <= 2 * n - (...
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
1300
<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> candles placed on a number line. The <var>i</var>-th candle from the left is placed on coordinate <var>x_i</var>. Here, <var>x_1 &lt; x_2 &lt; ... &lt; x_N</var> holds.</p> <p>Ini...
int solution(void) { int n; int k; scanf("%d%d", &n, &k); long long x[n]; for (int i = 0; i < n; i++) { scanf("%lld", &x[i]); } long long tmp; long long ans = LLONG_MAX; for (int i = 0; i + k - 1 < n; i++) { if (x[i] * x[i + k - 1] >= 0) { tmp = fmax(llabs(x[i]), llabs(x[i + k - 1])); ...
fn solution() { let mut nk = String::new(); std::io::stdin().read_line(&mut nk).unwrap(); let nk: Vec<usize> = nk.split_whitespace().flat_map(str::parse).collect(); let n = nk[0]; let k = nk[1]; let mut x = String::new(); std::io::stdin().read_line(&mut x).unwrap(); let x: Vec<isize> = ...
medium