problem_id
stringlengths
5
6
url
stringlengths
48
49
title
stringlengths
2
46
rating
int64
800
3.5k
tags
listlengths
1
11
div
stringclasses
16 values
time_limit_ms
int64
1k
13k
memory_limit_mb
int64
32
1.02k
description
stringlengths
67
2.52k
input
stringlengths
99
1.93k
output
stringlengths
47
1.35k
examples
listlengths
1
5
note
stringlengths
0
1.68k
prompt
stringlengths
505
6.96k
2038A
https://codeforces.com/problemset/problem/2038/A
Bonus Project
1,400
[ "games", "greedy" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
2,000
512
There is a team of $$$n$$$ software engineers numbered from $$$1$$$ to $$$n$$$. Their boss promises to give them a bonus if they complete an additional project. The project requires $$$k$$$ units of work in total. The bonus promised to the $$$i$$$-th engineer is $$$a_i$$$ burles. The boss doesn't assign specific tasks to engineers; it is expected that every engineer will voluntarily complete some integer amount of work units. The bonus will be paid to the entire team only if the project is completed; in other words, if the total amount of voluntary work units on the project is greater than or equal to $$$k$$$. The amount of work that can be performed by each engineer is not limited. However, all engineers value their labour. The $$$i$$$-th engineer estimates one unit of their work as $$$b_i$$$ burles. If the bonus is paid, the benefit $$$s_i$$$ of the $$$i$$$-th engineer for completing $$$c$$$ units of work is defined as $$$s_i = a_i - c \cdot b_i$$$. If the bonus is not paid, the engineer will not volunteer to do any work. Engineers work together for many years, so they know how the bonus is going to be distributed and how much their colleagues value the labour. That is, all $$$a_i$$$ and all $$$b_i$$$ are known to every engineer in the team. Engineers are eager to get the bonus, so they agreed on the following process of work distribution between them: - the first engineer says: "I will complete $$$c_1$$$ units of work", where $$$c_1$$$ is a non-negative integer; - then, the second engineer says: "I will complete $$$c_2$$$ units of work", where $$$c_2$$$ is a non-negative integer; - ... and so on; - finally, the $$$n$$$-th engineer says: "I will complete $$$c_n$$$ units of work", where $$$c_n$$$ is a non-negative integer. Every engineer voices $$$c_i$$$ in a way to maximize their own benefit $$$s_i$$$. If the expected benefit is going to be zero, an engineer will still agree to work to get the experience and to help their colleagues obtain the bonus. However, if the benefit is expected to be negative for some reason (an engineer needs to perform an excessive amount of work or the project is not going to be completed), that engineer will not work at all (completes zero amount of work units). Given that every engineer acts perfectly, your task is to find out the numbers $$$c_i$$$ voiced by every engineer.
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1000$$$; $$$1 \le k \le 10^6$$$) — the number of engineers in the company and the number of work units the project requires, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the bonus which will be paid to the $$$i$$$-th engineer if the project is completed. The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 1000$$$), where $$$b_i$$$ is the work unit cost for the $$$i$$$-th engineer.
Print $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$0 \le c_i \le k$$$) — the amount of work completed by each engineer given that every engineer behaves optimally. Note that the answer is unique.
[ [ "3 6\n4 7 6\n1 2 3", "1 3 2" ], [ "3 12\n4 7 6\n1 2 3", "0 0 0" ], [ "3 11\n6 7 8\n1 2 3", "6 3 2" ] ]
In the first example, engineers distributed the work across them and got the bonus, even though the benefit for the third engineer is zero. In the second example, the bonus project requires too many work units to complete, so it's more beneficial for engineers not to work at all.
Title: Bonus Project time_limit_ms: 2000 memory_limit_mb: 512 Description: There is a team of $$$n$$$ software engineers numbered from $$$1$$$ to $$$n$$$. Their boss promises to give them a bonus if they complete an additional project. The project requires $$$k$$$ units of work in total. The bonus promised to the $$$i$$$-th engineer is $$$a_i$$$ burles. The boss doesn't assign specific tasks to engineers; it is expected that every engineer will voluntarily complete some integer amount of work units. The bonus will be paid to the entire team only if the project is completed; in other words, if the total amount of voluntary work units on the project is greater than or equal to $$$k$$$. The amount of work that can be performed by each engineer is not limited. However, all engineers value their labour. The $$$i$$$-th engineer estimates one unit of their work as $$$b_i$$$ burles. If the bonus is paid, the benefit $$$s_i$$$ of the $$$i$$$-th engineer for completing $$$c$$$ units of work is defined as $$$s_i = a_i - c \cdot b_i$$$. If the bonus is not paid, the engineer will not volunteer to do any work. Engineers work together for many years, so they know how the bonus is going to be distributed and how much their colleagues value the labour. That is, all $$$a_i$$$ and all $$$b_i$$$ are known to every engineer in the team. Engineers are eager to get the bonus, so they agreed on the following process of work distribution between them: - the first engineer says: "I will complete $$$c_1$$$ units of work", where $$$c_1$$$ is a non-negative integer; - then, the second engineer says: "I will complete $$$c_2$$$ units of work", where $$$c_2$$$ is a non-negative integer; - ... and so on; - finally, the $$$n$$$-th engineer says: "I will complete $$$c_n$$$ units of work", where $$$c_n$$$ is a non-negative integer. Every engineer voices $$$c_i$$$ in a way to maximize their own benefit $$$s_i$$$. If the expected benefit is going to be zero, an engineer will still agree to work to get the experience and to help their colleagues obtain the bonus. However, if the benefit is expected to be negative for some reason (an engineer needs to perform an excessive amount of work or the project is not going to be completed), that engineer will not work at all (completes zero amount of work units). Given that every engineer acts perfectly, your task is to find out the numbers $$$c_i$$$ voiced by every engineer. Input: The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1000$$$; $$$1 \le k \le 10^6$$$) — the number of engineers in the company and the number of work units the project requires, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the bonus which will be paid to the $$$i$$$-th engineer if the project is completed. The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 1000$$$), where $$$b_i$$$ is the work unit cost for the $$$i$$$-th engineer. Output: Print $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$0 \le c_i \le k$$$) — the amount of work completed by each engineer given that every engineer behaves optimally. Note that the answer is unique. Examples: input: 3 6 4 7 6 1 2 3 output: 1 3 2 input: 3 12 4 7 6 1 2 3 output: 0 0 0 input: 3 11 6 7 8 1 2 3 output: 6 3 2 Note: In the first example, engineers distributed the work across them and got the bonus, even though the benefit for the third engineer is zero. In the second example, the bonus project requires too many work units to complete, so it's more beneficial for engineers not to work at all.
2038B
https://codeforces.com/problemset/problem/2038/B
Make It Equal
2,100
[ "greedy", "brute force", "binary search", "math" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
2,000
512
You are given an integer array $$$a$$$ of size $$$n$$$. The elements of the array are numbered from $$$1$$$ to $$$n$$$. You can perform the following operation any number of times (possibly, zero): choose an index $$$i$$$ from $$$1$$$ to $$$n$$$; decrease $$$a_i$$$ by $$$2$$$ and increase $$$a_{(i \bmod n) + 1}$$$ by $$$1$$$. After you perform the operations, all elements of the array should be non-negative equal integers. Your task is to calculate the minimum number of operations you have to perform.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer — the minimum number of operations you have to perform. If it is impossible to make all elements of the array equal, print -1.
[ [ "3\n2\n1 1\n3\n1 3 2\n4\n2 1 2 6", "0\n-1\n3" ] ]
Title: Make It Equal time_limit_ms: 2000 memory_limit_mb: 512 Description: You are given an integer array $$$a$$$ of size $$$n$$$. The elements of the array are numbered from $$$1$$$ to $$$n$$$. You can perform the following operation any number of times (possibly, zero): choose an index $$$i$$$ from $$$1$$$ to $$$n$$$; decrease $$$a_i$$$ by $$$2$$$ and increase $$$a_{(i \bmod n) + 1}$$$ by $$$1$$$. After you perform the operations, all elements of the array should be non-negative equal integers. Your task is to calculate the minimum number of operations you have to perform. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$. Output: For each test case, print a single integer — the minimum number of operations you have to perform. If it is impossible to make all elements of the array equal, print -1. Examples: input: 3 2 1 1 3 1 3 2 4 2 1 2 6 output: 0 -1 3 Note:
2038C
https://codeforces.com/problemset/problem/2038/C
DIY
1,400
[ "data structures", "geometry", "greedy", "sortings" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
2,000
512
You are given a list of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$. You need to pick $$$8$$$ elements from the list and use them as coordinates of four points. These four points should be corners of a rectangle which has its sides parallel to the coordinate axes. Your task is to pick coordinates in such a way that the resulting rectangle has the maximum possible area. The rectangle can be degenerate, i. e. its area can be $$$0$$$. Each integer can be used as many times as it occurs in the list (or less).
The first line contains one integer $$$t$$$ ($$$1 \le t \le 25\,000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$8 \le n \le 2 \cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print the answer as follows: - if it is impossible to construct a rectangle which meets the constraints from the statement, print a single line containing the word NO (case-insensitive); - otherwise, in the first line, print YES (case-insensitive). In the second line, print $$$8$$$ integers $$$x_1, y_1, x_2, y_2, x_3, y_3, x_4, y_4$$$ — the coordinates of the corners of the rectangle. You can print the corners in any order.
[ [ "3\n16\n-5 1 1 2 2 3 3 4 4 5 5 6 6 7 7 10\n8\n0 0 -1 2 2 1 1 3\n8\n0 0 0 0 0 5 0 5", "YES\n1 2 1 7 6 2 6 7\nNO\nYES\n0 0 0 5 0 0 0 5" ] ]
Title: DIY time_limit_ms: 2000 memory_limit_mb: 512 Description: You are given a list of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$. You need to pick $$$8$$$ elements from the list and use them as coordinates of four points. These four points should be corners of a rectangle which has its sides parallel to the coordinate axes. Your task is to pick coordinates in such a way that the resulting rectangle has the maximum possible area. The rectangle can be degenerate, i. e. its area can be $$$0$$$. Each integer can be used as many times as it occurs in the list (or less). Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 25\,000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$8 \le n \le 2 \cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: For each test case, print the answer as follows: - if it is impossible to construct a rectangle which meets the constraints from the statement, print a single line containing the word NO (case-insensitive); - otherwise, in the first line, print YES (case-insensitive). In the second line, print $$$8$$$ integers $$$x_1, y_1, x_2, y_2, x_3, y_3, x_4, y_4$$$ — the coordinates of the corners of the rectangle. You can print the corners in any order. Examples: input: 3 16 -5 1 1 2 2 3 3 4 4 5 5 6 6 7 7 10 8 0 0 -1 2 2 1 1 3 8 0 0 0 0 0 5 0 5 output: YES 1 2 1 7 6 2 6 7 NO YES 0 0 0 5 0 0 0 5 Note:
2038D
https://codeforces.com/problemset/problem/2038/D
Divide OR Conquer
2,400
[ "dp", "binary search", "implementation", "data structures", "bitmasks" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
3,000
512
You are given an array $$$[a_1, a_2, \ldots a_n]$$$ consisting of integers between $$$0$$$ and $$$10^9$$$. You have to split this array into several segments (possibly one) in such a way that each element belongs to exactly one segment. Let the first segment be the array $$$[a_{l_1}, a_{l_1 + 1}, \ldots, a_{r_1}]$$$, the second segment be $$$[a_{l_2}, a_{l_2+ 1}, \ldots, a_{r_2}]$$$, ..., the last segment be $$$[a_{l_k}, a_{l_k+ 1}, \ldots, a_{r_k}]$$$. Since every element should belong to exactly one array, $$$l_1 = 1$$$, $$$r_k = n$$$, and $$$r_i + 1 = l_{i+1}$$$ for each $$$i$$$ from $$$1$$$ to $$$k-1$$$. The split should meet the following condition: $$$f([a_{l_1}, a_{l_1 + 1}, \ldots, a_{r_1}]) \le f([a_{l_2}, a_{l_2+ 1}, \ldots, a_{r_2}]) \le \dots \le f([a_{l_k}, a_{l_k+1}, \ldots, a_{r_k}])$$$, where $$$f(a)$$$ is the bitwise OR of all elements of the array $$$a$$$. Calculate the number of ways to split the array, and print it modulo $$$998\,244\,353$$$. Two ways are considered different if the sequences $$$[l_1, r_1, l_2, r_2, \ldots, l_k, r_k]$$$ denoting the splits are different.
The first line contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10 ^9$$$) — the elements of the given array.
Print one integer — the number of ways to split the array, taken modulo $$$998\,244\,353$$$.
[ [ "3\n1 2 3", "4" ], [ "5\n1000 1000 1000 1000 1000", "16" ], [ "3\n3 4 6", "3" ] ]
In the first two examples, every way to split the array is valid. In the third example, there are three valid ways to split the array: - $$$k = 3$$$; $$$l_1 = 1, r_1 = 1, l_2 = 2, r_2 = 2, l_3 = 3, r_3 = 3$$$; the resulting arrays are $$$[3]$$$, $$$[4]$$$, $$$[6]$$$, and $$$3 \le 4 \le 6$$$; - $$$k = 2$$$; $$$l_1 = 1, r_1 = 1, l_2 = 2, r_2 = 3$$$; the resulting arrays are $$$[3]$$$ and $$$[4, 6]$$$, and $$$3 \le 6$$$; - $$$k = 1$$$; $$$l_1 = 1, r_1 = 3$$$; there will be only one array: $$$[3, 4, 6]$$$. If you split the array into two arrays $$$[3, 4]$$$ and $$$[6]$$$, the bitwise OR of the first array is $$$7$$$, and the bitwise OR of the second array is $$$6$$$; $$$7 > 6$$$, so this way to split the array is invalid.
Title: Divide OR Conquer time_limit_ms: 3000 memory_limit_mb: 512 Description: You are given an array $$$[a_1, a_2, \ldots a_n]$$$ consisting of integers between $$$0$$$ and $$$10^9$$$. You have to split this array into several segments (possibly one) in such a way that each element belongs to exactly one segment. Let the first segment be the array $$$[a_{l_1}, a_{l_1 + 1}, \ldots, a_{r_1}]$$$, the second segment be $$$[a_{l_2}, a_{l_2+ 1}, \ldots, a_{r_2}]$$$, ..., the last segment be $$$[a_{l_k}, a_{l_k+ 1}, \ldots, a_{r_k}]$$$. Since every element should belong to exactly one array, $$$l_1 = 1$$$, $$$r_k = n$$$, and $$$r_i + 1 = l_{i+1}$$$ for each $$$i$$$ from $$$1$$$ to $$$k-1$$$. The split should meet the following condition: $$$f([a_{l_1}, a_{l_1 + 1}, \ldots, a_{r_1}]) \le f([a_{l_2}, a_{l_2+ 1}, \ldots, a_{r_2}]) \le \dots \le f([a_{l_k}, a_{l_k+1}, \ldots, a_{r_k}])$$$, where $$$f(a)$$$ is the bitwise OR of all elements of the array $$$a$$$. Calculate the number of ways to split the array, and print it modulo $$$998\,244\,353$$$. Two ways are considered different if the sequences $$$[l_1, r_1, l_2, r_2, \ldots, l_k, r_k]$$$ denoting the splits are different. Input: The first line contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10 ^9$$$) — the elements of the given array. Output: Print one integer — the number of ways to split the array, taken modulo $$$998\,244\,353$$$. Examples: input: 3 1 2 3 output: 4 input: 5 1000 1000 1000 1000 1000 output: 16 input: 3 3 4 6 output: 3 Note: In the first two examples, every way to split the array is valid. In the third example, there are three valid ways to split the array: - $$$k = 3$$$; $$$l_1 = 1, r_1 = 1, l_2 = 2, r_2 = 2, l_3 = 3, r_3 = 3$$$; the resulting arrays are $$$[3]$$$, $$$[4]$$$, $$$[6]$$$, and $$$3 \le 4 \le 6$$$; - $$$k = 2$$$; $$$l_1 = 1, r_1 = 1, l_2 = 2, r_2 = 3$$$; the resulting arrays are $$$[3]$$$ and $$$[4, 6]$$$, and $$$3 \le 6$$$; - $$$k = 1$$$; $$$l_1 = 1, r_1 = 3$$$; there will be only one array: $$$[3, 4, 6]$$$. If you split the array into two arrays $$$[3, 4]$$$ and $$$[6]$$$, the bitwise OR of the first array is $$$7$$$, and the bitwise OR of the second array is $$$6$$$; $$$7 > 6$$$, so this way to split the array is invalid.
2038G
https://codeforces.com/problemset/problem/2038/G
Guess One Character
1,900
[ "constructive algorithms", "interactive", "implementation" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
2,000
512
This is an interactive problem. You have to use flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout) or cout.flush(), in Java or Kotlin — System.out.flush(), and in Python — sys.stdout.flush(). The jury has a string $$$s$$$ consisting of characters 0 and/or 1. The length of this string is $$$n$$$. You can ask the following queries: - $$$1$$$ $$$t$$$ — "how many times does $$$t$$$ appear in $$$s$$$ as a contiguous substring?" Here, $$$t$$$ should be a string consisting of characters 0 and/or 1; its length should be at least $$$1$$$ and at most $$$n$$$. For example, if the string $$$s$$$ is 111011 and the string $$$t$$$ is 11, the response to the query is $$$3$$$. You have to guess at least one character in the string $$$s$$$ by asking no more than $$$3$$$ queries. Note that giving the answer does not count as a query. In every test and in every test case, the string $$$s$$$ is fixed beforehand.
null
null
[ [ "3 // 3 test cases\n3 // the length of the string is 3\n\n1 // 101 occurs once\n\n1 // guessed correctly\n2 // the length of the string is 2\n\n0 // 00 occurs zero times\n\n0 // 0 occurs zero times\n\n1 // guessed correctly\n2 // the length of the string is 2\n\n1 // 1 occurs once\n\n0 // 01 occurs zero times\n\n1 // guessed correctly", "1 101 // how many times 101 occurs\n\n0 2 0 // guess: s[2] is 0\n\n\n1 00 // how many times 00 occurs\n\n1 0 // how many times 0 occurs\n\n0 1 1 // guess: s[1] is 1\n\n\n1 1 // how many times 1 occurs\n\n1 01 // how many times 01 occurs\n\n0 2 0 // guess: s[2] is 0" ] ]
In the example, there are $$$3$$$ test cases: 101, 11 and 10. Note that everything after the // sign is a comment that explains which line means what in the interaction. The jury program won't print these comments in the actual problem, and you shouldn't print them. The empty lines are also added for your convenience, the jury program won't print them, and your solution should not print any empty lines.
Title: Guess One Character time_limit_ms: 2000 memory_limit_mb: 512 Description: This is an interactive problem. You have to use flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout) or cout.flush(), in Java or Kotlin — System.out.flush(), and in Python — sys.stdout.flush(). The jury has a string $$$s$$$ consisting of characters 0 and/or 1. The length of this string is $$$n$$$. You can ask the following queries: - $$$1$$$ $$$t$$$ — "how many times does $$$t$$$ appear in $$$s$$$ as a contiguous substring?" Here, $$$t$$$ should be a string consisting of characters 0 and/or 1; its length should be at least $$$1$$$ and at most $$$n$$$. For example, if the string $$$s$$$ is 111011 and the string $$$t$$$ is 11, the response to the query is $$$3$$$. You have to guess at least one character in the string $$$s$$$ by asking no more than $$$3$$$ queries. Note that giving the answer does not count as a query. In every test and in every test case, the string $$$s$$$ is fixed beforehand. Input: None Output: None Examples: input: 3 // 3 test cases 3 // the length of the string is 3 1 // 101 occurs once 1 // guessed correctly 2 // the length of the string is 2 0 // 00 occurs zero times 0 // 0 occurs zero times 1 // guessed correctly 2 // the length of the string is 2 1 // 1 occurs once 0 // 01 occurs zero times 1 // guessed correctly output: 1 101 // how many times 101 occurs 0 2 0 // guess: s[2] is 0 1 00 // how many times 00 occurs 1 0 // how many times 0 occurs 0 1 1 // guess: s[1] is 1 1 1 // how many times 1 occurs 1 01 // how many times 01 occurs 0 2 0 // guess: s[2] is 0 Note: In the example, there are $$$3$$$ test cases: 101, 11 and 10. Note that everything after the // sign is a comment that explains which line means what in the interaction. The jury program won't print these comments in the actual problem, and you shouldn't print them. The empty lines are also added for your convenience, the jury program won't print them, and your solution should not print any empty lines.
2038J
https://codeforces.com/problemset/problem/2038/J
Waiting for...
800
[ "greedy", "implementation" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
2,000
512
Monocarp is waiting for a bus at the bus stop. Unfortunately, there are many people who want to ride a bus too. You are given a list of events of two types: - B $$$b_i$$$ — a bus with $$$b_i$$$ free seats arrives at the stop; - P $$$p_i$$$ — $$$p_i$$$ people arrive at the stop. These events are listed in a chronological order. When a bus arrives, the following happens. All people at the bus stop (except for Monocarp) try to enter the bus. If there are enough free seats for all of them, then they all enter the bus. Otherwise, some people remain at the bus stop (the number of people who enter the bus is equal to the number of free seats). If there is still at least one free seat after all people (except for Monocarp) enter the bus, then Monocarp can decide to enter this bus as well (but he might choose to wait for another bus). For each bus, you have to determine if it is possible for Monocarp to take that bus.
The first line contains one integer $$$n$$$ $$$(1 \le n \le 10^3)$$$ — the number of events. Then, $$$n$$$ lines follow. The $$$i$$$-th of them contains the description of the $$$i$$$-th event in one of the two following formats: - B $$$b_i$$$ ($$$1 \le b_i \le 10^6$$$) — a bus with $$$b_i$$$ free seats arrives at the stop; - P $$$p_i$$$ ($$$1 \le p_i \le 10^6$$$) — $$$p_i$$$ people arrive at the stop. Additional constraint on the input: there is at least one event of type B.
For each event of type B, print YES if it is possible for Monocarp to take the corresponding bus, or NO otherwise (case-insensitive).
[ [ "10\nP 2\nP 5\nB 8\nP 14\nB 5\nB 9\nB 3\nP 2\nB 1\nB 2", "YES\nNO\nNO\nYES\nNO\nYES" ] ]
Title: Waiting for... time_limit_ms: 2000 memory_limit_mb: 512 Description: Monocarp is waiting for a bus at the bus stop. Unfortunately, there are many people who want to ride a bus too. You are given a list of events of two types: - B $$$b_i$$$ — a bus with $$$b_i$$$ free seats arrives at the stop; - P $$$p_i$$$ — $$$p_i$$$ people arrive at the stop. These events are listed in a chronological order. When a bus arrives, the following happens. All people at the bus stop (except for Monocarp) try to enter the bus. If there are enough free seats for all of them, then they all enter the bus. Otherwise, some people remain at the bus stop (the number of people who enter the bus is equal to the number of free seats). If there is still at least one free seat after all people (except for Monocarp) enter the bus, then Monocarp can decide to enter this bus as well (but he might choose to wait for another bus). For each bus, you have to determine if it is possible for Monocarp to take that bus. Input: The first line contains one integer $$$n$$$ $$$(1 \le n \le 10^3)$$$ — the number of events. Then, $$$n$$$ lines follow. The $$$i$$$-th of them contains the description of the $$$i$$$-th event in one of the two following formats: - B $$$b_i$$$ ($$$1 \le b_i \le 10^6$$$) — a bus with $$$b_i$$$ free seats arrives at the stop; - P $$$p_i$$$ ($$$1 \le p_i \le 10^6$$$) — $$$p_i$$$ people arrive at the stop. Additional constraint on the input: there is at least one event of type B. Output: For each event of type B, print YES if it is possible for Monocarp to take the corresponding bus, or NO otherwise (case-insensitive). Examples: input: 10 P 2 P 5 B 8 P 14 B 5 B 9 B 3 P 2 B 1 B 2 output: YES NO NO YES NO YES Note:
2038K
https://codeforces.com/problemset/problem/2038/K
Grid Walk
2,100
[ "brute force", "dp", "math", "number theory", "greedy" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
2,000
512
You have an $$$n \times n$$$ grid and two integers $$$a$$$ and $$$b$$$. Both the rows and the columns are numbered from $$$1$$$ to $$$n$$$. Let's denote the cell at the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$(i, j)$$$. You are standing in the cell $$$(1, 1)$$$ and want to move into the cell $$$(n, n)$$$. Suppose you are in the cell $$$(i, j)$$$; in one step, you can move either into the cell $$$(i, j + 1)$$$ or into the cell $$$(i + 1, j)$$$ if the corresponding cells exist. Let's define the cost of the cell $$$(i, j)$$$ as $$$c(i, j) = \gcd(i, a) + \gcd(j, b)$$$ (here, $$$\gcd(x,y)$$$ denotes the greatest common divisor of $$$x$$$ and $$$y$$$). The cost of the route from $$$(1, 1)$$$ to $$$(n, n)$$$ is the sum of costs of the visited cells (including the starting cell and the finishing cell). Find the route with minimum possible cost and print its cost.
The only line contains three integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$2 \le n \le 10^6$$$; $$$1 \le a, b \le 10^6$$$).
Print one integer — the cost of the cheapest route from $$$(1, 1)$$$ to $$$(n, n)$$$.
[ [ "4 2 4", "21" ], [ "10 210 420", "125" ] ]
The first example is described in the picture above.
Title: Grid Walk time_limit_ms: 2000 memory_limit_mb: 512 Description: You have an $$$n \times n$$$ grid and two integers $$$a$$$ and $$$b$$$. Both the rows and the columns are numbered from $$$1$$$ to $$$n$$$. Let's denote the cell at the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$(i, j)$$$. You are standing in the cell $$$(1, 1)$$$ and want to move into the cell $$$(n, n)$$$. Suppose you are in the cell $$$(i, j)$$$; in one step, you can move either into the cell $$$(i, j + 1)$$$ or into the cell $$$(i + 1, j)$$$ if the corresponding cells exist. Let's define the cost of the cell $$$(i, j)$$$ as $$$c(i, j) = \gcd(i, a) + \gcd(j, b)$$$ (here, $$$\gcd(x,y)$$$ denotes the greatest common divisor of $$$x$$$ and $$$y$$$). The cost of the route from $$$(1, 1)$$$ to $$$(n, n)$$$ is the sum of costs of the visited cells (including the starting cell and the finishing cell). Find the route with minimum possible cost and print its cost. Input: The only line contains three integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$2 \le n \le 10^6$$$; $$$1 \le a, b \le 10^6$$$). Output: Print one integer — the cost of the cheapest route from $$$(1, 1)$$$ to $$$(n, n)$$$. Examples: input: 4 2 4 output: 21 input: 10 210 420 output: 125 Note: The first example is described in the picture above.
2038L
https://codeforces.com/problemset/problem/2038/L
Bridge Renovation
1,400
[ "brute force", "dp", "math", "two pointers", "greedy" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
2,000
512
Recently, Monocarp started working as a director of a park located near his house. The park is quite large, so it even has a small river splitting it into several zones. Several bridges are built across this river. Three of these bridges are especially old and need to be repaired. All three bridges have the same length but differ in width. Their widths are $$$18$$$, $$$21$$$ and $$$25$$$ units, respectively. During the park renovation process, Monocarp has to replace the old planks that served as the surface of the bridges with the new ones. Planks are sold with a standard length of $$$60$$$ units. Monocarp already knows that he needs $$$n$$$ planks for each bridge. But since the bridges have different widths, he needs $$$n$$$ planks of length $$$18$$$ for the first bridge, $$$n$$$ planks of length $$$21$$$ for the second one, and $$$n$$$ planks of length $$$25$$$ for the last one. Workers in charge of renovation have no problem with cutting planks into parts but refuse to join planks, since it creates weak spots and looks ugly. Monocarp wants to buy as few planks as possible but struggles to calculate the required number of planks. Can you help him?
The first and only line contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of planks required for each of the three bridges.
Print a single integer — the minimum number of planks of standard length ($$$60$$$ units) Monocarp needs to cover all three bridges if the planks can be cut into parts.
[ [ "1", "2" ], [ "3", "4" ], [ "1000", "1167" ] ]
In the first example, it is possible to cut one plank of length $$$60$$$ into three planks with lengths $$$25$$$, $$$18$$$ and $$$17$$$, and cut another plank of length $$$60$$$ into two planks with lengths $$$39$$$ and $$$21$$$. That way, Monocarp will have all the required planks.
Title: Bridge Renovation time_limit_ms: 2000 memory_limit_mb: 512 Description: Recently, Monocarp started working as a director of a park located near his house. The park is quite large, so it even has a small river splitting it into several zones. Several bridges are built across this river. Three of these bridges are especially old and need to be repaired. All three bridges have the same length but differ in width. Their widths are $$$18$$$, $$$21$$$ and $$$25$$$ units, respectively. During the park renovation process, Monocarp has to replace the old planks that served as the surface of the bridges with the new ones. Planks are sold with a standard length of $$$60$$$ units. Monocarp already knows that he needs $$$n$$$ planks for each bridge. But since the bridges have different widths, he needs $$$n$$$ planks of length $$$18$$$ for the first bridge, $$$n$$$ planks of length $$$21$$$ for the second one, and $$$n$$$ planks of length $$$25$$$ for the last one. Workers in charge of renovation have no problem with cutting planks into parts but refuse to join planks, since it creates weak spots and looks ugly. Monocarp wants to buy as few planks as possible but struggles to calculate the required number of planks. Can you help him? Input: The first and only line contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of planks required for each of the three bridges. Output: Print a single integer — the minimum number of planks of standard length ($$$60$$$ units) Monocarp needs to cover all three bridges if the planks can be cut into parts. Examples: input: 1 output: 2 input: 3 output: 4 input: 1000 output: 1167 Note: In the first example, it is possible to cut one plank of length $$$60$$$ into three planks with lengths $$$25$$$, $$$18$$$ and $$$17$$$, and cut another plank of length $$$60$$$ into two planks with lengths $$$39$$$ and $$$21$$$. That way, Monocarp will have all the required planks.
2042A
https://codeforces.com/problemset/problem/2042/A
Greedy Monocarp
800
[ "greedy", "sortings" ]
Div. 2
2,000
512
There are $$$n$$$ chests; the $$$i$$$-th chest initially contains $$$a_i$$$ coins. For each chest, you can choose any non-negative ($$$0$$$ or greater) number of coins to add to that chest, with one constraint: the total number of coins in all chests must become at least $$$k$$$. After you've finished adding coins to the chests, greedy Monocarp comes, who wants the coins. He will take the chests one by one, and since he is greedy, he will always choose the chest with the maximum number of coins. Monocarp will stop as soon as the total number of coins in chests he takes is at least $$$k$$$. You want Monocarp to take as few coins as possible, so you have to add coins to the chests in such a way that, when Monocarp stops taking chests, he will have exactly $$$k$$$ coins. Calculate the minimum number of coins you have to add.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines: - the first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 50$$$; $$$1 \le k \le 10^7$$$); - the second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le k$$$).
For each test case, print one integer — the minimum number of coins you have to add so that, when Monocarp stops taking the chests, he has exactly $$$k$$$ coins. It can be shown that under the constraints of the problem, it is always possible.
[ [ "4\n5 4\n4 1 2 3 2\n5 10\n4 1 2 3 2\n2 10\n1 1\n3 8\n3 3 3", "0\n1\n8\n2" ] ]
In the first test case of the example, you don't have to add any coins. When Monocarp arrives, he will take the chest with $$$4$$$ coins, so he will have exactly $$$4$$$ coins. In the second test case of the example, you can add $$$1$$$ coin to the $$$4$$$-th chest, so, when Monocarp arrives, he will take a chest with $$$4$$$ coins, then another chest with $$$4$$$ coins, and a chest with $$$2$$$ coins. In the third test case of the example, you can add $$$3$$$ coins to the $$$1$$$-st chest and $$$5$$$ coins to the $$$2$$$-nd chest. In the fourth test case of the example, you can add $$$1$$$ coin to the $$$1$$$-st chest and $$$1$$$ coin to the $$$3$$$-rd chest.
Title: Greedy Monocarp time_limit_ms: 2000 memory_limit_mb: 512 Description: There are $$$n$$$ chests; the $$$i$$$-th chest initially contains $$$a_i$$$ coins. For each chest, you can choose any non-negative ($$$0$$$ or greater) number of coins to add to that chest, with one constraint: the total number of coins in all chests must become at least $$$k$$$. After you've finished adding coins to the chests, greedy Monocarp comes, who wants the coins. He will take the chests one by one, and since he is greedy, he will always choose the chest with the maximum number of coins. Monocarp will stop as soon as the total number of coins in chests he takes is at least $$$k$$$. You want Monocarp to take as few coins as possible, so you have to add coins to the chests in such a way that, when Monocarp stops taking chests, he will have exactly $$$k$$$ coins. Calculate the minimum number of coins you have to add. Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines: - the first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 50$$$; $$$1 \le k \le 10^7$$$); - the second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le k$$$). Output: For each test case, print one integer — the minimum number of coins you have to add so that, when Monocarp stops taking the chests, he has exactly $$$k$$$ coins. It can be shown that under the constraints of the problem, it is always possible. Examples: input: 4 5 4 4 1 2 3 2 5 10 4 1 2 3 2 2 10 1 1 3 8 3 3 3 output: 0 1 8 2 Note: In the first test case of the example, you don't have to add any coins. When Monocarp arrives, he will take the chest with $$$4$$$ coins, so he will have exactly $$$4$$$ coins. In the second test case of the example, you can add $$$1$$$ coin to the $$$4$$$-th chest, so, when Monocarp arrives, he will take a chest with $$$4$$$ coins, then another chest with $$$4$$$ coins, and a chest with $$$2$$$ coins. In the third test case of the example, you can add $$$3$$$ coins to the $$$1$$$-st chest and $$$5$$$ coins to the $$$2$$$-nd chest. In the fourth test case of the example, you can add $$$1$$$ coin to the $$$1$$$-st chest and $$$1$$$ coin to the $$$3$$$-rd chest.
2042B
https://codeforces.com/problemset/problem/2042/B
Game with Colored Marbles
900
[ "games", "greedy" ]
Div. 2
2,000
512
Alice and Bob play a game. There are $$$n$$$ marbles, the $$$i$$$-th of them has color $$$c_i$$$. The players take turns; Alice goes first, then Bob, then Alice again, then Bob again, and so on. During their turn, a player must take one of the remaining marbles and remove it from the game. If there are no marbles left (all $$$n$$$ marbles have been taken), the game ends. Alice's score at the end of the game is calculated as follows: - she receives $$$1$$$ point for every color $$$x$$$ such that she has taken at least one marble of that color; - additionally, she receives $$$1$$$ point for every color $$$x$$$ such that she has taken all marbles of that color (of course, only colors present in the game are considered). For example, suppose there are $$$5$$$ marbles, their colors are $$$[1, 3, 1, 3, 4]$$$, and the game goes as follows: Alice takes the $$$1$$$-st marble, then Bob takes the $$$3$$$-rd marble, then Alice takes the $$$5$$$-th marble, then Bob takes the $$$2$$$-nd marble, and finally, Alice takes the $$$4$$$-th marble. Then, Alice receives $$$4$$$ points: $$$3$$$ points for having at least one marble for colors $$$1$$$, $$$3$$$ and $$$4$$$, and $$$1$$$ point for having all marbles of color $$$4$$$. Note that this strategy is not necessarily optimal for both players. Alice wants to maximize her score at the end of the game. Bob wants to minimize it. Both players play optimally (i. e. Alice chooses a strategy which allows her to get as many points as possible, and Bob chooses a strategy which minimizes the amount of points Alice can get). Calculate Alice's score at the end of the game.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines: - the first line contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of marbles; - the second line contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le n$$$) — the colors of the marbles. Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.
For each test case, print one integer — Alice's score at the end of the game, assuming that both players play optimally.
[ [ "3\n5\n1 3 1 3 4\n3\n1 2 3\n4\n4 4 4 4", "4\n4\n1" ] ]
In the second test case of the example, the colors of all marbles are distinct, so, no matter how the players act, Alice receives $$$4$$$ points for having all marbles of two colors, and no marbles of the third color. In the third test case of the example, the colors of all marbles are the same, so, no matter how the players act, Alice receives $$$1$$$ point for having at least one (but not all) marble of color $$$4$$$.
Title: Game with Colored Marbles time_limit_ms: 2000 memory_limit_mb: 512 Description: Alice and Bob play a game. There are $$$n$$$ marbles, the $$$i$$$-th of them has color $$$c_i$$$. The players take turns; Alice goes first, then Bob, then Alice again, then Bob again, and so on. During their turn, a player must take one of the remaining marbles and remove it from the game. If there are no marbles left (all $$$n$$$ marbles have been taken), the game ends. Alice's score at the end of the game is calculated as follows: - she receives $$$1$$$ point for every color $$$x$$$ such that she has taken at least one marble of that color; - additionally, she receives $$$1$$$ point for every color $$$x$$$ such that she has taken all marbles of that color (of course, only colors present in the game are considered). For example, suppose there are $$$5$$$ marbles, their colors are $$$[1, 3, 1, 3, 4]$$$, and the game goes as follows: Alice takes the $$$1$$$-st marble, then Bob takes the $$$3$$$-rd marble, then Alice takes the $$$5$$$-th marble, then Bob takes the $$$2$$$-nd marble, and finally, Alice takes the $$$4$$$-th marble. Then, Alice receives $$$4$$$ points: $$$3$$$ points for having at least one marble for colors $$$1$$$, $$$3$$$ and $$$4$$$, and $$$1$$$ point for having all marbles of color $$$4$$$. Note that this strategy is not necessarily optimal for both players. Alice wants to maximize her score at the end of the game. Bob wants to minimize it. Both players play optimally (i. e. Alice chooses a strategy which allows her to get as many points as possible, and Bob chooses a strategy which minimizes the amount of points Alice can get). Calculate Alice's score at the end of the game. Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines: - the first line contains one integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of marbles; - the second line contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le n$$$) — the colors of the marbles. Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. Output: For each test case, print one integer — Alice's score at the end of the game, assuming that both players play optimally. Examples: input: 3 5 1 3 1 3 4 3 1 2 3 4 4 4 4 4 output: 4 4 1 Note: In the second test case of the example, the colors of all marbles are distinct, so, no matter how the players act, Alice receives $$$4$$$ points for having all marbles of two colors, and no marbles of the third color. In the third test case of the example, the colors of all marbles are the same, so, no matter how the players act, Alice receives $$$1$$$ point for having at least one (but not all) marble of color $$$4$$$.
2042C
https://codeforces.com/problemset/problem/2042/C
Competitive Fishing
1,800
[ "greedy" ]
Div. 2
2,000
512
Alice and Bob participate in a fishing contest! In total, they caught $$$n$$$ fishes, numbered from $$$1$$$ to $$$n$$$ (the bigger the fish, the greater its index). Some of these fishes were caught by Alice, others — by Bob. Their performance will be evaluated as follows. First, an integer $$$m$$$ will be chosen, and all fish will be split into $$$m$$$ non-empty groups. The first group should contain several (at least one) smallest fishes, the second group — several (at least one) next smallest fishes, and so on. Each fish should belong to exactly one group, and each group should be a contiguous subsegment of fishes. Note that the groups are numbered in exactly that order; for example, the fishes from the second group cannot be smaller than the fishes from the first group, since the first group contains the smallest fishes. Then, each fish will be assigned a value according to its group index: each fish in the first group gets value equal to $$$0$$$, each fish in the second group gets value equal to $$$1$$$, and so on. So, each fish in the $$$i$$$-th group gets value equal to $$$(i-1)$$$. The score of each contestant is simply the total value of all fishes that contestant caught. You want Bob's score to exceed Alice's score by at least $$$k$$$ points. What is the minimum number of groups ($$$m$$$) you have to split the fishes into? If it is impossible, you should report that.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 2 \cdot 10^5$$$; $$$1 \le k \le 10^9$$$). The second line contains a string, consisting of exactly $$$n$$$ characters. The $$$i$$$-th character is either 0 (denoting that the $$$i$$$-th fish was caught by Alice) or 1 (denoting that the $$$i$$$-th fish was caught by Bob). Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer — the minimum number of groups you have to split the fishes into; or -1 if it's impossible.
[ [ "7\n4 1\n1001\n4 1\n1010\n4 1\n0110\n4 2\n0110\n6 3\n001110\n10 20\n1111111111\n5 11\n11111", "2\n-1\n2\n-1\n3\n4\n-1" ] ]
In the first test case of the example, you can split the fishes into groups as follows: the first three fishes form the $$$1$$$-st group, the last fish forms the $$$2$$$-nd group. Then, Bob's score will be $$$1$$$, and Alice's score will be $$$0$$$. In the third test case of the example, you can split the fishes into groups as follows: the first fish forms the $$$1$$$-st group, the last three fishes form the $$$2$$$-nd group. Then, Bob's score will be $$$2$$$, and Alice's score will be $$$1$$$.
Title: Competitive Fishing time_limit_ms: 2000 memory_limit_mb: 512 Description: Alice and Bob participate in a fishing contest! In total, they caught $$$n$$$ fishes, numbered from $$$1$$$ to $$$n$$$ (the bigger the fish, the greater its index). Some of these fishes were caught by Alice, others — by Bob. Their performance will be evaluated as follows. First, an integer $$$m$$$ will be chosen, and all fish will be split into $$$m$$$ non-empty groups. The first group should contain several (at least one) smallest fishes, the second group — several (at least one) next smallest fishes, and so on. Each fish should belong to exactly one group, and each group should be a contiguous subsegment of fishes. Note that the groups are numbered in exactly that order; for example, the fishes from the second group cannot be smaller than the fishes from the first group, since the first group contains the smallest fishes. Then, each fish will be assigned a value according to its group index: each fish in the first group gets value equal to $$$0$$$, each fish in the second group gets value equal to $$$1$$$, and so on. So, each fish in the $$$i$$$-th group gets value equal to $$$(i-1)$$$. The score of each contestant is simply the total value of all fishes that contestant caught. You want Bob's score to exceed Alice's score by at least $$$k$$$ points. What is the minimum number of groups ($$$m$$$) you have to split the fishes into? If it is impossible, you should report that. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 2 \cdot 10^5$$$; $$$1 \le k \le 10^9$$$). The second line contains a string, consisting of exactly $$$n$$$ characters. The $$$i$$$-th character is either 0 (denoting that the $$$i$$$-th fish was caught by Alice) or 1 (denoting that the $$$i$$$-th fish was caught by Bob). Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: For each test case, print a single integer — the minimum number of groups you have to split the fishes into; or -1 if it's impossible. Examples: input: 7 4 1 1001 4 1 1010 4 1 0110 4 2 0110 6 3 001110 10 20 1111111111 5 11 11111 output: 2 -1 2 -1 3 4 -1 Note: In the first test case of the example, you can split the fishes into groups as follows: the first three fishes form the $$$1$$$-st group, the last fish forms the $$$2$$$-nd group. Then, Bob's score will be $$$1$$$, and Alice's score will be $$$0$$$. In the third test case of the example, you can split the fishes into groups as follows: the first fish forms the $$$1$$$-st group, the last three fishes form the $$$2$$$-nd group. Then, Bob's score will be $$$2$$$, and Alice's score will be $$$1$$$.
2042D
https://codeforces.com/problemset/problem/2042/D
Recommendations
1,900
[ "two pointers", "data structures", "implementation", "sortings" ]
Div. 2
2,000
512
Suppose you are working in some audio streaming service. The service has $$$n$$$ active users and $$$10^9$$$ tracks users can listen to. Users can like tracks and, based on likes, the service should recommend them new tracks. Tracks are numbered from $$$1$$$ to $$$10^9$$$. It turned out that tracks the $$$i$$$-th user likes form a segment $$$[l_i, r_i]$$$. Let's say that the user $$$j$$$ is a predictor for user $$$i$$$ ($$$j \neq i$$$) if user $$$j$$$ likes all tracks the $$$i$$$-th user likes (and, possibly, some other tracks too). Also, let's say that a track is strongly recommended for user $$$i$$$ if the track is not liked by the $$$i$$$-th user yet, but it is liked by every predictor for the $$$i$$$-th user. Calculate the number of strongly recommended tracks for each user $$$i$$$. If a user doesn't have any predictors, then print $$$0$$$ for that user.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next, $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of users. The next $$$n$$$ lines contain two integers $$$l_i$$$ and $$$r_i$$$ per line ($$$1 \le l_i \le r_i \le 10^9$$$) — the segment of tracks the $$$i$$$-th user likes. Additional constraint on the input: the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
For each test case, print $$$n$$$ integers, where the $$$i$$$-th integer is the number of strongly recommended tracks for the $$$i$$$-th user (or $$$0$$$, if that user doesn't have any predictors).
[ [ "4\n3\n3 8\n2 5\n4 5\n2\n42 42\n1 1000000000\n3\n42 42\n1 1000000000\n42 42\n6\n1 10\n3 10\n3 7\n5 7\n4 4\n1 2", "0\n0\n1\n999999999\n0\n0\n0\n0\n0\n2\n3\n2\n4\n8" ] ]
In the first test case: - the first user has no predictors; - the second user has no predictors; - the third user has two predictors: users $$$1$$$ and $$$2$$$; only track $$$3$$$ is liked by both of them and not liked by the third user. In the second test case, the second user is a predictor for the first user. Therefore, all tracks, except $$$42$$$, are strongly recommended for the first user. In the third test case, the first user has two predictors: users $$$2$$$ and $$$3$$$, but there is no track that is liked by them and not liked by the first user himself.
Title: Recommendations time_limit_ms: 2000 memory_limit_mb: 512 Description: Suppose you are working in some audio streaming service. The service has $$$n$$$ active users and $$$10^9$$$ tracks users can listen to. Users can like tracks and, based on likes, the service should recommend them new tracks. Tracks are numbered from $$$1$$$ to $$$10^9$$$. It turned out that tracks the $$$i$$$-th user likes form a segment $$$[l_i, r_i]$$$. Let's say that the user $$$j$$$ is a predictor for user $$$i$$$ ($$$j \neq i$$$) if user $$$j$$$ likes all tracks the $$$i$$$-th user likes (and, possibly, some other tracks too). Also, let's say that a track is strongly recommended for user $$$i$$$ if the track is not liked by the $$$i$$$-th user yet, but it is liked by every predictor for the $$$i$$$-th user. Calculate the number of strongly recommended tracks for each user $$$i$$$. If a user doesn't have any predictors, then print $$$0$$$ for that user. Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next, $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of users. The next $$$n$$$ lines contain two integers $$$l_i$$$ and $$$r_i$$$ per line ($$$1 \le l_i \le r_i \le 10^9$$$) — the segment of tracks the $$$i$$$-th user likes. Additional constraint on the input: the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$. Output: For each test case, print $$$n$$$ integers, where the $$$i$$$-th integer is the number of strongly recommended tracks for the $$$i$$$-th user (or $$$0$$$, if that user doesn't have any predictors). Examples: input: 4 3 3 8 2 5 4 5 2 42 42 1 1000000000 3 42 42 1 1000000000 42 42 6 1 10 3 10 3 7 5 7 4 4 1 2 output: 0 0 1 999999999 0 0 0 0 0 2 3 2 4 8 Note: In the first test case: - the first user has no predictors; - the second user has no predictors; - the third user has two predictors: users $$$1$$$ and $$$2$$$; only track $$$3$$$ is liked by both of them and not liked by the third user. In the second test case, the second user is a predictor for the first user. Therefore, all tracks, except $$$42$$$, are strongly recommended for the first user. In the third test case, the first user has two predictors: users $$$2$$$ and $$$3$$$, but there is no track that is liked by them and not liked by the first user himself.
2009A
https://codeforces.com/problemset/problem/2009/A
Minimize!
800
[ "brute force", "math" ]
Div. 4
1,000
256
You are given two integers $$$a$$$ and $$$b$$$ ($$$a \leq b$$$). Over all possible integer values of $$$c$$$ ($$$a \leq c \leq b$$$), find the minimum value of $$$(c - a) + (b - c)$$$.
The first line contains $$$t$$$ ($$$1 \leq t \leq 55$$$) — the number of test cases. Each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a \leq b \leq 10$$$).
For each test case, output the minimum possible value of $$$(c - a) + (b - c)$$$ on a new line.
[ [ "3\n1 2\n3 10\n5 5", "1\n7\n0" ] ]
In the first test case, you can choose $$$c = 1$$$ and obtain an answer of $$$(1 - 1) + (2 - 1) = 1$$$. It can be shown this is the minimum value possible. In the second test case, you can choose $$$c = 6$$$ and obtain an answer of $$$(6 - 3) + (10 - 6) = 7$$$. It can be shown this is the minimum value possible.
Title: Minimize! time_limit_ms: 1000 memory_limit_mb: 256 Description: You are given two integers $$$a$$$ and $$$b$$$ ($$$a \leq b$$$). Over all possible integer values of $$$c$$$ ($$$a \leq c \leq b$$$), find the minimum value of $$$(c - a) + (b - c)$$$. Input: The first line contains $$$t$$$ ($$$1 \leq t \leq 55$$$) — the number of test cases. Each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a \leq b \leq 10$$$). Output: For each test case, output the minimum possible value of $$$(c - a) + (b - c)$$$ on a new line. Examples: input: 3 1 2 3 10 5 5 output: 1 7 0 Note: In the first test case, you can choose $$$c = 1$$$ and obtain an answer of $$$(1 - 1) + (2 - 1) = 1$$$. It can be shown this is the minimum value possible. In the second test case, you can choose $$$c = 6$$$ and obtain an answer of $$$(6 - 3) + (10 - 6) = 7$$$. It can be shown this is the minimum value possible.
2009B
https://codeforces.com/problemset/problem/2009/B
osu!mania
800
[ "brute force", "implementation" ]
Div. 4
1,000
256
You are playing your favorite rhythm game, osu!mania. The layout of your beatmap consists of $$$n$$$ rows and $$$4$$$ columns. Because notes at the bottom are closer, you will process the bottommost row first and the topmost row last. Each row will contain exactly one note, represented as a '#'. For each note $$$1, 2, \dots, n$$$, in the order of processing, output the column in which the note appears.
The first line contains $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. For each test case, the first line contains $$$n$$$ ($$$1 \leq n \leq 500$$$) — the number of rows of the beatmap. The following $$$n$$$ lines contain $$$4$$$ characters. The $$$i$$$-th line represents the $$$i$$$-th row of the beatmap from the top. It is guaranteed that the characters are either '.' or '#', and exactly one of the characters is '#'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$.
For each test case, output $$$n$$$ integers on a new line, the column that the $$$i$$$-th note appears in for all $$$i$$$ from $$$1$$$ to $$$n$$$.
[ [ "3\n4\n#...\n.#..\n..#.\n...#\n2\n.#..\n.#..\n1\n...#", "4 3 2 1 \n2 2 \n4" ] ]
Title: osu!mania time_limit_ms: 1000 memory_limit_mb: 256 Description: You are playing your favorite rhythm game, osu!mania. The layout of your beatmap consists of $$$n$$$ rows and $$$4$$$ columns. Because notes at the bottom are closer, you will process the bottommost row first and the topmost row last. Each row will contain exactly one note, represented as a '#'. For each note $$$1, 2, \dots, n$$$, in the order of processing, output the column in which the note appears. Input: The first line contains $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. For each test case, the first line contains $$$n$$$ ($$$1 \leq n \leq 500$$$) — the number of rows of the beatmap. The following $$$n$$$ lines contain $$$4$$$ characters. The $$$i$$$-th line represents the $$$i$$$-th row of the beatmap from the top. It is guaranteed that the characters are either '.' or '#', and exactly one of the characters is '#'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$. Output: For each test case, output $$$n$$$ integers on a new line, the column that the $$$i$$$-th note appears in for all $$$i$$$ from $$$1$$$ to $$$n$$$. Examples: input: 3 4 #... .#.. ..#. ...# 2 .#.. .#.. 1 ...# output: 4 3 2 1 2 2 4 Note:
2009C
https://codeforces.com/problemset/problem/2009/C
The Legend of Freya the Frog
1,100
[ "implementation", "math" ]
Div. 4
2,000
256
Freya the Frog is traveling on the 2D coordinate plane. She is currently at point $$$(0,0)$$$ and wants to go to point $$$(x,y)$$$. In one move, she chooses an integer $$$d$$$ such that $$$0 \leq d \leq k$$$ and jumps $$$d$$$ spots forward in the direction she is facing. Initially, she is facing the positive $$$x$$$ direction. After every move, she will alternate between facing the positive $$$x$$$ direction and the positive $$$y$$$ direction (i.e., she will face the positive $$$y$$$ direction on her second move, the positive $$$x$$$ direction on her third move, and so on). What is the minimum amount of moves she must perform to land on point $$$(x,y)$$$?
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case contains three integers $$$x$$$, $$$y$$$, and $$$k$$$ ($$$0 \leq x, y \leq 10^9, 1 \leq k \leq 10^9$$$).
For each test case, output the number of jumps Freya needs to make on a new line.
[ [ "3\n9 11 3\n0 10 8\n1000000 100000 10", "8\n4\n199999" ] ]
In the first sample, one optimal set of moves is if Freya jumps in the following way: ($$$0,0$$$) $$$\rightarrow$$$ ($$$2,0$$$) $$$\rightarrow$$$ ($$$2,2$$$) $$$\rightarrow$$$ ($$$3,2$$$) $$$\rightarrow$$$ ($$$3,5$$$) $$$\rightarrow$$$ ($$$6,5$$$) $$$\rightarrow$$$ ($$$6,8$$$) $$$\rightarrow$$$ ($$$9,8$$$) $$$\rightarrow$$$ ($$$9,11$$$). This takes 8 jumps.
Title: The Legend of Freya the Frog time_limit_ms: 2000 memory_limit_mb: 256 Description: Freya the Frog is traveling on the 2D coordinate plane. She is currently at point $$$(0,0)$$$ and wants to go to point $$$(x,y)$$$. In one move, she chooses an integer $$$d$$$ such that $$$0 \leq d \leq k$$$ and jumps $$$d$$$ spots forward in the direction she is facing. Initially, she is facing the positive $$$x$$$ direction. After every move, she will alternate between facing the positive $$$x$$$ direction and the positive $$$y$$$ direction (i.e., she will face the positive $$$y$$$ direction on her second move, the positive $$$x$$$ direction on her third move, and so on). What is the minimum amount of moves she must perform to land on point $$$(x,y)$$$? Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case contains three integers $$$x$$$, $$$y$$$, and $$$k$$$ ($$$0 \leq x, y \leq 10^9, 1 \leq k \leq 10^9$$$). Output: For each test case, output the number of jumps Freya needs to make on a new line. Examples: input: 3 9 11 3 0 10 8 1000000 100000 10 output: 8 4 199999 Note: In the first sample, one optimal set of moves is if Freya jumps in the following way: ($$$0,0$$$) $$$\rightarrow$$$ ($$$2,0$$$) $$$\rightarrow$$$ ($$$2,2$$$) $$$\rightarrow$$$ ($$$3,2$$$) $$$\rightarrow$$$ ($$$3,5$$$) $$$\rightarrow$$$ ($$$6,5$$$) $$$\rightarrow$$$ ($$$6,8$$$) $$$\rightarrow$$$ ($$$9,8$$$) $$$\rightarrow$$$ ($$$9,11$$$). This takes 8 jumps.
2009D
https://codeforces.com/problemset/problem/2009/D
Satyam and Counting
1,400
[ "geometry", "math" ]
Div. 4
2,000
256
Satyam is given $$$n$$$ distinct points on the 2D coordinate plane. It is guaranteed that $$$0 \leq y_i \leq 1$$$ for all given points $$$(x_i, y_i)$$$. How many different nondegenerate right triangles$$$^{\text{∗}}$$$ can be formed from choosing three different points as its vertices? Two triangles $$$a$$$ and $$$b$$$ are different if there is a point $$$v$$$ such that $$$v$$$ is a vertex of $$$a$$$ but not a vertex of $$$b$$$.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$) — the number of points. The following $$$n$$$ lines contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq n$$$, $$$0 \leq y_i \leq 1$$$) — the $$$i$$$'th point that Satyam can choose from. It is guaranteed that all $$$(x_i, y_i)$$$ are pairwise distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
Output an integer for each test case, the number of distinct nondegenerate right triangles that can be formed from choosing three points.
[ [ "3\n5\n1 0\n1 1\n3 0\n5 0\n2 1\n3\n0 0\n1 0\n3 0\n9\n1 0\n2 0\n3 0\n4 0\n5 0\n2 1\n7 1\n8 1\n9 1", "4\n0\n8" ] ]
The four triangles in question for the first test case:
Title: Satyam and Counting time_limit_ms: 2000 memory_limit_mb: 256 Description: Satyam is given $$$n$$$ distinct points on the 2D coordinate plane. It is guaranteed that $$$0 \leq y_i \leq 1$$$ for all given points $$$(x_i, y_i)$$$. How many different nondegenerate right triangles$$$^{\text{∗}}$$$ can be formed from choosing three different points as its vertices? Two triangles $$$a$$$ and $$$b$$$ are different if there is a point $$$v$$$ such that $$$v$$$ is a vertex of $$$a$$$ but not a vertex of $$$b$$$. Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$) — the number of points. The following $$$n$$$ lines contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i \leq n$$$, $$$0 \leq y_i \leq 1$$$) — the $$$i$$$'th point that Satyam can choose from. It is guaranteed that all $$$(x_i, y_i)$$$ are pairwise distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: Output an integer for each test case, the number of distinct nondegenerate right triangles that can be formed from choosing three points. Examples: input: 3 5 1 0 1 1 3 0 5 0 2 1 3 0 0 1 0 3 0 9 1 0 2 0 3 0 4 0 5 0 2 1 7 1 8 1 9 1 output: 4 0 8 Note: The four triangles in question for the first test case:
2009E
https://codeforces.com/problemset/problem/2009/E
Klee's SUPER DUPER LARGE Array!!!
1,400
[ "ternary search", "binary search", "math" ]
Div. 4
2,000
256
Klee has an array $$$a$$$ of length $$$n$$$ containing integers $$$[k, k+1, ..., k+n-1]$$$ in that order. Klee wants to choose an index $$$i$$$ ($$$1 \leq i \leq n$$$) such that $$$x = |a_1 + a_2 + \dots + a_i - a_{i+1} - \dots - a_n|$$$ is minimized. Note that for an arbitrary integer $$$z$$$, $$$|z|$$$ represents the absolute value of $$$z$$$. Output the minimum possible value of $$$x$$$.
The first line contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n, k \leq 10^9$$$) — the length of the array and the starting element of the array.
For each test case, output the minimum value of $$$x$$$ on a new line.
[ [ "4\n2 2\n7 2\n5 3\n1000000000 1000000000", "1\n5\n1\n347369930" ] ]
In the first sample, $$$a = [2, 3]$$$. When $$$i = 1$$$ is chosen, $$$x = |2-3| = 1$$$. It can be shown this is the minimum possible value of $$$x$$$. In the third sample, $$$a = [3, 4, 5, 6, 7]$$$. When $$$i = 3$$$ is chosen, $$$x = |3 + 4 + 5 - 6 - 7| = 1$$$. It can be shown this is the minimum possible value of $$$x$$$.
Title: Klee's SUPER DUPER LARGE Array!!! time_limit_ms: 2000 memory_limit_mb: 256 Description: Klee has an array $$$a$$$ of length $$$n$$$ containing integers $$$[k, k+1, ..., k+n-1]$$$ in that order. Klee wants to choose an index $$$i$$$ ($$$1 \leq i \leq n$$$) such that $$$x = |a_1 + a_2 + \dots + a_i - a_{i+1} - \dots - a_n|$$$ is minimized. Note that for an arbitrary integer $$$z$$$, $$$|z|$$$ represents the absolute value of $$$z$$$. Output the minimum possible value of $$$x$$$. Input: The first line contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n, k \leq 10^9$$$) — the length of the array and the starting element of the array. Output: For each test case, output the minimum value of $$$x$$$ on a new line. Examples: input: 4 2 2 7 2 5 3 1000000000 1000000000 output: 1 5 1 347369930 Note: In the first sample, $$$a = [2, 3]$$$. When $$$i = 1$$$ is chosen, $$$x = |2-3| = 1$$$. It can be shown this is the minimum possible value of $$$x$$$. In the third sample, $$$a = [3, 4, 5, 6, 7]$$$. When $$$i = 3$$$ is chosen, $$$x = |3 + 4 + 5 - 6 - 7| = 1$$$. It can be shown this is the minimum possible value of $$$x$$$.
2009F
https://codeforces.com/problemset/problem/2009/F
Firefly's Queries
1,700
[ "bitmasks", "data structures", "flows", "math" ]
Div. 4
2,000
256
Firefly is given an array $$$a$$$ of length $$$n$$$. Let $$$c_i$$$ denote the $$$i$$$'th cyclic shift$$$^{\text{∗}}$$$ of $$$a$$$. She creates a new array $$$b$$$ such that $$$b = c_1 + c_2 + \dots + c_n$$$ where $$$+$$$ represents concatenation$$$^{\text{†}}$$$. Then, she asks you $$$q$$$ queries. For each query, output the sum of all elements in the subarray of $$$b$$$ that starts from the $$$l$$$-th element and ends at the $$$r$$$-th element, inclusive of both ends.
The first line contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n, q \leq 2 \cdot 10^5$$$) — the length of the array and the number of queries. The following line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ ($$$1 \leq a_i \leq 10^6$$$). The following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n^2$$$) — the left and right bounds of the query. Note that the large inputs may require the use of 64-bit integers. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ and the sum of $$$q$$$ does not exceed $$$2 \cdot 10^5$$$.
For each query, output the answer on a new line.
[ [ "5\n3 3\n1 2 3\n1 9\n3 5\n8 8\n5 5\n4 8 3 2 4\n1 14\n3 7\n7 10\n2 11\n1 25\n1 1\n6\n1 1\n5 7\n3 1 6 10 4\n3 21\n6 17\n2 2\n1 5\n1 14\n9 15\n12 13\n5 3\n4 9 10 10 1\n20 25\n3 11\n20 22", "18\n8\n1\n55\n20\n13\n41\n105\n6\n96\n62\n1\n24\n71\n31\n14\n44\n65\n15" ] ]
For the first test case, $$$b = [1, 2, 3, 2, 3, 1, 3, 1, 2]$$$.
Title: Firefly's Queries time_limit_ms: 2000 memory_limit_mb: 256 Description: Firefly is given an array $$$a$$$ of length $$$n$$$. Let $$$c_i$$$ denote the $$$i$$$'th cyclic shift$$$^{\text{∗}}$$$ of $$$a$$$. She creates a new array $$$b$$$ such that $$$b = c_1 + c_2 + \dots + c_n$$$ where $$$+$$$ represents concatenation$$$^{\text{†}}$$$. Then, she asks you $$$q$$$ queries. For each query, output the sum of all elements in the subarray of $$$b$$$ that starts from the $$$l$$$-th element and ends at the $$$r$$$-th element, inclusive of both ends. Input: The first line contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n, q \leq 2 \cdot 10^5$$$) — the length of the array and the number of queries. The following line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ ($$$1 \leq a_i \leq 10^6$$$). The following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n^2$$$) — the left and right bounds of the query. Note that the large inputs may require the use of 64-bit integers. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ and the sum of $$$q$$$ does not exceed $$$2 \cdot 10^5$$$. Output: For each query, output the answer on a new line. Examples: input: 5 3 3 1 2 3 1 9 3 5 8 8 5 5 4 8 3 2 4 1 14 3 7 7 10 2 11 1 25 1 1 6 1 1 5 7 3 1 6 10 4 3 21 6 17 2 2 1 5 1 14 9 15 12 13 5 3 4 9 10 10 1 20 25 3 11 20 22 output: 18 8 1 55 20 13 41 105 6 96 62 1 24 71 31 14 44 65 15 Note: For the first test case, $$$b = [1, 2, 3, 2, 3, 1, 3, 1, 2]$$$.
2009G1
https://codeforces.com/problemset/problem/2009/G1
Yunli's Subarray Queries (easy version)
1,900
[ "two pointers", "data structures", "binary search" ]
Div. 4
3,000
512
This is the easy version of the problem. In this version, it is guaranteed that $$$r=l+k-1$$$ for all queries. For an arbitrary array $$$b$$$, Yunli can perform the following operation any number of times: - Select an index $$$i$$$. Set $$$b_i = x$$$ where $$$x$$$ is any integer she desires ($$$x$$$ is not limited to the interval $$$[1,n]$$$). Denote $$$f(b)$$$ as the minimum number of operations she needs to perform until there exists a consecutive subarray$$$^{\text{∗}}$$$ of length at least $$$k$$$ in $$$b$$$. Yunli is given an array $$$a$$$ of size $$$n$$$ and asks you $$$q$$$ queries. In each query, you must output $$$\sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, \ldots, a_j])$$$. Note that in this version, you are only required to output $$$f([a_l, a_{l+1}, \ldots, a_{l+k-1}])$$$.
The first line contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$k$$$, and $$$q$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq q \leq 2 \cdot 10^5$$$) — the length of the array, the length of the consecutive subarray, and the number of queries. The following line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq n$$$). The following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$, $$$r=l+k-1$$$) — the bounds of the query. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
Output $$$\sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, \ldots, a_j])$$$ for each query on a new line.
[ [ "3\n7 5 3\n1 2 3 2 1 2 3\n1 5\n2 6\n3 7\n8 4 2\n4 3 1 1 2 4 3 2\n3 6\n2 5\n5 4 2\n4 5 1 2 3\n1 4\n2 5", "2\n3\n2\n2\n2\n2\n1" ] ]
In the first query of the first testcase, $$$b=[1,2,3,2,1]$$$. Yunli can make a consecutive subarray of length $$$5$$$ in $$$2$$$ moves: - Set $$$b_4=4$$$ - Set $$$b_5=5$$$ In the second query of the first testcase, $$$b=[2,3,2,1,2]$$$. Yunli can make a consecutive subarray of length $$$5$$$ in $$$3$$$ moves: - Set $$$b_3=0$$$ - Set $$$b_2=-1$$$ - Set $$$b_1=-2$$$
Title: Yunli's Subarray Queries (easy version) time_limit_ms: 3000 memory_limit_mb: 512 Description: This is the easy version of the problem. In this version, it is guaranteed that $$$r=l+k-1$$$ for all queries. For an arbitrary array $$$b$$$, Yunli can perform the following operation any number of times: - Select an index $$$i$$$. Set $$$b_i = x$$$ where $$$x$$$ is any integer she desires ($$$x$$$ is not limited to the interval $$$[1,n]$$$). Denote $$$f(b)$$$ as the minimum number of operations she needs to perform until there exists a consecutive subarray$$$^{\text{∗}}$$$ of length at least $$$k$$$ in $$$b$$$. Yunli is given an array $$$a$$$ of size $$$n$$$ and asks you $$$q$$$ queries. In each query, you must output $$$\sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, \ldots, a_j])$$$. Note that in this version, you are only required to output $$$f([a_l, a_{l+1}, \ldots, a_{l+k-1}])$$$. Input: The first line contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$k$$$, and $$$q$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq q \leq 2 \cdot 10^5$$$) — the length of the array, the length of the consecutive subarray, and the number of queries. The following line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq n$$$). The following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$, $$$r=l+k-1$$$) — the bounds of the query. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: Output $$$\sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, \ldots, a_j])$$$ for each query on a new line. Examples: input: 3 7 5 3 1 2 3 2 1 2 3 1 5 2 6 3 7 8 4 2 4 3 1 1 2 4 3 2 3 6 2 5 5 4 2 4 5 1 2 3 1 4 2 5 output: 2 3 2 2 2 2 1 Note: In the first query of the first testcase, $$$b=[1,2,3,2,1]$$$. Yunli can make a consecutive subarray of length $$$5$$$ in $$$2$$$ moves: - Set $$$b_4=4$$$ - Set $$$b_5=5$$$ In the second query of the first testcase, $$$b=[2,3,2,1,2]$$$. Yunli can make a consecutive subarray of length $$$5$$$ in $$$3$$$ moves: - Set $$$b_3=0$$$ - Set $$$b_2=-1$$$ - Set $$$b_1=-2$$$
2009G2
https://codeforces.com/problemset/problem/2009/G2
Yunli's Subarray Queries (hard version)
2,200
[ "data structures", "dp", "binary search" ]
Div. 4
3,000
512
This is the hard version of the problem. In this version, it is guaranteed that $$$r \geq l+k-1$$$ for all queries. For an arbitrary array $$$b$$$, Yunli can perform the following operation any number of times: - Select an index $$$i$$$. Set $$$b_i = x$$$ where $$$x$$$ is any integer she desires ($$$x$$$ is not limited to the interval $$$[1,n]$$$). Denote $$$f(b)$$$ as the minimum number of operations she needs to perform until there exists a consecutive subarray$$$^{\text{∗}}$$$ of length at least $$$k$$$ in $$$b$$$. Yunli is given an array $$$a$$$ of size $$$n$$$ and asks you $$$q$$$ queries. In each query, you must output $$$\sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, \ldots, a_j])$$$.
The first line contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$k$$$, and $$$q$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq q \leq 2 \cdot 10^5$$$) — the length of the array, the length of the consecutive subarray, and the number of queries. The following line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ ($$$1 \leq a_i \leq n$$$). The following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$, $$$r \geq l+k-1$$$) — the bounds of the query. It is guaranteed the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
Output $$$\sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, \ldots, a_j])$$$ for each query on a new line.
[ [ "3\n7 5 3\n1 2 3 2 1 2 3\n1 7\n2 7\n3 7\n8 4 2\n4 3 1 1 2 4 3 2\n3 6\n1 5\n5 4 2\n4 5 1 2 3\n1 4\n1 5", "6\n5\n2\n2\n5\n2\n3" ] ]
In the second query of the first testcase, we calculate the following function values: - $$$f([2,3,2,1,2])=3$$$ because Yunli can set $$$b_3=4$$$, $$$b_4=5$$$, and $$$b_5=6$$$, making a consecutive subarray of size $$$5$$$ in $$$3$$$ moves. - $$$f([2,3,2,1,2,3])=2$$$ because we can set $$$b_3=0$$$ and $$$b_2=-1$$$, making a consecutive subarray of size $$$5$$$ in $$$2$$$ moves (starting at position $$$2$$$) The answer to this query is $$$3+2=5$$$.
Title: Yunli's Subarray Queries (hard version) time_limit_ms: 3000 memory_limit_mb: 512 Description: This is the hard version of the problem. In this version, it is guaranteed that $$$r \geq l+k-1$$$ for all queries. For an arbitrary array $$$b$$$, Yunli can perform the following operation any number of times: - Select an index $$$i$$$. Set $$$b_i = x$$$ where $$$x$$$ is any integer she desires ($$$x$$$ is not limited to the interval $$$[1,n]$$$). Denote $$$f(b)$$$ as the minimum number of operations she needs to perform until there exists a consecutive subarray$$$^{\text{∗}}$$$ of length at least $$$k$$$ in $$$b$$$. Yunli is given an array $$$a$$$ of size $$$n$$$ and asks you $$$q$$$ queries. In each query, you must output $$$\sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, \ldots, a_j])$$$. Input: The first line contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$k$$$, and $$$q$$$ ($$$1 \leq k \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq q \leq 2 \cdot 10^5$$$) — the length of the array, the length of the consecutive subarray, and the number of queries. The following line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ ($$$1 \leq a_i \leq n$$$). The following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$, $$$r \geq l+k-1$$$) — the bounds of the query. It is guaranteed the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: Output $$$\sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, \ldots, a_j])$$$ for each query on a new line. Examples: input: 3 7 5 3 1 2 3 2 1 2 3 1 7 2 7 3 7 8 4 2 4 3 1 1 2 4 3 2 3 6 1 5 5 4 2 4 5 1 2 3 1 4 1 5 output: 6 5 2 2 5 2 3 Note: In the second query of the first testcase, we calculate the following function values: - $$$f([2,3,2,1,2])=3$$$ because Yunli can set $$$b_3=4$$$, $$$b_4=5$$$, and $$$b_5=6$$$, making a consecutive subarray of size $$$5$$$ in $$$3$$$ moves. - $$$f([2,3,2,1,2,3])=2$$$ because we can set $$$b_3=0$$$ and $$$b_2=-1$$$, making a consecutive subarray of size $$$5$$$ in $$$2$$$ moves (starting at position $$$2$$$) The answer to this query is $$$3+2=5$$$.
2046A
https://codeforces.com/problemset/problem/2046/A
Swap Columns and Find a Path
1,200
[ "greedy", "sortings" ]
Div. 1
2,000
512
There is a matrix consisting of $$$2$$$ rows and $$$n$$$ columns. The rows are numbered from $$$1$$$ to $$$2$$$ from top to bottom; the columns are numbered from $$$1$$$ to $$$n$$$ from left to right. Let's denote the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$(i,j)$$$. Each cell contains an integer; initially, the integer in the cell $$$(i,j)$$$ is $$$a_{i,j}$$$. You can perform the following operation any number of times (possibly zero): - choose two columns and swap them (i. e. choose two integers $$$x$$$ and $$$y$$$ such that $$$1 \le x < y \le n$$$, then swap $$$a_{1,x}$$$ with $$$a_{1,y}$$$, and then swap $$$a_{2,x}$$$ with $$$a_{2,y}$$$). After performing the operations, you have to choose a path from the cell $$$(1,1)$$$ to the cell $$$(2,n)$$$. For every cell $$$(i,j)$$$ in the path except for the last, the next cell should be either $$$(i+1,j)$$$ or $$$(i,j+1)$$$. Obviously, the path cannot go outside the matrix. The cost of the path is the sum of all integers in all $$$(n+1)$$$ cells belonging to the path. You have to perform the operations and choose a path so that its cost is maximum possible.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 5000$$$). The description of the test cases follows. Each test case consists of three lines: - the first line contains one integer $$$n$$$ ($$$1 \le n \le 5000$$$) — the number of columns in the matrix; - the second line contains $$$n$$$ integers $$$a_{1,1}, a_{1,2}, \ldots, a_{1,n}$$$ ($$$-10^5 \le a_{i,j} \le 10^5$$$) — the first row of the matrix; - the third line contains $$$n$$$ integers $$$a_{2,1}, a_{2,2}, \ldots, a_{2,n}$$$ ($$$-10^5 \le a_{i,j} \le 10^5$$$) — the second row of the matrix. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$.
For each test case, print one integer — the maximum cost of a path you can obtain.
[ [ "3\n1\n-10\n5\n3\n1 2 3\n10 -5 -3\n4\n2 8 5 3\n1 10 3 4", "-5\n16\n29" ] ]
Here are the explanations of the first three test cases of the example. The left matrix is the matrix given in the input, the right one is the state of the matrix after several column swaps (possibly zero). The optimal path is highlighted in green.
Title: Swap Columns and Find a Path time_limit_ms: 2000 memory_limit_mb: 512 Description: There is a matrix consisting of $$$2$$$ rows and $$$n$$$ columns. The rows are numbered from $$$1$$$ to $$$2$$$ from top to bottom; the columns are numbered from $$$1$$$ to $$$n$$$ from left to right. Let's denote the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$(i,j)$$$. Each cell contains an integer; initially, the integer in the cell $$$(i,j)$$$ is $$$a_{i,j}$$$. You can perform the following operation any number of times (possibly zero): - choose two columns and swap them (i. e. choose two integers $$$x$$$ and $$$y$$$ such that $$$1 \le x < y \le n$$$, then swap $$$a_{1,x}$$$ with $$$a_{1,y}$$$, and then swap $$$a_{2,x}$$$ with $$$a_{2,y}$$$). After performing the operations, you have to choose a path from the cell $$$(1,1)$$$ to the cell $$$(2,n)$$$. For every cell $$$(i,j)$$$ in the path except for the last, the next cell should be either $$$(i+1,j)$$$ or $$$(i,j+1)$$$. Obviously, the path cannot go outside the matrix. The cost of the path is the sum of all integers in all $$$(n+1)$$$ cells belonging to the path. You have to perform the operations and choose a path so that its cost is maximum possible. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 5000$$$). The description of the test cases follows. Each test case consists of three lines: - the first line contains one integer $$$n$$$ ($$$1 \le n \le 5000$$$) — the number of columns in the matrix; - the second line contains $$$n$$$ integers $$$a_{1,1}, a_{1,2}, \ldots, a_{1,n}$$$ ($$$-10^5 \le a_{i,j} \le 10^5$$$) — the first row of the matrix; - the third line contains $$$n$$$ integers $$$a_{2,1}, a_{2,2}, \ldots, a_{2,n}$$$ ($$$-10^5 \le a_{i,j} \le 10^5$$$) — the second row of the matrix. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Output: For each test case, print one integer — the maximum cost of a path you can obtain. Examples: input: 3 1 -10 5 3 1 2 3 10 -5 -3 4 2 8 5 3 1 10 3 4 output: -5 16 29 Note: Here are the explanations of the first three test cases of the example. The left matrix is the matrix given in the input, the right one is the state of the matrix after several column swaps (possibly zero). The optimal path is highlighted in green.
2046B
https://codeforces.com/problemset/problem/2046/B
Move Back at a Cost
1,600
[ "data structures", "binary search", "sortings", "greedy" ]
Div. 1
2,000
256
You are given an array of integers $$$a$$$ of length $$$n$$$. You can perform the following operation zero or more times: - In one operation choose an index $$$i$$$ ($$$1 \le i \le n$$$), assign $$$a_i := a_i + 1$$$, and then move $$$a_i$$$ to the back of the array (to the rightmost position). For example, if $$$a = [3, 5, 1, 9]$$$, and you choose $$$i = 2$$$, the array becomes $$$[3, 1, 9, 6]$$$. Find the lexicographically smallest$$$^{\text{∗}}$$$ array you can get by performing these operations.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$), the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, print the lexicographically smallest array you can get.
[ [ "3\n3\n2 1 3\n5\n1 2 2 1 4\n6\n1 2 3 6 5 4", "1 3 3 \n1 1 3 3 5 \n1 2 3 4 6 7" ] ]
Title: Move Back at a Cost time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given an array of integers $$$a$$$ of length $$$n$$$. You can perform the following operation zero or more times: - In one operation choose an index $$$i$$$ ($$$1 \le i \le n$$$), assign $$$a_i := a_i + 1$$$, and then move $$$a_i$$$ to the back of the array (to the rightmost position). For example, if $$$a = [3, 5, 1, 9]$$$, and you choose $$$i = 2$$$, the array becomes $$$[3, 1, 9, 6]$$$. Find the lexicographically smallest$$$^{\text{∗}}$$$ array you can get by performing these operations. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$), the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output: For each test case, print the lexicographically smallest array you can get. Examples: input: 3 3 2 1 3 5 1 2 2 1 4 6 1 2 3 6 5 4 output: 1 3 3 1 1 3 3 5 1 2 3 4 6 7 Note:
2039C1
https://codeforces.com/problemset/problem/2039/C1
Shohag Loves XOR (Easy Version)
1,200
[ "bitmasks", "brute force", "math", "number theory" ]
Div. 1 + Div. 2, Rated, Prizes!
2,000
256
This is the easy version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved. Shohag has two integers $$$x$$$ and $$$m$$$. Help him count the number of integers $$$1 \le y \le m$$$ such that $$$\mathbf{x \neq y}$$$ and $$$x \oplus y$$$ is a divisor$$$^{\text{∗}}$$$ of either $$$x$$$, $$$y$$$, or both. Here $$$\oplus$$$ is the bitwise XOR operator.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first and only line of each test case contains two space-separated integers $$$x$$$ and $$$m$$$ ($$$1 \le x \le 10^6$$$, $$$1 \le m \le 10^{18}$$$). It is guaranteed that the sum of $$$x$$$ over all test cases does not exceed $$$10^7$$$.
For each test case, print a single integer — the number of suitable $$$y$$$.
[ [ "5\n6 9\n5 7\n2 3\n6 4\n4 1", "3\n2\n1\n1\n0" ] ]
In the first test case, for $$$x = 6$$$, there are $$$3$$$ valid values for $$$y$$$ among the integers from $$$1$$$ to $$$m = 9$$$, and they are $$$4$$$, $$$5$$$, and $$$7$$$. - $$$y = 4$$$ is valid because $$$x \oplus y = 6 \oplus 4 = 2$$$ and $$$2$$$ is a divisor of both $$$x = 6$$$ and $$$y = 4$$$. - $$$y = 5$$$ is valid because $$$x \oplus y = 6 \oplus 5 = 3$$$ and $$$3$$$ is a divisor of $$$x = 6$$$. - $$$y = 7$$$ is valid because $$$x \oplus y = 6 \oplus 7 = 1$$$ and $$$1$$$ is a divisor of both $$$x = 6$$$ and $$$y = 7$$$. In the second test case, for $$$x = 5$$$, there are $$$2$$$ valid values for $$$y$$$ among the integers from $$$1$$$ to $$$m = 7$$$, and they are $$$4$$$ and $$$6$$$. - $$$y = 4$$$ is valid because $$$x \oplus y = 5 \oplus 4 = 1$$$ and $$$1$$$ is a divisor of both $$$x = 5$$$ and $$$y = 4$$$. - $$$y = 6$$$ is valid because $$$x \oplus y = 5 \oplus 6 = 3$$$ and $$$3$$$ is a divisor of $$$y = 6$$$.
Title: Shohag Loves XOR (Easy Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the easy version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved. Shohag has two integers $$$x$$$ and $$$m$$$. Help him count the number of integers $$$1 \le y \le m$$$ such that $$$\mathbf{x \neq y}$$$ and $$$x \oplus y$$$ is a divisor$$$^{\text{∗}}$$$ of either $$$x$$$, $$$y$$$, or both. Here $$$\oplus$$$ is the bitwise XOR operator. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first and only line of each test case contains two space-separated integers $$$x$$$ and $$$m$$$ ($$$1 \le x \le 10^6$$$, $$$1 \le m \le 10^{18}$$$). It is guaranteed that the sum of $$$x$$$ over all test cases does not exceed $$$10^7$$$. Output: For each test case, print a single integer — the number of suitable $$$y$$$. Examples: input: 5 6 9 5 7 2 3 6 4 4 1 output: 3 2 1 1 0 Note: In the first test case, for $$$x = 6$$$, there are $$$3$$$ valid values for $$$y$$$ among the integers from $$$1$$$ to $$$m = 9$$$, and they are $$$4$$$, $$$5$$$, and $$$7$$$. - $$$y = 4$$$ is valid because $$$x \oplus y = 6 \oplus 4 = 2$$$ and $$$2$$$ is a divisor of both $$$x = 6$$$ and $$$y = 4$$$. - $$$y = 5$$$ is valid because $$$x \oplus y = 6 \oplus 5 = 3$$$ and $$$3$$$ is a divisor of $$$x = 6$$$. - $$$y = 7$$$ is valid because $$$x \oplus y = 6 \oplus 7 = 1$$$ and $$$1$$$ is a divisor of both $$$x = 6$$$ and $$$y = 7$$$. In the second test case, for $$$x = 5$$$, there are $$$2$$$ valid values for $$$y$$$ among the integers from $$$1$$$ to $$$m = 7$$$, and they are $$$4$$$ and $$$6$$$. - $$$y = 4$$$ is valid because $$$x \oplus y = 5 \oplus 4 = 1$$$ and $$$1$$$ is a divisor of both $$$x = 5$$$ and $$$y = 4$$$. - $$$y = 6$$$ is valid because $$$x \oplus y = 5 \oplus 6 = 3$$$ and $$$3$$$ is a divisor of $$$y = 6$$$.
2039C2
https://codeforces.com/problemset/problem/2039/C2
Shohag Loves XOR (Hard Version)
1,800
[ "bitmasks", "brute force", "math", "number theory" ]
Div. 1 + Div. 2, Rated, Prizes!
2,000
256
This is the hard version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved. Shohag has two integers $$$x$$$ and $$$m$$$. Help him count the number of integers $$$1 \le y \le m$$$ such that $$$x \oplus y$$$ is divisible$$$^{\text{∗}}$$$ by either $$$x$$$, $$$y$$$, or both. Here $$$\oplus$$$ is the bitwise XOR operator.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first and only line of each test case contains two space-separated integers $$$x$$$ and $$$m$$$ ($$$1 \le x \le 10^6$$$, $$$1 \le m \le 10^{18}$$$). It is guaranteed that the sum of $$$x$$$ over all test cases does not exceed $$$10^7$$$.
For each test case, print a single integer — the number of suitable $$$y$$$.
[ [ "5\n7 10\n2 3\n6 4\n1 6\n4 1", "3\n2\n2\n6\n1" ] ]
In the first test case, for $$$x = 7$$$, there are $$$3$$$ valid values for $$$y$$$ among the integers from $$$1$$$ to $$$m = 10$$$, and they are $$$1$$$, $$$7$$$, and $$$9$$$. - $$$y = 1$$$ is valid because $$$x \oplus y = 7 \oplus 1 = 6$$$ and $$$6$$$ is divisible by $$$y = 1$$$. - $$$y = 7$$$ is valid because $$$x \oplus y = 7 \oplus 7 = 0$$$ and $$$0$$$ is divisible by both $$$x = 7$$$ and $$$y = 7$$$. - $$$y = 9$$$ is valid because $$$x \oplus y = 7 \oplus 9 = 14$$$ and $$$14$$$ is divisible by $$$x = 7$$$.
Title: Shohag Loves XOR (Hard Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the hard version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved. Shohag has two integers $$$x$$$ and $$$m$$$. Help him count the number of integers $$$1 \le y \le m$$$ such that $$$x \oplus y$$$ is divisible$$$^{\text{∗}}$$$ by either $$$x$$$, $$$y$$$, or both. Here $$$\oplus$$$ is the bitwise XOR operator. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first and only line of each test case contains two space-separated integers $$$x$$$ and $$$m$$$ ($$$1 \le x \le 10^6$$$, $$$1 \le m \le 10^{18}$$$). It is guaranteed that the sum of $$$x$$$ over all test cases does not exceed $$$10^7$$$. Output: For each test case, print a single integer — the number of suitable $$$y$$$. Examples: input: 5 7 10 2 3 6 4 1 6 4 1 output: 3 2 2 6 1 Note: In the first test case, for $$$x = 7$$$, there are $$$3$$$ valid values for $$$y$$$ among the integers from $$$1$$$ to $$$m = 10$$$, and they are $$$1$$$, $$$7$$$, and $$$9$$$. - $$$y = 1$$$ is valid because $$$x \oplus y = 7 \oplus 1 = 6$$$ and $$$6$$$ is divisible by $$$y = 1$$$. - $$$y = 7$$$ is valid because $$$x \oplus y = 7 \oplus 7 = 0$$$ and $$$0$$$ is divisible by both $$$x = 7$$$ and $$$y = 7$$$. - $$$y = 9$$$ is valid because $$$x \oplus y = 7 \oplus 9 = 14$$$ and $$$14$$$ is divisible by $$$x = 7$$$.
2039D
https://codeforces.com/problemset/problem/2039/D
Shohag Loves GCD
1,700
[ "constructive algorithms", "greedy", "math", "number theory" ]
Div. 1 + Div. 2, Rated, Prizes!
2,000
256
Shohag has an integer $$$n$$$ and a set $$$S$$$ of $$$m$$$ unique integers. Help him find the lexicographically largest$$$^{\text{∗}}$$$ integer array $$$a_1, a_2, \ldots, a_n$$$ such that $$$a_i \in S$$$ for each $$$1 \le i \le n$$$ and $$$a_{\operatorname{gcd}(i, j)} \neq \operatorname{gcd}(a_i, a_j)$$$$$$^{\text{†}}$$$ is satisfied over all pairs $$$1 \le i \lt j \le n$$$, or state that no such array exists.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m \le n \le 10^5$$$). The second line contains $$$m$$$ unique integers in increasing order, representing the elements of the set $$$S$$$ ($$$1 \le x \le n$$$ for each $$$x \in S$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
For each test case, if there is no solution print $$$-1$$$, otherwise print $$$n$$$ integers — the lexicographically largest integer array that satisfies the conditions.
[ [ "3\n6 3\n3 4 6\n1 1\n1\n2 1\n2", "6 4 4 3 4 3 \n1 \n-1" ] ]
In the first test case, every element in the array belongs to the given set $$$S = \{3, 4, 6\}$$$, and all pairs of indices of the array satisfy the necessary conditions. In particular, for pair $$$(2, 3)$$$, $$$a_{\operatorname{gcd}(2, 3)} = a_1 = 6$$$ and $$$\operatorname{gcd}(a_2, a_3) = \operatorname{gcd}(4, 4) = 4$$$, so they are not equal. There are other arrays that satisfy the conditions as well but this one is the lexicographically largest among them. In the third test case, there is no solution possible because we are only allowed to use $$$a = [2, 2]$$$ but for this array, for pair $$$(1, 2)$$$, $$$a_{\operatorname{gcd}(1, 2)} = a_1 = 2$$$ and $$$\operatorname{gcd}(a_1, a_2) = \operatorname{gcd}(2, 2) = 2$$$, so they are equal which is not allowed!
Title: Shohag Loves GCD time_limit_ms: 2000 memory_limit_mb: 256 Description: Shohag has an integer $$$n$$$ and a set $$$S$$$ of $$$m$$$ unique integers. Help him find the lexicographically largest$$$^{\text{∗}}$$$ integer array $$$a_1, a_2, \ldots, a_n$$$ such that $$$a_i \in S$$$ for each $$$1 \le i \le n$$$ and $$$a_{\operatorname{gcd}(i, j)} \neq \operatorname{gcd}(a_i, a_j)$$$$$$^{\text{†}}$$$ is satisfied over all pairs $$$1 \le i \lt j \le n$$$, or state that no such array exists. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le m \le n \le 10^5$$$). The second line contains $$$m$$$ unique integers in increasing order, representing the elements of the set $$$S$$$ ($$$1 \le x \le n$$$ for each $$$x \in S$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$. Output: For each test case, if there is no solution print $$$-1$$$, otherwise print $$$n$$$ integers — the lexicographically largest integer array that satisfies the conditions. Examples: input: 3 6 3 3 4 6 1 1 1 2 1 2 output: 6 4 4 3 4 3 1 -1 Note: In the first test case, every element in the array belongs to the given set $$$S = \{3, 4, 6\}$$$, and all pairs of indices of the array satisfy the necessary conditions. In particular, for pair $$$(2, 3)$$$, $$$a_{\operatorname{gcd}(2, 3)} = a_1 = 6$$$ and $$$\operatorname{gcd}(a_2, a_3) = \operatorname{gcd}(4, 4) = 4$$$, so they are not equal. There are other arrays that satisfy the conditions as well but this one is the lexicographically largest among them. In the third test case, there is no solution possible because we are only allowed to use $$$a = [2, 2]$$$ but for this array, for pair $$$(1, 2)$$$, $$$a_{\operatorname{gcd}(1, 2)} = a_1 = 2$$$ and $$$\operatorname{gcd}(a_1, a_2) = \operatorname{gcd}(2, 2) = 2$$$, so they are equal which is not allowed!
2039E
https://codeforces.com/problemset/problem/2039/E
Shohag Loves Inversions
2,200
[ "combinatorics", "dp", "implementation", "math" ]
Div. 1 + Div. 2, Rated, Prizes!
2,000
256
Shohag has an array $$$a$$$ of integers. Initially $$$a = [0, 1]$$$. He can repeatedly perform the following operation any number of times: - Let $$$k$$$ be the number of inversions$$$^{\text{∗}}$$$ in the current array $$$a$$$. - Insert $$$k$$$ at any position in $$$a$$$, including the beginning or the end. For example, if $$$a = [4, 6, 2, 4]$$$, then the number of inversions is $$$k = 3$$$. So Shohag can obtain the following arrays after the operation: $$$[\textbf{3}, 4, 6, 2, 4]$$$, $$$[4, \textbf{3}, 6, 2, 4]$$$, $$$[4, 6, \textbf{3}, 2, 4]$$$, $$$[4, 6, 2, \textbf{3}, 4]$$$, and $$$[4, 6, 2, 4, \textbf{3}]$$$. Given an integer $$$n$$$, help Shohag count, modulo $$$998\,244\,353$$$, the number of distinct arrays of length $$$n$$$ that can be obtained after performing the operations.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first and only line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 10^6$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output an integer — the number of possible arrays modulo $$$998\,244\,353$$$.
[ [ "4\n4\n2\n7\n69", "5\n1\n682\n325188814" ] ]
In the first test case, the following $$$5$$$ arrays can be obtained (the inserted inversion count is shown in bold): - $$$[0, 1] \rightarrow [0, \textbf{0}, 1] \rightarrow [0, 0, 1, \textbf{0}]$$$, - $$$[0, 1] \rightarrow [0, \textbf{0}, 1] \rightarrow [0, 0, \textbf{0}, 1]$$$, - $$$[0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [0, 1, 0, \textbf{1}]$$$, - $$$[0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [0, 1, \textbf{1}, 0]$$$, - $$$[0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [\textbf{1}, 0, 1, 0]$$$.
Title: Shohag Loves Inversions time_limit_ms: 2000 memory_limit_mb: 256 Description: Shohag has an array $$$a$$$ of integers. Initially $$$a = [0, 1]$$$. He can repeatedly perform the following operation any number of times: - Let $$$k$$$ be the number of inversions$$$^{\text{∗}}$$$ in the current array $$$a$$$. - Insert $$$k$$$ at any position in $$$a$$$, including the beginning or the end. For example, if $$$a = [4, 6, 2, 4]$$$, then the number of inversions is $$$k = 3$$$. So Shohag can obtain the following arrays after the operation: $$$[\textbf{3}, 4, 6, 2, 4]$$$, $$$[4, \textbf{3}, 6, 2, 4]$$$, $$$[4, 6, \textbf{3}, 2, 4]$$$, $$$[4, 6, 2, \textbf{3}, 4]$$$, and $$$[4, 6, 2, 4, \textbf{3}]$$$. Given an integer $$$n$$$, help Shohag count, modulo $$$998\,244\,353$$$, the number of distinct arrays of length $$$n$$$ that can be obtained after performing the operations. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first and only line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 10^6$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output: For each test case, output an integer — the number of possible arrays modulo $$$998\,244\,353$$$. Examples: input: 4 4 2 7 69 output: 5 1 682 325188814 Note: In the first test case, the following $$$5$$$ arrays can be obtained (the inserted inversion count is shown in bold): - $$$[0, 1] \rightarrow [0, \textbf{0}, 1] \rightarrow [0, 0, 1, \textbf{0}]$$$, - $$$[0, 1] \rightarrow [0, \textbf{0}, 1] \rightarrow [0, 0, \textbf{0}, 1]$$$, - $$$[0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [0, 1, 0, \textbf{1}]$$$, - $$$[0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [0, 1, \textbf{1}, 0]$$$, - $$$[0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [\textbf{1}, 0, 1, 0]$$$.
2043A
https://codeforces.com/problemset/problem/2043/A
Coin Transformation
800
[ "brute force", "math" ]
Div. 2
2,000
512
Initially, you have a coin with value $$$n$$$. You can perform the following operation any number of times (possibly zero): - transform one coin with value $$$x$$$, where $$$x$$$ is greater than $$$3$$$ ($$$x>3$$$), into two coins with value $$$\lfloor \frac{x}{4} \rfloor$$$. What is the maximum number of coins you can have after performing this operation any number of times?
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n \le 10^{18}$$$).
For each test case, print one integer — the maximum number of coins you can have after performing the operation any number of times.
[ [ "4\n1\n5\n16\n1000000000000000000", "1\n2\n4\n536870912" ] ]
In the first example, you have a coin of value $$$1$$$, and you can't do anything with it. So, the answer is $$$1$$$. In the second example, you can transform a coin of value $$$5$$$ into two coins with value $$$1$$$. In the third example, you can transform a coin of value $$$16$$$ into two coins with value $$$4$$$. Each of the resulting coins can be transformed into two coins with value $$$1$$$.
Title: Coin Transformation time_limit_ms: 2000 memory_limit_mb: 512 Description: Initially, you have a coin with value $$$n$$$. You can perform the following operation any number of times (possibly zero): - transform one coin with value $$$x$$$, where $$$x$$$ is greater than $$$3$$$ ($$$x>3$$$), into two coins with value $$$\lfloor \frac{x}{4} \rfloor$$$. What is the maximum number of coins you can have after performing this operation any number of times? Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n \le 10^{18}$$$). Output: For each test case, print one integer — the maximum number of coins you can have after performing the operation any number of times. Examples: input: 4 1 5 16 1000000000000000000 output: 1 2 4 536870912 Note: In the first example, you have a coin of value $$$1$$$, and you can't do anything with it. So, the answer is $$$1$$$. In the second example, you can transform a coin of value $$$5$$$ into two coins with value $$$1$$$. In the third example, you can transform a coin of value $$$16$$$ into two coins with value $$$4$$$. Each of the resulting coins can be transformed into two coins with value $$$1$$$.
2043B
https://codeforces.com/problemset/problem/2043/B
Digits
1,100
[ "math", "number theory" ]
Div. 2
1,000
256
Artem wrote the digit $$$d$$$ on the board exactly $$$n!$$$ times in a row. So, he got the number $$$dddddd \dots ddd$$$ (exactly $$$n!$$$ digits). Now he is curious about which odd digits from $$$1$$$ to $$$9$$$ divide the number written on the board.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The next $$$t$$$ test cases follow. Each test case consists of a single line containing two integers $$$n$$$ and $$$d$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le d \le 9$$$).
For each test case, output the odd digits in ascending order that divide the number written on the board.
[ [ "3\n2 6\n7 1\n8 5", "1 3 \n1 3 7 9 \n1 3 5 7 9" ] ]
The factorial of a positive integer $$$n$$$ ($$$n!$$$) is the product of all integers from $$$1$$$ to $$$n$$$. For example, the factorial of $$$5$$$ is $$$1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 = 120$$$.
Title: Digits time_limit_ms: 1000 memory_limit_mb: 256 Description: Artem wrote the digit $$$d$$$ on the board exactly $$$n!$$$ times in a row. So, he got the number $$$dddddd \dots ddd$$$ (exactly $$$n!$$$ digits). Now he is curious about which odd digits from $$$1$$$ to $$$9$$$ divide the number written on the board. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The next $$$t$$$ test cases follow. Each test case consists of a single line containing two integers $$$n$$$ and $$$d$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le d \le 9$$$). Output: For each test case, output the odd digits in ascending order that divide the number written on the board. Examples: input: 3 2 6 7 1 8 5 output: 1 3 1 3 7 9 1 3 5 7 9 Note: The factorial of a positive integer $$$n$$$ ($$$n!$$$) is the product of all integers from $$$1$$$ to $$$n$$$. For example, the factorial of $$$5$$$ is $$$1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 = 120$$$.
2043C
https://codeforces.com/problemset/problem/2043/C
Sums on Segments
1,600
[ "brute force", "dp", "binary search", "math", "data structures", "greedy" ]
Div. 2
1,000
256
You are given an array $$$a$$$ of $$$n$$$ integers, where all elements except for at most one are equal to $$$-1$$$ or $$$1$$$. The remaining element $$$x$$$ satisfies $$$-10^9 \le x \le 10^9$$$. Find all possible sums of subarrays of $$$a$$$, including the empty subarray, whose sum is defined as $$$0$$$. In other words, find all integers $$$x$$$ such that the array $$$a$$$ has at least one subarray (possibly empty) with sum equal to $$$x$$$. A subarray is a contiguous subsegment of an array. Output these sums in ascending order. Each sum should be printed only once, even if it is achieved by multiple subarrays.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then, $$$t$$$ test cases follow. Each test case consists of two lines: - The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array. - The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the elements of the array $$$a$$$. In the array $$$a$$$, there is at most one element that is neither $$$1$$$ nor $$$-1$$$. Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output two lines: - In the first line, print a single integer — the number of distinct subarray sums. - In the second line, print these sums in ascending order. Each sum should be printed only once, even if it is produced by multiple subarrays.
[ [ "5\n5\n1 -1 10 1 1\n5\n-1 -1 -1 -1 -1\n2\n-1 2\n2\n7 1\n3\n1 4 -1", "8\n-1 0 1 2 9 10 11 12 \n6\n-5 -4 -3 -2 -1 0 \n4\n-1 0 1 2 \n4\n0 1 7 8 \n6\n-1 0 1 3 4 5" ] ]
Let's define $$$a[i,j]$$$ as the subarray of $$$a$$$ from position $$$i$$$ to position $$$j$$$. Consider the first test case of the example: - $$$-1$$$ is produced by $$$a[2,2]$$$; - $$$0$$$ is produced by the empty subarray; - $$$1$$$ is produced by $$$a[4,4]$$$; - $$$2$$$ is produced by $$$a[4,5]$$$; - $$$9$$$ is produced by $$$a[2,3]$$$; - $$$10$$$ is produced by $$$a[1,3]$$$; - $$$11$$$ is produced by $$$a[3,4]$$$; - $$$12$$$ is produced by $$$a[3,5]$$$.
Title: Sums on Segments time_limit_ms: 1000 memory_limit_mb: 256 Description: You are given an array $$$a$$$ of $$$n$$$ integers, where all elements except for at most one are equal to $$$-1$$$ or $$$1$$$. The remaining element $$$x$$$ satisfies $$$-10^9 \le x \le 10^9$$$. Find all possible sums of subarrays of $$$a$$$, including the empty subarray, whose sum is defined as $$$0$$$. In other words, find all integers $$$x$$$ such that the array $$$a$$$ has at least one subarray (possibly empty) with sum equal to $$$x$$$. A subarray is a contiguous subsegment of an array. Output these sums in ascending order. Each sum should be printed only once, even if it is achieved by multiple subarrays. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then, $$$t$$$ test cases follow. Each test case consists of two lines: - The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array. - The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the elements of the array $$$a$$$. In the array $$$a$$$, there is at most one element that is neither $$$1$$$ nor $$$-1$$$. Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: For each test case, output two lines: - In the first line, print a single integer — the number of distinct subarray sums. - In the second line, print these sums in ascending order. Each sum should be printed only once, even if it is produced by multiple subarrays. Examples: input: 5 5 1 -1 10 1 1 5 -1 -1 -1 -1 -1 2 -1 2 2 7 1 3 1 4 -1 output: 8 -1 0 1 2 9 10 11 12 6 -5 -4 -3 -2 -1 0 4 -1 0 1 2 4 0 1 7 8 6 -1 0 1 3 4 5 Note: Let's define $$$a[i,j]$$$ as the subarray of $$$a$$$ from position $$$i$$$ to position $$$j$$$. Consider the first test case of the example: - $$$-1$$$ is produced by $$$a[2,2]$$$; - $$$0$$$ is produced by the empty subarray; - $$$1$$$ is produced by $$$a[4,4]$$$; - $$$2$$$ is produced by $$$a[4,5]$$$; - $$$9$$$ is produced by $$$a[2,3]$$$; - $$$10$$$ is produced by $$$a[1,3]$$$; - $$$11$$$ is produced by $$$a[3,4]$$$; - $$$12$$$ is produced by $$$a[3,5]$$$.
2043D
https://codeforces.com/problemset/problem/2043/D
Problem about GCD
1,800
[ "brute force", "flows", "number theory", "math" ]
Div. 2
1,000
256
Given three integers $$$l$$$, $$$r$$$, and $$$G$$$, find two integers $$$A$$$ and $$$B$$$ ($$$l \le A \le B \le r$$$) such that their greatest common divisor (GCD) equals $$$G$$$ and the distance $$$|A - B|$$$ is maximized. If there are multiple such pairs, choose the one where $$$A$$$ is minimized. If no such pairs exist, output "-1 -1".
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. Then, $$$t$$$ test cases follow. Each test case consists of a single line containing three integers $$$l, r, G$$$ ($$$1 \le l \le r \le 10^{18}$$$; $$$1 \le G \le 10^{18}$$$) — the range boundaries and the required GCD.
For each test case, output two integers $$$A$$$ and $$$B$$$ — the solution to the problem, or "-1 -1" if no such pair exists.
[ [ "4\n4 8 2\n4 8 3\n4 8 4\n5 7 6", "4 6\n-1 -1\n4 8\n6 6" ] ]
Title: Problem about GCD time_limit_ms: 1000 memory_limit_mb: 256 Description: Given three integers $$$l$$$, $$$r$$$, and $$$G$$$, find two integers $$$A$$$ and $$$B$$$ ($$$l \le A \le B \le r$$$) such that their greatest common divisor (GCD) equals $$$G$$$ and the distance $$$|A - B|$$$ is maximized. If there are multiple such pairs, choose the one where $$$A$$$ is minimized. If no such pairs exist, output "-1 -1". Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. Then, $$$t$$$ test cases follow. Each test case consists of a single line containing three integers $$$l, r, G$$$ ($$$1 \le l \le r \le 10^{18}$$$; $$$1 \le G \le 10^{18}$$$) — the range boundaries and the required GCD. Output: For each test case, output two integers $$$A$$$ and $$$B$$$ — the solution to the problem, or "-1 -1" if no such pair exists. Examples: input: 4 4 8 2 4 8 3 4 8 4 5 7 6 output: 4 6 -1 -1 4 8 6 6 Note:
2043E
https://codeforces.com/problemset/problem/2043/E
Matrix Transformation
2,300
[ "brute force", "graphs", "greedy", "implementation", "data structures", "dfs and similar", "bitmasks" ]
Div. 2
1,000
256
You are given two matrices $$$A$$$ and $$$B$$$ of size $$$n \times m$$$, filled with integers between $$$0$$$ and $$$10^9$$$. You can perform the following operations on matrix $$$A$$$ in any order and any number of times: - &=: choose two integers $$$i$$$ and $$$x$$$ ($$$1 \le i \le n$$$, $$$x \ge 0$$$) and replace each element in row $$$i$$$ with the result of the bitwise AND operation between $$$x$$$ and that element. Formally, for every $$$j \in [1, m]$$$, the element $$$A_{i,j}$$$ is replaced with $$$A_{i,j} \text{ & } x$$$; - |=: choose two integers $$$j$$$ and $$$x$$$ ($$$1 \le j \le m$$$, $$$x \ge 0$$$) and replace each element in column $$$j$$$ with the result of the bitwise OR operation between $$$x$$$ and that element. Formally, for every $$$i \in [1, n]$$$, the element $$$A_{i,j}$$$ is replaced with $$$A_{i,j} \text{ | } x$$$. The value of $$$x$$$ may be chosen differently for different operations. Determine whether it is possible to transform matrix $$$A$$$ into matrix $$$B$$$ using the given operations any number of times (including zero).
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then, $$$t$$$ test cases follow. Each test case is given as follows: - the first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^3$$$; $$$n \cdot m \le 10^3$$$) — the dimensions of the matrices $$$A$$$ and $$$B$$$; - the following $$$n$$$ lines describe the matrix $$$A$$$, where the $$$i$$$-th line contains $$$m$$$ integers $$$A_{i,1}, A_{i,2}, \dots, A_{i,m}$$$ ($$$0 \le A_{i,j} \le 10^9$$$); - the following $$$n$$$ lines describe the matrix $$$B$$$, where the $$$i$$$-th line contains $$$m$$$ integers $$$B_{i,1}, B_{i,2}, \dots, B_{i,m}$$$ ($$$0 \le B_{i,j} \le 10^9$$$).
For each test case, output Yes if it is possible to transform the matrix $$$A$$$ into the matrix $$$B$$$; otherwise, output No. Each letter can be output in any case, upper or lower.
[ [ "4\n1 1\n12\n13\n2 2\n10 10\n42 42\n21 21\n21 21\n2 2\n74 10\n42 106\n21 85\n85 21\n2 4\n1 2 3 4\n5 6 7 8\n3 2 3 4\n1 0 1 0", "Yes\nYes\nNo\nYes" ] ]
Let's consider the second set of input data and show a sequence of operations that transforms matrix $$$A$$$ into matrix $$$B$$$: Initially, the matrix looks like this: $$$\begin{bmatrix} 10&10\\ 42&42\\ \end{bmatrix}$$$ Apply an operation of the first type with parameters $$$i = 1$$$ and $$$x = 0$$$. As a result, we get the matrix: $$$\begin{bmatrix} 0&0\\ 42&42\\ \end{bmatrix}$$$ Apply an operation of the first type with parameters $$$i = 2$$$ and $$$x = 0$$$. As a result, we get the matrix: $$$\begin{bmatrix} 0&0\\ 0&0\\ \end{bmatrix}$$$ Apply an operation of the second type with parameters $$$j = 1$$$ and $$$x = 21$$$. As a result, we get the matrix: $$$\begin{bmatrix} 21&0\\ 21&0\\ \end{bmatrix}$$$ Apply an operation of the second type with parameters $$$j = 2$$$ and $$$x = 21$$$. As a result, we get the matrix: $$$\begin{bmatrix} 21&21\\ 21&21\\ \end{bmatrix}$$$ Thus, we have transformed matrix $$$A$$$ into matrix $$$B$$$.
Title: Matrix Transformation time_limit_ms: 1000 memory_limit_mb: 256 Description: You are given two matrices $$$A$$$ and $$$B$$$ of size $$$n \times m$$$, filled with integers between $$$0$$$ and $$$10^9$$$. You can perform the following operations on matrix $$$A$$$ in any order and any number of times: - &=: choose two integers $$$i$$$ and $$$x$$$ ($$$1 \le i \le n$$$, $$$x \ge 0$$$) and replace each element in row $$$i$$$ with the result of the bitwise AND operation between $$$x$$$ and that element. Formally, for every $$$j \in [1, m]$$$, the element $$$A_{i,j}$$$ is replaced with $$$A_{i,j} \text{ & } x$$$; - |=: choose two integers $$$j$$$ and $$$x$$$ ($$$1 \le j \le m$$$, $$$x \ge 0$$$) and replace each element in column $$$j$$$ with the result of the bitwise OR operation between $$$x$$$ and that element. Formally, for every $$$i \in [1, n]$$$, the element $$$A_{i,j}$$$ is replaced with $$$A_{i,j} \text{ | } x$$$. The value of $$$x$$$ may be chosen differently for different operations. Determine whether it is possible to transform matrix $$$A$$$ into matrix $$$B$$$ using the given operations any number of times (including zero). Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then, $$$t$$$ test cases follow. Each test case is given as follows: - the first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^3$$$; $$$n \cdot m \le 10^3$$$) — the dimensions of the matrices $$$A$$$ and $$$B$$$; - the following $$$n$$$ lines describe the matrix $$$A$$$, where the $$$i$$$-th line contains $$$m$$$ integers $$$A_{i,1}, A_{i,2}, \dots, A_{i,m}$$$ ($$$0 \le A_{i,j} \le 10^9$$$); - the following $$$n$$$ lines describe the matrix $$$B$$$, where the $$$i$$$-th line contains $$$m$$$ integers $$$B_{i,1}, B_{i,2}, \dots, B_{i,m}$$$ ($$$0 \le B_{i,j} \le 10^9$$$). Output: For each test case, output Yes if it is possible to transform the matrix $$$A$$$ into the matrix $$$B$$$; otherwise, output No. Each letter can be output in any case, upper or lower. Examples: input: 4 1 1 12 13 2 2 10 10 42 42 21 21 21 21 2 2 74 10 42 106 21 85 85 21 2 4 1 2 3 4 5 6 7 8 3 2 3 4 1 0 1 0 output: Yes Yes No Yes Note: Let's consider the second set of input data and show a sequence of operations that transforms matrix $$$A$$$ into matrix $$$B$$$: Initially, the matrix looks like this: $$$\begin{bmatrix} 10&10\\ 42&42\\ \end{bmatrix}$$$ Apply an operation of the first type with parameters $$$i = 1$$$ and $$$x = 0$$$. As a result, we get the matrix: $$$\begin{bmatrix} 0&0\\ 42&42\\ \end{bmatrix}$$$ Apply an operation of the first type with parameters $$$i = 2$$$ and $$$x = 0$$$. As a result, we get the matrix: $$$\begin{bmatrix} 0&0\\ 0&0\\ \end{bmatrix}$$$ Apply an operation of the second type with parameters $$$j = 1$$$ and $$$x = 21$$$. As a result, we get the matrix: $$$\begin{bmatrix} 21&0\\ 21&0\\ \end{bmatrix}$$$ Apply an operation of the second type with parameters $$$j = 2$$$ and $$$x = 21$$$. As a result, we get the matrix: $$$\begin{bmatrix} 21&21\\ 21&21\\ \end{bmatrix}$$$ Thus, we have transformed matrix $$$A$$$ into matrix $$$B$$$.
2021C1
https://codeforces.com/problemset/problem/2021/C1
Adjust The Presentation (Easy Version)
1,300
[ "constructive algorithms", "greedy" ]
Div. 2, based on COMPFEST 16 - Final Round
2,000
256
This is the easy version of the problem. In the two versions, the constraints on $$$q$$$ and the time limit are different. In this version, $$$q=0$$$. You can make hacks only if all the versions of the problem are solved. A team consisting of $$$n$$$ members, numbered from $$$1$$$ to $$$n$$$, is set to present a slide show at a large meeting. The slide show contains $$$m$$$ slides. There is an array $$$a$$$ of length $$$n$$$. Initially, the members are standing in a line in the order of $$$a_1, a_2, \ldots, a_n$$$ from front to back. The slide show will be presented in order from slide $$$1$$$ to slide $$$m$$$. Each section will be presented by the member at the front of the line. After each slide is presented, you can move the member at the front of the line to any position in the lineup (without changing the order of the rest of the members). For example, suppose the line of members is $$$[\color{red}{3},1,2,4]$$$. After member $$$3$$$ presents the current slide, you can change the line of members into either $$$[\color{red}{3},1,2,4]$$$, $$$[1,\color{red}{3},2,4]$$$, $$$[1,2,\color{red}{3},4]$$$ or $$$[1,2,4,\color{red}{3}]$$$. There is also an array $$$b$$$ of length $$$m$$$. The slide show is considered good if it is possible to make member $$$b_i$$$ present slide $$$i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$ under these constraints. However, your annoying boss wants to make $$$q$$$ updates to the array $$$b$$$. In the $$$i$$$-th update, he will choose a slide $$$s_i$$$ and a member $$$t_i$$$ and set $$$b_{s_i} := t_i$$$. Note that these updates are persistent, that is changes made to the array $$$b$$$ will apply when processing future updates. For each of the $$$q+1$$$ states of array $$$b$$$, the initial state and after each of the $$$q$$$ updates, determine if the slideshow is good.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$; $$$q=0$$$) — the number of members, the number of sections and the number of updates. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$) — the initial order of the members from front to back. It is guaranteed that each integer from $$$1$$$ to $$$n$$$ appears exactly once in $$$a$$$. The third line of each test case contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$1 \le b_i \le n$$$) — the members who should present each section. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$ respectively.
For each test case, output $$$q+1$$$ lines corresponding to the $$$q+1$$$ states of the array $$$b$$$. Output "YA" if the slide show is good, and "TIDAK" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yA", "Ya", "ya", and "YA" will be recognized as positive responses.
[ [ "3\n4 2 0\n1 2 3 4\n1 1\n3 6 0\n1 2 3\n1 1 2 3 3 2\n4 6 0\n3 1 4 2\n3 1 1 2 3 4", "YA\nYA\nTIDAK" ] ]
For the first test case, you do not need to move the members as both slides are presented by member $$$1$$$, who is already at the front of the line. For the second test case, the following is a possible way to move members so that the presentation is good: 1. $$$[1,2,3]$$$, do not move member $$$1$$$. 2. $$$[1,2,3]$$$, move member $$$1$$$ after member $$$3$$$. 3. $$$[2,3,1]$$$, move member $$$2$$$ after member $$$3$$$. 4. $$$[3,2,1]$$$, do not move member $$$3$$$. 5. $$$[3,2,1]$$$, move member $$$3$$$ after member $$$1$$$. 6. $$$[2,1,3]$$$, do not move member $$$2$$$.
Title: Adjust The Presentation (Easy Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the easy version of the problem. In the two versions, the constraints on $$$q$$$ and the time limit are different. In this version, $$$q=0$$$. You can make hacks only if all the versions of the problem are solved. A team consisting of $$$n$$$ members, numbered from $$$1$$$ to $$$n$$$, is set to present a slide show at a large meeting. The slide show contains $$$m$$$ slides. There is an array $$$a$$$ of length $$$n$$$. Initially, the members are standing in a line in the order of $$$a_1, a_2, \ldots, a_n$$$ from front to back. The slide show will be presented in order from slide $$$1$$$ to slide $$$m$$$. Each section will be presented by the member at the front of the line. After each slide is presented, you can move the member at the front of the line to any position in the lineup (without changing the order of the rest of the members). For example, suppose the line of members is $$$[\color{red}{3},1,2,4]$$$. After member $$$3$$$ presents the current slide, you can change the line of members into either $$$[\color{red}{3},1,2,4]$$$, $$$[1,\color{red}{3},2,4]$$$, $$$[1,2,\color{red}{3},4]$$$ or $$$[1,2,4,\color{red}{3}]$$$. There is also an array $$$b$$$ of length $$$m$$$. The slide show is considered good if it is possible to make member $$$b_i$$$ present slide $$$i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$ under these constraints. However, your annoying boss wants to make $$$q$$$ updates to the array $$$b$$$. In the $$$i$$$-th update, he will choose a slide $$$s_i$$$ and a member $$$t_i$$$ and set $$$b_{s_i} := t_i$$$. Note that these updates are persistent, that is changes made to the array $$$b$$$ will apply when processing future updates. For each of the $$$q+1$$$ states of array $$$b$$$, the initial state and after each of the $$$q$$$ updates, determine if the slideshow is good. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$; $$$q=0$$$) — the number of members, the number of sections and the number of updates. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$) — the initial order of the members from front to back. It is guaranteed that each integer from $$$1$$$ to $$$n$$$ appears exactly once in $$$a$$$. The third line of each test case contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$1 \le b_i \le n$$$) — the members who should present each section. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$ respectively. Output: For each test case, output $$$q+1$$$ lines corresponding to the $$$q+1$$$ states of the array $$$b$$$. Output "YA" if the slide show is good, and "TIDAK" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yA", "Ya", "ya", and "YA" will be recognized as positive responses. Examples: input: 3 4 2 0 1 2 3 4 1 1 3 6 0 1 2 3 1 1 2 3 3 2 4 6 0 3 1 4 2 3 1 1 2 3 4 output: YA YA TIDAK Note: For the first test case, you do not need to move the members as both slides are presented by member $$$1$$$, who is already at the front of the line. For the second test case, the following is a possible way to move members so that the presentation is good: 1. $$$[1,2,3]$$$, do not move member $$$1$$$. 2. $$$[1,2,3]$$$, move member $$$1$$$ after member $$$3$$$. 3. $$$[2,3,1]$$$, move member $$$2$$$ after member $$$3$$$. 4. $$$[3,2,1]$$$, do not move member $$$3$$$. 5. $$$[3,2,1]$$$, move member $$$3$$$ after member $$$1$$$. 6. $$$[2,1,3]$$$, do not move member $$$2$$$.
2021C2
https://codeforces.com/problemset/problem/2021/C2
Adjust The Presentation (Hard Version)
1,900
[ "implementation", "data structures", "sortings", "constructive algorithms", "greedy" ]
Div. 2, based on COMPFEST 16 - Final Round
5,000
256
This is the hard version of the problem. In the two versions, the constraints on $$$q$$$ and the time limit are different. In this version, $$$0 \leq q \leq 2 \cdot 10^5$$$. You can make hacks only if all the versions of the problem are solved. A team consisting of $$$n$$$ members, numbered from $$$1$$$ to $$$n$$$, is set to present a slide show at a large meeting. The slide show contains $$$m$$$ slides. There is an array $$$a$$$ of length $$$n$$$. Initially, the members are standing in a line in the order of $$$a_1, a_2, \ldots, a_n$$$ from front to back. The slide show will be presented in order from slide $$$1$$$ to slide $$$m$$$. Each section will be presented by the member at the front of the line. After each slide is presented, you can move the member at the front of the line to any position in the lineup (without changing the order of the rest of the members). For example, suppose the line of members is $$$[\color{red}{3},1,2,4]$$$. After member $$$3$$$ presents the current slide, you can change the line of members into either $$$[\color{red}{3},1,2,4]$$$, $$$[1,\color{red}{3},2,4]$$$, $$$[1,2,\color{red}{3},4]$$$ or $$$[1,2,4,\color{red}{3}]$$$. There is also an array $$$b$$$ of length $$$m$$$. The slide show is considered good if it is possible to make member $$$b_i$$$ present slide $$$i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$ under these constraints. However, your annoying boss wants to make $$$q$$$ updates to the array $$$b$$$. In the $$$i$$$-th update, he will choose a slide $$$s_i$$$ and a member $$$t_i$$$ and set $$$b_{s_i} := t_i$$$. Note that these updates are persistent, that is changes made to the array $$$b$$$ will apply when processing future updates. For each of the $$$q+1$$$ states of array $$$b$$$, the initial state and after each of the $$$q$$$ updates, determine if the slideshow is good.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$; $$$0 \leq q \leq 2 \cdot 10^5$$$) — the number of members and the number of sections. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$) — the initial order of the members from front to back. It is guaranteed that each integer from $$$1$$$ to $$$n$$$ appears exactly once in $$$a$$$. The third line of each test case contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$1 \le b_i \le n$$$) — the members who should present each section. Each of the next $$$q$$$ lines contains two integers $$$s_i$$$ and $$$t_i$$$ ($$$1 \le s_i \le m$$$, $$$1 \le t_i \le n$$$) — parameters of an update. It is guaranteed that the sum of $$$n$$$, the sum of $$$m$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$ respectively.
For each test case, output $$$q+1$$$ lines corresponding to the $$$q+1$$$ states of the array $$$b$$$. Output "YA" if the slide show is good, and "TIDAK" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yA", "Ya", "ya", and "YA" will be recognized as positive responses.
[ [ "3\n4 2 2\n1 2 3 4\n1 1\n1 2\n1 1\n3 6 2\n1 2 3\n1 1 2 3 3 2\n3 3\n2 2\n4 6 2\n3 1 4 2\n3 1 1 2 3 4\n3 4\n4 2", "YA\nTIDAK\nYA\nYA\nTIDAK\nYA\nTIDAK\nYA\nYA" ] ]
For the first test case, you do not need to move the members as both slides are presented by member $$$1$$$, who is already at the front of the line. After that, set $$$b_1 := 2$$$, now slide $$$1$$$ must be presented by member $$$2$$$ which is impossible as member $$$1$$$ will present slide $$$1$$$ first. Then, set $$$b_1 = 1$$$, the $$$b$$$ is the same as the initial $$$b$$$, making a good presentation possible.
Title: Adjust The Presentation (Hard Version) time_limit_ms: 5000 memory_limit_mb: 256 Description: This is the hard version of the problem. In the two versions, the constraints on $$$q$$$ and the time limit are different. In this version, $$$0 \leq q \leq 2 \cdot 10^5$$$. You can make hacks only if all the versions of the problem are solved. A team consisting of $$$n$$$ members, numbered from $$$1$$$ to $$$n$$$, is set to present a slide show at a large meeting. The slide show contains $$$m$$$ slides. There is an array $$$a$$$ of length $$$n$$$. Initially, the members are standing in a line in the order of $$$a_1, a_2, \ldots, a_n$$$ from front to back. The slide show will be presented in order from slide $$$1$$$ to slide $$$m$$$. Each section will be presented by the member at the front of the line. After each slide is presented, you can move the member at the front of the line to any position in the lineup (without changing the order of the rest of the members). For example, suppose the line of members is $$$[\color{red}{3},1,2,4]$$$. After member $$$3$$$ presents the current slide, you can change the line of members into either $$$[\color{red}{3},1,2,4]$$$, $$$[1,\color{red}{3},2,4]$$$, $$$[1,2,\color{red}{3},4]$$$ or $$$[1,2,4,\color{red}{3}]$$$. There is also an array $$$b$$$ of length $$$m$$$. The slide show is considered good if it is possible to make member $$$b_i$$$ present slide $$$i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$ under these constraints. However, your annoying boss wants to make $$$q$$$ updates to the array $$$b$$$. In the $$$i$$$-th update, he will choose a slide $$$s_i$$$ and a member $$$t_i$$$ and set $$$b_{s_i} := t_i$$$. Note that these updates are persistent, that is changes made to the array $$$b$$$ will apply when processing future updates. For each of the $$$q+1$$$ states of array $$$b$$$, the initial state and after each of the $$$q$$$ updates, determine if the slideshow is good. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$; $$$0 \leq q \leq 2 \cdot 10^5$$$) — the number of members and the number of sections. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$) — the initial order of the members from front to back. It is guaranteed that each integer from $$$1$$$ to $$$n$$$ appears exactly once in $$$a$$$. The third line of each test case contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$1 \le b_i \le n$$$) — the members who should present each section. Each of the next $$$q$$$ lines contains two integers $$$s_i$$$ and $$$t_i$$$ ($$$1 \le s_i \le m$$$, $$$1 \le t_i \le n$$$) — parameters of an update. It is guaranteed that the sum of $$$n$$$, the sum of $$$m$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$ respectively. Output: For each test case, output $$$q+1$$$ lines corresponding to the $$$q+1$$$ states of the array $$$b$$$. Output "YA" if the slide show is good, and "TIDAK" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yA", "Ya", "ya", and "YA" will be recognized as positive responses. Examples: input: 3 4 2 2 1 2 3 4 1 1 1 2 1 1 3 6 2 1 2 3 1 1 2 3 3 2 3 3 2 2 4 6 2 3 1 4 2 3 1 1 2 3 4 3 4 4 2 output: YA TIDAK YA YA TIDAK YA TIDAK YA YA Note: For the first test case, you do not need to move the members as both slides are presented by member $$$1$$$, who is already at the front of the line. After that, set $$$b_1 := 2$$$, now slide $$$1$$$ must be presented by member $$$2$$$ which is impossible as member $$$1$$$ will present slide $$$1$$$ first. Then, set $$$b_1 = 1$$$, the $$$b$$$ is the same as the initial $$$b$$$, making a good presentation possible.
2010A
https://codeforces.com/problemset/problem/2010/A
Alternating Sum of Numbers
800
[ "brute force", "implementation", "math" ]
Div. 3
2,000
256
You are given a sequence of integers. Output the alternating sum of this sequence. In other words, output $$$a_1 - a_2 + a_3 - a_4 + a_5 - \dots$$$. That is, the signs of plus and minus alternate, starting with a plus.
The first line of the test contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then follow $$$t$$$ test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of the sequence. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$).
Output $$$t$$$ lines. For each test case, output the required alternating sum of the numbers.
[ [ "4\n4\n1 2 3 17\n1\n100\n2\n100 100\n5\n3 1 4 1 5", "-15\n100\n0\n10" ] ]
Title: Alternating Sum of Numbers time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a sequence of integers. Output the alternating sum of this sequence. In other words, output $$$a_1 - a_2 + a_3 - a_4 + a_5 - \dots$$$. That is, the signs of plus and minus alternate, starting with a plus. Input: The first line of the test contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then follow $$$t$$$ test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of the sequence. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). Output: Output $$$t$$$ lines. For each test case, output the required alternating sum of the numbers. Examples: input: 4 4 1 2 3 17 1 100 2 100 100 5 3 1 4 1 5 output: -15 100 0 10 Note:
2010B
https://codeforces.com/problemset/problem/2010/B
Three Brothers
800
[ "brute force", "implementation", "math" ]
Div. 3
1,000
256
Three brothers agreed to meet. Let's number the brothers as follows: the oldest brother is number 1, the middle brother is number 2, and the youngest brother is number 3. When it was time for the meeting, one of the brothers was late. Given the numbers of the two brothers who arrived on time, you need to determine the number of the brother who was late.
The first line of input contains two different integers a and b (1 ≤ a, b ≤ 3, a ≠ b) — the numbers of the brothers who arrived on time. The numbers are given in arbitrary order.
Output a single integer — the number of the brother who was late to the meeting.
[ [ "3 1", "2" ] ]
Title: Three Brothers time_limit_ms: 1000 memory_limit_mb: 256 Description: Three brothers agreed to meet. Let's number the brothers as follows: the oldest brother is number 1, the middle brother is number 2, and the youngest brother is number 3. When it was time for the meeting, one of the brothers was late. Given the numbers of the two brothers who arrived on time, you need to determine the number of the brother who was late. Input: The first line of input contains two different integers a and b (1 ≤ a, b ≤ 3, a ≠ b) — the numbers of the brothers who arrived on time. The numbers are given in arbitrary order. Output: Output a single integer — the number of the brother who was late to the meeting. Examples: input: 3 1 output: 2 Note:
2047A
https://codeforces.com/problemset/problem/2047/A
Alyona and a Square Jigsaw Puzzle
800
[ "implementation", "math" ]
Div. 2
1,000
256
Alyona assembles an unusual square Jigsaw Puzzle. She does so in $$$n$$$ days in the following manner: - On the first day, she starts by placing the central piece in the center of the table. - On each day after the first one, she places a certain number of pieces around the central piece in clockwise order, always finishing each square layer completely before starting a new one. For example, she places the first $$$14$$$ pieces in the following order: The colors denote the layers. The third layer is still unfinished. Alyona is happy if at the end of the day the assembled part of the puzzle does not have any started but unfinished layers. Given the number of pieces she assembles on each day, find the number of days Alyona is happy on.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows. The first line contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$), the number of days. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 100$$$, $$$a_1 = 1$$$), where $$$a_i$$$ is the number of pieces Alyona assembles on the $$$i$$$-th day. It is guaranteed in each test case that at the end of the $$$n$$$ days, there are no unfinished layers.
For each test case, print a single integer: the number of days when Alyona is happy.
[ [ "5\n1\n1\n2\n1 8\n5\n1 3 2 1 2\n7\n1 2 1 10 2 7 2\n14\n1 10 10 100 1 1 10 1 10 2 10 2 10 1", "1\n2\n2\n2\n3" ] ]
In the first test case, in the only day Alyona finishes the only layer. In the second test case, on the first day, Alyona finishes the first layer, and on the second day, she finishes the second layer. In the third test case, she finishes the second layer in a few days. In the fourth test case, she finishes the second layer and immediately starts the next one on the same day, therefore, she is not happy on that day. She is only happy on the first and last days. In the fifth test case, Alyona is happy on the first, fourth, and last days.
Title: Alyona and a Square Jigsaw Puzzle time_limit_ms: 1000 memory_limit_mb: 256 Description: Alyona assembles an unusual square Jigsaw Puzzle. She does so in $$$n$$$ days in the following manner: - On the first day, she starts by placing the central piece in the center of the table. - On each day after the first one, she places a certain number of pieces around the central piece in clockwise order, always finishing each square layer completely before starting a new one. For example, she places the first $$$14$$$ pieces in the following order: The colors denote the layers. The third layer is still unfinished. Alyona is happy if at the end of the day the assembled part of the puzzle does not have any started but unfinished layers. Given the number of pieces she assembles on each day, find the number of days Alyona is happy on. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows. The first line contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$), the number of days. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 100$$$, $$$a_1 = 1$$$), where $$$a_i$$$ is the number of pieces Alyona assembles on the $$$i$$$-th day. It is guaranteed in each test case that at the end of the $$$n$$$ days, there are no unfinished layers. Output: For each test case, print a single integer: the number of days when Alyona is happy. Examples: input: 5 1 1 2 1 8 5 1 3 2 1 2 7 1 2 1 10 2 7 2 14 1 10 10 100 1 1 10 1 10 2 10 2 10 1 output: 1 2 2 2 3 Note: In the first test case, in the only day Alyona finishes the only layer. In the second test case, on the first day, Alyona finishes the first layer, and on the second day, she finishes the second layer. In the third test case, she finishes the second layer in a few days. In the fourth test case, she finishes the second layer and immediately starts the next one on the same day, therefore, she is not happy on that day. She is only happy on the first and last days. In the fifth test case, Alyona is happy on the first, fourth, and last days.
2053A
https://codeforces.com/problemset/problem/2053/A
Tender Carpenter
800
[ "greedy", "geometry", "dp", "math" ]
Good Bye 2024: 2025 is NEAR
1,000
256
In his dream, Cocoly would go on a long holiday with no worries around him. So he would try out for many new things, such as... being a carpenter. To learn it well, Cocoly decides to become an apprentice of Master, but in front of him lies a hard task waiting for him to solve. Cocoly is given an array $$$a_1, a_2,\ldots, a_n$$$. Master calls a set of integers $$$S$$$ stable if and only if, for any possible $$$u$$$, $$$v$$$, and $$$w$$$ from the set $$$S$$$ (note that $$$u$$$, $$$v$$$, and $$$w$$$ do not necessarily have to be pairwise distinct), sticks of length $$$u$$$, $$$v$$$, and $$$w$$$ can form a non-degenerate triangle$$$^{\text{∗}}$$$. Cocoly is asked to partition the array $$$a$$$ into several (possibly, $$$1$$$ or $$$n$$$) non-empty continuous subsegments$$$^{\text{†}}$$$, such that: for each of the subsegments, the set containing all the elements in it is stable. Master wants Cocoly to partition $$$a$$$ in at least two different$$$^{\text{‡}}$$$ ways. You have to help him determine whether it is possible.
Each test contains multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 200$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 200$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the elements in the array $$$a$$$.
For each test case, print $$$\texttt{YES}$$$ if there are at least two ways to partition $$$a$$$, and $$$\texttt{NO}$$$ otherwise. You can output the answer in any case (upper or lower). For example, the strings $$$\texttt{yEs}$$$, $$$\texttt{yes}$$$, $$$\texttt{Yes}$$$, and $$$\texttt{YES}$$$ will be recognized as positive responses.
[ [ "5\n4\n2 3 5 7\n4\n115 9 2 28\n5\n8 4 1 6 2\n6\n1 5 4 1 4 7\n2\n100000 100000", "YES\nNO\nNO\nYES\nYES" ] ]
In the first test case, here are two possible partitions: - $$$[2, 3], [5, 7]$$$, since $$$[2, 3]$$$ is stable because sticks of lengths $$$(2, 2, 2), (2, 2, 3), (2, 3, 3), (3, 3, 3)$$$ respectively can all form non-degenerate triangles. $$$[5, 7]$$$ is stable because sticks of lengths $$$(5, 5, 5), (5, 5, 7), (5, 7, 7), (7, 7, 7)$$$ respectively can all form non-degenerate triangles. - and $$$[2], [3, 5], [7]$$$, since $$$[2]$$$ is stable because sticks of lengths $$$(2, 2, 2)$$$ respectively can form a non-degenerate triangle. $$$[3, 5]$$$ is stable because sticks of lengths $$$(3, 3, 3), (3, 3, 5), (3, 5, 5), (5, 5, 5)$$$ respectively can all form non-degenerate triangles. $$$[7]$$$ is stable because sticks of lengths $$$(7, 7, 7)$$$ respectively can form a non-degenerate triangle. Note that some other partitions also satisfy the constraints, such as $$$[2], [3], [5], [7]$$$ and $$$[2], [3], [5, 7]$$$. In the second test case, Cocoly can only partition each element as a single subsegment, resulting in $$$[115], [9], [2], [28]$$$. Since we only have one possible partition, the answer is $$$\texttt{NO}$$$. In the third test case, please note that the partition $$$[8, 4], [1], [6], [2]$$$ does not satisfy the constraints, because $$$\{8, 4\}$$$ is not a stable set: sticks of lengths $$$4$$$, $$$4$$$, and $$$8$$$ cannot form a non-degenerate triangle.
Title: Tender Carpenter time_limit_ms: 1000 memory_limit_mb: 256 Description: In his dream, Cocoly would go on a long holiday with no worries around him. So he would try out for many new things, such as... being a carpenter. To learn it well, Cocoly decides to become an apprentice of Master, but in front of him lies a hard task waiting for him to solve. Cocoly is given an array $$$a_1, a_2,\ldots, a_n$$$. Master calls a set of integers $$$S$$$ stable if and only if, for any possible $$$u$$$, $$$v$$$, and $$$w$$$ from the set $$$S$$$ (note that $$$u$$$, $$$v$$$, and $$$w$$$ do not necessarily have to be pairwise distinct), sticks of length $$$u$$$, $$$v$$$, and $$$w$$$ can form a non-degenerate triangle$$$^{\text{∗}}$$$. Cocoly is asked to partition the array $$$a$$$ into several (possibly, $$$1$$$ or $$$n$$$) non-empty continuous subsegments$$$^{\text{†}}$$$, such that: for each of the subsegments, the set containing all the elements in it is stable. Master wants Cocoly to partition $$$a$$$ in at least two different$$$^{\text{‡}}$$$ ways. You have to help him determine whether it is possible. Input: Each test contains multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 200$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 200$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — the elements in the array $$$a$$$. Output: For each test case, print $$$\texttt{YES}$$$ if there are at least two ways to partition $$$a$$$, and $$$\texttt{NO}$$$ otherwise. You can output the answer in any case (upper or lower). For example, the strings $$$\texttt{yEs}$$$, $$$\texttt{yes}$$$, $$$\texttt{Yes}$$$, and $$$\texttt{YES}$$$ will be recognized as positive responses. Examples: input: 5 4 2 3 5 7 4 115 9 2 28 5 8 4 1 6 2 6 1 5 4 1 4 7 2 100000 100000 output: YES NO NO YES YES Note: In the first test case, here are two possible partitions: - $$$[2, 3], [5, 7]$$$, since $$$[2, 3]$$$ is stable because sticks of lengths $$$(2, 2, 2), (2, 2, 3), (2, 3, 3), (3, 3, 3)$$$ respectively can all form non-degenerate triangles. $$$[5, 7]$$$ is stable because sticks of lengths $$$(5, 5, 5), (5, 5, 7), (5, 7, 7), (7, 7, 7)$$$ respectively can all form non-degenerate triangles. - and $$$[2], [3, 5], [7]$$$, since $$$[2]$$$ is stable because sticks of lengths $$$(2, 2, 2)$$$ respectively can form a non-degenerate triangle. $$$[3, 5]$$$ is stable because sticks of lengths $$$(3, 3, 3), (3, 3, 5), (3, 5, 5), (5, 5, 5)$$$ respectively can all form non-degenerate triangles. $$$[7]$$$ is stable because sticks of lengths $$$(7, 7, 7)$$$ respectively can form a non-degenerate triangle. Note that some other partitions also satisfy the constraints, such as $$$[2], [3], [5], [7]$$$ and $$$[2], [3], [5, 7]$$$. In the second test case, Cocoly can only partition each element as a single subsegment, resulting in $$$[115], [9], [2], [28]$$$. Since we only have one possible partition, the answer is $$$\texttt{NO}$$$. In the third test case, please note that the partition $$$[8, 4], [1], [6], [2]$$$ does not satisfy the constraints, because $$$\{8, 4\}$$$ is not a stable set: sticks of lengths $$$4$$$, $$$4$$$, and $$$8$$$ cannot form a non-degenerate triangle.
2053B
https://codeforces.com/problemset/problem/2053/B
Outstanding Impressionist
1,200
[ "data structures", "brute force", "binary search", "greedy" ]
Good Bye 2024: 2025 is NEAR
1,000
256
Even after copying the paintings from famous artists for ten years, unfortunately, Eric is still unable to become a skillful impressionist painter. He wants to forget something, but the white bear phenomenon just keeps hanging over him. Eric still remembers $$$n$$$ pieces of impressions in the form of an integer array. He records them as $$$w_1, w_2, \ldots, w_n$$$. However, he has a poor memory of the impressions. For each $$$1 \leq i \leq n$$$, he can only remember that $$$l_i \leq w_i \leq r_i$$$. Eric believes that impression $$$i$$$ is unique if and only if there exists a possible array $$$w_1, w_2, \ldots, w_n$$$ such that $$$w_i \neq w_j$$$ holds for all $$$1 \leq j \leq n$$$ with $$$j \neq i$$$. Please help Eric determine whether impression $$$i$$$ is unique for every $$$1 \leq i \leq n$$$, independently for each $$$i$$$. Perhaps your judgment can help rewrite the final story.
Each test contains multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2\cdot 10^5$$$) — the number of impressions. Then $$$n$$$ lines follow, the $$$i$$$-th containing two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \leq l_i \leq r_i \leq 2\cdot n$$$) — the minimum possible value and the maximum possible value of $$$w_i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output a binary string $$$s$$$ of length $$$n$$$: for each $$$1 \leq i \leq n$$$, if impression $$$i$$$ is unique, $$$s_i=\texttt{1}$$$; otherwise, $$$s_i=\texttt{0}$$$. Do not output spaces.
[ [ "5\n2\n1 1\n1 1\n4\n1 3\n1 3\n1 3\n1 3\n6\n3 6\n2 2\n1 2\n1 1\n3 4\n2 2\n7\n3 4\n4 4\n4 4\n1 3\n2 5\n1 4\n2 2\n3\n4 5\n4 4\n5 5", "00\n1111\n100110\n1001111\n011" ] ]
In the first test case, the only possible array $$$w$$$ is $$$[1, 1]$$$, making neither impression $$$1$$$ nor $$$2$$$ unique (since $$$w_1 = w_2$$$). In the second test case, all impressions can be made unique: - For $$$i = 1$$$, we can set $$$w$$$ to $$$[1, 3, 2, 3]$$$, in which $$$w_1 \neq w_2$$$, $$$w_1 \neq w_3$$$, and $$$w_1 \neq w_4$$$; - For $$$i = 2$$$, we can set $$$w$$$ to $$$[2, 3, 1, 2]$$$, in which $$$w_2 \neq w_1$$$, $$$w_2 \neq w_3$$$, and $$$w_2 \neq w_4$$$; - For $$$i = 3$$$, we can set $$$w$$$ to $$$[1, 1, 3, 1]$$$; - For $$$i = 4$$$, we can set $$$w$$$ to $$$[2, 3, 3, 1]$$$. In the third test case, for $$$i = 4$$$, we can set $$$w$$$ to $$$[3, 2, 2, 1, 3, 2]$$$. Thus, impression $$$4$$$ is unique.
Title: Outstanding Impressionist time_limit_ms: 1000 memory_limit_mb: 256 Description: Even after copying the paintings from famous artists for ten years, unfortunately, Eric is still unable to become a skillful impressionist painter. He wants to forget something, but the white bear phenomenon just keeps hanging over him. Eric still remembers $$$n$$$ pieces of impressions in the form of an integer array. He records them as $$$w_1, w_2, \ldots, w_n$$$. However, he has a poor memory of the impressions. For each $$$1 \leq i \leq n$$$, he can only remember that $$$l_i \leq w_i \leq r_i$$$. Eric believes that impression $$$i$$$ is unique if and only if there exists a possible array $$$w_1, w_2, \ldots, w_n$$$ such that $$$w_i \neq w_j$$$ holds for all $$$1 \leq j \leq n$$$ with $$$j \neq i$$$. Please help Eric determine whether impression $$$i$$$ is unique for every $$$1 \leq i \leq n$$$, independently for each $$$i$$$. Perhaps your judgment can help rewrite the final story. Input: Each test contains multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2\cdot 10^5$$$) — the number of impressions. Then $$$n$$$ lines follow, the $$$i$$$-th containing two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \leq l_i \leq r_i \leq 2\cdot n$$$) — the minimum possible value and the maximum possible value of $$$w_i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$. Output: For each test case, output a binary string $$$s$$$ of length $$$n$$$: for each $$$1 \leq i \leq n$$$, if impression $$$i$$$ is unique, $$$s_i=\texttt{1}$$$; otherwise, $$$s_i=\texttt{0}$$$. Do not output spaces. Examples: input: 5 2 1 1 1 1 4 1 3 1 3 1 3 1 3 6 3 6 2 2 1 2 1 1 3 4 2 2 7 3 4 4 4 4 4 1 3 2 5 1 4 2 2 3 4 5 4 4 5 5 output: 00 1111 100110 1001111 011 Note: In the first test case, the only possible array $$$w$$$ is $$$[1, 1]$$$, making neither impression $$$1$$$ nor $$$2$$$ unique (since $$$w_1 = w_2$$$). In the second test case, all impressions can be made unique: - For $$$i = 1$$$, we can set $$$w$$$ to $$$[1, 3, 2, 3]$$$, in which $$$w_1 \neq w_2$$$, $$$w_1 \neq w_3$$$, and $$$w_1 \neq w_4$$$; - For $$$i = 2$$$, we can set $$$w$$$ to $$$[2, 3, 1, 2]$$$, in which $$$w_2 \neq w_1$$$, $$$w_2 \neq w_3$$$, and $$$w_2 \neq w_4$$$; - For $$$i = 3$$$, we can set $$$w$$$ to $$$[1, 1, 3, 1]$$$; - For $$$i = 4$$$, we can set $$$w$$$ to $$$[2, 3, 3, 1]$$$. In the third test case, for $$$i = 4$$$, we can set $$$w$$$ to $$$[3, 2, 2, 1, 3, 2]$$$. Thus, impression $$$4$$$ is unique.
2053C
https://codeforces.com/problemset/problem/2053/C
Bewitching Stargazer
1,500
[ "bitmasks", "dp", "math", "divide and conquer" ]
Good Bye 2024: 2025 is NEAR
2,000
256
Iris looked at the stars and a beautiful problem emerged in her mind. She is inviting you to solve it so that a meteor shower is believed to form. There are $$$n$$$ stars in the sky, arranged in a row. Iris has a telescope, which she uses to look at the stars. Initially, Iris observes stars in the segment $$$[1, n]$$$, and she has a lucky value of $$$0$$$. Iris wants to look for the star in the middle position for each segment $$$[l, r]$$$ that she observes. So the following recursive procedure is used: - First, she will calculate $$$m = \left\lfloor \frac{l+r}{2} \right\rfloor$$$. - If the length of the segment (i.e. $$$r - l + 1$$$) is even, Iris will divide it into two equally long segments $$$[l, m]$$$ and $$$[m+1, r]$$$ for further observation. - Otherwise, Iris will aim the telescope at star $$$m$$$, and her lucky value will increase by $$$m$$$; subsequently, if $$$l \neq r$$$, Iris will continue to observe two segments $$$[l, m-1]$$$ and $$$[m+1, r]$$$. Iris is a bit lazy. She defines her laziness by an integer $$$k$$$: as the observation progresses, she will not continue to observe any segment $$$[l, r]$$$ with a length strictly less than $$$k$$$. In this case, please predict her final lucky value.
Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2\cdot 10^9$$$).
For each test case, output a single integer — the final lucky value.
[ [ "6\n7 2\n11 3\n55 13\n5801 6\n8919 64\n8765432 1", "12\n18\n196\n1975581\n958900\n38416403456028" ] ]
In the first test case, at the beginning, Iris observes $$$[1, 7]$$$. Since $$$[1, 7]$$$ has an odd length, she aims at star $$$4$$$ and therefore increases her lucky value by $$$4$$$. Then it is split into $$$2$$$ new segments: $$$[1, 3]$$$ and $$$[5, 7]$$$. The segment $$$[1, 3]$$$ again has an odd length, so Iris aims at star $$$2$$$ and increases her lucky value by $$$2$$$. Then it is split into $$$2$$$ new segments: $$$[1, 1]$$$ and $$$[3, 3]$$$, both having a length less than $$$2$$$, so no further observation is conducted. For range $$$[5, 7]$$$, the progress is similar and the lucky value eventually increases by $$$6$$$. Therefore, the final lucky value is $$$4 + 2 + 6 = 12$$$. In the last test case, Iris finally observes all the stars and the final lucky value is $$$1 + 2 + \cdots + 8\,765\,432 = 38\,416\,403\,456\,028$$$.
Title: Bewitching Stargazer time_limit_ms: 2000 memory_limit_mb: 256 Description: Iris looked at the stars and a beautiful problem emerged in her mind. She is inviting you to solve it so that a meteor shower is believed to form. There are $$$n$$$ stars in the sky, arranged in a row. Iris has a telescope, which she uses to look at the stars. Initially, Iris observes stars in the segment $$$[1, n]$$$, and she has a lucky value of $$$0$$$. Iris wants to look for the star in the middle position for each segment $$$[l, r]$$$ that she observes. So the following recursive procedure is used: - First, she will calculate $$$m = \left\lfloor \frac{l+r}{2} \right\rfloor$$$. - If the length of the segment (i.e. $$$r - l + 1$$$) is even, Iris will divide it into two equally long segments $$$[l, m]$$$ and $$$[m+1, r]$$$ for further observation. - Otherwise, Iris will aim the telescope at star $$$m$$$, and her lucky value will increase by $$$m$$$; subsequently, if $$$l \neq r$$$, Iris will continue to observe two segments $$$[l, m-1]$$$ and $$$[m+1, r]$$$. Iris is a bit lazy. She defines her laziness by an integer $$$k$$$: as the observation progresses, she will not continue to observe any segment $$$[l, r]$$$ with a length strictly less than $$$k$$$. In this case, please predict her final lucky value. Input: Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 2\cdot 10^9$$$). Output: For each test case, output a single integer — the final lucky value. Examples: input: 6 7 2 11 3 55 13 5801 6 8919 64 8765432 1 output: 12 18 196 1975581 958900 38416403456028 Note: In the first test case, at the beginning, Iris observes $$$[1, 7]$$$. Since $$$[1, 7]$$$ has an odd length, she aims at star $$$4$$$ and therefore increases her lucky value by $$$4$$$. Then it is split into $$$2$$$ new segments: $$$[1, 3]$$$ and $$$[5, 7]$$$. The segment $$$[1, 3]$$$ again has an odd length, so Iris aims at star $$$2$$$ and increases her lucky value by $$$2$$$. Then it is split into $$$2$$$ new segments: $$$[1, 1]$$$ and $$$[3, 3]$$$, both having a length less than $$$2$$$, so no further observation is conducted. For range $$$[5, 7]$$$, the progress is similar and the lucky value eventually increases by $$$6$$$. Therefore, the final lucky value is $$$4 + 2 + 6 = 12$$$. In the last test case, Iris finally observes all the stars and the final lucky value is $$$1 + 2 + \cdots + 8\,765\,432 = 38\,416\,403\,456\,028$$$.
2053D
https://codeforces.com/problemset/problem/2053/D
Refined Product Optimality
1,700
[ "math", "binary search", "schedules", "data structures", "sortings", "greedy" ]
Good Bye 2024: 2025 is NEAR
3,000
512
Although Iris occasionally sets a problem where the solution is possibly wrong, she still insists on creating problems with her imagination; after all, everyone has always been on the road with their stubbornness... And like ever before, Iris has set a problem to which she gave a wrong solution, but Chris is always supposed to save it! You are going to play the role of Chris now: - Chris is given two arrays $$$a$$$ and $$$b$$$, both consisting of $$$n$$$ integers. - Iris is interested in the largest possible value of $$$P = \prod\limits_{i=1}^n \min(a_i, b_i)$$$ after an arbitrary rearrangement of $$$b$$$. Note that she only wants to know the maximum value of $$$P$$$, and no actual rearrangement is performed on $$$b$$$. - There will be $$$q$$$ modifications. Each modification can be denoted by two integers $$$o$$$ and $$$x$$$ ($$$o$$$ is either $$$1$$$ or $$$2$$$, $$$1 \leq x \leq n$$$). If $$$o = 1$$$, then Iris will increase $$$a_x$$$ by $$$1$$$; otherwise, she will increase $$$b_x$$$ by $$$1$$$. - Iris asks Chris the maximum value of $$$P$$$ for $$$q + 1$$$ times: once before any modification, then after every modification. - Since $$$P$$$ might be huge, Chris only needs to calculate it modulo $$$998\,244\,353$$$. Chris soon worked out this problem, but he was so tired that he fell asleep. Besides saying thanks to Chris, now it is your turn to write a program to calculate the answers for given input data. Note: since the input and output are large, you may need to optimize them for this problem. For example, in C++, it is enough to use the following lines at the start of the main() function:
Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n \leq 2\cdot 10^5$$$, $$$1 \leq q \leq 2\cdot 10^5$$$) — the length of the array and the number of operations. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 5\cdot 10^8$$$) — the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 5\cdot 10^8$$$) — the array $$$b$$$. Then $$$q$$$ lines follow, each line contains two integers $$$o$$$ and $$$x$$$ ($$$o \in \{1, 2\}$$$, $$$1 \leq x \leq n$$$), representing an operation. It's guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$4\cdot 10^5$$$, respectively.
For each test case, output $$$q + 1$$$ integers in a line, representing the answers that Chris will calculate, modulo $$$998\,244\,353$$$.
[ [ "4\n3 4\n1 1 2\n3 2 1\n1 3\n2 3\n1 1\n2 1\n6 8\n1 4 2 7 3 5\n7 6 5 6 3 3\n2 5\n1 6\n1 5\n1 5\n1 5\n2 3\n2 3\n1 6\n13 8\n7 7 6 6 5 5 5 2 2 3 4 5 1\n1 4 1 9 6 6 9 1 5 1 3 8 4\n2 2\n2 11\n2 4\n2 4\n1 7\n1 1\n2 12\n1 5\n5 3\n10000000 20000000 30000000 40000000 50000000\n10000000 20000000 30000000 40000000 50000000\n1 1\n2 2\n2 1", "2 3 3 6 6\n840 840 1008 1344 1680 2016 2016 2016 2352\n2116800 2646000 3528000 3528000 3528000 4233600 4838400 4838400 4838400\n205272023 205272023 205272023 264129429" ] ]
In the first test case: - Before the modifications, Chris can rearrange $$$b$$$ to $$$[1, 2, 3]$$$ so that $$$P = \prod\limits_{i=1}^n \min(a_i, b_i) = 1 \cdot 1 \cdot 2 = 2$$$. We can prove that this is the maximum possible value. For example, if Chris rearranges $$$b = [2, 3, 1]$$$, $$$P$$$ will be equal $$$1 \cdot 1 \cdot 1 = 1 < 2$$$, which is not optimal. - After the first modification, Chris can rearrange $$$b$$$ to $$$[1, 2, 3]$$$ so that $$$P = 1 \cdot 1 \cdot 3 = 3$$$, which is maximized. - After the second modification, Chris can rearrange $$$b$$$ to $$$[2, 2, 3]$$$ so that $$$P = 1 \cdot 1 \cdot 3 = 3$$$, which is maximized. - After the third modification, Chris can rearrange $$$b$$$ to $$$[2, 2, 3]$$$ so that $$$P = 6$$$, which is maximized. - After the fourth modification, Chris can rearrange $$$b$$$ to $$$[2, 2, 4]$$$ so that $$$P = 6$$$, which is maximized.
Title: Refined Product Optimality time_limit_ms: 3000 memory_limit_mb: 512 Description: Although Iris occasionally sets a problem where the solution is possibly wrong, she still insists on creating problems with her imagination; after all, everyone has always been on the road with their stubbornness... And like ever before, Iris has set a problem to which she gave a wrong solution, but Chris is always supposed to save it! You are going to play the role of Chris now: - Chris is given two arrays $$$a$$$ and $$$b$$$, both consisting of $$$n$$$ integers. - Iris is interested in the largest possible value of $$$P = \prod\limits_{i=1}^n \min(a_i, b_i)$$$ after an arbitrary rearrangement of $$$b$$$. Note that she only wants to know the maximum value of $$$P$$$, and no actual rearrangement is performed on $$$b$$$. - There will be $$$q$$$ modifications. Each modification can be denoted by two integers $$$o$$$ and $$$x$$$ ($$$o$$$ is either $$$1$$$ or $$$2$$$, $$$1 \leq x \leq n$$$). If $$$o = 1$$$, then Iris will increase $$$a_x$$$ by $$$1$$$; otherwise, she will increase $$$b_x$$$ by $$$1$$$. - Iris asks Chris the maximum value of $$$P$$$ for $$$q + 1$$$ times: once before any modification, then after every modification. - Since $$$P$$$ might be huge, Chris only needs to calculate it modulo $$$998\,244\,353$$$. Chris soon worked out this problem, but he was so tired that he fell asleep. Besides saying thanks to Chris, now it is your turn to write a program to calculate the answers for given input data. Note: since the input and output are large, you may need to optimize them for this problem. For example, in C++, it is enough to use the following lines at the start of the main() function: Input: Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n \leq 2\cdot 10^5$$$, $$$1 \leq q \leq 2\cdot 10^5$$$) — the length of the array and the number of operations. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 5\cdot 10^8$$$) — the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 5\cdot 10^8$$$) — the array $$$b$$$. Then $$$q$$$ lines follow, each line contains two integers $$$o$$$ and $$$x$$$ ($$$o \in \{1, 2\}$$$, $$$1 \leq x \leq n$$$), representing an operation. It's guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$4\cdot 10^5$$$, respectively. Output: For each test case, output $$$q + 1$$$ integers in a line, representing the answers that Chris will calculate, modulo $$$998\,244\,353$$$. Examples: input: 4 3 4 1 1 2 3 2 1 1 3 2 3 1 1 2 1 6 8 1 4 2 7 3 5 7 6 5 6 3 3 2 5 1 6 1 5 1 5 1 5 2 3 2 3 1 6 13 8 7 7 6 6 5 5 5 2 2 3 4 5 1 1 4 1 9 6 6 9 1 5 1 3 8 4 2 2 2 11 2 4 2 4 1 7 1 1 2 12 1 5 5 3 10000000 20000000 30000000 40000000 50000000 10000000 20000000 30000000 40000000 50000000 1 1 2 2 2 1 output: 2 3 3 6 6 840 840 1008 1344 1680 2016 2016 2016 2352 2116800 2646000 3528000 3528000 3528000 4233600 4838400 4838400 4838400 205272023 205272023 205272023 264129429 Note: In the first test case: - Before the modifications, Chris can rearrange $$$b$$$ to $$$[1, 2, 3]$$$ so that $$$P = \prod\limits_{i=1}^n \min(a_i, b_i) = 1 \cdot 1 \cdot 2 = 2$$$. We can prove that this is the maximum possible value. For example, if Chris rearranges $$$b = [2, 3, 1]$$$, $$$P$$$ will be equal $$$1 \cdot 1 \cdot 1 = 1 < 2$$$, which is not optimal. - After the first modification, Chris can rearrange $$$b$$$ to $$$[1, 2, 3]$$$ so that $$$P = 1 \cdot 1 \cdot 3 = 3$$$, which is maximized. - After the second modification, Chris can rearrange $$$b$$$ to $$$[2, 2, 3]$$$ so that $$$P = 1 \cdot 1 \cdot 3 = 3$$$, which is maximized. - After the third modification, Chris can rearrange $$$b$$$ to $$$[2, 2, 3]$$$ so that $$$P = 6$$$, which is maximized. - After the fourth modification, Chris can rearrange $$$b$$$ to $$$[2, 2, 4]$$$ so that $$$P = 6$$$, which is maximized.
2031A
https://codeforces.com/problemset/problem/2031/A
Penchick and Modern Monument
800
[ "constructive algorithms", "greedy", "dp", "math" ]
Div. 2
1,000
256
Amidst skyscrapers in the bustling metropolis of Metro Manila, the newest Noiph mall in the Philippines has just been completed! The construction manager, Penchick, ordered a state-of-the-art monument to be built with $$$n$$$ pillars. The heights of the monument's pillars can be represented as an array $$$h$$$ of $$$n$$$ positive integers, where $$$h_i$$$ represents the height of the $$$i$$$-th pillar for all $$$i$$$ between $$$1$$$ and $$$n$$$. Penchick wants the heights of the pillars to be in non-decreasing order, i.e. $$$h_i \le h_{i + 1}$$$ for all $$$i$$$ between $$$1$$$ and $$$n - 1$$$. However, due to confusion, the monument was built such that the heights of the pillars are in non-increasing order instead, i.e. $$$h_i \ge h_{i + 1}$$$ for all $$$i$$$ between $$$1$$$ and $$$n - 1$$$. Luckily, Penchick can modify the monument and do the following operation on the pillars as many times as necessary: - Modify the height of a pillar to any positive integer. Formally, choose an index $$$1\le i\le n$$$ and a positive integer $$$x$$$. Then, assign $$$h_i := x$$$. Help Penchick determine the minimum number of operations needed to make the heights of the monument's pillars non-decreasing.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 50$$$) — the number of pillars. The second line of each test case contains $$$n$$$ integers $$$h_1, h_2, \ldots, h_n$$$ ($$$1 \le h_i \le n$$$ and $$$h_i\ge h_{i+1}$$$) — the height of the pillars. Please take note that the given array $$$h$$$ is non-increasing. Note that there are no constraints on the sum of $$$n$$$ over all test cases.
For each test case, output a single integer representing the minimum number of operations needed to make the heights of the pillars non-decreasing.
[ [ "3\n5\n5 4 3 2 1\n3\n2 2 1\n1\n1", "4\n1\n0" ] ]
In the first test case, the initial heights of pillars are $$$h = [5, 4, 3, 2, 1]$$$. - In the first operation, Penchick changes the height of pillar $$$1$$$ to $$$h_1 := 2$$$. - In the second operation, he changes the height of pillar $$$2$$$ to $$$h_2 := 2$$$. - In the third operation, he changes the height of pillar $$$4$$$ to $$$h_4 := 4$$$. - In the fourth operation, he changes the height of pillar $$$5$$$ to $$$h_5 := 4$$$. After the operation, the heights of the pillars are $$$h = [2, 2, 3, 4, 4]$$$, which is non-decreasing. It can be proven that it is not possible for Penchick to make the heights of the pillars non-decreasing in fewer than $$$4$$$ operations. In the second test case, Penchick can make the heights of the pillars non-decreasing by modifying the height of pillar $$$3$$$ to $$$h_3 := 2$$$. In the third test case, the heights of pillars are already non-decreasing, so no operations are required.
Title: Penchick and Modern Monument time_limit_ms: 1000 memory_limit_mb: 256 Description: Amidst skyscrapers in the bustling metropolis of Metro Manila, the newest Noiph mall in the Philippines has just been completed! The construction manager, Penchick, ordered a state-of-the-art monument to be built with $$$n$$$ pillars. The heights of the monument's pillars can be represented as an array $$$h$$$ of $$$n$$$ positive integers, where $$$h_i$$$ represents the height of the $$$i$$$-th pillar for all $$$i$$$ between $$$1$$$ and $$$n$$$. Penchick wants the heights of the pillars to be in non-decreasing order, i.e. $$$h_i \le h_{i + 1}$$$ for all $$$i$$$ between $$$1$$$ and $$$n - 1$$$. However, due to confusion, the monument was built such that the heights of the pillars are in non-increasing order instead, i.e. $$$h_i \ge h_{i + 1}$$$ for all $$$i$$$ between $$$1$$$ and $$$n - 1$$$. Luckily, Penchick can modify the monument and do the following operation on the pillars as many times as necessary: - Modify the height of a pillar to any positive integer. Formally, choose an index $$$1\le i\le n$$$ and a positive integer $$$x$$$. Then, assign $$$h_i := x$$$. Help Penchick determine the minimum number of operations needed to make the heights of the monument's pillars non-decreasing. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 50$$$) — the number of pillars. The second line of each test case contains $$$n$$$ integers $$$h_1, h_2, \ldots, h_n$$$ ($$$1 \le h_i \le n$$$ and $$$h_i\ge h_{i+1}$$$) — the height of the pillars. Please take note that the given array $$$h$$$ is non-increasing. Note that there are no constraints on the sum of $$$n$$$ over all test cases. Output: For each test case, output a single integer representing the minimum number of operations needed to make the heights of the pillars non-decreasing. Examples: input: 3 5 5 4 3 2 1 3 2 2 1 1 1 output: 4 1 0 Note: In the first test case, the initial heights of pillars are $$$h = [5, 4, 3, 2, 1]$$$. - In the first operation, Penchick changes the height of pillar $$$1$$$ to $$$h_1 := 2$$$. - In the second operation, he changes the height of pillar $$$2$$$ to $$$h_2 := 2$$$. - In the third operation, he changes the height of pillar $$$4$$$ to $$$h_4 := 4$$$. - In the fourth operation, he changes the height of pillar $$$5$$$ to $$$h_5 := 4$$$. After the operation, the heights of the pillars are $$$h = [2, 2, 3, 4, 4]$$$, which is non-decreasing. It can be proven that it is not possible for Penchick to make the heights of the pillars non-decreasing in fewer than $$$4$$$ operations. In the second test case, Penchick can make the heights of the pillars non-decreasing by modifying the height of pillar $$$3$$$ to $$$h_3 := 2$$$. In the third test case, the heights of pillars are already non-decreasing, so no operations are required.
2031B
https://codeforces.com/problemset/problem/2031/B
Penchick and Satay Sticks
900
[ "greedy", "brute force", "sortings" ]
Div. 2
1,500
256
Penchick and his friend Kohane are touring Indonesia, and their next stop is in Surabaya! In the bustling food stalls of Surabaya, Kohane bought $$$n$$$ satay sticks and arranged them in a line, with the $$$i$$$-th satay stick having length $$$p_i$$$. It is given that $$$p$$$ is a permutation$$$^{\text{∗}}$$$ of length $$$n$$$. Penchick wants to sort the satay sticks in increasing order of length, so that $$$p_i=i$$$ for each $$$1\le i\le n$$$. For fun, they created a rule: they can only swap neighboring satay sticks whose lengths differ by exactly $$$1$$$. Formally, they can perform the following operation any number of times (including zero): - Select an index $$$i$$$ ($$$1\le i\le n-1$$$) such that $$$|p_{i+1}-p_i|=1$$$; - Swap $$$p_i$$$ and $$$p_{i+1}$$$. Determine whether it is possible to sort the permutation $$$p$$$, thus the satay sticks, by performing the above operation.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2\cdot 10^5$$$) — the number of satay sticks. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$) — the permutation $$$p$$$ representing the length of the satay sticks. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output "YES" if it is possible to sort permutation $$$p$$$ by performing the operation. Otherwise, output "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
[ [ "2\n4\n2 1 3 4\n4\n4 2 3 1", "YES\nNO" ] ]
In the first test case, we can sort permutation $$$p = [2, 1, 3, 4]$$$ by performing an operation on index $$$1$$$ ($$$|p_2 - p_1| = |1 - 2| = 1$$$), resulting in $$$p = [1, 2, 3, 4]$$$. In the second test case, it can be proven that it is impossible to sort permutation $$$p = [4, 2, 3, 1]$$$ by performing the operation. Here is an example of a sequence of operations that can be performed on the permutation: - Select $$$i = 2$$$ ($$$|p_3 - p_2| = |3 - 2| = 1$$$). This results in $$$p = [4, 3, 2, 1]$$$. - Select $$$i = 1$$$ ($$$|p_2 - p_1| = |3 - 4| = 1$$$). This results in $$$p = [3, 4, 2, 1]$$$. - Select $$$i = 3$$$ ($$$|p_4 - p_3| = |1 - 2| = 1$$$). This results in $$$p = [3, 4, 1, 2]$$$. Unfortunately, permutation $$$p$$$ remains unsorted after performing the operations.
Title: Penchick and Satay Sticks time_limit_ms: 1500 memory_limit_mb: 256 Description: Penchick and his friend Kohane are touring Indonesia, and their next stop is in Surabaya! In the bustling food stalls of Surabaya, Kohane bought $$$n$$$ satay sticks and arranged them in a line, with the $$$i$$$-th satay stick having length $$$p_i$$$. It is given that $$$p$$$ is a permutation$$$^{\text{∗}}$$$ of length $$$n$$$. Penchick wants to sort the satay sticks in increasing order of length, so that $$$p_i=i$$$ for each $$$1\le i\le n$$$. For fun, they created a rule: they can only swap neighboring satay sticks whose lengths differ by exactly $$$1$$$. Formally, they can perform the following operation any number of times (including zero): - Select an index $$$i$$$ ($$$1\le i\le n-1$$$) such that $$$|p_{i+1}-p_i|=1$$$; - Swap $$$p_i$$$ and $$$p_{i+1}$$$. Determine whether it is possible to sort the permutation $$$p$$$, thus the satay sticks, by performing the above operation. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2\cdot 10^5$$$) — the number of satay sticks. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$) — the permutation $$$p$$$ representing the length of the satay sticks. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$. Output: For each test case, output "YES" if it is possible to sort permutation $$$p$$$ by performing the operation. Otherwise, output "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Examples: input: 2 4 2 1 3 4 4 4 2 3 1 output: YES NO Note: In the first test case, we can sort permutation $$$p = [2, 1, 3, 4]$$$ by performing an operation on index $$$1$$$ ($$$|p_2 - p_1| = |1 - 2| = 1$$$), resulting in $$$p = [1, 2, 3, 4]$$$. In the second test case, it can be proven that it is impossible to sort permutation $$$p = [4, 2, 3, 1]$$$ by performing the operation. Here is an example of a sequence of operations that can be performed on the permutation: - Select $$$i = 2$$$ ($$$|p_3 - p_2| = |3 - 2| = 1$$$). This results in $$$p = [4, 3, 2, 1]$$$. - Select $$$i = 1$$$ ($$$|p_2 - p_1| = |3 - 4| = 1$$$). This results in $$$p = [3, 4, 2, 1]$$$. - Select $$$i = 3$$$ ($$$|p_4 - p_3| = |1 - 2| = 1$$$). This results in $$$p = [3, 4, 1, 2]$$$. Unfortunately, permutation $$$p$$$ remains unsorted after performing the operations.
2031D
https://codeforces.com/problemset/problem/2031/D
Penchick and Desert Rabbit
1,700
[ "dp", "binary search", "implementation", "data structures", "two pointers", "dfs and similar", "dsu", "greedy" ]
Div. 2
3,000
256
Dedicated to pushing himself to his limits, Penchick challenged himself to survive the midday sun in the Arabian Desert! While trekking along a linear oasis, Penchick spots a desert rabbit preparing to jump along a line of palm trees. There are $$$n$$$ trees, each with a height denoted by $$$a_i$$$. The rabbit can jump from the $$$i$$$-th tree to the $$$j$$$-th tree if exactly one of the following conditions is true: - $$$j < i$$$ and $$$a_j > a_i$$$: the rabbit can jump backward to a taller tree. - $$$j > i$$$ and $$$a_j < a_i$$$: the rabbit can jump forward to a shorter tree. For each $$$i$$$ from $$$1$$$ to $$$n$$$, determine the maximum height among all trees that the rabbit can reach if it starts from the $$$i$$$-th tree.
The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 5 \cdot 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 5 \cdot 10^5$$$) — the number of trees. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — the height of the trees. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.
For each test case, output $$$n$$$ integers. The $$$i$$$-th integer should contain the maximum height among all trees that the rabbit can reach if it starts from the $$$i$$$-th tree.
[ [ "5\n4\n2 3 1 4\n5\n5 4 3 2 1\n4\n2 1 1 3\n4\n1 1 3 1\n8\n2 4 1 6 3 8 5 7", "3 3 3 4 \n5 5 5 5 5 \n2 2 2 3 \n1 1 3 3 \n8 8 8 8 8 8 8 8" ] ]
In the first test case, the initial heights of trees are $$$a = [2, 3, 1, 4]$$$. - If the rabbit starts from the first tree, it can jump to the third tree as $$$3 > 1$$$ and $$$1 < 2$$$. Then, the rabbit can jump to the second tree as $$$2 < 3$$$ and $$$3 > 1$$$. It can be proved that the rabbit cannot reach the fourth tree; hence, the maximum height of the tree that the rabbit can reach is $$$a_2 = 3$$$. - If the rabbit starts from the fourth tree, it does not need to jump anywhere as it is already at the highest tree. In the second test case, the rabbit can jump to the first tree regardless of which tree it starts from. In the fifth test case, if the rabbit starts from the fifth tree, it can jump to the fourth tree. Then the rabbit can jump to the seventh tree and finally reach the sixth tree. Therefore, the maximum height of the tree that the rabbit can reach is $$$8$$$.
Title: Penchick and Desert Rabbit time_limit_ms: 3000 memory_limit_mb: 256 Description: Dedicated to pushing himself to his limits, Penchick challenged himself to survive the midday sun in the Arabian Desert! While trekking along a linear oasis, Penchick spots a desert rabbit preparing to jump along a line of palm trees. There are $$$n$$$ trees, each with a height denoted by $$$a_i$$$. The rabbit can jump from the $$$i$$$-th tree to the $$$j$$$-th tree if exactly one of the following conditions is true: - $$$j < i$$$ and $$$a_j > a_i$$$: the rabbit can jump backward to a taller tree. - $$$j > i$$$ and $$$a_j < a_i$$$: the rabbit can jump forward to a shorter tree. For each $$$i$$$ from $$$1$$$ to $$$n$$$, determine the maximum height among all trees that the rabbit can reach if it starts from the $$$i$$$-th tree. Input: The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 5 \cdot 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 5 \cdot 10^5$$$) — the number of trees. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — the height of the trees. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$. Output: For each test case, output $$$n$$$ integers. The $$$i$$$-th integer should contain the maximum height among all trees that the rabbit can reach if it starts from the $$$i$$$-th tree. Examples: input: 5 4 2 3 1 4 5 5 4 3 2 1 4 2 1 1 3 4 1 1 3 1 8 2 4 1 6 3 8 5 7 output: 3 3 3 4 5 5 5 5 5 2 2 2 3 1 1 3 3 8 8 8 8 8 8 8 8 Note: In the first test case, the initial heights of trees are $$$a = [2, 3, 1, 4]$$$. - If the rabbit starts from the first tree, it can jump to the third tree as $$$3 > 1$$$ and $$$1 < 2$$$. Then, the rabbit can jump to the second tree as $$$2 < 3$$$ and $$$3 > 1$$$. It can be proved that the rabbit cannot reach the fourth tree; hence, the maximum height of the tree that the rabbit can reach is $$$a_2 = 3$$$. - If the rabbit starts from the fourth tree, it does not need to jump anywhere as it is already at the highest tree. In the second test case, the rabbit can jump to the first tree regardless of which tree it starts from. In the fifth test case, if the rabbit starts from the fifth tree, it can jump to the fourth tree. Then the rabbit can jump to the seventh tree and finally reach the sixth tree. Therefore, the maximum height of the tree that the rabbit can reach is $$$8$$$.
2031E
https://codeforces.com/problemset/problem/2031/E
Penchick and Chloe's Trees
2,100
[ "dp", "implementation", "math", "data structures", "sortings", "dfs and similar", "greedy", "trees" ]
Div. 2
3,500
512
With just a few hours left until Penchick and Chloe leave for Singapore, they could hardly wait to see the towering trees at the Singapore Botanic Gardens! Attempting to contain their excitement, Penchick crafted a rooted tree to keep Chloe and himself busy. Penchick has a rooted tree$$$^{\text{∗}}$$$ consisting of $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$, with vertex $$$1$$$ as the root, and Chloe can select a non-negative integer $$$d$$$ to create a perfect binary tree$$$^{\text{†}}$$$ of depth $$$d$$$. Since Penchick and Chloe are good friends, Chloe wants her tree to be isomorphic$$$^{\text{‡}}$$$ to Penchick's tree. To meet this condition, Chloe can perform the following operation on her own tree any number of times: - Select an edge $$$(u,v)$$$, where $$$u$$$ is the parent of $$$v$$$. - Remove vertex $$$v$$$ and all the edges connected to $$$v$$$, then connect all of $$$v$$$'s previous children directly to $$$u$$$. In particular, doing an operation on an edge $$$(u, v)$$$ where $$$v$$$ is a leaf will delete vertex $$$v$$$ without adding any new edges. Since constructing a perfect binary tree can be time-consuming, Chloe wants to choose the minimum $$$d$$$ such that a perfect binary tree of depth $$$d$$$ can be made isomorphic to Penchick's tree using the above operation. Note that she can't change the roots of the trees.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^6$$$) — the number of vertices in Penchick's tree. The second line of each test case contains $$$n-1$$$ integers $$$p_2, p_3, \ldots, p_n$$$ ($$$1 \leq p_i \leq i-1$$$) — the parent of vertex $$$i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output a single integer on each line: the minimum depth of Chloe's perfect binary tree.
[ [ "5\n6\n1 2 2 1 1\n15\n1 1 2 2 3 3 4 4 5 5 6 6 7 7\n5\n1 2 2 2\n7\n1 1 2 1 1 2\n10\n1 1 1 2 2 2 4 3 3", "2\n3\n3\n3\n3" ] ]
For the first test case, create a perfect binary tree with depth $$$2$$$. Consider carrying out the operation on edge $$$AC$$$. Then the edges $$$AC$$$, $$$CF$$$, and $$$CG$$$ are removed, and edges $$$AF$$$ and $$$AG$$$ are added. The resulting tree is isomorphic to the tree given in the input. It can be proven that no sequence of operations carried out on a binary tree of depth less than $$$2$$$ can lead to a tree isomorphic to the tree given in the input. In the second test case, the tree is already isomorphic to a perfect binary tree of depth $$$3$$$.
Title: Penchick and Chloe's Trees time_limit_ms: 3500 memory_limit_mb: 512 Description: With just a few hours left until Penchick and Chloe leave for Singapore, they could hardly wait to see the towering trees at the Singapore Botanic Gardens! Attempting to contain their excitement, Penchick crafted a rooted tree to keep Chloe and himself busy. Penchick has a rooted tree$$$^{\text{∗}}$$$ consisting of $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$, with vertex $$$1$$$ as the root, and Chloe can select a non-negative integer $$$d$$$ to create a perfect binary tree$$$^{\text{†}}$$$ of depth $$$d$$$. Since Penchick and Chloe are good friends, Chloe wants her tree to be isomorphic$$$^{\text{‡}}$$$ to Penchick's tree. To meet this condition, Chloe can perform the following operation on her own tree any number of times: - Select an edge $$$(u,v)$$$, where $$$u$$$ is the parent of $$$v$$$. - Remove vertex $$$v$$$ and all the edges connected to $$$v$$$, then connect all of $$$v$$$'s previous children directly to $$$u$$$. In particular, doing an operation on an edge $$$(u, v)$$$ where $$$v$$$ is a leaf will delete vertex $$$v$$$ without adding any new edges. Since constructing a perfect binary tree can be time-consuming, Chloe wants to choose the minimum $$$d$$$ such that a perfect binary tree of depth $$$d$$$ can be made isomorphic to Penchick's tree using the above operation. Note that she can't change the roots of the trees. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^6$$$) — the number of vertices in Penchick's tree. The second line of each test case contains $$$n-1$$$ integers $$$p_2, p_3, \ldots, p_n$$$ ($$$1 \leq p_i \leq i-1$$$) — the parent of vertex $$$i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output: For each test case, output a single integer on each line: the minimum depth of Chloe's perfect binary tree. Examples: input: 5 6 1 2 2 1 1 15 1 1 2 2 3 3 4 4 5 5 6 6 7 7 5 1 2 2 2 7 1 1 2 1 1 2 10 1 1 1 2 2 2 4 3 3 output: 2 3 3 3 3 Note: For the first test case, create a perfect binary tree with depth $$$2$$$. Consider carrying out the operation on edge $$$AC$$$. Then the edges $$$AC$$$, $$$CF$$$, and $$$CG$$$ are removed, and edges $$$AF$$$ and $$$AG$$$ are added. The resulting tree is isomorphic to the tree given in the input. It can be proven that no sequence of operations carried out on a binary tree of depth less than $$$2$$$ can lead to a tree isomorphic to the tree given in the input. In the second test case, the tree is already isomorphic to a perfect binary tree of depth $$$3$$$.
1999G1
https://codeforces.com/problemset/problem/1999/G1
Ruler (easy version)
1,500
[ "interactive", "binary search" ]
Div. 4
1,000
256
This is the easy version of the problem. The only difference between the two versions is that in this version, you can make at most $$$\mathbf{10}$$$ queries. This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read the guide for participants. We have a secret ruler that is missing one number $$$x$$$ ($$$2 \leq x \leq 999$$$). When you measure an object of length $$$y$$$, the ruler reports the following values: - If $$$y < x$$$, the ruler (correctly) measures the object as having length $$$y$$$. - If $$$y \geq x$$$, the ruler incorrectly measures the object as having length $$$y+1$$$. The ruler above is missing the number $$$4$$$, so it correctly measures the first segment as length $$$3$$$ but incorrectly measures the second segment as length $$$6$$$ even though it is actually $$$5$$$. You need to find the value of $$$x$$$. To do that, you can make queries of the following form: - $$$\texttt{?}~a~b$$$ — in response, we will measure the side lengths of an $$$a \times b$$$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $$$x=4$$$ and you query a $$$3 \times 5$$$ rectangle, we will measure its side lengths as $$$3 \times 6$$$ and report $$$18$$$ back to you. Find the value of $$$x$$$. You can ask at most $$$\mathbf{10}$$$ queries.
Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.
null
[ [ "2\n\n18\n\n25\n\n\n9999", "? 3 5\n\n? 4 4\n\n! 4\n? 99 100\n\n! 100" ] ]
In the first test, the interaction proceeds as follows. SolutionJuryExplanation$$$\texttt{2}$$$There are 2 test cases.$$$\texttt{? 3 5}$$$$$$\texttt{18}$$$Secretly, the jury picked $$$x=4$$$. The solution requests the $$$3 \times 5$$$ rectangle, and the jury responds with $$$3 \times 6 = 18$$$, as described in the statement.$$$\texttt{? 4 4}$$$$$$\texttt{25}$$$The solution requests the $$$4 \times 4$$$ rectangle, which the jury measures as $$$5 \times 5$$$ and responds with $$$25$$$.$$$\texttt{! 4}$$$The solution has somehow determined that $$$x=4$$$, and outputs it. Since the output is correct, the jury continues to the next test case.$$$\texttt{? 99 100}$$$$$$\texttt{1}$$$Secretly, the jury picked $$$x=100$$$. The solution requests the $$$99 \times 100$$$ rectangle, which the jury measures as $$$99 \times 101$$$ and responds with $$$9999$$$.$$$\texttt{! 100}$$$The solution has somehow determined that $$$x=100$$$, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.
Title: Ruler (easy version) time_limit_ms: 1000 memory_limit_mb: 256 Description: This is the easy version of the problem. The only difference between the two versions is that in this version, you can make at most $$$\mathbf{10}$$$ queries. This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read the guide for participants. We have a secret ruler that is missing one number $$$x$$$ ($$$2 \leq x \leq 999$$$). When you measure an object of length $$$y$$$, the ruler reports the following values: - If $$$y < x$$$, the ruler (correctly) measures the object as having length $$$y$$$. - If $$$y \geq x$$$, the ruler incorrectly measures the object as having length $$$y+1$$$. The ruler above is missing the number $$$4$$$, so it correctly measures the first segment as length $$$3$$$ but incorrectly measures the second segment as length $$$6$$$ even though it is actually $$$5$$$. You need to find the value of $$$x$$$. To do that, you can make queries of the following form: - $$$\texttt{?}~a~b$$$ — in response, we will measure the side lengths of an $$$a \times b$$$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $$$x=4$$$ and you query a $$$3 \times 5$$$ rectangle, we will measure its side lengths as $$$3 \times 6$$$ and report $$$18$$$ back to you. Find the value of $$$x$$$. You can ask at most $$$\mathbf{10}$$$ queries. Input: Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Output: None Examples: input: 2 18 25 9999 output: ? 3 5 ? 4 4 ! 4 ? 99 100 ! 100 Note: In the first test, the interaction proceeds as follows. SolutionJuryExplanation$$$\texttt{2}$$$There are 2 test cases.$$$\texttt{? 3 5}$$$$$$\texttt{18}$$$Secretly, the jury picked $$$x=4$$$. The solution requests the $$$3 \times 5$$$ rectangle, and the jury responds with $$$3 \times 6 = 18$$$, as described in the statement.$$$\texttt{? 4 4}$$$$$$\texttt{25}$$$The solution requests the $$$4 \times 4$$$ rectangle, which the jury measures as $$$5 \times 5$$$ and responds with $$$25$$$.$$$\texttt{! 4}$$$The solution has somehow determined that $$$x=4$$$, and outputs it. Since the output is correct, the jury continues to the next test case.$$$\texttt{? 99 100}$$$$$$\texttt{1}$$$Secretly, the jury picked $$$x=100$$$. The solution requests the $$$99 \times 100$$$ rectangle, which the jury measures as $$$99 \times 101$$$ and responds with $$$9999$$$.$$$\texttt{! 100}$$$The solution has somehow determined that $$$x=100$$$, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.
1999G2
https://codeforces.com/problemset/problem/1999/G2
Ruler (hard version)
1,700
[ "ternary search", "interactive", "binary search" ]
Div. 4
1,000
256
This is the hard version of the problem. The only difference between the two versions is that in this version, you can make at most $$$\mathbf{7}$$$ queries. This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read the guide for participants. We have a secret ruler that is missing one number $$$x$$$ ($$$2 \leq x \leq 999$$$). When you measure an object of length $$$y$$$, the ruler reports the following values: - If $$$y < x$$$, the ruler (correctly) measures the object as having length $$$y$$$. - If $$$y \geq x$$$, the ruler incorrectly measures the object as having length $$$y+1$$$. The ruler above is missing the number $$$4$$$, so it correctly measures the first segment as length $$$3$$$ but incorrectly measures the second segment as length $$$6$$$ even though it is actually $$$5$$$. You need to find the value of $$$x$$$. To do that, you can make queries of the following form: - $$$\texttt{?}~a~b$$$ — in response, we will measure the side lengths of an $$$a \times b$$$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $$$x=4$$$ and you query a $$$3 \times 5$$$ rectangle, we will measure its side lengths as $$$3 \times 6$$$ and report $$$18$$$ back to you. Find the value of $$$x$$$. You can ask at most $$$\mathbf{7}$$$ queries.
Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.
null
[ [ "2\n\n18\n\n25\n\n\n9999", "? 3 5\n\n? 4 4\n\n! 4\n? 99 100\n\n! 100" ] ]
In the first test, the interaction proceeds as follows. SolutionJuryExplanation$$$\texttt{2}$$$There are 2 test cases.$$$\texttt{? 3 5}$$$$$$\texttt{18}$$$Secretly, the jury picked $$$x=4$$$. The solution requests the $$$3 \times 5$$$ rectangle, and the jury responds with $$$3 \times 6 = 18$$$, as described in the statement.$$$\texttt{? 4 4}$$$$$$\texttt{25}$$$The solution requests the $$$4 \times 4$$$ rectangle, which the jury measures as $$$5 \times 5$$$ and responds with $$$25$$$.$$$\texttt{! 4}$$$The solution has somehow determined that $$$x=4$$$, and outputs it. Since the output is correct, the jury continues to the next test case.$$$\texttt{? 99 100}$$$$$$\texttt{1}$$$Secretly, the jury picked $$$x=100$$$. The solution requests the $$$99 \times 100$$$ rectangle, which the jury measures as $$$99 \times 101$$$ and responds with $$$9999$$$.$$$\texttt{! 100}$$$The solution has somehow determined that $$$x=100$$$, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.
Title: Ruler (hard version) time_limit_ms: 1000 memory_limit_mb: 256 Description: This is the hard version of the problem. The only difference between the two versions is that in this version, you can make at most $$$\mathbf{7}$$$ queries. This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read the guide for participants. We have a secret ruler that is missing one number $$$x$$$ ($$$2 \leq x \leq 999$$$). When you measure an object of length $$$y$$$, the ruler reports the following values: - If $$$y < x$$$, the ruler (correctly) measures the object as having length $$$y$$$. - If $$$y \geq x$$$, the ruler incorrectly measures the object as having length $$$y+1$$$. The ruler above is missing the number $$$4$$$, so it correctly measures the first segment as length $$$3$$$ but incorrectly measures the second segment as length $$$6$$$ even though it is actually $$$5$$$. You need to find the value of $$$x$$$. To do that, you can make queries of the following form: - $$$\texttt{?}~a~b$$$ — in response, we will measure the side lengths of an $$$a \times b$$$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $$$x=4$$$ and you query a $$$3 \times 5$$$ rectangle, we will measure its side lengths as $$$3 \times 6$$$ and report $$$18$$$ back to you. Find the value of $$$x$$$. You can ask at most $$$\mathbf{7}$$$ queries. Input: Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Output: None Examples: input: 2 18 25 9999 output: ? 3 5 ? 4 4 ! 4 ? 99 100 ! 100 Note: In the first test, the interaction proceeds as follows. SolutionJuryExplanation$$$\texttt{2}$$$There are 2 test cases.$$$\texttt{? 3 5}$$$$$$\texttt{18}$$$Secretly, the jury picked $$$x=4$$$. The solution requests the $$$3 \times 5$$$ rectangle, and the jury responds with $$$3 \times 6 = 18$$$, as described in the statement.$$$\texttt{? 4 4}$$$$$$\texttt{25}$$$The solution requests the $$$4 \times 4$$$ rectangle, which the jury measures as $$$5 \times 5$$$ and responds with $$$25$$$.$$$\texttt{! 4}$$$The solution has somehow determined that $$$x=4$$$, and outputs it. Since the output is correct, the jury continues to the next test case.$$$\texttt{? 99 100}$$$$$$\texttt{1}$$$Secretly, the jury picked $$$x=100$$$. The solution requests the $$$99 \times 100$$$ rectangle, which the jury measures as $$$99 \times 101$$$ and responds with $$$9999$$$.$$$\texttt{! 100}$$$The solution has somehow determined that $$$x=100$$$, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.
2041A
https://codeforces.com/problemset/problem/2041/A
The Bento Box Adventure
1,300
[ "implementation", "sortings" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
1,000
1,024
Image generated by ChatGPT 4o. Boxed meals in Taiwan are very common, offering convenient and affordable nutrition-balanced choices for students and office workers. These meals typically include a variety of vegetables, protein, and rice, providing a well-rounded diet. With numerous options available at local self-service restaurants, they are a popular choice for those looking for a quick, healthy lunch. There are five Taiwanese self-service restaurants numbered from 1 to 5. You plan to visit a different restaurant each day from Monday to Friday to pack a boxed meal for lunch. You've already visited one restaurant from Monday to Thursday, each time visiting a different one. Now, it's Friday, and you want to visit the last remaining restaurant that you haven't been to yet this week. Write a program that takes as input the four restaurants you've already visited (one for each day from Monday to Thursday) and outputs the restaurant you should visit on Friday.
A single line of input containing four integers $$$a, b, c, d$$$, each between 1 and 5 (inclusive), representing the restaurant numbers you visited from Monday to Thursday, in order. - $$$1\le a, b, c, d\le 5$$$ - All four numbers will be different.
Output the restaurant number you should visit on Friday.
[ [ "1 3 2 5", "4" ], [ "2 5 4 3", "1" ] ]
Title: The Bento Box Adventure time_limit_ms: 1000 memory_limit_mb: 1024 Description: Image generated by ChatGPT 4o. Boxed meals in Taiwan are very common, offering convenient and affordable nutrition-balanced choices for students and office workers. These meals typically include a variety of vegetables, protein, and rice, providing a well-rounded diet. With numerous options available at local self-service restaurants, they are a popular choice for those looking for a quick, healthy lunch. There are five Taiwanese self-service restaurants numbered from 1 to 5. You plan to visit a different restaurant each day from Monday to Friday to pack a boxed meal for lunch. You've already visited one restaurant from Monday to Thursday, each time visiting a different one. Now, it's Friday, and you want to visit the last remaining restaurant that you haven't been to yet this week. Write a program that takes as input the four restaurants you've already visited (one for each day from Monday to Thursday) and outputs the restaurant you should visit on Friday. Input: A single line of input containing four integers $$$a, b, c, d$$$, each between 1 and 5 (inclusive), representing the restaurant numbers you visited from Monday to Thursday, in order. - $$$1\le a, b, c, d\le 5$$$ - All four numbers will be different. Output: Output the restaurant number you should visit on Friday. Examples: input: 1 3 2 5 output: 4 input: 2 5 4 3 output: 1 Note:
2041B
https://codeforces.com/problemset/problem/2041/B
Bowling Frame
1,200
[ "brute force", "binary search", "math" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
1,000
1,024
Bowling is a national sport in Taiwan; everyone in the country plays the sport on a daily basis since their youth. Naturally, there are a lot of bowling alleys all over the country, and the competition between them is as intense as you can imagine. Maw-Shang owns one such bowling alley. To stand out from other competitors in the industry and draw attention from customers, he decided to hold a special event every month that features various unorthodox bowling rules. For the event this month, he came up with a new version of the game called X-pin bowling. In the traditional $$$10$$$-pin bowling game, a frame is built out of ten bowling pins forming a triangular shape of side length four. The pin closest to the player forms the first row, and the two pins behind it form the second row, and so on. Unlike the standard version, the game of $$$X$$$-pin bowling Maw-Shang designed allows a much larger number of pins that form a larger frame. The following figure shows a standard $$$10$$$-pin frame on the left, and on the right it shows a $$$21$$$-pin frame that forms a triangular shape of side length six which is allowed in the game of $$$X$$$-pin bowling. Being the national sport, the government of Taiwan strictly regulates and standardizes the manufacturing of bowling pins. There are two types of bowling pins allowed, one in black and the other in white, and the bowling alley Maw-Shang owns has $$$w$$$ white pins and $$$b$$$ black pins. To make this new game exciting for the customers, Maw-Shang wants to build the largest possible frame from these $$$w+b$$$ pins. However, even though he is okay with using both colors in building the frame, for aesthetic reasons, Maw-Shang still wants the colors of the pins on the same row to be identical. For example, the following figure shows two possible frames of side length six, but only the left one is acceptable to Maw-Shang since the other one has white and black pins mixed in the third row. The monthly special event is happening in just a few hours. Please help Maw-Shang calculate the side length of the largest frame that he can build from his $$$w+b$$$ pins!
The first line of the input contains a single integer $$$t$$$, the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$w$$$ and $$$b$$$, the number of white and black pins, respectively. - $$$1 \leq t \leq 100$$$ - $$$0 \leq w, b \leq 10^9$$$
For each test case, output in a single line the side length $$$k$$$ of the largest pin satisfying Maw-Shang's requirement you can build with the given pins.
[ [ "4\n1 2\n3 2\n3 3\n12 0", "2\n2\n3\n4" ] ]
Title: Bowling Frame time_limit_ms: 1000 memory_limit_mb: 1024 Description: Bowling is a national sport in Taiwan; everyone in the country plays the sport on a daily basis since their youth. Naturally, there are a lot of bowling alleys all over the country, and the competition between them is as intense as you can imagine. Maw-Shang owns one such bowling alley. To stand out from other competitors in the industry and draw attention from customers, he decided to hold a special event every month that features various unorthodox bowling rules. For the event this month, he came up with a new version of the game called X-pin bowling. In the traditional $$$10$$$-pin bowling game, a frame is built out of ten bowling pins forming a triangular shape of side length four. The pin closest to the player forms the first row, and the two pins behind it form the second row, and so on. Unlike the standard version, the game of $$$X$$$-pin bowling Maw-Shang designed allows a much larger number of pins that form a larger frame. The following figure shows a standard $$$10$$$-pin frame on the left, and on the right it shows a $$$21$$$-pin frame that forms a triangular shape of side length six which is allowed in the game of $$$X$$$-pin bowling. Being the national sport, the government of Taiwan strictly regulates and standardizes the manufacturing of bowling pins. There are two types of bowling pins allowed, one in black and the other in white, and the bowling alley Maw-Shang owns has $$$w$$$ white pins and $$$b$$$ black pins. To make this new game exciting for the customers, Maw-Shang wants to build the largest possible frame from these $$$w+b$$$ pins. However, even though he is okay with using both colors in building the frame, for aesthetic reasons, Maw-Shang still wants the colors of the pins on the same row to be identical. For example, the following figure shows two possible frames of side length six, but only the left one is acceptable to Maw-Shang since the other one has white and black pins mixed in the third row. The monthly special event is happening in just a few hours. Please help Maw-Shang calculate the side length of the largest frame that he can build from his $$$w+b$$$ pins! Input: The first line of the input contains a single integer $$$t$$$, the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$w$$$ and $$$b$$$, the number of white and black pins, respectively. - $$$1 \leq t \leq 100$$$ - $$$0 \leq w, b \leq 10^9$$$ Output: For each test case, output in a single line the side length $$$k$$$ of the largest pin satisfying Maw-Shang's requirement you can build with the given pins. Examples: input: 4 1 2 3 2 3 3 12 0 output: 2 2 3 4 Note:
2041C
https://codeforces.com/problemset/problem/2041/C
Cube
2,000
[ "dfs and similar", "bitmasks", "dp" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
3,000
1,024
You are given an $$$n\times n\times n$$$ big three-dimensional cube that contains $$$n^3$$$ numbers. You have to choose $$$n$$$ of those numbers so that their sum is as small as possible. It is, however, forbidden to choose two numbers that are located in the same plane. That is, if we identify the positions in the cube by three Cartesian coordinates, then choosing two numbers from positions $$$(x,y,z)$$$ and $$$(x',y',z')$$$ is forbidden if $$$x=x'$$$, $$$y=y'$$$, or $$$z=z'$$$.
The input consists of the number $$$n$$$ followed by $$$n^3$$$ numbers in the cube. The numbers are presented as $$$n$$$ two-dimensional matrices, one for each layer of the cube. More precisely, there will be $$$n^2$$$ lines follow, each having $$$n$$$ numbers. For each $$$x, y, z$$$ ($$$1\le x, y, z\le n$$$), the number at the position $$$(x, y, z)$$$ is listed as the $$$z$$$-th number in the $$$((x-1)\times n+y)$$$-th line. - $$$2 \leq n \leq 12$$$ - All numbers in the cube are integers between $$$0$$$ and $$$2\times 10^7$$$.
The output consists of a single number. It is the minimum sum of $$$n$$$ numbers chosen from the cube according to the above rules.
[ [ "3\n1 2 3\n4 5 6\n7 8 9\n1 1 1\n2 2 2\n3 3 3\n4 3 0\n2 1 4\n9 8 9", "5" ] ]
Title: Cube time_limit_ms: 3000 memory_limit_mb: 1024 Description: You are given an $$$n\times n\times n$$$ big three-dimensional cube that contains $$$n^3$$$ numbers. You have to choose $$$n$$$ of those numbers so that their sum is as small as possible. It is, however, forbidden to choose two numbers that are located in the same plane. That is, if we identify the positions in the cube by three Cartesian coordinates, then choosing two numbers from positions $$$(x,y,z)$$$ and $$$(x',y',z')$$$ is forbidden if $$$x=x'$$$, $$$y=y'$$$, or $$$z=z'$$$. Input: The input consists of the number $$$n$$$ followed by $$$n^3$$$ numbers in the cube. The numbers are presented as $$$n$$$ two-dimensional matrices, one for each layer of the cube. More precisely, there will be $$$n^2$$$ lines follow, each having $$$n$$$ numbers. For each $$$x, y, z$$$ ($$$1\le x, y, z\le n$$$), the number at the position $$$(x, y, z)$$$ is listed as the $$$z$$$-th number in the $$$((x-1)\times n+y)$$$-th line. - $$$2 \leq n \leq 12$$$ - All numbers in the cube are integers between $$$0$$$ and $$$2\times 10^7$$$. Output: The output consists of a single number. It is the minimum sum of $$$n$$$ numbers chosen from the cube according to the above rules. Examples: input: 3 1 2 3 4 5 6 7 8 9 1 1 1 2 2 2 3 3 3 4 3 0 2 1 4 9 8 9 output: 5 Note:
2041D
https://codeforces.com/problemset/problem/2041/D
Drunken Maze
1,700
[ "dfs and similar", "brute force", "graphs", "shortest paths" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
2,000
1,024
Image generated by ChatGPT 4o. You are given a two-dimensional maze with a start and end position. Your task is to find the fastest way to get from the start to the end position. The fastest way is to make the minimum number of steps where one step is going left, right, up, or down. Of course, you cannot walk through walls. There is, however, a catch: If you make more than three steps in the same direction, you lose balance and fall down. Therefore, it is forbidden to make more than three consecutive steps in the same direction. It is okay to walk three times to the right, then one step to the left, and then again three steps to the right. This has the same effect as taking five steps to the right, but is slower.
The first line contains two numbers $$$n$$$ and $$$m$$$, which are the height and width of the maze. This is followed by an ASCII-representation of the maze where $$$\tt{\#}$$$ is a wall, $$$\tt{.}$$$ is an empty space, and $$$\tt S$$$ and $$$\tt T$$$ are the start and end positions. - $$$12 \leq n\times m \leq 200000$$$. - $$$3\leq n,m \leq 10000$$$. - Characters are only $$$\tt{.\#ST}$$$ and there is exactly one $$$\tt{S}$$$ and one $$$\tt{T}$$$. - The outer borders are only $$$\tt{\#}$$$ (walls).
The minimum number of steps to reach the end position from the start position or -1 if that is impossible.
[ [ "7 12\n############\n#S........T#\n#.########.#\n#..........#\n#..........#\n#..#..#....#\n############", "15" ], [ "5 8\n########\n#......#\n#.####.#\n#...T#S#\n########", "14" ], [ "5 8\n########\n#.#S...#\n#.####.#\n#...T#.#\n########", "-1" ] ]
Title: Drunken Maze time_limit_ms: 2000 memory_limit_mb: 1024 Description: Image generated by ChatGPT 4o. You are given a two-dimensional maze with a start and end position. Your task is to find the fastest way to get from the start to the end position. The fastest way is to make the minimum number of steps where one step is going left, right, up, or down. Of course, you cannot walk through walls. There is, however, a catch: If you make more than three steps in the same direction, you lose balance and fall down. Therefore, it is forbidden to make more than three consecutive steps in the same direction. It is okay to walk three times to the right, then one step to the left, and then again three steps to the right. This has the same effect as taking five steps to the right, but is slower. Input: The first line contains two numbers $$$n$$$ and $$$m$$$, which are the height and width of the maze. This is followed by an ASCII-representation of the maze where $$$\tt{\#}$$$ is a wall, $$$\tt{.}$$$ is an empty space, and $$$\tt S$$$ and $$$\tt T$$$ are the start and end positions. - $$$12 \leq n\times m \leq 200000$$$. - $$$3\leq n,m \leq 10000$$$. - Characters are only $$$\tt{.\#ST}$$$ and there is exactly one $$$\tt{S}$$$ and one $$$\tt{T}$$$. - The outer borders are only $$$\tt{\#}$$$ (walls). Output: The minimum number of steps to reach the end position from the start position or -1 if that is impossible. Examples: input: 7 12 ############ #S........T# #.########.# #..........# #..........# #..#..#....# ############ output: 15 input: 5 8 ######## #......# #.####.# #...T#S# ######## output: 14 input: 5 8 ######## #.#S...# #.####.# #...T#.# ######## output: -1 Note:
2041F
https://codeforces.com/problemset/problem/2041/F
Segmentation Folds
2,400
[ "dfs and similar", "brute force", "number theory" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
1,000
1,024
Peter loves folding segments. There is a segment on a number line occupying the interval $$$[\ell, r]$$$. Since it is the prime time for folding the segments, Peter decides to fold the segment carefully. In each step, he chooses one of the two following operations whenever possible: 1. Operation $$$\tt{LTR}$$$: he folds the segment from left to right, where $$$\ell$$$ coincides with a point $$$x$$$ ($$$\ell < x \le r$$$) such that $$$\ell+x$$$ is a prime number$$$^{\text{∗}}$$$. When Peter chooses this operation, he always chooses the largest possible value $$$x$$$. Note that the segment occupies the interval $$$[\frac{1}{2}(\ell+x), r]$$$ afterwards. 2. Operation $$$\tt{RTL}$$$: he folds the segment from right to left, where $$$r$$$ coincides with a point $$$x$$$ ($$$\ell \le x < r$$$) such that $$$r+x$$$ is a prime number. When Peter chooses this operation, he always chooses the smallest possible value $$$x$$$. Note that the segment occupies the interval $$$[\ell, \frac{1}{2}(r+x)]$$$ afterwards. A folding sequence refers to a sequence of operations specified above. Peter wants to fold the segment several times, resulting in the shortest possible interval whose length that cannot be further reduced. The length of an interval $$$[\ell, r]$$$ is defined naturally to be $$$r-\ell$$$. Let's consider the following example. Suppose that we are folding a segment initially occupying the interval $$$[1, 30]$$$. There are three folding sequences that lead to the shortest possible resulting interval, as shown in the following figure. Please help Peter determine the number of folding sequences such that the resulting interval has a shortest possible length. Output the number modulo $$$998244353$$$.
The first line contains an integer $$$t$$$, denoting the number of test cases. In each of the following $$$t$$$ lines, there are two integers $$$\ell$$$ and $$$r$$$. - $$$1 \le t \le 10$$$ - $$$1 \le \ell < r \le 10^{12}$$$ - $$$r - \ell \le 10^5$$$
For each test case, please output a line denoting the number of ways to fold the given segment such that the resulting segment has the shortest possible length, modulo $$$998244353$$$.
[ [ "3\n1 30\n16 18\n142857 240135", "3\n1\n63" ] ]
Title: Segmentation Folds time_limit_ms: 1000 memory_limit_mb: 1024 Description: Peter loves folding segments. There is a segment on a number line occupying the interval $$$[\ell, r]$$$. Since it is the prime time for folding the segments, Peter decides to fold the segment carefully. In each step, he chooses one of the two following operations whenever possible: 1. Operation $$$\tt{LTR}$$$: he folds the segment from left to right, where $$$\ell$$$ coincides with a point $$$x$$$ ($$$\ell < x \le r$$$) such that $$$\ell+x$$$ is a prime number$$$^{\text{∗}}$$$. When Peter chooses this operation, he always chooses the largest possible value $$$x$$$. Note that the segment occupies the interval $$$[\frac{1}{2}(\ell+x), r]$$$ afterwards. 2. Operation $$$\tt{RTL}$$$: he folds the segment from right to left, where $$$r$$$ coincides with a point $$$x$$$ ($$$\ell \le x < r$$$) such that $$$r+x$$$ is a prime number. When Peter chooses this operation, he always chooses the smallest possible value $$$x$$$. Note that the segment occupies the interval $$$[\ell, \frac{1}{2}(r+x)]$$$ afterwards. A folding sequence refers to a sequence of operations specified above. Peter wants to fold the segment several times, resulting in the shortest possible interval whose length that cannot be further reduced. The length of an interval $$$[\ell, r]$$$ is defined naturally to be $$$r-\ell$$$. Let's consider the following example. Suppose that we are folding a segment initially occupying the interval $$$[1, 30]$$$. There are three folding sequences that lead to the shortest possible resulting interval, as shown in the following figure. Please help Peter determine the number of folding sequences such that the resulting interval has a shortest possible length. Output the number modulo $$$998244353$$$. Input: The first line contains an integer $$$t$$$, denoting the number of test cases. In each of the following $$$t$$$ lines, there are two integers $$$\ell$$$ and $$$r$$$. - $$$1 \le t \le 10$$$ - $$$1 \le \ell < r \le 10^{12}$$$ - $$$r - \ell \le 10^5$$$ Output: For each test case, please output a line denoting the number of ways to fold the given segment such that the resulting segment has the shortest possible length, modulo $$$998244353$$$. Examples: input: 3 1 30 16 18 142857 240135 output: 3 1 63 Note:
2041H
https://codeforces.com/problemset/problem/2041/H
Sheet Music
2,300
[ "combinatorics", "dp", "math" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
1,000
1,024
Image generated by ChatGPT 4o. Alice likes singing. As a singing enthusiast, Alice has listened to countless songs and has tried singing them many times. However, occasionally, some songs make Alice feel bored. After some research, Alice believes that this is because even though the songs she chose are all different, due to her instinctive preference, they all turn out to be musically similar to one another. To thoroughly analyze this, Alice decided to study the sheet music of the songs. For convenience, Alice represented a song of length $$$n$$$ as an integer sequence $$$a_1, a_2, \ldots, a_n$$$, where $$$a_i$$$ is the pitch of the $$$i$$$-th note. Then she defined the musical equivalence between songs. Two songs $$$a_1, a_2, \ldots, a_n$$$ and $$$b_1, b_2, \ldots, b_n$$$ of length $$$n$$$ are musically equivalent if for all $$$1\leq i<n$$$, both $$$a_i, a_{i+1}$$$ and $$$b_{i}, b_{i+1}$$$ have the same pitch relationship. More specifically, $$$a_i, a_{i+1}$$$ and $$$b_i, b_{i+1}$$$ have the same pitch relationship if either - $$$a_i < a_{i + 1}$$$ and $$$b_i < b_{i + 1}$$$, - $$$a_i = a_{i + 1}$$$ and $$$b_i = b_{i + 1}$$$, or - $$$a_i > a_{i + 1}$$$ and $$$b_i > b_{i + 1}$$$. Having practiced consistently for a long time, Alice is able to sing any note in the range of $$$[1, k]$$$. She wants to know how many different songs of length $$$n$$$ within her range there are, if we treat musically equivalent songs as the same one. Can you help her calculate the number? Since the answer might be large, print the answer modulo $$$998244353$$$.
The only line contains two integers $$$n, k$$$. - $$$1\leq n\leq 10^6$$$ - $$$1\leq k \leq 10^9$$$
Output the number of different songs modulo $$$998244353$$$.
[ [ "3 2", "7" ], [ "5 3", "67" ] ]
Title: Sheet Music time_limit_ms: 1000 memory_limit_mb: 1024 Description: Image generated by ChatGPT 4o. Alice likes singing. As a singing enthusiast, Alice has listened to countless songs and has tried singing them many times. However, occasionally, some songs make Alice feel bored. After some research, Alice believes that this is because even though the songs she chose are all different, due to her instinctive preference, they all turn out to be musically similar to one another. To thoroughly analyze this, Alice decided to study the sheet music of the songs. For convenience, Alice represented a song of length $$$n$$$ as an integer sequence $$$a_1, a_2, \ldots, a_n$$$, where $$$a_i$$$ is the pitch of the $$$i$$$-th note. Then she defined the musical equivalence between songs. Two songs $$$a_1, a_2, \ldots, a_n$$$ and $$$b_1, b_2, \ldots, b_n$$$ of length $$$n$$$ are musically equivalent if for all $$$1\leq i<n$$$, both $$$a_i, a_{i+1}$$$ and $$$b_{i}, b_{i+1}$$$ have the same pitch relationship. More specifically, $$$a_i, a_{i+1}$$$ and $$$b_i, b_{i+1}$$$ have the same pitch relationship if either - $$$a_i < a_{i + 1}$$$ and $$$b_i < b_{i + 1}$$$, - $$$a_i = a_{i + 1}$$$ and $$$b_i = b_{i + 1}$$$, or - $$$a_i > a_{i + 1}$$$ and $$$b_i > b_{i + 1}$$$. Having practiced consistently for a long time, Alice is able to sing any note in the range of $$$[1, k]$$$. She wants to know how many different songs of length $$$n$$$ within her range there are, if we treat musically equivalent songs as the same one. Can you help her calculate the number? Since the answer might be large, print the answer modulo $$$998244353$$$. Input: The only line contains two integers $$$n, k$$$. - $$$1\leq n\leq 10^6$$$ - $$$1\leq k \leq 10^9$$$ Output: Output the number of different songs modulo $$$998244353$$$. Examples: input: 3 2 output: 7 input: 5 3 output: 67 Note:
2041I
https://codeforces.com/problemset/problem/2041/I
Auto Complete
2,300
[ "binary search", "implementation", "data structures", "sortings", "hashing", "strings", "trees" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
4,000
1,024
You are designing a snazzy new text editor, and you want to add a nifty auto-complete feature to help users save time. Here is how it will work: if a user types "App", your editor will magically suggest the word "Application"! Even better, users can personalize the words that auto-complete in your editor. Your editor will support 4 kinds of operations (Let's say the current text in your editor is $$$t$$$): 1. Add an auto complete pattern $$$p_i$$$. 2. Delete an auto complete pattern $$$p_i$$$. 3. Append a string $$$s$$$ to the end of $$$t$$$. 4. Delete $$$c$$$ characters from the end of $$$t$$$. Note that if $$$c$$$ is larger then the length of $$$t$$$, delete all the characters from $$$t$$$. After each action, your editor should suggest an auto-complete candidate $$$i$$$ that matches the following criteria: 1. The string $$$p_i$$$ has a prefix equal to $$$t$$$. 2. If there are multiple $$$p_i$$$, pick the longest one. 3. If there are still multiple $$$p_i$$$, pick the one with the smallest lexicographic order. 4. If there are still multiple $$$p_i$$$, pick the one with the smallest ID. For example, let us say we have three candidates: "alice", "bob", and "charlie", with ID 1, 2, and 3. At first, there is nothing on the screen, so "charlie" (3) should be suggested because it is the longest. Then, let us say the user types "b". You should suggest "bob" (2) because it is the only one that starts with "b". Finally, let us say the user type "body". You should print -1 because there is no matched pattern.
The first line contains an integer $$$n$$$, followed by $$$n$$$ lines, with each line containing an action. There are four types of actions: 1. $$$i$$$ $$$p_i$$$ 2. $$$i$$$ 3. $$$s$$$ 4. $$$c$$$ - $$$1 \leq n \leq 10^6$$$ - The total number of characters in all $$$p_i$$$ and $$$s$$$ does not exceed $$$2\times 10^6$$$. - $$$1 \leq c \leq 2\times 10^6$$$ - The strings $$$p_i$$$ and $$$s$$$ may contain any printable characters, excluding all space characters (ASCII number in the range from $$$33$$$ to $$$126$$$). - The ID $$$i$$$ for each add operation is unique. - The ID $$$i$$$ for each delete is guaranteed to be valid. - Each ID $$$i$$$ satisfies $$$0\le i \le n$$$.
The program should output $$$n$$$ lines. For each action, output an integer $$$i$$$, which means that after the action, $$$p_i$$$ is the suggested auto complete candidate. If there is no $$$p_i$$$ that matches the requirement, output -1.
[ [ "6\nadd 1 pattern1_alice\nadd 2 pattern2_bob\nadd 3 pattern3_charlie\nappend pattern\nappend 2_bobabc\nbackspace 3", "1\n1\n3\n3\n-1\n2" ], [ "6\nappend pattern\nadd 1 pattern1_alice____\nadd 2 pattern2_bob______\nadd 3 pattern3_charlie__\ndelete 1\ndelete 2", "-1\n1\n1\n1\n2\n3" ] ]
Title: Auto Complete time_limit_ms: 4000 memory_limit_mb: 1024 Description: You are designing a snazzy new text editor, and you want to add a nifty auto-complete feature to help users save time. Here is how it will work: if a user types "App", your editor will magically suggest the word "Application"! Even better, users can personalize the words that auto-complete in your editor. Your editor will support 4 kinds of operations (Let's say the current text in your editor is $$$t$$$): 1. Add an auto complete pattern $$$p_i$$$. 2. Delete an auto complete pattern $$$p_i$$$. 3. Append a string $$$s$$$ to the end of $$$t$$$. 4. Delete $$$c$$$ characters from the end of $$$t$$$. Note that if $$$c$$$ is larger then the length of $$$t$$$, delete all the characters from $$$t$$$. After each action, your editor should suggest an auto-complete candidate $$$i$$$ that matches the following criteria: 1. The string $$$p_i$$$ has a prefix equal to $$$t$$$. 2. If there are multiple $$$p_i$$$, pick the longest one. 3. If there are still multiple $$$p_i$$$, pick the one with the smallest lexicographic order. 4. If there are still multiple $$$p_i$$$, pick the one with the smallest ID. For example, let us say we have three candidates: "alice", "bob", and "charlie", with ID 1, 2, and 3. At first, there is nothing on the screen, so "charlie" (3) should be suggested because it is the longest. Then, let us say the user types "b". You should suggest "bob" (2) because it is the only one that starts with "b". Finally, let us say the user type "body". You should print -1 because there is no matched pattern. Input: The first line contains an integer $$$n$$$, followed by $$$n$$$ lines, with each line containing an action. There are four types of actions: 1. $$$i$$$ $$$p_i$$$ 2. $$$i$$$ 3. $$$s$$$ 4. $$$c$$$ - $$$1 \leq n \leq 10^6$$$ - The total number of characters in all $$$p_i$$$ and $$$s$$$ does not exceed $$$2\times 10^6$$$. - $$$1 \leq c \leq 2\times 10^6$$$ - The strings $$$p_i$$$ and $$$s$$$ may contain any printable characters, excluding all space characters (ASCII number in the range from $$$33$$$ to $$$126$$$). - The ID $$$i$$$ for each add operation is unique. - The ID $$$i$$$ for each delete is guaranteed to be valid. - Each ID $$$i$$$ satisfies $$$0\le i \le n$$$. Output: The program should output $$$n$$$ lines. For each action, output an integer $$$i$$$, which means that after the action, $$$p_i$$$ is the suggested auto complete candidate. If there is no $$$p_i$$$ that matches the requirement, output -1. Examples: input: 6 add 1 pattern1_alice add 2 pattern2_bob add 3 pattern3_charlie append pattern append 2_bobabc backspace 3 output: 1 1 3 3 -1 2 input: 6 append pattern add 1 pattern1_alice____ add 2 pattern2_bob______ add 3 pattern3_charlie__ delete 1 delete 2 output: -1 1 1 1 2 3 Note:
2041M
https://codeforces.com/problemset/problem/2041/M
Selection Sort
2,000
[ "two pointers", "data structures", "binary search", "greedy" ]
Unrated, Online Mirror, ICPC Rules, Preferably Teams
2,000
1,024
Every student enrolled in the algorithms course is required to submit an assignment this week. The task is to implement an $$$O(n^2)$$$-time algorithm to sort $$$n$$$ given integers in non-decreasing order. Alice has already completed her assignment, and her implementation is shown below. While you have access to Alice's code, you prefer not to simply copy it. Instead, you want to use Alice's sorting function as a building block for your own solution. There are two ways as listed below you can utilize her function, but each of them can be applied at most once. The order in which these two operations are invoked can be arbitrary. - Prefix sort: choose a length $$$i \in \{1, 2, \ldots, n\}$$$ and call $$$\texttt{alicesort(}s, i\texttt{)}$$$. This sorts the first $$$i$$$ elements in the array $$$s$$$. - Suffix sort: choose a length $$$i \in \{1, 2, \ldots, n\}$$$ and call $$$\texttt{alicesort(}s+n-i, i\texttt{)}$$$. This sorts the last $$$i$$$ elements in the array $$$s$$$. Due to the time complexity of the sorting algorithm, the cost of performing either a prefix or suffix sort is $$$i^2$$$, where $$$i$$$ is the length of the chosen subarray. Your goal is to determine the minimum cost to sort the input array $$$s$$$ of $$$n$$$ integers in non-decreasing order using Alice's function, following the rules mentioned above. For example, Let $$$s=[3,2,5,5,4,1]$$$. We can first perform a suffix sort of length $$$4$$$, and the array becomes $$$[3,2,1,4,5,5]$$$. Then, we perform a prefix sort of length $$$3$$$, and the array becomes $$$[1,2,3,4,5,5]$$$, which is a sorted array. The cost is $$$4^2+3^2=25$$$. Here is another example, let $$$s=[4,3,2,1]$$$. We can complete the sorting by performing only a prefix sort of length $$$4$$$, and the cost is $$$4^2=16$$$.
The first line contains exactly one integer $$$n$$$ which indicates the number of integers in the array $$$s$$$. The second line contains the $$$n$$$ integers in $$$s=[s_0, s_1, \ldots, s_{n-1}]$$$. - $$$1 \le n \le 10^6$$$ - For all $$$i$$$ ($$$0\le i < n$$$), $$$0\le s_i < 2^{31}-1$$$.
Output an integer on a line, indicating the minimum cost to sort the input array $$$s$$$ of $$$n$$$ integers in non-decreasing order using Alice's function, following the rules mentioned above.
[ [ "6\n3 2 5 5 4 1", "25" ], [ "4\n4 3 2 1", "16" ] ]
Title: Selection Sort time_limit_ms: 2000 memory_limit_mb: 1024 Description: Every student enrolled in the algorithms course is required to submit an assignment this week. The task is to implement an $$$O(n^2)$$$-time algorithm to sort $$$n$$$ given integers in non-decreasing order. Alice has already completed her assignment, and her implementation is shown below. While you have access to Alice's code, you prefer not to simply copy it. Instead, you want to use Alice's sorting function as a building block for your own solution. There are two ways as listed below you can utilize her function, but each of them can be applied at most once. The order in which these two operations are invoked can be arbitrary. - Prefix sort: choose a length $$$i \in \{1, 2, \ldots, n\}$$$ and call $$$\texttt{alicesort(}s, i\texttt{)}$$$. This sorts the first $$$i$$$ elements in the array $$$s$$$. - Suffix sort: choose a length $$$i \in \{1, 2, \ldots, n\}$$$ and call $$$\texttt{alicesort(}s+n-i, i\texttt{)}$$$. This sorts the last $$$i$$$ elements in the array $$$s$$$. Due to the time complexity of the sorting algorithm, the cost of performing either a prefix or suffix sort is $$$i^2$$$, where $$$i$$$ is the length of the chosen subarray. Your goal is to determine the minimum cost to sort the input array $$$s$$$ of $$$n$$$ integers in non-decreasing order using Alice's function, following the rules mentioned above. For example, Let $$$s=[3,2,5,5,4,1]$$$. We can first perform a suffix sort of length $$$4$$$, and the array becomes $$$[3,2,1,4,5,5]$$$. Then, we perform a prefix sort of length $$$3$$$, and the array becomes $$$[1,2,3,4,5,5]$$$, which is a sorted array. The cost is $$$4^2+3^2=25$$$. Here is another example, let $$$s=[4,3,2,1]$$$. We can complete the sorting by performing only a prefix sort of length $$$4$$$, and the cost is $$$4^2=16$$$. Input: The first line contains exactly one integer $$$n$$$ which indicates the number of integers in the array $$$s$$$. The second line contains the $$$n$$$ integers in $$$s=[s_0, s_1, \ldots, s_{n-1}]$$$. - $$$1 \le n \le 10^6$$$ - For all $$$i$$$ ($$$0\le i < n$$$), $$$0\le s_i < 2^{31}-1$$$. Output: Output an integer on a line, indicating the minimum cost to sort the input array $$$s$$$ of $$$n$$$ integers in non-decreasing order using Alice's function, following the rules mentioned above. Examples: input: 6 3 2 5 5 4 1 output: 25 input: 4 4 3 2 1 output: 16 Note:
2045A
https://codeforces.com/problemset/problem/2045/A
Scrambled Scrabble
1,700
[ "greedy", "brute force" ]
Unrated, Online Mirror, ICPC Rules, Teams Preferred
1,000
1,024
You are playing a word game using a standard set of $$$26$$$ uppercase English letters: A — Z. In this game, you can form vowels and consonants as follows. - The letters A, E, I, O, and U can only form a vowel. - The letter Y can form either a vowel or a consonant. - Each of the remaining letters other than A, E, I, O, U, and Y can only form a consonant. - The string NG can form a single consonant when concatenated together. Denote a syllable as a concatenation of a consonant, a vowel, and a consonant in that order. A word is a concatenation of one or more syllables. You are given a string $$$S$$$ and you want to create a word from it. You are allowed to delete zero or more letters from $$$S$$$ and rearrange the remaining letters to form the word. Find the length of the longest word that can be created, or determine if no words can be created.
A single line consisting of a string $$$S$$$ ($$$1 \leq |S| \leq 5000$$$). The string $$$S$$$ consists of only uppercase English letters.
If a word cannot be created, output 0. Otherwise, output a single integer representing the length of longest word that can be created.
[ [ "ICPCJAKARTA", "9" ], [ "NGENG", "5" ], [ "YYY", "3" ], [ "DANGAN", "6" ], [ "AEIOUY", "0" ] ]
Explanation for the sample input/output #1 A possible longest word is JAKCARTAP, consisting of the syllables JAK, CAR, and TAP. Explanation for the sample input/output #2 The whole string $$$S$$$ is a word consisting of one syllable which is the concatenation of the consonant NG, the vowel E, and the consonant NG. Explanation for the sample input/output #3 The whole string $$$S$$$ is a word consisting of one syllable which is the concatenation of the consonant Y, the vowel Y, and the consonant Y. Explanation for the sample input/output #4 The whole string $$$S$$$ is a word consisting of two syllables: DAN and GAN.
Title: Scrambled Scrabble time_limit_ms: 1000 memory_limit_mb: 1024 Description: You are playing a word game using a standard set of $$$26$$$ uppercase English letters: A — Z. In this game, you can form vowels and consonants as follows. - The letters A, E, I, O, and U can only form a vowel. - The letter Y can form either a vowel or a consonant. - Each of the remaining letters other than A, E, I, O, U, and Y can only form a consonant. - The string NG can form a single consonant when concatenated together. Denote a syllable as a concatenation of a consonant, a vowel, and a consonant in that order. A word is a concatenation of one or more syllables. You are given a string $$$S$$$ and you want to create a word from it. You are allowed to delete zero or more letters from $$$S$$$ and rearrange the remaining letters to form the word. Find the length of the longest word that can be created, or determine if no words can be created. Input: A single line consisting of a string $$$S$$$ ($$$1 \leq |S| \leq 5000$$$). The string $$$S$$$ consists of only uppercase English letters. Output: If a word cannot be created, output 0. Otherwise, output a single integer representing the length of longest word that can be created. Examples: input: ICPCJAKARTA output: 9 input: NGENG output: 5 input: YYY output: 3 input: DANGAN output: 6 input: AEIOUY output: 0 Note: Explanation for the sample input/output #1 A possible longest word is JAKCARTAP, consisting of the syllables JAK, CAR, and TAP. Explanation for the sample input/output #2 The whole string $$$S$$$ is a word consisting of one syllable which is the concatenation of the consonant NG, the vowel E, and the consonant NG. Explanation for the sample input/output #3 The whole string $$$S$$$ is a word consisting of one syllable which is the concatenation of the consonant Y, the vowel Y, and the consonant Y. Explanation for the sample input/output #4 The whole string $$$S$$$ is a word consisting of two syllables: DAN and GAN.
2045B
https://codeforces.com/problemset/problem/2045/B
ICPC Square
2,000
[ "math", "number theory" ]
Unrated, Online Mirror, ICPC Rules, Teams Preferred
1,000
1,024
ICPC Square is a hotel provided by the ICPC Committee for the accommodation of the participants. It consists of $$$N$$$ floors (numbered from $$$1$$$ to $$$N$$$). This hotel has a very unique elevator. If a person is currently at floor $$$x$$$, by riding the elevator once, they can go to floor $$$y$$$ if and only if $$$y$$$ is a multiple of $$$x$$$ and $$$y - x \leq D$$$. You are currently at floor $$$S$$$. You want to go to the highest possible floor by riding the elevator zero or more times. Determine the highest floor you can reach.
A single line consisting of three integers $$$N$$$ $$$D$$$ $$$S$$$ ($$$2 \leq N \leq 10^{12}; 1 \leq D \leq N - 1; 1 \leq S \leq N$$$).
Output a single integer representing the highest floor you can reach by riding the elevator zero or more times.
[ [ "64 35 3", "60" ], [ "2024 2023 1273", "1273" ] ]
Explanation for the sample input/output #1 First, ride the elevator from floor $$$3$$$ to floor $$$15$$$. This is possible because $$$15$$$ is a multiple of $$$3$$$ and $$$15 - 3 \leq 35$$$. Then, ride the elevator from floor $$$15$$$ to floor $$$30$$$. This is possible because $$$30$$$ is a multiple of $$$15$$$ and $$$30 - 15 \leq 35$$$. Finally, ride the elevator from floor $$$30$$$ to floor $$$60$$$. This is possible because $$$60$$$ is a multiple of $$$30$$$ and $$$60 - 30 \leq 35$$$.
Title: ICPC Square time_limit_ms: 1000 memory_limit_mb: 1024 Description: ICPC Square is a hotel provided by the ICPC Committee for the accommodation of the participants. It consists of $$$N$$$ floors (numbered from $$$1$$$ to $$$N$$$). This hotel has a very unique elevator. If a person is currently at floor $$$x$$$, by riding the elevator once, they can go to floor $$$y$$$ if and only if $$$y$$$ is a multiple of $$$x$$$ and $$$y - x \leq D$$$. You are currently at floor $$$S$$$. You want to go to the highest possible floor by riding the elevator zero or more times. Determine the highest floor you can reach. Input: A single line consisting of three integers $$$N$$$ $$$D$$$ $$$S$$$ ($$$2 \leq N \leq 10^{12}; 1 \leq D \leq N - 1; 1 \leq S \leq N$$$). Output: Output a single integer representing the highest floor you can reach by riding the elevator zero or more times. Examples: input: 64 35 3 output: 60 input: 2024 2023 1273 output: 1273 Note: Explanation for the sample input/output #1 First, ride the elevator from floor $$$3$$$ to floor $$$15$$$. This is possible because $$$15$$$ is a multiple of $$$3$$$ and $$$15 - 3 \leq 35$$$. Then, ride the elevator from floor $$$15$$$ to floor $$$30$$$. This is possible because $$$30$$$ is a multiple of $$$15$$$ and $$$30 - 15 \leq 35$$$. Finally, ride the elevator from floor $$$30$$$ to floor $$$60$$$. This is possible because $$$60$$$ is a multiple of $$$30$$$ and $$$60 - 30 \leq 35$$$.
2045G
https://codeforces.com/problemset/problem/2045/G
X Aura
2,200
[ "shortest paths", "graphs", "math" ]
Unrated, Online Mirror, ICPC Rules, Teams Preferred
1,000
1,024
Mount ICPC can be represented as a grid of $$$R$$$ rows (numbered from $$$1$$$ to $$$R$$$) and $$$C$$$ columns (numbered from $$$1$$$ to $$$C$$$). The cell located at row $$$r$$$ and column $$$c$$$ is denoted as $$$(r, c)$$$ and has a height of $$$H_{r, c}$$$. Two cells are adjacent to each other if they share a side. Formally, $$$(r, c)$$$ is adjacent to $$$(r-1, c)$$$, $$$(r+1, c)$$$, $$$(r, c-1)$$$, and $$$(r, c+1)$$$, if any exists. You can move only between adjacent cells, and each move comes with a penalty. With an aura of an odd positive integer $$$X$$$, moving from a cell with height $$$h_1$$$ to a cell with height $$$h_2$$$ gives you a penalty of $$$(h_1 - h_2)^X$$$. Note that the penalty can be negative. You want to answer $$$Q$$$ independent scenarios. In each scenario, you start at the starting cell $$$(R_s, C_s)$$$ and you want to go to the destination cell $$$(R_f, C_f)$$$ with minimum total penalty. In some scenarios, the total penalty might become arbitrarily small; such a scenario is called invalid. Find the minimum total penalty to move from the starting cell to the destination cell, or determine if the scenario is invalid.
The first line consists of three integers $$$R$$$ $$$C$$$ $$$X$$$ ($$$1 \leq R, C \leq 1000; 1 \leq X \leq 9; X$$$ is an odd integer). Each of the next $$$R$$$ lines consists of a string $$$H_r$$$ of length $$$C$$$. Each character in $$$H_r$$$ is a number from 0 to 9. The $$$c$$$-th character of $$$H_r$$$ represents the height of cell $$$(r, c)$$$, or $$$H_{r, c}$$$. The next line consists of an integer $$$Q$$$ ($$$1 \leq Q \leq 100\,000)$$$. Each of the next $$$Q$$$ lines consists of four integers $$$R_s$$$ $$$C_s$$$ $$$R_f$$$ $$$C_f$$$ ($$$1 \leq R_s, R_f \leq R; 1 \leq C_s, C_f \leq C$$$).
For each scenario, output the following in a single line. If the scenario is invalid, output INVALID. Otherwise, output a single integer representing the minimum total penalty to move from the starting cell to the destination cell.
[ [ "3 4 1\n3359\n4294\n3681\n5\n1 1 3 4\n3 3 2 1\n2 2 1 4\n1 3 3 2\n1 1 1 1", "2\n4\n-7\n-1\n0" ], [ "2 4 5\n1908\n2023\n2\n1 1 2 4\n1 1 1 1", "INVALID\nINVALID" ], [ "3 3 9\n135\n357\n579\n2\n3 3 1 1\n2 2 2 2", "2048\n0" ] ]
Explanation for the sample input/output #1 For the first scenario, one of the solutions is to move as follows: $$$(1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4)$$$. The total penalty of this solution is $$$(3 - 4)^1 + (4 - 3)^1 + (3 - 6)^1 + (6 - 8)^1 + (8 - 1)^1 = 2$$$. Explanation for the sample input/output #2 For the first scenario, the cycle $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (1, 2) \rightarrow (1, 1)$$$ has a penalty of $$$(1 - 2)^5 + (2 - 0)^5 + (0 - 9)^5 + (9 - 1)^5 = -26250$$$. You can keep repeating this cycle to make your total penalty arbitrarily small. Similarly, for the second scenario, you can move to $$$(1, 1)$$$ first, then repeat the same cycle.
Title: X Aura time_limit_ms: 1000 memory_limit_mb: 1024 Description: Mount ICPC can be represented as a grid of $$$R$$$ rows (numbered from $$$1$$$ to $$$R$$$) and $$$C$$$ columns (numbered from $$$1$$$ to $$$C$$$). The cell located at row $$$r$$$ and column $$$c$$$ is denoted as $$$(r, c)$$$ and has a height of $$$H_{r, c}$$$. Two cells are adjacent to each other if they share a side. Formally, $$$(r, c)$$$ is adjacent to $$$(r-1, c)$$$, $$$(r+1, c)$$$, $$$(r, c-1)$$$, and $$$(r, c+1)$$$, if any exists. You can move only between adjacent cells, and each move comes with a penalty. With an aura of an odd positive integer $$$X$$$, moving from a cell with height $$$h_1$$$ to a cell with height $$$h_2$$$ gives you a penalty of $$$(h_1 - h_2)^X$$$. Note that the penalty can be negative. You want to answer $$$Q$$$ independent scenarios. In each scenario, you start at the starting cell $$$(R_s, C_s)$$$ and you want to go to the destination cell $$$(R_f, C_f)$$$ with minimum total penalty. In some scenarios, the total penalty might become arbitrarily small; such a scenario is called invalid. Find the minimum total penalty to move from the starting cell to the destination cell, or determine if the scenario is invalid. Input: The first line consists of three integers $$$R$$$ $$$C$$$ $$$X$$$ ($$$1 \leq R, C \leq 1000; 1 \leq X \leq 9; X$$$ is an odd integer). Each of the next $$$R$$$ lines consists of a string $$$H_r$$$ of length $$$C$$$. Each character in $$$H_r$$$ is a number from 0 to 9. The $$$c$$$-th character of $$$H_r$$$ represents the height of cell $$$(r, c)$$$, or $$$H_{r, c}$$$. The next line consists of an integer $$$Q$$$ ($$$1 \leq Q \leq 100\,000)$$$. Each of the next $$$Q$$$ lines consists of four integers $$$R_s$$$ $$$C_s$$$ $$$R_f$$$ $$$C_f$$$ ($$$1 \leq R_s, R_f \leq R; 1 \leq C_s, C_f \leq C$$$). Output: For each scenario, output the following in a single line. If the scenario is invalid, output INVALID. Otherwise, output a single integer representing the minimum total penalty to move from the starting cell to the destination cell. Examples: input: 3 4 1 3359 4294 3681 5 1 1 3 4 3 3 2 1 2 2 1 4 1 3 3 2 1 1 1 1 output: 2 4 -7 -1 0 input: 2 4 5 1908 2023 2 1 1 2 4 1 1 1 1 output: INVALID INVALID input: 3 3 9 135 357 579 2 3 3 1 1 2 2 2 2 output: 2048 0 Note: Explanation for the sample input/output #1 For the first scenario, one of the solutions is to move as follows: $$$(1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4)$$$. The total penalty of this solution is $$$(3 - 4)^1 + (4 - 3)^1 + (3 - 6)^1 + (6 - 8)^1 + (8 - 1)^1 = 2$$$. Explanation for the sample input/output #2 For the first scenario, the cycle $$$(1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (1, 2) \rightarrow (1, 1)$$$ has a penalty of $$$(1 - 2)^5 + (2 - 0)^5 + (0 - 9)^5 + (9 - 1)^5 = -26250$$$. You can keep repeating this cycle to make your total penalty arbitrarily small. Similarly, for the second scenario, you can move to $$$(1, 1)$$$ first, then repeat the same cycle.
2045I
https://codeforces.com/problemset/problem/2045/I
Microwavable Subsequence
2,100
[ "data structures" ]
Unrated, Online Mirror, ICPC Rules, Teams Preferred
1,000
1,024
You are given an array of $$$N$$$ integers: $$$[A_1, A_2, \dots, A_N]$$$. A subsequence can be derived from an array by removing zero or more elements without changing the order of the remaining elements. For example, $$$[2, 1, 2]$$$, $$$[3, 3]$$$, $$$[1]$$$, and $$$[3, 2, 1, 3, 2]$$$ are subsequences of array $$$[3, 2, 1, 3, 2]$$$, while $$$[1, 2, 3]$$$ is not a subsequence of array $$$[3, 2, 1, 3, 2]$$$. A subsequence is microwavable if the subsequence consists of at most two distinct values and each element differs from its adjacent elements. For example, $$$[2, 1, 2]$$$, $$$[3, 2, 3, 2]$$$, and $$$[1]$$$ are microwavable, while $$$[3, 3]$$$ and $$$[3, 2, 1, 3, 2]$$$ are not microwavable. Denote a function $$$f(x, y)$$$ as the length of the longest microwavable subsequence of array $$$A$$$ such that each element within the subsequence is either $$$x$$$ or $$$y$$$. Find the sum of $$$f(x, y)$$$ for all $$$1 \leq x < y \leq M$$$.
The first line consists of two integers $$$N$$$ $$$M$$$ ($$$1 \leq N, M \leq 300\,000$$$). The second line consists of $$$N$$$ integers $$$A_i$$$ ($$$1 \leq A_i \leq M$$$).
Output a single integer representing the sum of $$$f(x, y)$$$ for all $$$1 \leq x < y \leq M$$$.
[ [ "5 4\n3 2 1 3 2", "13" ], [ "3 3\n1 1 1", "2" ] ]
Explanation for the sample input/output #1 The value of $$$f(1, 2)$$$ is $$$3$$$, taken from the subsequence $$$[2, 1, 2]$$$ that can be obtained by removing $$$A_1$$$ and $$$A_4$$$. The value of $$$f(1, 3)$$$ is $$$3$$$, taken from the subsequence $$$[3, 1, 3]$$$ that can be obtained by removing $$$A_2$$$ and $$$A_5$$$. The value of $$$f(2, 3)$$$ is $$$4$$$, taken from the subsequence $$$[3, 2, 3, 2]$$$ that can be obtained by removing $$$A_3$$$. The value of $$$f(1, 4)$$$, $$$f(2, 4)$$$, and $$$f(3, 4)$$$ are all $$$1$$$. Explanation for the sample input/output #2 The value of $$$f(1, 2)$$$ and $$$f(1, 3)$$$ are both $$$1$$$, while the value of $$$f(2, 3)$$$ is $$$0$$$.
Title: Microwavable Subsequence time_limit_ms: 1000 memory_limit_mb: 1024 Description: You are given an array of $$$N$$$ integers: $$$[A_1, A_2, \dots, A_N]$$$. A subsequence can be derived from an array by removing zero or more elements without changing the order of the remaining elements. For example, $$$[2, 1, 2]$$$, $$$[3, 3]$$$, $$$[1]$$$, and $$$[3, 2, 1, 3, 2]$$$ are subsequences of array $$$[3, 2, 1, 3, 2]$$$, while $$$[1, 2, 3]$$$ is not a subsequence of array $$$[3, 2, 1, 3, 2]$$$. A subsequence is microwavable if the subsequence consists of at most two distinct values and each element differs from its adjacent elements. For example, $$$[2, 1, 2]$$$, $$$[3, 2, 3, 2]$$$, and $$$[1]$$$ are microwavable, while $$$[3, 3]$$$ and $$$[3, 2, 1, 3, 2]$$$ are not microwavable. Denote a function $$$f(x, y)$$$ as the length of the longest microwavable subsequence of array $$$A$$$ such that each element within the subsequence is either $$$x$$$ or $$$y$$$. Find the sum of $$$f(x, y)$$$ for all $$$1 \leq x < y \leq M$$$. Input: The first line consists of two integers $$$N$$$ $$$M$$$ ($$$1 \leq N, M \leq 300\,000$$$). The second line consists of $$$N$$$ integers $$$A_i$$$ ($$$1 \leq A_i \leq M$$$). Output: Output a single integer representing the sum of $$$f(x, y)$$$ for all $$$1 \leq x < y \leq M$$$. Examples: input: 5 4 3 2 1 3 2 output: 13 input: 3 3 1 1 1 output: 2 Note: Explanation for the sample input/output #1 The value of $$$f(1, 2)$$$ is $$$3$$$, taken from the subsequence $$$[2, 1, 2]$$$ that can be obtained by removing $$$A_1$$$ and $$$A_4$$$. The value of $$$f(1, 3)$$$ is $$$3$$$, taken from the subsequence $$$[3, 1, 3]$$$ that can be obtained by removing $$$A_2$$$ and $$$A_5$$$. The value of $$$f(2, 3)$$$ is $$$4$$$, taken from the subsequence $$$[3, 2, 3, 2]$$$ that can be obtained by removing $$$A_3$$$. The value of $$$f(1, 4)$$$, $$$f(2, 4)$$$, and $$$f(3, 4)$$$ are all $$$1$$$. Explanation for the sample input/output #2 The value of $$$f(1, 2)$$$ and $$$f(1, 3)$$$ are both $$$1$$$, while the value of $$$f(2, 3)$$$ is $$$0$$$.
2045M
https://codeforces.com/problemset/problem/2045/M
Mirror Maze
1,800
[ "dfs and similar", "brute force", "graphs", "implementation" ]
Unrated, Online Mirror, ICPC Rules, Teams Preferred
1,000
1,024
You are given a grid of $$$R$$$ rows (numbered from $$$1$$$ to $$$R$$$ from north to south) and $$$C$$$ columns (numbered from $$$1$$$ to $$$C$$$ from west to east). Every cell in this grid is a square of the same size. The cell located at row $$$r$$$ and column $$$c$$$ is denoted as $$$(r, c)$$$. Each cell can either be empty or have a mirror in one of the cell's diagonals. Each mirror is represented by a line segment. The mirror is type $$$1$$$ if it is positioned diagonally from the southwest corner to the northeast corner of the cell, or type $$$2$$$ for the other diagonal. These mirrors follow the law of reflection, that is, the angle of reflection equals the angle of incidence. Formally, for type $$$1$$$ mirror, if a beam of light comes from the north, south, west, or east of the cell, then it will be reflected to the west, east, north, and south of the cell, respectively. Similarly, for type $$$2$$$ mirror, if a beam of light comes from the north, south, west, or east of the cell, then it will be reflected to the east, west, south, and north of the cell, respectively. You want to put a laser from outside the grid such that all mirrors are hit by the laser beam. There are $$$2 \cdot (R+C)$$$ possible locations to put the laser: - from the north side of the grid at column $$$c$$$, for $$$1 \leq c \leq C$$$, shooting a laser beam to the south; - from the south side of the grid at column $$$c$$$, for $$$1 \leq c \leq C$$$, shooting a laser beam to the north; - from the east side of the grid at row $$$r$$$, for $$$1 \leq r \leq R$$$, shooting a laser beam to the west; and - from the west side of the grid at row $$$r$$$, for $$$1 \leq r \leq R$$$, shooting a laser beam to the east. Determine all possible locations for the laser such that all mirrors are hit by the laser beam.
The first line consists of two integers $$$R$$$ $$$C$$$ ($$$1 \leq R, C \leq 200$$$). Each of the next $$$R$$$ lines consists of a string $$$S_r$$$ of length $$$C$$$. The $$$c$$$-th character of string $$$S_r$$$ represents cell $$$(r, c)$$$. Each character can either be . if the cell is empty, / if the cell has type $$$1$$$ mirror, or \ if the cell has type $$$2$$$ mirror. There is at least one mirror in the grid.
Output a single integer representing the number of possible locations for the laser such that all mirrors are hit by the laser beam. Denote this number as $$$k$$$. If $$$k > 0$$$, then output $$$k$$$ space-separated strings representing the location of the laser. Each string consists of a character followed without any space by an integer. The character represents the side of the grid, which could be N, S, E, or W if you put the laser on the north, south, east, or west side of the grid, respectively. The integer represents the row/column number. You can output the strings in any order.
[ [ "4 4\n.//.\n.\\\\.\n.\\/.\n....", "2\nN3 W2" ], [ "4 6\n./..\\.\n.\\...\\\n./../\\\n......", "2\nE3 S2" ], [ "4 4\n....\n./\\.\n.\\/.\n....", "0" ] ]
Explanation for the sample input/output #1 The following illustration shows one of the solutions of this sample. Explanation for the sample input/output #2 The following illustration shows one of the solutions of this sample.
Title: Mirror Maze time_limit_ms: 1000 memory_limit_mb: 1024 Description: You are given a grid of $$$R$$$ rows (numbered from $$$1$$$ to $$$R$$$ from north to south) and $$$C$$$ columns (numbered from $$$1$$$ to $$$C$$$ from west to east). Every cell in this grid is a square of the same size. The cell located at row $$$r$$$ and column $$$c$$$ is denoted as $$$(r, c)$$$. Each cell can either be empty or have a mirror in one of the cell's diagonals. Each mirror is represented by a line segment. The mirror is type $$$1$$$ if it is positioned diagonally from the southwest corner to the northeast corner of the cell, or type $$$2$$$ for the other diagonal. These mirrors follow the law of reflection, that is, the angle of reflection equals the angle of incidence. Formally, for type $$$1$$$ mirror, if a beam of light comes from the north, south, west, or east of the cell, then it will be reflected to the west, east, north, and south of the cell, respectively. Similarly, for type $$$2$$$ mirror, if a beam of light comes from the north, south, west, or east of the cell, then it will be reflected to the east, west, south, and north of the cell, respectively. You want to put a laser from outside the grid such that all mirrors are hit by the laser beam. There are $$$2 \cdot (R+C)$$$ possible locations to put the laser: - from the north side of the grid at column $$$c$$$, for $$$1 \leq c \leq C$$$, shooting a laser beam to the south; - from the south side of the grid at column $$$c$$$, for $$$1 \leq c \leq C$$$, shooting a laser beam to the north; - from the east side of the grid at row $$$r$$$, for $$$1 \leq r \leq R$$$, shooting a laser beam to the west; and - from the west side of the grid at row $$$r$$$, for $$$1 \leq r \leq R$$$, shooting a laser beam to the east. Determine all possible locations for the laser such that all mirrors are hit by the laser beam. Input: The first line consists of two integers $$$R$$$ $$$C$$$ ($$$1 \leq R, C \leq 200$$$). Each of the next $$$R$$$ lines consists of a string $$$S_r$$$ of length $$$C$$$. The $$$c$$$-th character of string $$$S_r$$$ represents cell $$$(r, c)$$$. Each character can either be . if the cell is empty, / if the cell has type $$$1$$$ mirror, or \ if the cell has type $$$2$$$ mirror. There is at least one mirror in the grid. Output: Output a single integer representing the number of possible locations for the laser such that all mirrors are hit by the laser beam. Denote this number as $$$k$$$. If $$$k > 0$$$, then output $$$k$$$ space-separated strings representing the location of the laser. Each string consists of a character followed without any space by an integer. The character represents the side of the grid, which could be N, S, E, or W if you put the laser on the north, south, east, or west side of the grid, respectively. The integer represents the row/column number. You can output the strings in any order. Examples: input: 4 4 .//. .\\. .\/. .... output: 2 N3 W2 input: 4 6 ./..\. .\...\ ./../\ ...... output: 2 E3 S2 input: 4 4 .... ./\. .\/. .... output: 0 Note: Explanation for the sample input/output #1 The following illustration shows one of the solutions of this sample. Explanation for the sample input/output #2 The following illustration shows one of the solutions of this sample.
2051A
https://codeforces.com/problemset/problem/2051/A
Preparing for the Olympiad
800
[ "greedy" ]
Div. 3
2,000
256
Monocarp and Stereocarp are preparing for the Olympiad. There are $$$n$$$ days left until the Olympiad. On the $$$i$$$-th day, if Monocarp plans to practice, he will solve $$$a_i$$$ problems. Similarly, if Stereocarp plans to practice on the same day, he will solve $$$b_i$$$ problems. Monocarp can train on any day he wants. However, Stereocarp watches Monocarp and follows a different schedule: if Monocarp trained on day $$$i$$$ and $$$i < n$$$, then Stereocarp will train on day $$$(i+1)$$$. Monocarp wants to organize his training process in a way that the difference between the number of problems he solves and the number of problems Stereocarp solves is as large as possible. Formally, Monocarp wants to maximize the value of $$$(m-s)$$$, where $$$m$$$ is the number of problems he solves, and $$$s$$$ is the number of problems Stereocarp solves. Help Monocarp determine the maximum possible difference in the number of solved problems between them.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 100$$$).
For each test case, print a single integer — the maximum possible difference between the number of problems Monocarp solves and the number of problems Stereocarp solves.
[ [ "4\n2\n3 2\n2 1\n1\n5\n8\n3\n1 1 1\n2 2 2\n6\n8 2 5 6 2 6\n8 2 7 4 3 4", "4\n5\n1\n16" ] ]
Let's analyze the example from the statement: - In the first test case, it is optimal for Monocarp to train both days; then Stereocarp will train on day $$$2$$$. - In the second test case, it is optimal for Monocarp to train on the only day, and Stereocarp will not train at all. - In the third test case, it is optimal for Monocarp to train on the last day (and only on that day). - In the fourth test case, it is optimal for Monocarp to train on days $$$1, 3, 4, 6$$$; then Stereocarp will train on days $$$2, 4, 5$$$.
Title: Preparing for the Olympiad time_limit_ms: 2000 memory_limit_mb: 256 Description: Monocarp and Stereocarp are preparing for the Olympiad. There are $$$n$$$ days left until the Olympiad. On the $$$i$$$-th day, if Monocarp plans to practice, he will solve $$$a_i$$$ problems. Similarly, if Stereocarp plans to practice on the same day, he will solve $$$b_i$$$ problems. Monocarp can train on any day he wants. However, Stereocarp watches Monocarp and follows a different schedule: if Monocarp trained on day $$$i$$$ and $$$i < n$$$, then Stereocarp will train on day $$$(i+1)$$$. Monocarp wants to organize his training process in a way that the difference between the number of problems he solves and the number of problems Stereocarp solves is as large as possible. Formally, Monocarp wants to maximize the value of $$$(m-s)$$$, where $$$m$$$ is the number of problems he solves, and $$$s$$$ is the number of problems Stereocarp solves. Help Monocarp determine the maximum possible difference in the number of solved problems between them. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 100$$$). Output: For each test case, print a single integer — the maximum possible difference between the number of problems Monocarp solves and the number of problems Stereocarp solves. Examples: input: 4 2 3 2 2 1 1 5 8 3 1 1 1 2 2 2 6 8 2 5 6 2 6 8 2 7 4 3 4 output: 4 5 1 16 Note: Let's analyze the example from the statement: - In the first test case, it is optimal for Monocarp to train both days; then Stereocarp will train on day $$$2$$$. - In the second test case, it is optimal for Monocarp to train on the only day, and Stereocarp will not train at all. - In the third test case, it is optimal for Monocarp to train on the last day (and only on that day). - In the fourth test case, it is optimal for Monocarp to train on days $$$1, 3, 4, 6$$$; then Stereocarp will train on days $$$2, 4, 5$$$.
2051B
https://codeforces.com/problemset/problem/2051/B
Journey
800
[ "binary search", "math" ]
Div. 3
1,000
256
Monocarp decided to embark on a long hiking journey. He decided that on the first day he would walk $$$a$$$ kilometers, on the second day he would walk $$$b$$$ kilometers, on the third day he would walk $$$c$$$ kilometers, on the fourth day, just like on the first, he would walk $$$a$$$ kilometers, on the fifth day, just like on the second, he would walk $$$b$$$ kilometers, on the sixth day, just like on the third, he would walk $$$c$$$ kilometers, and so on. Monocarp will complete his journey on the day when he has walked at least $$$n$$$ kilometers in total. Your task is to determine the day on which Monocarp will complete his journey.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of one line containing four integers $$$n$$$, $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \le n \le 10^9$$$; $$$1 \le a, b, c \le 10^6$$$).
For each test case, output one integer — the day on which Monocarp will have walked at least $$$n$$$ kilometers in total and will complete his journey.
[ [ "4\n12 1 5 3\n6 6 7 4\n16 3 4 1\n1000000000 1 1 1", "5\n1\n6\n1000000000" ] ]
In the first example, over the first four days, Monocarp will cover $$$1 + 5 + 3 + 1 = 10$$$ kilometers. On the fifth day, he will cover another $$$5$$$ kilometers, meaning that in total over five days he will have covered $$$10 + 5 = 15$$$ kilometers. Since $$$n = 12$$$, Monocarp will complete his journey on the fifth day. In the second example, Monocarp will cover $$$6$$$ kilometers on the first day. Since $$$n = 6$$$, Monocarp will complete his journey on the very first day. In the third example, Monocarp will cover $$$3 + 4 + 1 + 3 + 4 + 1 = 16$$$ kilometers over the first six days. Since $$$n = 16$$$, Monocarp will complete his journey on the sixth day.
Title: Journey time_limit_ms: 1000 memory_limit_mb: 256 Description: Monocarp decided to embark on a long hiking journey. He decided that on the first day he would walk $$$a$$$ kilometers, on the second day he would walk $$$b$$$ kilometers, on the third day he would walk $$$c$$$ kilometers, on the fourth day, just like on the first, he would walk $$$a$$$ kilometers, on the fifth day, just like on the second, he would walk $$$b$$$ kilometers, on the sixth day, just like on the third, he would walk $$$c$$$ kilometers, and so on. Monocarp will complete his journey on the day when he has walked at least $$$n$$$ kilometers in total. Your task is to determine the day on which Monocarp will complete his journey. Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of one line containing four integers $$$n$$$, $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \le n \le 10^9$$$; $$$1 \le a, b, c \le 10^6$$$). Output: For each test case, output one integer — the day on which Monocarp will have walked at least $$$n$$$ kilometers in total and will complete his journey. Examples: input: 4 12 1 5 3 6 6 7 4 16 3 4 1 1000000000 1 1 1 output: 5 1 6 1000000000 Note: In the first example, over the first four days, Monocarp will cover $$$1 + 5 + 3 + 1 = 10$$$ kilometers. On the fifth day, he will cover another $$$5$$$ kilometers, meaning that in total over five days he will have covered $$$10 + 5 = 15$$$ kilometers. Since $$$n = 12$$$, Monocarp will complete his journey on the fifth day. In the second example, Monocarp will cover $$$6$$$ kilometers on the first day. Since $$$n = 6$$$, Monocarp will complete his journey on the very first day. In the third example, Monocarp will cover $$$3 + 4 + 1 + 3 + 4 + 1 = 16$$$ kilometers over the first six days. Since $$$n = 16$$$, Monocarp will complete his journey on the sixth day.
2051C
https://codeforces.com/problemset/problem/2051/C
Preparing for the Exam
1,000
[ "constructive algorithms", "implementation" ]
Div. 3
1,500
256
Monocarp is preparing for his first exam at the university. There are $$$n$$$ different questions which can be asked during the exam, numbered from $$$1$$$ to $$$n$$$. There are $$$m$$$ different lists of questions; each list consists of exactly $$$n-1$$$ different questions. Each list $$$i$$$ is characterized by one integer $$$a_i$$$, which is the index of the only question which is not present in the $$$i$$$-th list. For example, if $$$n = 4$$$ and $$$a_i = 3$$$, the $$$i$$$-th list contains questions $$$[1, 2, 4]$$$. During the exam, Monocarp will receive one of these $$$m$$$ lists of questions. Then, the professor will make Monocarp answer all questions from the list. So, Monocarp will pass only if he knows all questions from the list. Monocarp knows the answers for $$$k$$$ questions $$$q_1, q_2, \dots, q_k$$$. For each list, determine if Monocarp will pass the exam if he receives that list.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of three lines: - the first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 3 \cdot 10^5$$$; $$$1 \le m, k \le n$$$); - the second line contains $$$m$$$ distinct integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_i \le n$$$; $$$a_i < a_{i+1}$$$); - the third line contains $$$k$$$ distinct integers $$$q_1, q_2, \dots, q_k$$$ ($$$1 \le q_i \le n$$$; $$$q_i < q_{i+1}$$$). Additional constraints on the input: - the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
For each test case, print a string of $$$m$$$ characters. The $$$i$$$-th character should be 1 if Monocarp passes the exam if he receives the $$$i$$$-th question list, 0 if Monocarp won't pass.
[ [ "4\n4 4 3\n1 2 3 4\n1 3 4\n5 4 3\n1 2 3 4\n1 3 4\n4 4 4\n1 2 3 4\n1 2 3 4\n2 2 1\n1 2\n2", "0100\n0000\n1111\n10" ] ]
In the first test case, Monocarp knows the questions $$$[1, 3, 4]$$$. Let's consider all the question lists: - the first list consists of questions $$$[2, 3, 4]$$$. Monocarp doesn't know the $$$2$$$-nd question, so he won't pass; - the second list consists of questions $$$[1, 3, 4]$$$. Monocarp knows all these questions, so he will pass; - the third list consists of questions $$$[1, 2, 4]$$$. Monocarp doesn't know the $$$2$$$-nd question, so he won't pass; - the fourth list consists of questions $$$[1, 2, 3]$$$. Monocarp doesn't know the $$$2$$$-nd question, so he won't pass.
Title: Preparing for the Exam time_limit_ms: 1500 memory_limit_mb: 256 Description: Monocarp is preparing for his first exam at the university. There are $$$n$$$ different questions which can be asked during the exam, numbered from $$$1$$$ to $$$n$$$. There are $$$m$$$ different lists of questions; each list consists of exactly $$$n-1$$$ different questions. Each list $$$i$$$ is characterized by one integer $$$a_i$$$, which is the index of the only question which is not present in the $$$i$$$-th list. For example, if $$$n = 4$$$ and $$$a_i = 3$$$, the $$$i$$$-th list contains questions $$$[1, 2, 4]$$$. During the exam, Monocarp will receive one of these $$$m$$$ lists of questions. Then, the professor will make Monocarp answer all questions from the list. So, Monocarp will pass only if he knows all questions from the list. Monocarp knows the answers for $$$k$$$ questions $$$q_1, q_2, \dots, q_k$$$. For each list, determine if Monocarp will pass the exam if he receives that list. Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of three lines: - the first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 3 \cdot 10^5$$$; $$$1 \le m, k \le n$$$); - the second line contains $$$m$$$ distinct integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_i \le n$$$; $$$a_i < a_{i+1}$$$); - the third line contains $$$k$$$ distinct integers $$$q_1, q_2, \dots, q_k$$$ ($$$1 \le q_i \le n$$$; $$$q_i < q_{i+1}$$$). Additional constraints on the input: - the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$. Output: For each test case, print a string of $$$m$$$ characters. The $$$i$$$-th character should be 1 if Monocarp passes the exam if he receives the $$$i$$$-th question list, 0 if Monocarp won't pass. Examples: input: 4 4 4 3 1 2 3 4 1 3 4 5 4 3 1 2 3 4 1 3 4 4 4 4 1 2 3 4 1 2 3 4 2 2 1 1 2 2 output: 0100 0000 1111 10 Note: In the first test case, Monocarp knows the questions $$$[1, 3, 4]$$$. Let's consider all the question lists: - the first list consists of questions $$$[2, 3, 4]$$$. Monocarp doesn't know the $$$2$$$-nd question, so he won't pass; - the second list consists of questions $$$[1, 3, 4]$$$. Monocarp knows all these questions, so he will pass; - the third list consists of questions $$$[1, 2, 4]$$$. Monocarp doesn't know the $$$2$$$-nd question, so he won't pass; - the fourth list consists of questions $$$[1, 2, 3]$$$. Monocarp doesn't know the $$$2$$$-nd question, so he won't pass.
2051D
https://codeforces.com/problemset/problem/2051/D
Counting Pairs
1,200
[ "two pointers", "binary search", "sortings" ]
Div. 3
2,000
256
You are given a sequence $$$a$$$, consisting of $$$n$$$ integers, where the $$$i$$$-th element of the sequence is equal to $$$a_i$$$. You are also given two integers $$$x$$$ and $$$y$$$ ($$$x \le y$$$). A pair of integers $$$(i, j)$$$ is considered interesting if the following conditions are met: - $$$1 \le i < j \le n$$$; - if you simultaneously remove the elements at positions $$$i$$$ and $$$j$$$ from the sequence $$$a$$$, the sum of the remaining elements is at least $$$x$$$ and at most $$$y$$$. Your task is to determine the number of interesting pairs of integers for the given sequence $$$a$$$.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines: - The first line contains three integers $$$n, x, y$$$ ($$$3 \le n \le 2 \cdot 10^5$$$, $$$1 \le x \le y \le 2 \cdot 10^{14}$$$); - The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{9}$$$). Additional constraint on the input: the sum of $$$n$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output one integer — the number of interesting pairs of integers for the given sequence $$$a$$$.
[ [ "7\n4 8 10\n4 6 3 6\n6 22 27\n4 9 6 3 4 5\n3 8 10\n3 2 1\n3 1 1\n2 3 4\n3 3 6\n3 2 1\n4 4 12\n3 3 2 1\n6 8 8\n1 1 2 2 2 3", "4\n7\n0\n0\n1\n5\n6" ] ]
In the first example, there are $$$4$$$ interesting pairs of integers: 1. $$$(1, 2)$$$; 2. $$$(1, 4)$$$; 3. $$$(2, 3)$$$; 4. $$$(3, 4)$$$.
Title: Counting Pairs time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a sequence $$$a$$$, consisting of $$$n$$$ integers, where the $$$i$$$-th element of the sequence is equal to $$$a_i$$$. You are also given two integers $$$x$$$ and $$$y$$$ ($$$x \le y$$$). A pair of integers $$$(i, j)$$$ is considered interesting if the following conditions are met: - $$$1 \le i < j \le n$$$; - if you simultaneously remove the elements at positions $$$i$$$ and $$$j$$$ from the sequence $$$a$$$, the sum of the remaining elements is at least $$$x$$$ and at most $$$y$$$. Your task is to determine the number of interesting pairs of integers for the given sequence $$$a$$$. Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines: - The first line contains three integers $$$n, x, y$$$ ($$$3 \le n \le 2 \cdot 10^5$$$, $$$1 \le x \le y \le 2 \cdot 10^{14}$$$); - The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^{9}$$$). Additional constraint on the input: the sum of $$$n$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$. Output: For each test case, output one integer — the number of interesting pairs of integers for the given sequence $$$a$$$. Examples: input: 7 4 8 10 4 6 3 6 6 22 27 4 9 6 3 4 5 3 8 10 3 2 1 3 1 1 2 3 4 3 3 6 3 2 1 4 4 12 3 3 2 1 6 8 8 1 1 2 2 2 3 output: 4 7 0 0 1 5 6 Note: In the first example, there are $$$4$$$ interesting pairs of integers: 1. $$$(1, 2)$$$; 2. $$$(1, 4)$$$; 3. $$$(2, 3)$$$; 4. $$$(3, 4)$$$.
2051E
https://codeforces.com/problemset/problem/2051/E
Best Price
1,600
[ "brute force", "binary search", "data structures", "sortings", "greedy" ]
Div. 3
2,000
256
A batch of Christmas trees has arrived at the largest store in Berland. $$$n$$$ customers have already come to the store, wanting to buy them. Before the sales begin, the store needs to determine the price for one tree (the price is the same for all customers). To do this, the store has some information about each customer. For the $$$i$$$-th customer, two integers $$$a_i$$$ and $$$b_i$$$ are known, which define their behavior: - if the price of the product is at most $$$a_i$$$, the customer will buy a tree and leave a positive review; - otherwise, if the price of the product is at most $$$b_i$$$, the customer will buy a tree but leave a negative review; - otherwise, the customer will not buy a tree at all. Your task is to calculate the maximum possible earnings for the store, given that it can receive no more than $$$k$$$ negative reviews.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$0 \le k \le n$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^9$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 2 \cdot 10^9$$$; $$$a_i < b_i$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer — the maximum possible earnings for the store, given that it can receive no more than $$$k$$$ negative reviews.
[ [ "5\n2 0\n2 1\n3 4\n1 1\n2\n5\n3 3\n1 5 2\n3 6 4\n4 3\n2 3 2 8\n3 7 3 9\n3 1\n2 9 5\n12 14 9", "2\n5\n9\n14\n15" ] ]
Consider the example from the statement: - In the first test case, the price should be set to $$$1$$$. Then both customers will buy one tree each and leave no negative reviews; - In the second test case, the price should be set to $$$5$$$. Then the only customer will buy a tree and leave a negative review; - In the third test case, the price should be set to $$$3$$$. Then all customers will buy one tree each, and the store will receive two negative reviews. - In the fourth test case, the price should be set to $$$7$$$. Then two customers will buy one tree each, and the store will receive one negative review.
Title: Best Price time_limit_ms: 2000 memory_limit_mb: 256 Description: A batch of Christmas trees has arrived at the largest store in Berland. $$$n$$$ customers have already come to the store, wanting to buy them. Before the sales begin, the store needs to determine the price for one tree (the price is the same for all customers). To do this, the store has some information about each customer. For the $$$i$$$-th customer, two integers $$$a_i$$$ and $$$b_i$$$ are known, which define their behavior: - if the price of the product is at most $$$a_i$$$, the customer will buy a tree and leave a positive review; - otherwise, if the price of the product is at most $$$b_i$$$, the customer will buy a tree but leave a negative review; - otherwise, the customer will not buy a tree at all. Your task is to calculate the maximum possible earnings for the store, given that it can receive no more than $$$k$$$ negative reviews. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$0 \le k \le n$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^9$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 2 \cdot 10^9$$$; $$$a_i < b_i$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: For each test case, print a single integer — the maximum possible earnings for the store, given that it can receive no more than $$$k$$$ negative reviews. Examples: input: 5 2 0 2 1 3 4 1 1 2 5 3 3 1 5 2 3 6 4 4 3 2 3 2 8 3 7 3 9 3 1 2 9 5 12 14 9 output: 2 5 9 14 15 Note: Consider the example from the statement: - In the first test case, the price should be set to $$$1$$$. Then both customers will buy one tree each and leave no negative reviews; - In the second test case, the price should be set to $$$5$$$. Then the only customer will buy a tree and leave a negative review; - In the third test case, the price should be set to $$$3$$$. Then all customers will buy one tree each, and the store will receive two negative reviews. - In the fourth test case, the price should be set to $$$7$$$. Then two customers will buy one tree each, and the store will receive one negative review.
2051F
https://codeforces.com/problemset/problem/2051/F
Joker
2,000
[ "greedy", "brute force", "implementation", "math" ]
Div. 3
2,000
256
Consider a deck of $$$n$$$ cards. The positions in the deck are numbered from $$$1$$$ to $$$n$$$ from top to bottom. A joker is located at position $$$m$$$. $$$q$$$ operations are applied sequentially to the deck. During the $$$i$$$-th operation, you need to take the card at position $$$a_i$$$ and move it either to the beginning or to the end of the deck. For example, if the deck is $$$[2, 1, 3, 5, 4]$$$, and $$$a_i=2$$$, then after the operation the deck will be either $$$[1, 2, 3, 5, 4]$$$ (the card from the second position moved to the beginning) or $$$[2, 3, 5, 4, 1]$$$ (the card from the second position moved to the end). Your task is to calculate the number of distinct positions where the joker can be after each operation.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$2 \le n \le 10^9$$$; $$$1 \le m \le n$$$; $$$1 \le q \le 2 \cdot 10^5$$$). The second line contains $$$q$$$ integers $$$a_1, a_2, \dots, a_q$$$ ($$$1 \le a_i \le n$$$). Additional constraint on the input: the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print $$$q$$$ integers — the number of distinct positions where the joker can be after each operation.
[ [ "5\n6 5 3\n1 2 3\n2 1 4\n2 1 1 2\n5 3 1\n3\n3 2 4\n2 1 1 1\n18 15 4\n13 15 1 16", "2 3 5 \n2 2 2 2 \n2 \n2 3 3 3 \n2 4 6 8" ] ]
Title: Joker time_limit_ms: 2000 memory_limit_mb: 256 Description: Consider a deck of $$$n$$$ cards. The positions in the deck are numbered from $$$1$$$ to $$$n$$$ from top to bottom. A joker is located at position $$$m$$$. $$$q$$$ operations are applied sequentially to the deck. During the $$$i$$$-th operation, you need to take the card at position $$$a_i$$$ and move it either to the beginning or to the end of the deck. For example, if the deck is $$$[2, 1, 3, 5, 4]$$$, and $$$a_i=2$$$, then after the operation the deck will be either $$$[1, 2, 3, 5, 4]$$$ (the card from the second position moved to the beginning) or $$$[2, 3, 5, 4, 1]$$$ (the card from the second position moved to the end). Your task is to calculate the number of distinct positions where the joker can be after each operation. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$2 \le n \le 10^9$$$; $$$1 \le m \le n$$$; $$$1 \le q \le 2 \cdot 10^5$$$). The second line contains $$$q$$$ integers $$$a_1, a_2, \dots, a_q$$$ ($$$1 \le a_i \le n$$$). Additional constraint on the input: the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: For each test case, print $$$q$$$ integers — the number of distinct positions where the joker can be after each operation. Examples: input: 5 6 5 3 1 2 3 2 1 4 2 1 1 2 5 3 1 3 3 2 4 2 1 1 1 18 15 4 13 15 1 16 output: 2 3 5 2 2 2 2 2 2 3 3 3 2 4 6 8 Note:
2051G
https://codeforces.com/problemset/problem/2051/G
Snakes
2,100
[ "graphs", "bitmasks", "dp", "dsu" ]
Div. 3
3,000
512
Suppose you play a game where the game field looks like a strip of $$$1 \times 10^9$$$ square cells, numbered from $$$1$$$ to $$$10^9$$$. You have $$$n$$$ snakes (numbered from $$$1$$$ to $$$n$$$) you need to place into some cells. Initially, each snake occupies exactly one cell, and you can't place more than one snake into one cell. After that, the game starts. The game lasts for $$$q$$$ seconds. There are two types of events that may happen each second: - snake $$$s_i$$$ enlarges: if snake $$$s_i$$$ occupied cells $$$[l, r]$$$, it enlarges to a segment $$$[l, r + 1]$$$; - snake $$$s_i$$$ shrinks: if snake $$$s_i$$$ occupied cells $$$[l, r]$$$, it shrinks to a segment $$$[l + 1, r]$$$. Each second, exactly one of the events happens. If at any moment of time, any snake runs into some obstacle (either another snake or the end of the strip), you lose. Otherwise, you win with the score equal to the maximum cell occupied by any snake so far. What is the minimum possible score you can achieve?
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n \le 20$$$; $$$1 \le q \le 2 \cdot 10^5$$$) — the number of snakes and the number of events. Next $$$q$$$ lines contain the description of events — one per line. The $$$i$$$-th line contains - either "$$$s_i$$$ +" ($$$1 \le s_i \le n$$$) meaning that the $$$s_i$$$-th snake enlarges - or "$$$s_i$$$ -" ($$$1 \le s_i \le n$$$) meaning that the $$$s_i$$$-th snake shrinks. Additional constraint on the input: the given sequence of events is valid, i. e. a snake of length $$$1$$$ never shrinks.
Print one integer — the minimum possible score.
[ [ "3 6\n1 +\n1 -\n3 +\n3 -\n2 +\n2 -", "4" ], [ "5 13\n5 +\n3 +\n5 -\n2 +\n4 +\n3 +\n5 +\n5 -\n2 +\n3 -\n3 +\n3 -\n2 +", "11" ] ]
In the first test, the optimal strategy is to place the second snake at cell $$$1$$$, the third snake — at $$$2$$$, and the first one — at $$$3$$$. The maximum occupied cell is cell $$$4$$$, and it's the minimum possible score. In the second test, one of the optimal strategies is to place: - snake $$$2$$$ at position $$$1$$$; - snake $$$3$$$ at position $$$4$$$; - snake $$$5$$$ at position $$$6$$$; - snake $$$1$$$ at position $$$9$$$; - snake $$$4$$$ at position $$$10$$$.
Title: Snakes time_limit_ms: 3000 memory_limit_mb: 512 Description: Suppose you play a game where the game field looks like a strip of $$$1 \times 10^9$$$ square cells, numbered from $$$1$$$ to $$$10^9$$$. You have $$$n$$$ snakes (numbered from $$$1$$$ to $$$n$$$) you need to place into some cells. Initially, each snake occupies exactly one cell, and you can't place more than one snake into one cell. After that, the game starts. The game lasts for $$$q$$$ seconds. There are two types of events that may happen each second: - snake $$$s_i$$$ enlarges: if snake $$$s_i$$$ occupied cells $$$[l, r]$$$, it enlarges to a segment $$$[l, r + 1]$$$; - snake $$$s_i$$$ shrinks: if snake $$$s_i$$$ occupied cells $$$[l, r]$$$, it shrinks to a segment $$$[l + 1, r]$$$. Each second, exactly one of the events happens. If at any moment of time, any snake runs into some obstacle (either another snake or the end of the strip), you lose. Otherwise, you win with the score equal to the maximum cell occupied by any snake so far. What is the minimum possible score you can achieve? Input: The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n \le 20$$$; $$$1 \le q \le 2 \cdot 10^5$$$) — the number of snakes and the number of events. Next $$$q$$$ lines contain the description of events — one per line. The $$$i$$$-th line contains - either "$$$s_i$$$ +" ($$$1 \le s_i \le n$$$) meaning that the $$$s_i$$$-th snake enlarges - or "$$$s_i$$$ -" ($$$1 \le s_i \le n$$$) meaning that the $$$s_i$$$-th snake shrinks. Additional constraint on the input: the given sequence of events is valid, i. e. a snake of length $$$1$$$ never shrinks. Output: Print one integer — the minimum possible score. Examples: input: 3 6 1 + 1 - 3 + 3 - 2 + 2 - output: 4 input: 5 13 5 + 3 + 5 - 2 + 4 + 3 + 5 + 5 - 2 + 3 - 3 + 3 - 2 + output: 11 Note: In the first test, the optimal strategy is to place the second snake at cell $$$1$$$, the third snake — at $$$2$$$, and the first one — at $$$3$$$. The maximum occupied cell is cell $$$4$$$, and it's the minimum possible score. In the second test, one of the optimal strategies is to place: - snake $$$2$$$ at position $$$1$$$; - snake $$$3$$$ at position $$$4$$$; - snake $$$5$$$ at position $$$6$$$; - snake $$$1$$$ at position $$$9$$$; - snake $$$4$$$ at position $$$10$$$.
2049A
https://codeforces.com/problemset/problem/2049/A
MEX Destruction
800
[ "greedy", "implementation" ]
Div. 2
1,000
256
Evirir the dragon snuck into a wizard's castle and found a mysterious contraption, and their playful instincts caused them to play with (destroy) it... Evirir the dragon found an array $$$a_1, a_2, \ldots, a_n$$$ of $$$n$$$ non-negative integers. In one operation, they can choose a non-empty subarray$$$^{\text{∗}}$$$ $$$b$$$ of $$$a$$$ and replace it with the integer $$$\operatorname{mex}(b)$$$$$$^{\text{†}}$$$. They want to use this operation any number of times to make $$$a$$$ only contain zeros. It can be proven that this is always possible under the problem constraints. What is the minimum number of operations needed?
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 200$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 50$$$), the length of $$$a$$$. The second line of each test case contains $$$n$$$ space-separated integers, $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 100$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$.
For each test case, output a single integer on a line, the minimum number of operations needed to make $$$a$$$ contain only zeros.
[ [ "10\n4\n0 1 2 3\n6\n0 0 0 0 0 0\n5\n1 0 1 0 1\n5\n3 1 4 1 5\n4\n3 2 1 0\n7\n9 100 0 89 12 2 3\n4\n0 3 9 0\n7\n0 7 0 2 0 7 0\n1\n0\n2\n0 1", "1\n0\n2\n1\n1\n2\n1\n2\n0\n1" ] ]
In the first test case, Evirir can choose the subarray $$$b = [1, 2, 3]$$$ and replace it with $$$\operatorname{mex}(1, 2, 3) = 0$$$, changing $$$a$$$ from $$$[0, \underline{1, 2, 3}]$$$ to $$$[0, 0]$$$ (where the chosen subarray is underlined). Therefore, the answer is $$$1$$$. In the second test case, $$$a$$$ already contains only $$$0$$$s, so no operation is needed. In the third test case, Evirir can change $$$a$$$ as follows: $$$[1, \underline{0, 1, 0, 1}] \to [\underline{1, 2}] \to [0]$$$. Here, $$$\operatorname{mex}(0, 1, 0, 1) = 2$$$ and $$$\operatorname{mex}(1, 2) = 0$$$. In the fourth test case, Evirir can choose $$$b$$$ to be the entire array $$$a$$$, changing $$$a$$$ from $$$[\underline{3, 1, 4, 1, 5}]$$$ to $$$[0]$$$.
Title: MEX Destruction time_limit_ms: 1000 memory_limit_mb: 256 Description: Evirir the dragon snuck into a wizard's castle and found a mysterious contraption, and their playful instincts caused them to play with (destroy) it... Evirir the dragon found an array $$$a_1, a_2, \ldots, a_n$$$ of $$$n$$$ non-negative integers. In one operation, they can choose a non-empty subarray$$$^{\text{∗}}$$$ $$$b$$$ of $$$a$$$ and replace it with the integer $$$\operatorname{mex}(b)$$$$$$^{\text{†}}$$$. They want to use this operation any number of times to make $$$a$$$ only contain zeros. It can be proven that this is always possible under the problem constraints. What is the minimum number of operations needed? Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 200$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 50$$$), the length of $$$a$$$. The second line of each test case contains $$$n$$$ space-separated integers, $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 100$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$. Output: For each test case, output a single integer on a line, the minimum number of operations needed to make $$$a$$$ contain only zeros. Examples: input: 10 4 0 1 2 3 6 0 0 0 0 0 0 5 1 0 1 0 1 5 3 1 4 1 5 4 3 2 1 0 7 9 100 0 89 12 2 3 4 0 3 9 0 7 0 7 0 2 0 7 0 1 0 2 0 1 output: 1 0 2 1 1 2 1 2 0 1 Note: In the first test case, Evirir can choose the subarray $$$b = [1, 2, 3]$$$ and replace it with $$$\operatorname{mex}(1, 2, 3) = 0$$$, changing $$$a$$$ from $$$[0, \underline{1, 2, 3}]$$$ to $$$[0, 0]$$$ (where the chosen subarray is underlined). Therefore, the answer is $$$1$$$. In the second test case, $$$a$$$ already contains only $$$0$$$s, so no operation is needed. In the third test case, Evirir can change $$$a$$$ as follows: $$$[1, \underline{0, 1, 0, 1}] \to [\underline{1, 2}] \to [0]$$$. Here, $$$\operatorname{mex}(0, 1, 0, 1) = 2$$$ and $$$\operatorname{mex}(1, 2) = 0$$$. In the fourth test case, Evirir can choose $$$b$$$ to be the entire array $$$a$$$, changing $$$a$$$ from $$$[\underline{3, 1, 4, 1, 5}]$$$ to $$$[0]$$$.
2049B
https://codeforces.com/problemset/problem/2049/B
pspspsps
1,300
[ "constructive algorithms", "graph matchings", "brute force", "implementation" ]
Div. 2
1,000
256
Cats are attracted to pspspsps, but Evirir, being a dignified dragon, is only attracted to pspspsps with oddly specific requirements... Given a string $$$s = s_1s_2\ldots s_n$$$ of length $$$n$$$ consisting of characters p, s, and . (dot), determine whether a permutation$$$^{\text{∗}}$$$ $$$p$$$ of length $$$n$$$ exists, such that for all integers $$$i$$$ ($$$1 \le i \le n$$$): - If $$$s_i$$$ is p, then $$$[p_1, p_2, \ldots, p_i]$$$ forms a permutation (of length $$$i$$$); - If $$$s_i$$$ is s, then $$$[p_i, p_{i+1}, \ldots, p_{n}]$$$ forms a permutation (of length $$$n-i+1$$$); - If $$$s_i$$$ is ., then there is no additional restriction.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 500$$$), the length of $$$s$$$. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ that consists of the characters p, s, and .. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$.
For each test case, output YES or NO on a line. Output YES if there is such a permutation and NO otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
[ [ "9\n4\ns.sp\n6\npss..s\n5\nppppp\n2\nsp\n4\n.sp.\n8\npsss....\n1\n.\n8\npspspsps\n20\n....................", "YES\nNO\nYES\nYES\nNO\nNO\nYES\nNO\nYES" ] ]
For the first test case, one permutation that works is $$$p = [3, 4, 1, 2]$$$. The restrictions are as follows: - $$$s_1 =$$$ s: $$$[p_1, p_2, p_3, p_4] = [3, 4, 1, 2]$$$ forms a permutation. - $$$s_2 =$$$ .: No additional restriction. - $$$s_3 =$$$ s: $$$[p_3, p_4] = [1, 2]$$$ forms a permutation. - $$$s_4 =$$$ p: $$$[p_1, p_2, p_3, p_4] = [3, 4, 1, 2]$$$ forms a permutation. For the second test case, it can be proven that there is no permutation that satisfies all restrictions. For the third test case, one permutation that satisfies the constraints is $$$p = [1, 2, 3, 4, 5]$$$.
Title: pspspsps time_limit_ms: 1000 memory_limit_mb: 256 Description: Cats are attracted to pspspsps, but Evirir, being a dignified dragon, is only attracted to pspspsps with oddly specific requirements... Given a string $$$s = s_1s_2\ldots s_n$$$ of length $$$n$$$ consisting of characters p, s, and . (dot), determine whether a permutation$$$^{\text{∗}}$$$ $$$p$$$ of length $$$n$$$ exists, such that for all integers $$$i$$$ ($$$1 \le i \le n$$$): - If $$$s_i$$$ is p, then $$$[p_1, p_2, \ldots, p_i]$$$ forms a permutation (of length $$$i$$$); - If $$$s_i$$$ is s, then $$$[p_i, p_{i+1}, \ldots, p_{n}]$$$ forms a permutation (of length $$$n-i+1$$$); - If $$$s_i$$$ is ., then there is no additional restriction. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 500$$$), the length of $$$s$$$. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ that consists of the characters p, s, and .. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Output: For each test case, output YES or NO on a line. Output YES if there is such a permutation and NO otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Examples: input: 9 4 s.sp 6 pss..s 5 ppppp 2 sp 4 .sp. 8 psss.... 1 . 8 pspspsps 20 .................... output: YES NO YES YES NO NO YES NO YES Note: For the first test case, one permutation that works is $$$p = [3, 4, 1, 2]$$$. The restrictions are as follows: - $$$s_1 =$$$ s: $$$[p_1, p_2, p_3, p_4] = [3, 4, 1, 2]$$$ forms a permutation. - $$$s_2 =$$$ .: No additional restriction. - $$$s_3 =$$$ s: $$$[p_3, p_4] = [1, 2]$$$ forms a permutation. - $$$s_4 =$$$ p: $$$[p_1, p_2, p_3, p_4] = [3, 4, 1, 2]$$$ forms a permutation. For the second test case, it can be proven that there is no permutation that satisfies all restrictions. For the third test case, one permutation that satisfies the constraints is $$$p = [1, 2, 3, 4, 5]$$$.
2049D
https://codeforces.com/problemset/problem/2049/D
Shift + Esc
1,900
[ "brute force", "dp" ]
Div. 2
2,500
512
After having fun with a certain contraption and getting caught, Evirir the dragon decides to put their magical skills to good use — warping reality to escape fast! You are given a grid with $$$n$$$ rows and $$$m$$$ columns of non-negative integers and an integer $$$k$$$. Let $$$(i, j)$$$ denote the cell in the $$$i$$$-th row from the top and $$$j$$$-th column from the left ($$$1 \le i \le n$$$, $$$1 \le j \le m$$$). For every cell $$$(i, j)$$$, the integer $$$a_{i, j}$$$ is written on the cell $$$(i, j)$$$. You are initially at $$$(1, 1)$$$ and want to go to $$$(n, m)$$$. You may only move down or right. That is, if you are at $$$(i, j)$$$, you can only move to $$$(i+1, j)$$$ or $$$(i, j+1)$$$ (if the corresponding cell exists). Before you begin moving, you may do the following operation any number of times: - Choose an integer $$$i$$$ between $$$1$$$ and $$$n$$$ and cyclically shift row $$$i$$$ to the left by $$$1$$$. Formally, simultaneously set $$$a_{i,j}$$$ to $$$a_{i,(j \bmod m) + 1}$$$ for all integers $$$j$$$ ($$$1 \le j \le m$$$). After moving from $$$(1, 1)$$$ to $$$(n, m)$$$, let $$$x$$$ be the number of operations you have performed before moving, and let $$$y$$$ be the sum of the integers written on visited cells (including $$$(1, 1)$$$ and $$$(n, m)$$$). Then the cost is defined as $$$kx + y$$$. Find the minimum cost to move from $$$(1, 1)$$$ to $$$(n, m)$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line contains three space-separated integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \leq n, m \leq 200$$$, $$$0 \leq k \leq 10^9$$$). Then, $$$n$$$ lines follow. The $$$i$$$-th line contains $$$m$$$ space-separated integers, $$$a_{i,1},\,a_{i,2},\,\ldots,\,a_{i,m}$$$ ($$$0 \leq a_{i,j} \leq 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$5 \cdot 10^4$$$.
For each test case, output a single integer, the minimum cost to move from $$$(1, 1)$$$ to $$$(n, m)$$$.
[ [ "5\n3 3 100\n3 4 9\n5 2 4\n0 101 101\n3 4 1\n10 0 0 10\n0 0 10 0\n10 10 0 10\n1 1 3\n4\n3 2 3\n1 2\n3 6\n5 4\n10 10 14\n58 49 25 12 89 69 8 49 71 23\n45 27 65 59 36 100 73 23 5 84\n82 91 54 92 53 15 43 46 11 65\n61 69 71 87 67 72 51 42 55 80\n1 64 8 54 61 70 47 100 84 50\n86 93 43 51 47 35 56 20 33 61\n100 59 5 68 15 55 69 8 8 60\n33 61 20 79 69 51 23 24 56 28\n67 76 3 69 58 79 75 10 65 63\n6 64 73 79 17 62 55 53 61 58", "113\n6\n4\n13\n618" ] ]
In the first test case, the minimum cost of $$$113$$$ can be achieved as follows: 1. Cyclically shift row 3 once. The grid now becomes $$$$$$\begin{bmatrix}3 & 4 & 9\\5 & 2 & 4\\101 & 101 & 0\end{bmatrix}.$$$$$$ 2. Move as follows: $$$(1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (3, 3)$$$. $$$x = 1$$$ operation is done before moving. The sum of integers on visited cells is $$$y = 3 + 4 + 2 + 4 + 0 = 13$$$. Therefore, the cost is $$$kx + y = 100 \cdot 1 + 13 = 113$$$. In the second test case, one can shift row 1 once, row 2 twice, and row 3 thrice. Then, the grid becomes $$$$$$\begin{bmatrix}0 & 0 & 10 & 10\\10 & 0 & 0 & 0\\10 & 10 & 10 & 0\end{bmatrix}.$$$$$$ $$$x = 6$$$ operations were done before moving, and there is a path of cost $$$y = 0$$$. Therefore, the cost is $$$6 \cdot 1 + 0 = 6$$$.
Title: Shift + Esc time_limit_ms: 2500 memory_limit_mb: 512 Description: After having fun with a certain contraption and getting caught, Evirir the dragon decides to put their magical skills to good use — warping reality to escape fast! You are given a grid with $$$n$$$ rows and $$$m$$$ columns of non-negative integers and an integer $$$k$$$. Let $$$(i, j)$$$ denote the cell in the $$$i$$$-th row from the top and $$$j$$$-th column from the left ($$$1 \le i \le n$$$, $$$1 \le j \le m$$$). For every cell $$$(i, j)$$$, the integer $$$a_{i, j}$$$ is written on the cell $$$(i, j)$$$. You are initially at $$$(1, 1)$$$ and want to go to $$$(n, m)$$$. You may only move down or right. That is, if you are at $$$(i, j)$$$, you can only move to $$$(i+1, j)$$$ or $$$(i, j+1)$$$ (if the corresponding cell exists). Before you begin moving, you may do the following operation any number of times: - Choose an integer $$$i$$$ between $$$1$$$ and $$$n$$$ and cyclically shift row $$$i$$$ to the left by $$$1$$$. Formally, simultaneously set $$$a_{i,j}$$$ to $$$a_{i,(j \bmod m) + 1}$$$ for all integers $$$j$$$ ($$$1 \le j \le m$$$). After moving from $$$(1, 1)$$$ to $$$(n, m)$$$, let $$$x$$$ be the number of operations you have performed before moving, and let $$$y$$$ be the sum of the integers written on visited cells (including $$$(1, 1)$$$ and $$$(n, m)$$$). Then the cost is defined as $$$kx + y$$$. Find the minimum cost to move from $$$(1, 1)$$$ to $$$(n, m)$$$. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line contains three space-separated integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \leq n, m \leq 200$$$, $$$0 \leq k \leq 10^9$$$). Then, $$$n$$$ lines follow. The $$$i$$$-th line contains $$$m$$$ space-separated integers, $$$a_{i,1},\,a_{i,2},\,\ldots,\,a_{i,m}$$$ ($$$0 \leq a_{i,j} \leq 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$5 \cdot 10^4$$$. Output: For each test case, output a single integer, the minimum cost to move from $$$(1, 1)$$$ to $$$(n, m)$$$. Examples: input: 5 3 3 100 3 4 9 5 2 4 0 101 101 3 4 1 10 0 0 10 0 0 10 0 10 10 0 10 1 1 3 4 3 2 3 1 2 3 6 5 4 10 10 14 58 49 25 12 89 69 8 49 71 23 45 27 65 59 36 100 73 23 5 84 82 91 54 92 53 15 43 46 11 65 61 69 71 87 67 72 51 42 55 80 1 64 8 54 61 70 47 100 84 50 86 93 43 51 47 35 56 20 33 61 100 59 5 68 15 55 69 8 8 60 33 61 20 79 69 51 23 24 56 28 67 76 3 69 58 79 75 10 65 63 6 64 73 79 17 62 55 53 61 58 output: 113 6 4 13 618 Note: In the first test case, the minimum cost of $$$113$$$ can be achieved as follows: 1. Cyclically shift row 3 once. The grid now becomes $$$$$$\begin{bmatrix}3 & 4 & 9\\5 & 2 & 4\\101 & 101 & 0\end{bmatrix}.$$$$$$ 2. Move as follows: $$$(1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (3, 3)$$$. $$$x = 1$$$ operation is done before moving. The sum of integers on visited cells is $$$y = 3 + 4 + 2 + 4 + 0 = 13$$$. Therefore, the cost is $$$kx + y = 100 \cdot 1 + 13 = 113$$$. In the second test case, one can shift row 1 once, row 2 twice, and row 3 thrice. Then, the grid becomes $$$$$$\begin{bmatrix}0 & 0 & 10 & 10\\10 & 0 & 0 & 0\\10 & 10 & 10 & 0\end{bmatrix}.$$$$$$ $$$x = 6$$$ operations were done before moving, and there is a path of cost $$$y = 0$$$. Therefore, the cost is $$$6 \cdot 1 + 0 = 6$$$.
2049E
https://codeforces.com/problemset/problem/2049/E
Broken Queries
2,400
[ "brute force", "binary search", "implementation", "constructive algorithms", "interactive", "bitmasks" ]
Div. 2
2,000
256
You, a wizard whose creation was destroyed by a dragon, are determined to hunt it down with a magical AOE tracker. But it seems to be toyed with... This is an interactive problem. There is a hidden binary array $$$a$$$ of length $$$n$$$ ($$$\mathbf{n}$$$ is a power of 2) and a hidden integer $$$k\ (2 \le k \le n - 1)$$$. The array $$$a$$$ contains exactly one 1 (and all other elements are 0). For two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$), define the range sum $$$s(l, r) = a_l + a_{l+1} + \cdots + a_r$$$. You have a magical device that takes ranges and returns range sums, but it returns the opposite result when the range has length at least $$$k$$$. Formally, in one query, you can give it a pair of integers $$$[l, r]$$$ where $$$1 \le l \le r \le n$$$, and it will return either $$$0$$$ or $$$1$$$ according to the following rules: - If $$$r - l + 1 < k$$$, it will return $$$s(l, r)$$$. - If $$$r - l + 1 \ge k$$$, it will return $$$1 - s(l, r)$$$. Find $$$k$$$ using at most $$$33$$$ queries. The device is not adaptive. It means that the hidden $$$a$$$ and $$$k$$$ are fixed before the interaction and will not change during the interaction.
null
null
[ [ "2\n8\n\n0\n\n0\n\n1\n\n0\n\n4\n\n1\n\n0", "? 3 5\n\n? 1 8\n\n? 4 8\n\n? 3 8\n\n! 6\n\n? 3 3\n\n? 3 4\n\n! 2" ] ]
In the first test case, $$$k = 6$$$ and the 1 in the hidden array is at index 6, so $$$a = [0, 0, 0, 0, 0, 1, 0, 0]$$$. - For the query 3 5, since $$$5-3+1 = 3 < k$$$, the device answers correctly. Since 6 is not contained in the range $$$[3, 5]$$$, the device answers $$$0$$$. - For the query 1 8, since $$$8 - 1 + 1 = 8 \ge k$$$, the device answers $$$0$$$ incorrectly. - For the query 4 8, since $$$8 - 4 + 1 = 5 < k$$$, the device answers $$$1$$$ correctly. - For the query 3 8, since $$$8 - 3 + 1 = 6 \ge k$$$, the device answers $$$0$$$ incorrectly. In the second test case, $$$k = 2$$$ and the 1 in the hidden array is at index 3, so $$$a = [0, 0, 1, 0]$$$. Note that the example solution may not have enough information to determine $$$k$$$ above; this is only an example.
Title: Broken Queries time_limit_ms: 2000 memory_limit_mb: 256 Description: You, a wizard whose creation was destroyed by a dragon, are determined to hunt it down with a magical AOE tracker. But it seems to be toyed with... This is an interactive problem. There is a hidden binary array $$$a$$$ of length $$$n$$$ ($$$\mathbf{n}$$$ is a power of 2) and a hidden integer $$$k\ (2 \le k \le n - 1)$$$. The array $$$a$$$ contains exactly one 1 (and all other elements are 0). For two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$), define the range sum $$$s(l, r) = a_l + a_{l+1} + \cdots + a_r$$$. You have a magical device that takes ranges and returns range sums, but it returns the opposite result when the range has length at least $$$k$$$. Formally, in one query, you can give it a pair of integers $$$[l, r]$$$ where $$$1 \le l \le r \le n$$$, and it will return either $$$0$$$ or $$$1$$$ according to the following rules: - If $$$r - l + 1 < k$$$, it will return $$$s(l, r)$$$. - If $$$r - l + 1 \ge k$$$, it will return $$$1 - s(l, r)$$$. Find $$$k$$$ using at most $$$33$$$ queries. The device is not adaptive. It means that the hidden $$$a$$$ and $$$k$$$ are fixed before the interaction and will not change during the interaction. Input: None Output: None Examples: input: 2 8 0 0 1 0 4 1 0 output: ? 3 5 ? 1 8 ? 4 8 ? 3 8 ! 6 ? 3 3 ? 3 4 ! 2 Note: In the first test case, $$$k = 6$$$ and the 1 in the hidden array is at index 6, so $$$a = [0, 0, 0, 0, 0, 1, 0, 0]$$$. - For the query 3 5, since $$$5-3+1 = 3 < k$$$, the device answers correctly. Since 6 is not contained in the range $$$[3, 5]$$$, the device answers $$$0$$$. - For the query 1 8, since $$$8 - 1 + 1 = 8 \ge k$$$, the device answers $$$0$$$ incorrectly. - For the query 4 8, since $$$8 - 4 + 1 = 5 < k$$$, the device answers $$$1$$$ correctly. - For the query 3 8, since $$$8 - 3 + 1 = 6 \ge k$$$, the device answers $$$0$$$ incorrectly. In the second test case, $$$k = 2$$$ and the 1 in the hidden array is at index 3, so $$$a = [0, 0, 1, 0]$$$. Note that the example solution may not have enough information to determine $$$k$$$ above; this is only an example.
2037A
https://codeforces.com/problemset/problem/2037/A
Twice
800
[ "implementation" ]
Div. 3
1,000
256
Kinich wakes up to the start of a new day. He turns on his phone, checks his mailbox, and finds a mysterious present. He decides to unbox the present. Kinich unboxes an array $$$a$$$ with $$$n$$$ integers. Initially, Kinich's score is $$$0$$$. He will perform the following operation any number of times: - Select two indices $$$i$$$ and $$$j$$$ $$$(1 \leq i < j \leq n)$$$ such that neither $$$i$$$ nor $$$j$$$ has been chosen in any previous operation and $$$a_i = a_j$$$. Then, add $$$1$$$ to his score. Output the maximum score Kinich can achieve after performing the aforementioned operation any number of times.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 500$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 20$$$) — the length of $$$a$$$. The following line of each test case contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$).
For each test case, output the maximum score achievable on a new line.
[ [ "5\n1\n1\n2\n2 2\n2\n1 2\n4\n1 2 3 1\n6\n1 2 3 1 2 3", "0\n1\n0\n1\n3" ] ]
In the first and third testcases, Kinich cannot perform any operations. In the second testcase, Kinich can perform one operation with $$$i=1$$$ and $$$j=2$$$. In the fourth testcase, Kinich can perform one operation with $$$i=1$$$ and $$$j=4$$$.
Title: Twice time_limit_ms: 1000 memory_limit_mb: 256 Description: Kinich wakes up to the start of a new day. He turns on his phone, checks his mailbox, and finds a mysterious present. He decides to unbox the present. Kinich unboxes an array $$$a$$$ with $$$n$$$ integers. Initially, Kinich's score is $$$0$$$. He will perform the following operation any number of times: - Select two indices $$$i$$$ and $$$j$$$ $$$(1 \leq i < j \leq n)$$$ such that neither $$$i$$$ nor $$$j$$$ has been chosen in any previous operation and $$$a_i = a_j$$$. Then, add $$$1$$$ to his score. Output the maximum score Kinich can achieve after performing the aforementioned operation any number of times. Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 500$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 20$$$) — the length of $$$a$$$. The following line of each test case contains $$$n$$$ space-separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq n$$$). Output: For each test case, output the maximum score achievable on a new line. Examples: input: 5 1 1 2 2 2 2 1 2 4 1 2 3 1 6 1 2 3 1 2 3 output: 0 1 0 1 3 Note: In the first and third testcases, Kinich cannot perform any operations. In the second testcase, Kinich can perform one operation with $$$i=1$$$ and $$$j=2$$$. In the fourth testcase, Kinich can perform one operation with $$$i=1$$$ and $$$j=4$$$.
2037C
https://codeforces.com/problemset/problem/2037/C
Superultra's Favorite Permutation
1,000
[ "constructive algorithms", "greedy", "math", "number theory" ]
Div. 3
2,000
256
Superultra, a little red panda, desperately wants primogems. In his dreams, a voice tells him that he must solve the following task to obtain a lifetime supply of primogems. Help Superultra! Construct a permutation$$$^{\text{∗}}$$$ $$$p$$$ of length $$$n$$$ such that $$$p_i + p_{i+1}$$$ is composite$$$^{\text{†}}$$$ over all $$$1 \leq i \leq n - 1$$$. If it's not possible, output $$$-1$$$.
The first line contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of the permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, if it's not possible to construct $$$p$$$, output $$$-1$$$ on a new line. Otherwise, output $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ on a new line.
[ [ "2\n3\n8", "-1\n1 8 7 3 6 2 4 5" ] ]
In the first example, it can be shown that all permutation of size $$$3$$$ contain two adjacent elements whose sum is prime. For example, in the permutation $$$[2,3,1]$$$ the sum $$$2+3=5$$$ is prime. In the second example, we can verify that the sample output is correct because $$$1+8$$$, $$$8+7$$$, $$$7+3$$$, $$$3+6$$$, $$$6+2$$$, $$$2+4$$$, and $$$4+5$$$ are all composite. There may be other constructions that are correct.
Title: Superultra's Favorite Permutation time_limit_ms: 2000 memory_limit_mb: 256 Description: Superultra, a little red panda, desperately wants primogems. In his dreams, a voice tells him that he must solve the following task to obtain a lifetime supply of primogems. Help Superultra! Construct a permutation$$$^{\text{∗}}$$$ $$$p$$$ of length $$$n$$$ such that $$$p_i + p_{i+1}$$$ is composite$$$^{\text{†}}$$$ over all $$$1 \leq i \leq n - 1$$$. If it's not possible, output $$$-1$$$. Input: The first line contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the length of the permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: For each test case, if it's not possible to construct $$$p$$$, output $$$-1$$$ on a new line. Otherwise, output $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ on a new line. Examples: input: 2 3 8 output: -1 1 8 7 3 6 2 4 5 Note: In the first example, it can be shown that all permutation of size $$$3$$$ contain two adjacent elements whose sum is prime. For example, in the permutation $$$[2,3,1]$$$ the sum $$$2+3=5$$$ is prime. In the second example, we can verify that the sample output is correct because $$$1+8$$$, $$$8+7$$$, $$$7+3$$$, $$$3+6$$$, $$$6+2$$$, $$$2+4$$$, and $$$4+5$$$ are all composite. There may be other constructions that are correct.
2037D
https://codeforces.com/problemset/problem/2037/D
Sharky Surfing
1,300
[ "two pointers", "data structures", "greedy" ]
Div. 3
3,000
256
Mualani loves surfing on her sharky surfboard! Mualani's surf path can be modeled by a number line. She starts at position $$$1$$$, and the path ends at position $$$L$$$. When she is at position $$$x$$$ with a jump power of $$$k$$$, she can jump to any integer position in the interval $$$[x, x+k]$$$. Initially, her jump power is $$$1$$$. However, her surf path isn't completely smooth. There are $$$n$$$ hurdles on her path. Each hurdle is represented by an interval $$$[l, r]$$$, meaning she cannot jump to any position in the interval $$$[l, r]$$$. There are also $$$m$$$ power-ups at certain positions on the path. Power-up $$$i$$$ is located at position $$$x_i$$$ and has a value of $$$v_i$$$. When Mualani is at position $$$x_i$$$, she has the option to collect the power-up to increase her jump power by $$$v_i$$$. There may be multiple power-ups at the same position. When she is at a position with some power-ups, she may choose to take or ignore each individual power-up. No power-up is in the interval of any hurdle. What is the minimum number of power-ups she must collect to reach position $$$L$$$ to finish the path? If it is not possible to finish the surf path, output $$$-1$$$.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$L$$$ ($$$1 \leq n, m \leq 2 \cdot 10^5, 3 \leq L \leq 10^9$$$) — the number of hurdles, the number of power-ups, and the position of the end. The following $$$n$$$ lines contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$2 \leq l_i \leq r_i \leq L-1$$$) — the bounds of the interval for the $$$i$$$'th hurdle. It is guaranteed that $$$r_i + 1 < l_{i+1}$$$ for all $$$1 \leq i < n$$$ (i.e. all hurdles are non-overlapping, sorted by increasing positions, and the end point of a previous hurdle is not consecutive with the start point of the next hurdle). The following $$$m$$$ lines contain two integers $$$x_i$$$ and $$$v_i$$$ ($$$1 \leq x_i, v_i \leq L$$$) — the position and the value for the $$$i$$$'th power-up. There may be multiple power-ups with the same $$$x$$$. It is guaranteed that $$$x_i \leq x_{i+1}$$$ for all $$$1 \leq i < m$$$ (i.e. the power-ups are sorted by non-decreasing position) and no power-up is in the interval of any hurdle. It is guaranteed the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output the minimum number of power-ups she must collect to reach position $$$L$$$. If it is not possible, output $$$-1$$$.
[ [ "4\n2 5 50\n7 14\n30 40\n2 2\n3 1\n3 5\n18 2\n22 32\n4 3 50\n4 6\n15 18\n20 26\n34 38\n1 2\n8 2\n10 2\n1 4 17\n10 14\n1 6\n1 2\n1 2\n16 9\n1 2 10\n5 9\n2 3\n2 2", "4\n-1\n1\n2" ] ]
In the first test case, she can collect power-ups $$$1$$$, $$$2$$$, $$$3$$$, and $$$5$$$ to clear all hurdles. In the second test case, she cannot jump over the first hurdle. In the fourth test case, by collecting both power-ups, she can jump over the hurdle.
Title: Sharky Surfing time_limit_ms: 3000 memory_limit_mb: 256 Description: Mualani loves surfing on her sharky surfboard! Mualani's surf path can be modeled by a number line. She starts at position $$$1$$$, and the path ends at position $$$L$$$. When she is at position $$$x$$$ with a jump power of $$$k$$$, she can jump to any integer position in the interval $$$[x, x+k]$$$. Initially, her jump power is $$$1$$$. However, her surf path isn't completely smooth. There are $$$n$$$ hurdles on her path. Each hurdle is represented by an interval $$$[l, r]$$$, meaning she cannot jump to any position in the interval $$$[l, r]$$$. There are also $$$m$$$ power-ups at certain positions on the path. Power-up $$$i$$$ is located at position $$$x_i$$$ and has a value of $$$v_i$$$. When Mualani is at position $$$x_i$$$, she has the option to collect the power-up to increase her jump power by $$$v_i$$$. There may be multiple power-ups at the same position. When she is at a position with some power-ups, she may choose to take or ignore each individual power-up. No power-up is in the interval of any hurdle. What is the minimum number of power-ups she must collect to reach position $$$L$$$ to finish the path? If it is not possible to finish the surf path, output $$$-1$$$. Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$L$$$ ($$$1 \leq n, m \leq 2 \cdot 10^5, 3 \leq L \leq 10^9$$$) — the number of hurdles, the number of power-ups, and the position of the end. The following $$$n$$$ lines contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$2 \leq l_i \leq r_i \leq L-1$$$) — the bounds of the interval for the $$$i$$$'th hurdle. It is guaranteed that $$$r_i + 1 < l_{i+1}$$$ for all $$$1 \leq i < n$$$ (i.e. all hurdles are non-overlapping, sorted by increasing positions, and the end point of a previous hurdle is not consecutive with the start point of the next hurdle). The following $$$m$$$ lines contain two integers $$$x_i$$$ and $$$v_i$$$ ($$$1 \leq x_i, v_i \leq L$$$) — the position and the value for the $$$i$$$'th power-up. There may be multiple power-ups with the same $$$x$$$. It is guaranteed that $$$x_i \leq x_{i+1}$$$ for all $$$1 \leq i < m$$$ (i.e. the power-ups are sorted by non-decreasing position) and no power-up is in the interval of any hurdle. It is guaranteed the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: For each test case, output the minimum number of power-ups she must collect to reach position $$$L$$$. If it is not possible, output $$$-1$$$. Examples: input: 4 2 5 50 7 14 30 40 2 2 3 1 3 5 18 2 22 32 4 3 50 4 6 15 18 20 26 34 38 1 2 8 2 10 2 1 4 17 10 14 1 6 1 2 1 2 16 9 1 2 10 5 9 2 3 2 2 output: 4 -1 1 2 Note: In the first test case, she can collect power-ups $$$1$$$, $$$2$$$, $$$3$$$, and $$$5$$$ to clear all hurdles. In the second test case, she cannot jump over the first hurdle. In the fourth test case, by collecting both power-ups, she can jump over the hurdle.
2037E
https://codeforces.com/problemset/problem/2037/E
Kachina's Favorite Binary String
1,600
[ "interactive", "greedy", "dp", "two pointers" ]
Div. 3
2,000
256
This is an interactive problem. Kachina challenges you to guess her favorite binary string$$$^{\text{∗}}$$$ $$$s$$$ of length $$$n$$$. She defines $$$f(l, r)$$$ as the number of subsequences$$$^{\text{†}}$$$ of $$$\texttt{01}$$$ in $$$s_l s_{l+1} \ldots s_r$$$. Two subsequences are considered different if they are formed by deleting characters from different positions in the original string, even if the resulting subsequences consist of the same characters. To determine $$$s$$$, you can ask her some questions. In each question, you can choose two indices $$$l$$$ and $$$r$$$ ($$$1 \leq l < r \leq n$$$) and ask her for the value of $$$f(l, r)$$$. Determine and output $$$s$$$ after asking Kachina no more than $$$n$$$ questions. However, it may be the case that $$$s$$$ is impossible to be determined. In this case, you would need to report $$$\texttt{IMPOSSIBLE}$$$ instead. Formally, $$$s$$$ is impossible to be determined if after asking $$$n$$$ questions, there are always multiple possible strings for $$$s$$$, regardless of what questions are asked. Note that if you report $$$\texttt{IMPOSSIBLE}$$$ when there exists a sequence of at most $$$n$$$ queries that will uniquely determine the binary string, you will get the Wrong Answer verdict.
The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$) — the length of $$$s$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
null
[ [ "2\n5\n\n4\n\n0\n\n1\n\n2\n\n2\n\n0", "? 1 5\n\n? 2 4\n\n? 4 5\n\n? 3 5\n\n! 01001\n\n? 1 2\n\n! IMPOSSIBLE" ] ]
In the first test case: In the first query, you ask Kachina for the value of $$$f(1, 5)$$$, and she responds with $$$4$$$ in the input stream. In the second query, you ask Kachina for the value of $$$f(2, 4)$$$. Because there are no subsequences of $$$\texttt{01}$$$ in the string $$$\texttt{100}$$$, she responds with $$$0$$$ in the input stream. After asking $$$4$$$ questions, you report $$$\texttt{01001}$$$ as $$$s$$$, and it is correct. In the second test case: In the first query, you ask Kachina for the value of $$$f(1, 2)$$$, and she responds with $$$0$$$ in the input stream. Notice that this is the only distinct question you can ask. However, notice that the strings $$$00$$$ and $$$11$$$ both have an answer of $$$0$$$, and it is impossible to differentiate between the two. Therefore, we report IMPOSSIBLE. Please note that this example only serves to demonstrate the interaction format. It is not guaranteed the queries provided are optimal or uniquely determine the answer. However, it can be shown there exists a sequence of at most $$$5$$$ queries that does uniquely determine sample test case $$$1$$$.
Title: Kachina's Favorite Binary String time_limit_ms: 2000 memory_limit_mb: 256 Description: This is an interactive problem. Kachina challenges you to guess her favorite binary string$$$^{\text{∗}}$$$ $$$s$$$ of length $$$n$$$. She defines $$$f(l, r)$$$ as the number of subsequences$$$^{\text{†}}$$$ of $$$\texttt{01}$$$ in $$$s_l s_{l+1} \ldots s_r$$$. Two subsequences are considered different if they are formed by deleting characters from different positions in the original string, even if the resulting subsequences consist of the same characters. To determine $$$s$$$, you can ask her some questions. In each question, you can choose two indices $$$l$$$ and $$$r$$$ ($$$1 \leq l < r \leq n$$$) and ask her for the value of $$$f(l, r)$$$. Determine and output $$$s$$$ after asking Kachina no more than $$$n$$$ questions. However, it may be the case that $$$s$$$ is impossible to be determined. In this case, you would need to report $$$\texttt{IMPOSSIBLE}$$$ instead. Formally, $$$s$$$ is impossible to be determined if after asking $$$n$$$ questions, there are always multiple possible strings for $$$s$$$, regardless of what questions are asked. Note that if you report $$$\texttt{IMPOSSIBLE}$$$ when there exists a sequence of at most $$$n$$$ queries that will uniquely determine the binary string, you will get the Wrong Answer verdict. Input: The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 10^4$$$) — the length of $$$s$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$. Output: None Examples: input: 2 5 4 0 1 2 2 0 output: ? 1 5 ? 2 4 ? 4 5 ? 3 5 ! 01001 ? 1 2 ! IMPOSSIBLE Note: In the first test case: In the first query, you ask Kachina for the value of $$$f(1, 5)$$$, and she responds with $$$4$$$ in the input stream. In the second query, you ask Kachina for the value of $$$f(2, 4)$$$. Because there are no subsequences of $$$\texttt{01}$$$ in the string $$$\texttt{100}$$$, she responds with $$$0$$$ in the input stream. After asking $$$4$$$ questions, you report $$$\texttt{01001}$$$ as $$$s$$$, and it is correct. In the second test case: In the first query, you ask Kachina for the value of $$$f(1, 2)$$$, and she responds with $$$0$$$ in the input stream. Notice that this is the only distinct question you can ask. However, notice that the strings $$$00$$$ and $$$11$$$ both have an answer of $$$0$$$, and it is impossible to differentiate between the two. Therefore, we report IMPOSSIBLE. Please note that this example only serves to demonstrate the interaction format. It is not guaranteed the queries provided are optimal or uniquely determine the answer. However, it can be shown there exists a sequence of at most $$$5$$$ queries that does uniquely determine sample test case $$$1$$$.
2037F
https://codeforces.com/problemset/problem/2037/F
Ardent Flames
2,100
[ "binary search", "math", "data structures", "two pointers", "sortings" ]
Div. 3
4,000
256
You have obtained the new limited event character Xilonen. You decide to use her in combat. There are $$$n$$$ enemies in a line. The $$$i$$$'th enemy from the left has health $$$h_i$$$ and is currently at position $$$x_i$$$. Xilonen has an attack damage of $$$m$$$, and you are ready to defeat the enemies with her. Xilonen has a powerful "ground stomp" attack. Before you perform any attacks, you select an integer $$$p$$$ and position Xilonen there ($$$p$$$ can be any integer position, including a position with an enemy currently). Afterwards, for each attack, she deals $$$m$$$ damage to an enemy at position $$$p$$$ (if there are any), $$$m-1$$$ damage to enemies at positions $$$p-1$$$ and $$$p+1$$$, $$$m-2$$$ damage to enemies at positions $$$p-2$$$ and $$$p+2$$$, and so on. Enemies that are at least a distance of $$$m$$$ away from Xilonen take no damage from attacks. Formally, if there is an enemy at position $$$x$$$, she will deal $$$\max(0,m - |p - x|)$$$ damage to that enemy each hit. Note that you may not choose a different $$$p$$$ for different attacks. Over all possible $$$p$$$, output the minimum number of attacks Xilonen must perform to defeat at least $$$k$$$ enemies. If it is impossible to find a $$$p$$$ such that eventually at least $$$k$$$ enemies will be defeated, output $$$-1$$$ instead. Note that an enemy is considered to be defeated if its health reaches $$$0$$$ or below.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) – the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \leq k \leq n \leq 10^5$$$, $$$1 \leq m \leq 10^9$$$). The following line contains $$$n$$$ integers $$$h_1, h_2, ..., h_n$$$ ($$$1 \leq h_i \leq 10^9$$$). The last line of each testcase contains $$$n$$$ integers $$$x_1, x_2, ..., x_n$$$ ($$$1\leq x_i \leq 10^9$$$, $$$x_i < x_{i+1}$$$ for all $$$1 \leq i < n$$$) It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output an integer on a new line, the minimum number of attacks that must be performed to defeat at least $$$k$$$ enemies. If it is impossible to find a $$$p$$$ such that eventually at least $$$k$$$ enemies will be defeated, output $$$-1$$$ instead.
[ [ "6\n5 5 3\n7 7 7 7 7\n1 2 3 4 5\n9 5 9\n2 4 6 8 10 8 6 4 2\n1 2 3 4 5 6 7 8 9\n2 10 2\n1 1\n1 20\n2 10 1\n69696969 420420420\n1 20\n2 10 2\n10 15\n1 19\n2 2 2\n1000000000 1\n1 3", "2\n2\n-1\n6969697\n15\n1000000000" ] ]
In the first testcase, it is optimal to select $$$p=2$$$. Each attack, the first enemy takes $$$5-|2-1|=4$$$ damage, the second enemy takes $$$5$$$ damage, the third enemy takes $$$4$$$ damage, the fourth enemy takes $$$3$$$ damage, and the fifth enemy takes $$$2$$$ damage. After $$$2$$$ attacks, the first three enemies will be defeated. It can be shown that it is impossible to defeat $$$3$$$ enemies in less than $$$2$$$ attacks, no matter which $$$p$$$ is selected. In the second testcase, we must kill all $$$9$$$ enemies. By selecting $$$p=5$$$, all nine enemies will be defeated in $$$2$$$ attacks. In the third testcase, we must kill both enemies. However, it can be shown that no $$$p$$$ selected will damage both enemies at the same time, so the answer is $$$-1$$$. In the fourth testcase, selecting $$$p=1$$$ will enable us to defeat the first enemy in $$$6969697$$$ attacks. In the fifth testcase, selecting $$$p=10$$$ will make each enemy take $$$1$$$ damage per attack. Both enemies will be defeated in $$$15$$$ attacks.
Title: Ardent Flames time_limit_ms: 4000 memory_limit_mb: 256 Description: You have obtained the new limited event character Xilonen. You decide to use her in combat. There are $$$n$$$ enemies in a line. The $$$i$$$'th enemy from the left has health $$$h_i$$$ and is currently at position $$$x_i$$$. Xilonen has an attack damage of $$$m$$$, and you are ready to defeat the enemies with her. Xilonen has a powerful "ground stomp" attack. Before you perform any attacks, you select an integer $$$p$$$ and position Xilonen there ($$$p$$$ can be any integer position, including a position with an enemy currently). Afterwards, for each attack, she deals $$$m$$$ damage to an enemy at position $$$p$$$ (if there are any), $$$m-1$$$ damage to enemies at positions $$$p-1$$$ and $$$p+1$$$, $$$m-2$$$ damage to enemies at positions $$$p-2$$$ and $$$p+2$$$, and so on. Enemies that are at least a distance of $$$m$$$ away from Xilonen take no damage from attacks. Formally, if there is an enemy at position $$$x$$$, she will deal $$$\max(0,m - |p - x|)$$$ damage to that enemy each hit. Note that you may not choose a different $$$p$$$ for different attacks. Over all possible $$$p$$$, output the minimum number of attacks Xilonen must perform to defeat at least $$$k$$$ enemies. If it is impossible to find a $$$p$$$ such that eventually at least $$$k$$$ enemies will be defeated, output $$$-1$$$ instead. Note that an enemy is considered to be defeated if its health reaches $$$0$$$ or below. Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) – the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \leq k \leq n \leq 10^5$$$, $$$1 \leq m \leq 10^9$$$). The following line contains $$$n$$$ integers $$$h_1, h_2, ..., h_n$$$ ($$$1 \leq h_i \leq 10^9$$$). The last line of each testcase contains $$$n$$$ integers $$$x_1, x_2, ..., x_n$$$ ($$$1\leq x_i \leq 10^9$$$, $$$x_i < x_{i+1}$$$ for all $$$1 \leq i < n$$$) It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output: For each test case, output an integer on a new line, the minimum number of attacks that must be performed to defeat at least $$$k$$$ enemies. If it is impossible to find a $$$p$$$ such that eventually at least $$$k$$$ enemies will be defeated, output $$$-1$$$ instead. Examples: input: 6 5 5 3 7 7 7 7 7 1 2 3 4 5 9 5 9 2 4 6 8 10 8 6 4 2 1 2 3 4 5 6 7 8 9 2 10 2 1 1 1 20 2 10 1 69696969 420420420 1 20 2 10 2 10 15 1 19 2 2 2 1000000000 1 1 3 output: 2 2 -1 6969697 15 1000000000 Note: In the first testcase, it is optimal to select $$$p=2$$$. Each attack, the first enemy takes $$$5-|2-1|=4$$$ damage, the second enemy takes $$$5$$$ damage, the third enemy takes $$$4$$$ damage, the fourth enemy takes $$$3$$$ damage, and the fifth enemy takes $$$2$$$ damage. After $$$2$$$ attacks, the first three enemies will be defeated. It can be shown that it is impossible to defeat $$$3$$$ enemies in less than $$$2$$$ attacks, no matter which $$$p$$$ is selected. In the second testcase, we must kill all $$$9$$$ enemies. By selecting $$$p=5$$$, all nine enemies will be defeated in $$$2$$$ attacks. In the third testcase, we must kill both enemies. However, it can be shown that no $$$p$$$ selected will damage both enemies at the same time, so the answer is $$$-1$$$. In the fourth testcase, selecting $$$p=1$$$ will enable us to defeat the first enemy in $$$6969697$$$ attacks. In the fifth testcase, selecting $$$p=10$$$ will make each enemy take $$$1$$$ damage per attack. Both enemies will be defeated in $$$15$$$ attacks.
2037G
https://codeforces.com/problemset/problem/2037/G
Natlan Exploring
2,000
[ "dp", "math", "data structures", "number theory", "combinatorics", "bitmasks" ]
Div. 3
4,000
256
You are exploring the stunning region of Natlan! This region consists of $$$n$$$ cities, and each city is rated with an attractiveness $$$a_i$$$. A directed edge exists from City $$$i$$$ to City $$$j$$$ if and only if $$$i < j$$$ and $$$\gcd(a_i,a_j)\neq 1$$$, where $$$\gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$. Starting from City $$$1$$$, your task is to determine the total number of distinct paths you can take to reach City $$$n$$$, modulo $$$998\,244\,353$$$. Two paths are different if and only if the set of cities visited is different.
The first line contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the number of cities. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$2 \leq a_i \leq 10^6$$$) — the attractiveness of each city.
Output the total number of distinct paths you can take to reach City $$$n$$$, modulo $$$998\,244\,353$$$.
[ [ "5\n2 6 3 4 6", "5" ], [ "5\n4 196 2662 2197 121", "2" ], [ "7\n3 6 8 9 11 12 20", "7" ], [ "2\n2 3", "0" ] ]
In the first example, the five paths are the following: - City $$$1\rightarrow$$$ City $$$5$$$ - City $$$1\rightarrow$$$ City $$$2\rightarrow$$$ City $$$5$$$ - City $$$1\rightarrow$$$ City $$$2\rightarrow$$$ City $$$3\rightarrow$$$ City $$$5$$$ - City $$$1\rightarrow$$$ City $$$2\rightarrow$$$ City $$$4\rightarrow$$$ City $$$5$$$ - City $$$1\rightarrow$$$ City $$$4\rightarrow$$$ City $$$5$$$ In the second example, the two paths are the following: - City $$$1\rightarrow$$$ City $$$3\rightarrow$$$ City $$$5$$$ - City $$$1\rightarrow$$$ City $$$2\rightarrow$$$ City $$$3\rightarrow$$$ City $$$5$$$
Title: Natlan Exploring time_limit_ms: 4000 memory_limit_mb: 256 Description: You are exploring the stunning region of Natlan! This region consists of $$$n$$$ cities, and each city is rated with an attractiveness $$$a_i$$$. A directed edge exists from City $$$i$$$ to City $$$j$$$ if and only if $$$i < j$$$ and $$$\gcd(a_i,a_j)\neq 1$$$, where $$$\gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$. Starting from City $$$1$$$, your task is to determine the total number of distinct paths you can take to reach City $$$n$$$, modulo $$$998\,244\,353$$$. Two paths are different if and only if the set of cities visited is different. Input: The first line contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the number of cities. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$2 \leq a_i \leq 10^6$$$) — the attractiveness of each city. Output: Output the total number of distinct paths you can take to reach City $$$n$$$, modulo $$$998\,244\,353$$$. Examples: input: 5 2 6 3 4 6 output: 5 input: 5 4 196 2662 2197 121 output: 2 input: 7 3 6 8 9 11 12 20 output: 7 input: 2 2 3 output: 0 Note: In the first example, the five paths are the following: - City $$$1\rightarrow$$$ City $$$5$$$ - City $$$1\rightarrow$$$ City $$$2\rightarrow$$$ City $$$5$$$ - City $$$1\rightarrow$$$ City $$$2\rightarrow$$$ City $$$3\rightarrow$$$ City $$$5$$$ - City $$$1\rightarrow$$$ City $$$2\rightarrow$$$ City $$$4\rightarrow$$$ City $$$5$$$ - City $$$1\rightarrow$$$ City $$$4\rightarrow$$$ City $$$5$$$ In the second example, the two paths are the following: - City $$$1\rightarrow$$$ City $$$3\rightarrow$$$ City $$$5$$$ - City $$$1\rightarrow$$$ City $$$2\rightarrow$$$ City $$$3\rightarrow$$$ City $$$5$$$
2050A
https://codeforces.com/problemset/problem/2050/A
Line Breaks
800
[ "implementation" ]
Div. 3
1,000
256
Kostya has a text $$$s$$$ consisting of $$$n$$$ words made up of Latin alphabet letters. He also has two strips on which he must write the text. The first strip can hold $$$m$$$ characters, while the second can hold as many as needed. Kostya must choose a number $$$x$$$ and write the first $$$x$$$ words from $$$s$$$ on the first strip, while all the remaining words are written on the second strip. To save space, the words are written without gaps, but each word must be entirely on one strip. Since space on the second strip is very valuable, Kostya asks you to choose the maximum possible number $$$x$$$ such that all words $$$s_1, s_2, \dots, s_x$$$ fit on the first strip of length $$$m$$$.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 50$$$; $$$1 \le m \le 500$$$) — the number of words in the list and the maximum number of characters that can be on the first strip. The next $$$n$$$ lines contain one word $$$s_i$$$ of lowercase Latin letters, where the length of $$$s_i$$$ does not exceed $$$10$$$.
For each test case, output the maximum number of words $$$x$$$ such that the first $$$x$$$ words have a total length of no more than $$$m$$$.
[ [ "5\n3 1\na\nb\nc\n2 9\nalpha\nbeta\n4 12\nhello\nworld\nand\ncodeforces\n3 2\nab\nc\nd\n3 2\nabc\nab\na", "1\n2\n2\n1\n0" ] ]
Title: Line Breaks time_limit_ms: 1000 memory_limit_mb: 256 Description: Kostya has a text $$$s$$$ consisting of $$$n$$$ words made up of Latin alphabet letters. He also has two strips on which he must write the text. The first strip can hold $$$m$$$ characters, while the second can hold as many as needed. Kostya must choose a number $$$x$$$ and write the first $$$x$$$ words from $$$s$$$ on the first strip, while all the remaining words are written on the second strip. To save space, the words are written without gaps, but each word must be entirely on one strip. Since space on the second strip is very valuable, Kostya asks you to choose the maximum possible number $$$x$$$ such that all words $$$s_1, s_2, \dots, s_x$$$ fit on the first strip of length $$$m$$$. Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 50$$$; $$$1 \le m \le 500$$$) — the number of words in the list and the maximum number of characters that can be on the first strip. The next $$$n$$$ lines contain one word $$$s_i$$$ of lowercase Latin letters, where the length of $$$s_i$$$ does not exceed $$$10$$$. Output: For each test case, output the maximum number of words $$$x$$$ such that the first $$$x$$$ words have a total length of no more than $$$m$$$. Examples: input: 5 3 1 a b c 2 9 alpha beta 4 12 hello world and codeforces 3 2 ab c d 3 2 abc ab a output: 1 2 2 1 0 Note:
2050B
https://codeforces.com/problemset/problem/2050/B
Transfusion
1,100
[ "greedy", "brute force", "math" ]
Div. 3
2,000
256
You are given an array $$$a$$$ of length $$$n$$$. In one operation, you can pick an index $$$i$$$ from $$$2$$$ to $$$n-1$$$ inclusive, and do one of the following actions: - Decrease $$$a_{i-1}$$$ by $$$1$$$, then increase $$$a_{i+1}$$$ by $$$1$$$. - Decrease $$$a_{i+1}$$$ by $$$1$$$, then increase $$$a_{i-1}$$$ by $$$1$$$. After each operation, all the values must be non-negative. Can you make all the elements equal after any number of operations?
First line of input consists of one integer $$$t$$$ ($$$1 \le t \le 10^4$$$)  — the number of test cases. First line of each test case consists of one integer $$$n$$$ ($$$3 \le n \le 2\cdot 10^5$$$). Second line of each test case consists of $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ of all test cases doesn't exceed $$$2\cdot 10^5$$$.
For each test case, print "YES" without quotation marks if it is possible to make all the elements equal after any number of operations; otherwise, print "NO" without quotation marks. You can print answers in any register: "yes", "YeS", "nO"  — will also be considered correct.
[ [ "8\n3\n3 2 1\n3\n1 1 3\n4\n1 2 5 4\n4\n1 6 6 1\n5\n6 2 1 4 2\n4\n1 4 2 1\n5\n3 1 2 1 3\n3\n2 4 2", "YES\nNO\nYES\nNO\nYES\nNO\nNO\nNO" ] ]
Title: Transfusion time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given an array $$$a$$$ of length $$$n$$$. In one operation, you can pick an index $$$i$$$ from $$$2$$$ to $$$n-1$$$ inclusive, and do one of the following actions: - Decrease $$$a_{i-1}$$$ by $$$1$$$, then increase $$$a_{i+1}$$$ by $$$1$$$. - Decrease $$$a_{i+1}$$$ by $$$1$$$, then increase $$$a_{i-1}$$$ by $$$1$$$. After each operation, all the values must be non-negative. Can you make all the elements equal after any number of operations? Input: First line of input consists of one integer $$$t$$$ ($$$1 \le t \le 10^4$$$)  — the number of test cases. First line of each test case consists of one integer $$$n$$$ ($$$3 \le n \le 2\cdot 10^5$$$). Second line of each test case consists of $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ of all test cases doesn't exceed $$$2\cdot 10^5$$$. Output: For each test case, print "YES" without quotation marks if it is possible to make all the elements equal after any number of operations; otherwise, print "NO" without quotation marks. You can print answers in any register: "yes", "YeS", "nO"  — will also be considered correct. Examples: input: 8 3 3 2 1 3 1 1 3 4 1 2 5 4 4 1 6 6 1 5 6 2 1 4 2 4 1 4 2 1 5 3 1 2 1 3 3 2 4 2 output: YES NO YES NO YES NO NO NO Note:
2050C
https://codeforces.com/problemset/problem/2050/C
Uninteresting Number
1,200
[ "brute force", "dp", "math" ]
Div. 3
2,000
256
You are given a number $$$n$$$ with a length of no more than $$$10^5$$$. You can perform the following operation any number of times: choose one of its digits, square it, and replace the original digit with the result. The result must be a digit (that is, if you choose the digit $$$x$$$, then the value of $$$x^2$$$ must be less than $$$10$$$). Is it possible to obtain a number that is divisible by $$$9$$$ through these operations?
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The only line of each test case contains the number $$$n$$$, without leading zeros. The length of the number does not exceed $$$10^5$$$. It is guaranteed that the sum of the lengths of the numbers across all test cases does not exceed $$$10^5$$$.
For each test case, output "YES" if it is possible to obtain a number divisible by $$$9$$$ using the described operations, and "NO" otherwise. You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
[ [ "9\n123\n322\n333333333333\n9997\n5472778912773\n1234567890\n23\n33\n52254522632", "NO\nYES\nYES\nNO\nNO\nYES\nNO\nYES\nYES" ] ]
In the first example, from the integer $$$123$$$, it is possible to obtain only $$$123$$$, $$$143$$$, $$$129$$$, and $$$149$$$, none of which are divisible by $$$9$$$. In the second example, you need to replace the second digit with its square; then $$$n$$$ will equal $$$342 = 38 \cdot 9$$$. In the third example, the integer is already divisible by $$$9$$$.
Title: Uninteresting Number time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a number $$$n$$$ with a length of no more than $$$10^5$$$. You can perform the following operation any number of times: choose one of its digits, square it, and replace the original digit with the result. The result must be a digit (that is, if you choose the digit $$$x$$$, then the value of $$$x^2$$$ must be less than $$$10$$$). Is it possible to obtain a number that is divisible by $$$9$$$ through these operations? Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The only line of each test case contains the number $$$n$$$, without leading zeros. The length of the number does not exceed $$$10^5$$$. It is guaranteed that the sum of the lengths of the numbers across all test cases does not exceed $$$10^5$$$. Output: For each test case, output "YES" if it is possible to obtain a number divisible by $$$9$$$ using the described operations, and "NO" otherwise. You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer. Examples: input: 9 123 322 333333333333 9997 5472778912773 1234567890 23 33 52254522632 output: NO YES YES NO NO YES NO YES YES Note: In the first example, from the integer $$$123$$$, it is possible to obtain only $$$123$$$, $$$143$$$, $$$129$$$, and $$$149$$$, none of which are divisible by $$$9$$$. In the second example, you need to replace the second digit with its square; then $$$n$$$ will equal $$$342 = 38 \cdot 9$$$. In the third example, the integer is already divisible by $$$9$$$.
2050D
https://codeforces.com/problemset/problem/2050/D
Digital string maximization
1,300
[ "greedy", "brute force", "math", "strings" ]
Div. 3
2,000
256
You are given a string $$$s$$$, consisting of digits from $$$0$$$ to $$$9$$$. In one operation, you can pick any digit in this string, except for $$$0$$$ or the leftmost digit, decrease it by $$$1$$$, and then swap it with the digit left to the picked. For example, in one operation from the string $$$1023$$$, you can get $$$1103$$$ or $$$1022$$$. Find the lexicographically maximum string you can obtain after any number of operations.
The first line of the input consists of an integer $$$t$$$ ($$$1 \le t \le 10^4$$$)  — the number of test cases. Each test case consists of a single line consisting of a digital string $$$s$$$ ($$$1 \le |s| \le 2\cdot 10^5$$$), where $$$|s|$$$ denotes the length of $$$s$$$. The string does not contain leading zeroes. It is guaranteed that the sum of $$$|s|$$$ of all test cases doesn't exceed $$$2\cdot 10^5$$$.
For each test case, print the answer on a separate line.
[ [ "6\n19\n1709\n11555\n51476\n9876543210\n5891917899", "81\n6710\n33311\n55431\n9876543210\n7875567711" ] ]
In the first example, the following sequence of operations is suitable: $$$19 \rightarrow 81$$$. In the second example, the following sequence of operations is suitable: $$$1709 \rightarrow 1780 \rightarrow 6180 \rightarrow 6710$$$. In the fourth example, the following sequence of operations is suitable: $$$51476 \rightarrow 53176 \rightarrow 53616 \rightarrow 53651 \rightarrow 55351 \rightarrow 55431$$$.
Title: Digital string maximization time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a string $$$s$$$, consisting of digits from $$$0$$$ to $$$9$$$. In one operation, you can pick any digit in this string, except for $$$0$$$ or the leftmost digit, decrease it by $$$1$$$, and then swap it with the digit left to the picked. For example, in one operation from the string $$$1023$$$, you can get $$$1103$$$ or $$$1022$$$. Find the lexicographically maximum string you can obtain after any number of operations. Input: The first line of the input consists of an integer $$$t$$$ ($$$1 \le t \le 10^4$$$)  — the number of test cases. Each test case consists of a single line consisting of a digital string $$$s$$$ ($$$1 \le |s| \le 2\cdot 10^5$$$), where $$$|s|$$$ denotes the length of $$$s$$$. The string does not contain leading zeroes. It is guaranteed that the sum of $$$|s|$$$ of all test cases doesn't exceed $$$2\cdot 10^5$$$. Output: For each test case, print the answer on a separate line. Examples: input: 6 19 1709 11555 51476 9876543210 5891917899 output: 81 6710 33311 55431 9876543210 7875567711 Note: In the first example, the following sequence of operations is suitable: $$$19 \rightarrow 81$$$. In the second example, the following sequence of operations is suitable: $$$1709 \rightarrow 1780 \rightarrow 6180 \rightarrow 6710$$$. In the fourth example, the following sequence of operations is suitable: $$$51476 \rightarrow 53176 \rightarrow 53616 \rightarrow 53651 \rightarrow 55351 \rightarrow 55431$$$.
2050E
https://codeforces.com/problemset/problem/2050/E
Three Strings
1,500
[ "strings", "dp", "implementation" ]
Div. 3
2,500
256
You are given three strings: $$$a$$$, $$$b$$$, and $$$c$$$, consisting of lowercase Latin letters. The string $$$c$$$ was obtained in the following way: 1. At each step, either string $$$a$$$ or string $$$b$$$ was randomly chosen, and the first character of the chosen string was removed from it and appended to the end of string $$$c$$$, until one of the strings ran out. After that, the remaining characters of the non-empty string were added to the end of $$$c$$$. 2. Then, a certain number of characters in string $$$c$$$ were randomly changed. For example, from the strings $$$a=\color{red}{\text{abra}}$$$ and $$$b=\color{blue}{\text{cada}}$$$, without character replacements, the strings $$$\color{blue}{\text{ca}}\color{red}{\text{ab}}\color{blue}{\text{d}}\color{red}{\text{ra}}\color{blue}{\text{a}}$$$, $$$\color{red}{\text{abra}}\color{blue}{\text{cada}}$$$, $$$\color{red}{\text{a}}\color{blue}{\text{cada}}\color{red}{\text{bra}}$$$ could be obtained. Find the minimum number of characters that could have been changed in string $$$c$$$.
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. The first line of each test case contains one string of lowercase Latin letters $$$a$$$ ($$$1 \leq |a| \leq 10^3$$$) — the first string, where $$$|a|$$$ denotes the length of string $$$a$$$. The second line of each test case contains one string of lowercase Latin letters $$$b$$$ ($$$1 \leq |b| \leq 10^3$$$) — the second string, where $$$|b|$$$ denotes the length of string $$$b$$$. The third line of each test case contains one string of lowercase Latin letters $$$c$$$ ($$$|c| = |a| + |b|$$$) — the third string. It is guaranteed that the sum of $$$|a|$$$ across all test cases does not exceed $$$2 \cdot 10^3$$$. Also, the sum of $$$|b|$$$ across all test cases does not exceed $$$2 \cdot 10^3$$$.
For each test case, output a single integer — the minimum number of characters that could have been changed in string $$$c$$$.
[ [ "7\na\nb\ncb\nab\ncd\nacbd\nab\nba\naabb\nxxx\nyyy\nxyxyxy\na\nbcd\ndecf\ncodes\nhorse\ncodeforces\negg\nannie\negaegaeg", "1\n0\n2\n0\n3\n2\n3" ] ]
Title: Three Strings time_limit_ms: 2500 memory_limit_mb: 256 Description: You are given three strings: $$$a$$$, $$$b$$$, and $$$c$$$, consisting of lowercase Latin letters. The string $$$c$$$ was obtained in the following way: 1. At each step, either string $$$a$$$ or string $$$b$$$ was randomly chosen, and the first character of the chosen string was removed from it and appended to the end of string $$$c$$$, until one of the strings ran out. After that, the remaining characters of the non-empty string were added to the end of $$$c$$$. 2. Then, a certain number of characters in string $$$c$$$ were randomly changed. For example, from the strings $$$a=\color{red}{\text{abra}}$$$ and $$$b=\color{blue}{\text{cada}}$$$, without character replacements, the strings $$$\color{blue}{\text{ca}}\color{red}{\text{ab}}\color{blue}{\text{d}}\color{red}{\text{ra}}\color{blue}{\text{a}}$$$, $$$\color{red}{\text{abra}}\color{blue}{\text{cada}}$$$, $$$\color{red}{\text{a}}\color{blue}{\text{cada}}\color{red}{\text{bra}}$$$ could be obtained. Find the minimum number of characters that could have been changed in string $$$c$$$. Input: The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. The first line of each test case contains one string of lowercase Latin letters $$$a$$$ ($$$1 \leq |a| \leq 10^3$$$) — the first string, where $$$|a|$$$ denotes the length of string $$$a$$$. The second line of each test case contains one string of lowercase Latin letters $$$b$$$ ($$$1 \leq |b| \leq 10^3$$$) — the second string, where $$$|b|$$$ denotes the length of string $$$b$$$. The third line of each test case contains one string of lowercase Latin letters $$$c$$$ ($$$|c| = |a| + |b|$$$) — the third string. It is guaranteed that the sum of $$$|a|$$$ across all test cases does not exceed $$$2 \cdot 10^3$$$. Also, the sum of $$$|b|$$$ across all test cases does not exceed $$$2 \cdot 10^3$$$. Output: For each test case, output a single integer — the minimum number of characters that could have been changed in string $$$c$$$. Examples: input: 7 a b cb ab cd acbd ab ba aabb xxx yyy xyxyxy a bcd decf codes horse codeforces egg annie egaegaeg output: 1 0 2 0 3 2 3 Note:
2050F
https://codeforces.com/problemset/problem/2050/F
Maximum modulo equality
1,700
[ "data structures", "number theory", "math", "divide and conquer" ]
Div. 3
5,000
256
You are given an array $$$a$$$ of length $$$n$$$ and $$$q$$$ queries $$$l$$$, $$$r$$$. For each query, find the maximum possible $$$m$$$, such that all elements $$$a_l$$$, $$$a_{l+1}$$$, ..., $$$a_r$$$ are equal modulo $$$m$$$. In other words, $$$a_l \bmod m = a_{l+1} \bmod m = \dots = a_r \bmod m$$$, where $$$a \bmod b$$$ — is the remainder of division $$$a$$$ by $$$b$$$. In particular, when $$$m$$$ can be infinite, print $$$0$$$.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$q$$$ ($$$1 \le n, q \le 2\cdot 10^5$$$) — the length of the array and the number of queries. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array. In the following $$$q$$$ lines of each test case, two integers $$$l$$$, $$$r$$$ are provided ($$$1 \le l \le r \le n$$$) — the range of the query. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2\cdot 10^5$$$, and the sum of $$$q$$$ does not exceed $$$2\cdot 10^5$$$.
For each query, output the maximum value $$$m$$$ described in the statement.
[ [ "3\n5 5\n5 14 2 6 3\n4 5\n1 4\n2 4\n3 5\n1 1\n1 1\n7\n1 1\n3 2\n1 7 8\n2 3\n1 2", "3 1 4 1 0 \n0 \n1 6" ] ]
In the first query of the first sample, $$$6 \bmod 3 = 3 \bmod 3 = 0$$$. It can be shown that for greater $$$m$$$, the required condition will not be fulfilled. In the third query of the first sample, $$$14 \bmod 4 = 2 \bmod 4 = 6 \bmod 4 = 2$$$. It can be shown that for greater $$$m$$$, the required condition will not be fulfilled.
Title: Maximum modulo equality time_limit_ms: 5000 memory_limit_mb: 256 Description: You are given an array $$$a$$$ of length $$$n$$$ and $$$q$$$ queries $$$l$$$, $$$r$$$. For each query, find the maximum possible $$$m$$$, such that all elements $$$a_l$$$, $$$a_{l+1}$$$, ..., $$$a_r$$$ are equal modulo $$$m$$$. In other words, $$$a_l \bmod m = a_{l+1} \bmod m = \dots = a_r \bmod m$$$, where $$$a \bmod b$$$ — is the remainder of division $$$a$$$ by $$$b$$$. In particular, when $$$m$$$ can be infinite, print $$$0$$$. Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$q$$$ ($$$1 \le n, q \le 2\cdot 10^5$$$) — the length of the array and the number of queries. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array. In the following $$$q$$$ lines of each test case, two integers $$$l$$$, $$$r$$$ are provided ($$$1 \le l \le r \le n$$$) — the range of the query. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2\cdot 10^5$$$, and the sum of $$$q$$$ does not exceed $$$2\cdot 10^5$$$. Output: For each query, output the maximum value $$$m$$$ described in the statement. Examples: input: 3 5 5 5 14 2 6 3 4 5 1 4 2 4 3 5 1 1 1 1 7 1 1 3 2 1 7 8 2 3 1 2 output: 3 1 4 1 0 0 1 6 Note: In the first query of the first sample, $$$6 \bmod 3 = 3 \bmod 3 = 0$$$. It can be shown that for greater $$$m$$$, the required condition will not be fulfilled. In the third query of the first sample, $$$14 \bmod 4 = 2 \bmod 4 = 6 \bmod 4 = 2$$$. It can be shown that for greater $$$m$$$, the required condition will not be fulfilled.
2050G
https://codeforces.com/problemset/problem/2050/G
Tree Destruction
1,900
[ "dfs and similar", "dp", "trees" ]
Div. 3
2,000
256
Given a tree$$$^{\text{∗}}$$$ with $$$n$$$ vertices. You can choose two vertices $$$a$$$ and $$$b$$$ once and remove all vertices on the path from $$$a$$$ to $$$b$$$, including the vertices themselves. If you choose $$$a=b$$$, only one vertex will be removed. Your task is to find the maximum number of connected components$$$^{\text{†}}$$$ that can be formed after removing the path from the tree.
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the size of the tree. The next $$$n-1$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \ne v$$$) — the vertices connected by an edge. It is guaranteed that the edges form a tree. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output one integer — the maximum number of connected components that can be achieved using the described operation.
[ [ "6\n2\n1 2\n5\n1 2\n2 3\n3 4\n3 5\n4\n1 2\n2 3\n3 4\n5\n2 1\n3 1\n4 1\n5 4\n6\n2 1\n3 1\n4 1\n5 3\n6 3\n6\n2 1\n3 2\n4 2\n5 3\n6 4", "1\n3\n2\n3\n4\n3" ] ]
Title: Tree Destruction time_limit_ms: 2000 memory_limit_mb: 256 Description: Given a tree$$$^{\text{∗}}$$$ with $$$n$$$ vertices. You can choose two vertices $$$a$$$ and $$$b$$$ once and remove all vertices on the path from $$$a$$$ to $$$b$$$, including the vertices themselves. If you choose $$$a=b$$$, only one vertex will be removed. Your task is to find the maximum number of connected components$$$^{\text{†}}$$$ that can be formed after removing the path from the tree. Input: The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the size of the tree. The next $$$n-1$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$, $$$u \ne v$$$) — the vertices connected by an edge. It is guaranteed that the edges form a tree. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$. Output: For each test case, output one integer — the maximum number of connected components that can be achieved using the described operation. Examples: input: 6 2 1 2 5 1 2 2 3 3 4 3 5 4 1 2 2 3 3 4 5 2 1 3 1 4 1 5 4 6 2 1 3 1 4 1 5 3 6 3 6 2 1 3 2 4 2 5 3 6 4 output: 1 3 2 3 4 3 Note:
2032D
https://codeforces.com/problemset/problem/2032/D
Genokraken
1,800
[ "graphs", "implementation", "data structures", "two pointers", "constructive algorithms", "interactive", "greedy", "trees" ]
Div. 2
2,000
256
This is an interactive problem. Upon clearing the Waterside Area, Gretel has found a monster named Genokraken, and she's keeping it contained for her scientific studies. The monster's nerve system can be structured as a tree$$$^{\dagger}$$$ of $$$n$$$ nodes (really, everything should stop resembling trees all the time$$$\ldots$$$), numbered from $$$0$$$ to $$$n-1$$$, with node $$$0$$$ as the root. Gretel's objective is to learn the exact structure of the monster's nerve system — more specifically, she wants to know the values $$$p_1, p_2, \ldots, p_{n-1}$$$ of the tree, where $$$p_i$$$ ($$$0 \le p_i < i$$$) is the direct parent node of node $$$i$$$ ($$$1 \le i \le n - 1$$$). She doesn't know exactly how the nodes are placed, but she knows a few convenient facts: - If we remove root node $$$0$$$ and all adjacent edges, this tree will turn into a forest consisting of only paths$$$^{\ddagger}$$$. Each node that was initially adjacent to the node $$$0$$$ will be the end of some path. - The nodes are indexed in a way that if $$$1 \le x \le y \le n - 1$$$, then $$$p_x \le p_y$$$. - Node $$$1$$$ has exactly two adjacent nodes (including the node $$$0$$$). The tree in this picture does not satisfy the condition, because if we remove node $$$0$$$, then node $$$2$$$ (which was initially adjacent to the node $$$0$$$) will not be the end of the path $$$4-2-5$$$.The tree in this picture does not satisfy the condition, because $$$p_3 \le p_4$$$ must hold.The tree in this picture does not satisfy the condition, because node $$$1$$$ has only one adjacent node. Gretel can make queries to the containment cell: - "? a b" ($$$1 \le a, b < n$$$, $$$a \ne b$$$) — the cell will check if the simple path between nodes $$$a$$$ and $$$b$$$ contains the node $$$0$$$. However, to avoid unexpected consequences by overstimulating the creature, Gretel wants to query at most $$$2n - 6$$$ times. Though Gretel is gifted, she can't do everything all at once, so can you give her a helping hand? $$$^{\dagger}$$$A tree is a connected graph where every pair of distinct nodes has exactly one simple path connecting them. $$$^{\ddagger}$$$A path is a tree whose vertices can be listed in the order $$$v_1, v_2, \ldots, v_k$$$ such that the edges are $$$(v_i, v_{i+1})$$$ ($$$1 \le i < k$$$).
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$4 \le n \le 10^4$$$) — the number of nodes in Genokraken's nerve system. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
null
[ [ "3\n4\n\n1\n\n5\n\n1\n\n0\n\n9", "? 2 3\n\n! 0 0 1\n\n? 2 3\n\n? 2 4\n\n! 0 0 1 2\n\n! 0 0 0 1 3 5 6 7" ] ]
In the first test case, Genokraken's nerve system forms the following tree: - The answer to "? 2 3" is $$$1$$$. This means that the simple path between nodes $$$2$$$ and $$$3$$$ contains node $$$0$$$. In the second test case, Genokraken's nerve system forms the following tree: - The answer to "? 2 3" is $$$1$$$. This means that the simple path between nodes $$$2$$$ and $$$3$$$ contains node $$$0$$$. - The answer to "? 2 4" is $$$0$$$. This means that the simple path between nodes $$$2$$$ and $$$4$$$ doesn't contain node $$$0$$$. In the third test case, Genokraken's nerve system forms the following tree:
Title: Genokraken time_limit_ms: 2000 memory_limit_mb: 256 Description: This is an interactive problem. Upon clearing the Waterside Area, Gretel has found a monster named Genokraken, and she's keeping it contained for her scientific studies. The monster's nerve system can be structured as a tree$$$^{\dagger}$$$ of $$$n$$$ nodes (really, everything should stop resembling trees all the time$$$\ldots$$$), numbered from $$$0$$$ to $$$n-1$$$, with node $$$0$$$ as the root. Gretel's objective is to learn the exact structure of the monster's nerve system — more specifically, she wants to know the values $$$p_1, p_2, \ldots, p_{n-1}$$$ of the tree, where $$$p_i$$$ ($$$0 \le p_i < i$$$) is the direct parent node of node $$$i$$$ ($$$1 \le i \le n - 1$$$). She doesn't know exactly how the nodes are placed, but she knows a few convenient facts: - If we remove root node $$$0$$$ and all adjacent edges, this tree will turn into a forest consisting of only paths$$$^{\ddagger}$$$. Each node that was initially adjacent to the node $$$0$$$ will be the end of some path. - The nodes are indexed in a way that if $$$1 \le x \le y \le n - 1$$$, then $$$p_x \le p_y$$$. - Node $$$1$$$ has exactly two adjacent nodes (including the node $$$0$$$). The tree in this picture does not satisfy the condition, because if we remove node $$$0$$$, then node $$$2$$$ (which was initially adjacent to the node $$$0$$$) will not be the end of the path $$$4-2-5$$$.The tree in this picture does not satisfy the condition, because $$$p_3 \le p_4$$$ must hold.The tree in this picture does not satisfy the condition, because node $$$1$$$ has only one adjacent node. Gretel can make queries to the containment cell: - "? a b" ($$$1 \le a, b < n$$$, $$$a \ne b$$$) — the cell will check if the simple path between nodes $$$a$$$ and $$$b$$$ contains the node $$$0$$$. However, to avoid unexpected consequences by overstimulating the creature, Gretel wants to query at most $$$2n - 6$$$ times. Though Gretel is gifted, she can't do everything all at once, so can you give her a helping hand? $$$^{\dagger}$$$A tree is a connected graph where every pair of distinct nodes has exactly one simple path connecting them. $$$^{\ddagger}$$$A path is a tree whose vertices can be listed in the order $$$v_1, v_2, \ldots, v_k$$$ such that the edges are $$$(v_i, v_{i+1})$$$ ($$$1 \le i < k$$$). Input: Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$4 \le n \le 10^4$$$) — the number of nodes in Genokraken's nerve system. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$. Output: None Examples: input: 3 4 1 5 1 0 9 output: ? 2 3 ! 0 0 1 ? 2 3 ? 2 4 ! 0 0 1 2 ! 0 0 0 1 3 5 6 7 Note: In the first test case, Genokraken's nerve system forms the following tree: - The answer to "? 2 3" is $$$1$$$. This means that the simple path between nodes $$$2$$$ and $$$3$$$ contains node $$$0$$$. In the second test case, Genokraken's nerve system forms the following tree: - The answer to "? 2 3" is $$$1$$$. This means that the simple path between nodes $$$2$$$ and $$$3$$$ contains node $$$0$$$. - The answer to "? 2 4" is $$$0$$$. This means that the simple path between nodes $$$2$$$ and $$$4$$$ doesn't contain node $$$0$$$. In the third test case, Genokraken's nerve system forms the following tree:
2048A
https://codeforces.com/problemset/problem/2048/A
Kevin and Combination Lock
800
[ "brute force", "implementation", "math", "number theory", "greedy" ]
Codeforces Global Round 28
1,000
256
Kevin is trapped in Lakeside Village by Grace. At the exit of the village, there is a combination lock that can only be unlocked if Kevin solves it. The combination lock starts with an integer $$$ x $$$. Kevin can perform one of the following two operations zero or more times: 1. If $$$ x \neq 33 $$$, he can select two consecutive digits $$$ 3 $$$ from $$$ x $$$ and remove them simultaneously. For example, if $$$ x = 13\,323 $$$, he can remove the second and third $$$ 3 $$$, changing $$$ x $$$ to $$$ 123 $$$. 2. If $$$ x \geq 33 $$$, he can change $$$ x $$$ to $$$ x - 33 $$$. For example, if $$$ x = 99 $$$, he can choose this operation to change $$$ x $$$ to $$$ 99 - 33 = 66 $$$. When the value of $$$ x $$$ on the combination lock becomes $$$ 0 $$$, Kevin can unlock the lock and escape from Lakeside Village. Please determine whether it is possible for Kevin to unlock the combination lock and escape.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The only line of each test case contains a positive integer $$$x$$$ ($$$1\leq x\leq 10^9$$$).
For each test case, output "YES" or "NO" (without quotes) in one line, representing whether Kevin can unlock the combination lock and escape. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
[ [ "5\n165\n6369\n666\n114514\n133333332", "YES\nYES\nNO\nNO\nYES" ] ]
For the first test case, $$$165\xrightarrow{-33}132\xrightarrow{-33}99\xrightarrow{-33}66\xrightarrow{-33}33\xrightarrow{-33}0$$$. For the second test case, $$$6369\xrightarrow{-33}6{\color{red}{33}}6\xrightarrow{\text{remove "33"}}66\xrightarrow{-33}33\xrightarrow{-33}0$$$. For the third test case, it can be proven that, regardless of the operations performed, $$$666$$$ cannot be transformed into $$$0$$$.
Title: Kevin and Combination Lock time_limit_ms: 1000 memory_limit_mb: 256 Description: Kevin is trapped in Lakeside Village by Grace. At the exit of the village, there is a combination lock that can only be unlocked if Kevin solves it. The combination lock starts with an integer $$$ x $$$. Kevin can perform one of the following two operations zero or more times: 1. If $$$ x \neq 33 $$$, he can select two consecutive digits $$$ 3 $$$ from $$$ x $$$ and remove them simultaneously. For example, if $$$ x = 13\,323 $$$, he can remove the second and third $$$ 3 $$$, changing $$$ x $$$ to $$$ 123 $$$. 2. If $$$ x \geq 33 $$$, he can change $$$ x $$$ to $$$ x - 33 $$$. For example, if $$$ x = 99 $$$, he can choose this operation to change $$$ x $$$ to $$$ 99 - 33 = 66 $$$. When the value of $$$ x $$$ on the combination lock becomes $$$ 0 $$$, Kevin can unlock the lock and escape from Lakeside Village. Please determine whether it is possible for Kevin to unlock the combination lock and escape. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The only line of each test case contains a positive integer $$$x$$$ ($$$1\leq x\leq 10^9$$$). Output: For each test case, output "YES" or "NO" (without quotes) in one line, representing whether Kevin can unlock the combination lock and escape. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Examples: input: 5 165 6369 666 114514 133333332 output: YES YES NO NO YES Note: For the first test case, $$$165\xrightarrow{-33}132\xrightarrow{-33}99\xrightarrow{-33}66\xrightarrow{-33}33\xrightarrow{-33}0$$$. For the second test case, $$$6369\xrightarrow{-33}6{\color{red}{33}}6\xrightarrow{\text{remove "33"}}66\xrightarrow{-33}33\xrightarrow{-33}0$$$. For the third test case, it can be proven that, regardless of the operations performed, $$$666$$$ cannot be transformed into $$$0$$$.
2048D
https://codeforces.com/problemset/problem/2048/D
Kevin and Competition Memories
1,600
[ "brute force", "binary search", "data structures", "two pointers", "sortings", "greedy" ]
Codeforces Global Round 28
2,000
256
Kevin used to get into Rio's Memories, and in Rio's Memories, a series of contests was once held. Kevin remembers all the participants and all the contest problems from that time, but he has forgotten the specific rounds, the distribution of problems, and the exact rankings. There are $$$ m $$$ problems in total, with the $$$ i $$$-th problem having a difficulty of $$$ b_i $$$. Let each contest consist of $$$ k $$$ problems, resulting in a total of $$$ \lfloor \frac{m}{k} \rfloor $$$ contests. This means that you select exactly $$$ \lfloor \frac{m}{k} \rfloor \cdot k $$$ problems for the contests in any combination you want, with each problem being selected at most once, and the remaining $$$m\bmod k$$$ problems are left unused. For example, if $$$m = 17$$$ and $$$k = 3$$$, you should create exactly $$$5$$$ contests consisting of $$$3$$$ problems each, and exactly $$$2$$$ problems will be left unused. There are $$$ n $$$ participants in the contests, with Kevin being the $$$1$$$-st participant. The $$$ i $$$-th participant has a rating of $$$ a_i $$$. During the contests, each participant solves all problems with a difficulty not exceeding their rating, meaning the $$$ i $$$-th participant solves the $$$ j $$$-th problem if and only if $$$ a_i \geq b_j $$$. In each contest, Kevin's rank is one plus the number of participants who solve more problems than he does. For each $$$ k = 1, 2, \ldots, m $$$, Kevin wants to know the minimum sum of his ranks across all $$$ \lfloor \frac{m}{k} \rfloor $$$ contests. In other words, for some value of $$$k$$$, after selecting the problems for each contest, you calculate the rank of Kevin in each contest and sum up these ranks over all $$$ \lfloor \frac{m}{k} \rfloor $$$ contests. Your goal is to minimize this value. Note that contests for different values of $$$k$$$ are independent. It means that for different values of $$$k$$$, you can select the distribution of problems into the contests independently.
Each test contains multiple test cases. The first line contains the number of test cases $$$ t $$$ ($$$ 1 \le t \le 5\cdot 10^4 $$$). The first line of each test case contains two integers $$$ n $$$ and $$$ m $$$ ($$$ 1 \le n, m \leq 3\cdot 10^5 $$$) — the number of participants and the number of problems. The second line of each test case contains $$$ n $$$ integers $$$ a_1, a_2, \ldots, a_n $$$ ($$$ 0 \le a_i \le 10^9 $$$) — the rating of each participant. The third line of each test case contains $$$ m $$$ integers $$$ b_1, b_2, \ldots, b_m $$$ ($$$ 0 \le b_i \le 10^9 $$$) — the difficulty of each problem. It is guaranteed that both the sum of $$$ n $$$ and the sum of $$$ m $$$ over all test cases do not exceed $$$ 3 \cdot 10^5 $$$.
For each test case, output $$$m$$$ integers — the minimum sum of Kevin's ranks for each $$$ k = 1, 2, \ldots, m$$$.
[ [ "4\n4 4\n4 3 7 5\n2 5 4 6\n5 5\n5 0 4 8 6\n1 3 9 2 7\n6 7\n1 1 4 5 1 4\n1 9 1 9 8 1 0\n7 6\n1 9 1 9 8 1 0\n1 1 4 5 1 4", "7 4 2 3\n6 2 1 1 2\n7 3 2 1 1 1 1\n15 9 5 4 4 4" ] ]
For the first test case: When $$$k=1$$$, since each contest only contains one problem, the distribution is in fact unique. For example, in the contest which only includes the third problem (which has a difficulty of $$$4$$$), all participants except the $$$2$$$-nd can solve it. Since no one solves strictly more problems than Kevin, his ranking in this contest is $$$1$$$. Similarly, in all $$$4$$$ contests, Kevin's rankings are $$$1,3,1,2$$$, and the sum is $$$7$$$. When $$$k=2$$$, one optimal way is to choose the $$$1$$$-st and the $$$3$$$-rd problem to form a contest, while the $$$2$$$-nd and $$$4$$$-th for another. In the former contest, $$$4$$$ participants respectively solve $$$2,1,2,2$$$ problems, so Kevin's ranking is $$$1$$$; in the latter one, they respectively solve $$$0,0,2,1$$$, since there are $$$2$$$ participants ($$$3$$$-rd and $$$4$$$-th) solve more problems than Kevin, his ranking is $$$1+2=3$$$. Thus the answer is $$$1+3=4$$$. It can be proven that there's no way to achieve a lower sum. When $$$k=3$$$, we can simply choose the $$$1$$$-st, the $$$3$$$-rd, and the $$$4$$$-th problem to make a contest, and Kevin has a ranking of $$$2$$$, which is optimal. When $$$k=4$$$, since there's only one contest, the distribution is also unique, and Kevin's ranking is $$$3$$$.
Title: Kevin and Competition Memories time_limit_ms: 2000 memory_limit_mb: 256 Description: Kevin used to get into Rio's Memories, and in Rio's Memories, a series of contests was once held. Kevin remembers all the participants and all the contest problems from that time, but he has forgotten the specific rounds, the distribution of problems, and the exact rankings. There are $$$ m $$$ problems in total, with the $$$ i $$$-th problem having a difficulty of $$$ b_i $$$. Let each contest consist of $$$ k $$$ problems, resulting in a total of $$$ \lfloor \frac{m}{k} \rfloor $$$ contests. This means that you select exactly $$$ \lfloor \frac{m}{k} \rfloor \cdot k $$$ problems for the contests in any combination you want, with each problem being selected at most once, and the remaining $$$m\bmod k$$$ problems are left unused. For example, if $$$m = 17$$$ and $$$k = 3$$$, you should create exactly $$$5$$$ contests consisting of $$$3$$$ problems each, and exactly $$$2$$$ problems will be left unused. There are $$$ n $$$ participants in the contests, with Kevin being the $$$1$$$-st participant. The $$$ i $$$-th participant has a rating of $$$ a_i $$$. During the contests, each participant solves all problems with a difficulty not exceeding their rating, meaning the $$$ i $$$-th participant solves the $$$ j $$$-th problem if and only if $$$ a_i \geq b_j $$$. In each contest, Kevin's rank is one plus the number of participants who solve more problems than he does. For each $$$ k = 1, 2, \ldots, m $$$, Kevin wants to know the minimum sum of his ranks across all $$$ \lfloor \frac{m}{k} \rfloor $$$ contests. In other words, for some value of $$$k$$$, after selecting the problems for each contest, you calculate the rank of Kevin in each contest and sum up these ranks over all $$$ \lfloor \frac{m}{k} \rfloor $$$ contests. Your goal is to minimize this value. Note that contests for different values of $$$k$$$ are independent. It means that for different values of $$$k$$$, you can select the distribution of problems into the contests independently. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$ t $$$ ($$$ 1 \le t \le 5\cdot 10^4 $$$). The first line of each test case contains two integers $$$ n $$$ and $$$ m $$$ ($$$ 1 \le n, m \leq 3\cdot 10^5 $$$) — the number of participants and the number of problems. The second line of each test case contains $$$ n $$$ integers $$$ a_1, a_2, \ldots, a_n $$$ ($$$ 0 \le a_i \le 10^9 $$$) — the rating of each participant. The third line of each test case contains $$$ m $$$ integers $$$ b_1, b_2, \ldots, b_m $$$ ($$$ 0 \le b_i \le 10^9 $$$) — the difficulty of each problem. It is guaranteed that both the sum of $$$ n $$$ and the sum of $$$ m $$$ over all test cases do not exceed $$$ 3 \cdot 10^5 $$$. Output: For each test case, output $$$m$$$ integers — the minimum sum of Kevin's ranks for each $$$ k = 1, 2, \ldots, m$$$. Examples: input: 4 4 4 4 3 7 5 2 5 4 6 5 5 5 0 4 8 6 1 3 9 2 7 6 7 1 1 4 5 1 4 1 9 1 9 8 1 0 7 6 1 9 1 9 8 1 0 1 1 4 5 1 4 output: 7 4 2 3 6 2 1 1 2 7 3 2 1 1 1 1 15 9 5 4 4 4 Note: For the first test case: When $$$k=1$$$, since each contest only contains one problem, the distribution is in fact unique. For example, in the contest which only includes the third problem (which has a difficulty of $$$4$$$), all participants except the $$$2$$$-nd can solve it. Since no one solves strictly more problems than Kevin, his ranking in this contest is $$$1$$$. Similarly, in all $$$4$$$ contests, Kevin's rankings are $$$1,3,1,2$$$, and the sum is $$$7$$$. When $$$k=2$$$, one optimal way is to choose the $$$1$$$-st and the $$$3$$$-rd problem to form a contest, while the $$$2$$$-nd and $$$4$$$-th for another. In the former contest, $$$4$$$ participants respectively solve $$$2,1,2,2$$$ problems, so Kevin's ranking is $$$1$$$; in the latter one, they respectively solve $$$0,0,2,1$$$, since there are $$$2$$$ participants ($$$3$$$-rd and $$$4$$$-th) solve more problems than Kevin, his ranking is $$$1+2=3$$$. Thus the answer is $$$1+3=4$$$. It can be proven that there's no way to achieve a lower sum. When $$$k=3$$$, we can simply choose the $$$1$$$-st, the $$$3$$$-rd, and the $$$4$$$-th problem to make a contest, and Kevin has a ranking of $$$2$$$, which is optimal. When $$$k=4$$$, since there's only one contest, the distribution is also unique, and Kevin's ranking is $$$3$$$.
2036G
https://codeforces.com/problemset/problem/2036/G
Library of Magic
2,200
[ "binary search", "math", "divide and conquer", "number theory", "constructive algorithms", "interactive" ]
Div. 3
2,000
256
This is an interactive problem. The Department of Supernatural Phenomena at the Oxenfurt Academy has opened the Library of Magic, which contains the works of the greatest sorcerers of Redania — $$$n$$$ ($$$3 \leq n \leq 10^{18}$$$) types of books, numbered from $$$1$$$ to $$$n$$$. Each book's type number is indicated on its spine. Moreover, each type of book is stored in the library in exactly two copies! And you have been appointed as the librarian. One night, you wake up to a strange noise and see a creature leaving the building through a window. Three thick tomes of different colors were sticking out of the mysterious thief's backpack. Before you start searching for them, you decide to compute the numbers $$$a$$$, $$$b$$$, and $$$c$$$ written on the spines of these books. All three numbers are distinct. So, you have an unordered set of tomes, which includes one tome with each of the pairwise distinct numbers $$$a$$$, $$$b$$$, and $$$c$$$, and two tomes for all numbers from $$$1$$$ to $$$n$$$, except for $$$a$$$, $$$b$$$, and $$$c$$$. You want to find these values $$$a$$$, $$$b$$$, and $$$c$$$. Since you are not working in a simple library, but in the Library of Magic, you can only use one spell in the form of a query to check the presence of books in their place: - "xor l r" — Bitwise XOR query with parameters $$$l$$$ and $$$r$$$. Let $$$k$$$ be the number of such tomes in the library whose numbers are greater than or equal to $$$l$$$ and less than or equal to $$$r$$$. You will receive the result of the computation $$$v_1 \oplus v_2 \oplus ... \oplus v_k$$$, where $$$v_1 ... v_k$$$ are the numbers on the spines of these tomes, and $$$\oplus$$$ denotes the operation of bitwise exclusive OR. Since your magical abilities as a librarian are severely limited, you can make no more than $$$150$$$ queries.
The first line of input contains an integer $$$t$$$ ($$$1 \le t \le 300$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$3 \leq n \leq 10^{18}$$$) — the number of types of tomes.
null
[ [ "2\n6\n\n0\n\n2\n\n3\n\n5\n\n3", "xor 1 1\n\nxor 2 2\n\nxor 3 3\n\nxor 4 6\n\nans 2 3 5\n\nans 1 2 3" ] ]
In the first test case, the books in the library after the theft look like this: Now consider the answers to the queries: - For the query "xor 1 1", you receive the result $$$1 \oplus 1 = 0$$$. Two tomes satisfy the condition specified in the query — both with the number $$$1$$$. - For the query "xor 2 2", you receive the result $$$2$$$, as only one tome satisfies the specified condition. - For the query "xor 3 3", you receive the result $$$3$$$. - For the query "xor 4 6", you receive the result $$$4 \oplus 6 \oplus 4 \oplus 5 \oplus 6 = 5$$$. In the second test case, there are only $$$3$$$ types of books, and it is easy to guess that the missing ones have the numbers $$$1$$$, $$$2$$$, and $$$3$$$.
Title: Library of Magic time_limit_ms: 2000 memory_limit_mb: 256 Description: This is an interactive problem. The Department of Supernatural Phenomena at the Oxenfurt Academy has opened the Library of Magic, which contains the works of the greatest sorcerers of Redania — $$$n$$$ ($$$3 \leq n \leq 10^{18}$$$) types of books, numbered from $$$1$$$ to $$$n$$$. Each book's type number is indicated on its spine. Moreover, each type of book is stored in the library in exactly two copies! And you have been appointed as the librarian. One night, you wake up to a strange noise and see a creature leaving the building through a window. Three thick tomes of different colors were sticking out of the mysterious thief's backpack. Before you start searching for them, you decide to compute the numbers $$$a$$$, $$$b$$$, and $$$c$$$ written on the spines of these books. All three numbers are distinct. So, you have an unordered set of tomes, which includes one tome with each of the pairwise distinct numbers $$$a$$$, $$$b$$$, and $$$c$$$, and two tomes for all numbers from $$$1$$$ to $$$n$$$, except for $$$a$$$, $$$b$$$, and $$$c$$$. You want to find these values $$$a$$$, $$$b$$$, and $$$c$$$. Since you are not working in a simple library, but in the Library of Magic, you can only use one spell in the form of a query to check the presence of books in their place: - "xor l r" — Bitwise XOR query with parameters $$$l$$$ and $$$r$$$. Let $$$k$$$ be the number of such tomes in the library whose numbers are greater than or equal to $$$l$$$ and less than or equal to $$$r$$$. You will receive the result of the computation $$$v_1 \oplus v_2 \oplus ... \oplus v_k$$$, where $$$v_1 ... v_k$$$ are the numbers on the spines of these tomes, and $$$\oplus$$$ denotes the operation of bitwise exclusive OR. Since your magical abilities as a librarian are severely limited, you can make no more than $$$150$$$ queries. Input: The first line of input contains an integer $$$t$$$ ($$$1 \le t \le 300$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$3 \leq n \leq 10^{18}$$$) — the number of types of tomes. Output: None Examples: input: 2 6 0 2 3 5 3 output: xor 1 1 xor 2 2 xor 3 3 xor 4 6 ans 2 3 5 ans 1 2 3 Note: In the first test case, the books in the library after the theft look like this: Now consider the answers to the queries: - For the query "xor 1 1", you receive the result $$$1 \oplus 1 = 0$$$. Two tomes satisfy the condition specified in the query — both with the number $$$1$$$. - For the query "xor 2 2", you receive the result $$$2$$$, as only one tome satisfies the specified condition. - For the query "xor 3 3", you receive the result $$$3$$$. - For the query "xor 4 6", you receive the result $$$4 \oplus 6 \oplus 4 \oplus 5 \oplus 6 = 5$$$. In the second test case, there are only $$$3$$$ types of books, and it is easy to guess that the missing ones have the numbers $$$1$$$, $$$2$$$, and $$$3$$$.
2040B
https://codeforces.com/problemset/problem/2040/B
Paint a Strip
1,000
[ "constructive algorithms", "greedy", "math" ]
Div. 2
1,000
256
You have an array of zeros $$$a_1, a_2, \ldots, a_n$$$ of length $$$n$$$. You can perform two types of operations on it: 1. Choose an index $$$i$$$ such that $$$1 \le i \le n$$$ and $$$a_i = 0$$$, and assign $$$1$$$ to $$$a_i$$$; 2. Choose a pair of indices $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$, $$$a_l = 1$$$, $$$a_r = 1$$$, $$$a_l + \ldots + a_r \ge \lceil\frac{r - l + 1}{2}\rceil$$$, and assign $$$1$$$ to $$$a_i$$$ for all $$$l \le i \le r$$$. What is the minimum number of operations of the first type needed to make all elements of the array equal to one?
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The only line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array. Note that there is no limit on the sum of $$$n$$$ over all test cases.
For each test case, print one integer — the minimum number of needed operations of first type.
[ [ "4\n1\n2\n4\n20", "1\n2\n2\n4" ] ]
In the first test case, you can perform an operation of the $$$1$$$st type with $$$i = 1$$$. In the second test case, you can perform the following sequence of operations: 1. Operation of $$$1$$$st type, $$$i = 1$$$. After performing this operation, the array will look like this: $$$[1, 0]$$$. 2. Operation of $$$1$$$st type, $$$i = 2$$$. After performing this operation, the array will look like this: $$$[1, 1]$$$. The sequence of operations in the second test case In the third test case, you can perform the following sequence of operations: 1. Operation of $$$1$$$st type, $$$i = 1$$$. After performing this operation, the array will look like this: $$$[1, 0, 0, 0]$$$. 2. Operation of $$$1$$$st type, $$$i = 4$$$. After performing this operation, the array will look like this: $$$[1, 0, 0, 1]$$$. 3. Operation of $$$2$$$nd type, $$$l = 1$$$, $$$r = 4$$$. On this segment, $$$a_l + \ldots + a_r = a_1 + a_2 + a_3 + a_4 = 2$$$, which is not less than $$$\lceil\frac{r - l + 1}{2}\rceil = 2$$$. After performing this operation, the array will look like this: $$$[1, 1, 1, 1]$$$. The sequence of operations in the third test case
Title: Paint a Strip time_limit_ms: 1000 memory_limit_mb: 256 Description: You have an array of zeros $$$a_1, a_2, \ldots, a_n$$$ of length $$$n$$$. You can perform two types of operations on it: 1. Choose an index $$$i$$$ such that $$$1 \le i \le n$$$ and $$$a_i = 0$$$, and assign $$$1$$$ to $$$a_i$$$; 2. Choose a pair of indices $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$, $$$a_l = 1$$$, $$$a_r = 1$$$, $$$a_l + \ldots + a_r \ge \lceil\frac{r - l + 1}{2}\rceil$$$, and assign $$$1$$$ to $$$a_i$$$ for all $$$l \le i \le r$$$. What is the minimum number of operations of the first type needed to make all elements of the array equal to one? Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The only line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array. Note that there is no limit on the sum of $$$n$$$ over all test cases. Output: For each test case, print one integer — the minimum number of needed operations of first type. Examples: input: 4 1 2 4 20 output: 1 2 2 4 Note: In the first test case, you can perform an operation of the $$$1$$$st type with $$$i = 1$$$. In the second test case, you can perform the following sequence of operations: 1. Operation of $$$1$$$st type, $$$i = 1$$$. After performing this operation, the array will look like this: $$$[1, 0]$$$. 2. Operation of $$$1$$$st type, $$$i = 2$$$. After performing this operation, the array will look like this: $$$[1, 1]$$$. The sequence of operations in the second test case In the third test case, you can perform the following sequence of operations: 1. Operation of $$$1$$$st type, $$$i = 1$$$. After performing this operation, the array will look like this: $$$[1, 0, 0, 0]$$$. 2. Operation of $$$1$$$st type, $$$i = 4$$$. After performing this operation, the array will look like this: $$$[1, 0, 0, 1]$$$. 3. Operation of $$$2$$$nd type, $$$l = 1$$$, $$$r = 4$$$. On this segment, $$$a_l + \ldots + a_r = a_1 + a_2 + a_3 + a_4 = 2$$$, which is not less than $$$\lceil\frac{r - l + 1}{2}\rceil = 2$$$. After performing this operation, the array will look like this: $$$[1, 1, 1, 1]$$$. The sequence of operations in the third test case
2040C
https://codeforces.com/problemset/problem/2040/C
Ordered Permutations
1,600
[ "constructive algorithms", "math", "greedy", "two pointers", "combinatorics", "bitmasks" ]
Div. 2
2,000
256
Consider a permutation$$$^{\text{∗}}$$$ $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$. We can introduce the following sum for it$$$^{\text{†}}$$$: $$$$$$S(p) = \sum_{1 \le l \le r \le n} \min(p_l, p_{l + 1}, \ldots, p_r)$$$$$$ Let us consider all permutations of length $$$n$$$ with the maximum possible value of $$$S(p)$$$. Output the $$$k$$$-th of them in lexicographical$$$^{\text{‡}}$$$order, or report that there are less than $$$k$$$ of them.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le k \le 10^{12}$$$) — the length of the permutation and the index number of the desired permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$.
For each test case, if there are less than $$$k$$$ suitable permutations, print $$$-1$$$. Otherwise, print the $$$k$$$-th suitable permutation.
[ [ "6\n3 2\n3 3\n4 11\n4 6\n6 39\n7 34", "1 3 2 \n2 3 1 \n-1\n2 4 3 1 \n-1\n2 3 4 5 7 6 1" ] ]
Let us calculate the required sum for all permutations of length $$$3$$$ (ordered lexicographically): PermutationValue of $$$S(p)$$$$$$[1, 2, 3]$$$$$$10$$$$$$[1, 3, 2]$$$$$$10$$$$$$[2, 1, 3]$$$$$$9$$$$$$[2, 3, 1]$$$$$$10$$$$$$[3, 1, 2]$$$$$$9$$$$$$[3, 2, 1]$$$$$$10$$$ In the first test case, you have to print the second suitable permutation of length $$$3$$$. Looking at the table, we see that it is the permutation $$$[1, 3, 2]$$$. In the second test case, you have to print the third suitable permutation of length $$$3$$$. Looking at the table, we see that it is the permutation $$$[2, 3, 1]$$$.
Title: Ordered Permutations time_limit_ms: 2000 memory_limit_mb: 256 Description: Consider a permutation$$$^{\text{∗}}$$$ $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$. We can introduce the following sum for it$$$^{\text{†}}$$$: $$$$$$S(p) = \sum_{1 \le l \le r \le n} \min(p_l, p_{l + 1}, \ldots, p_r)$$$$$$ Let us consider all permutations of length $$$n$$$ with the maximum possible value of $$$S(p)$$$. Output the $$$k$$$-th of them in lexicographical$$$^{\text{‡}}$$$order, or report that there are less than $$$k$$$ of them. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le k \le 10^{12}$$$) — the length of the permutation and the index number of the desired permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$. Output: For each test case, if there are less than $$$k$$$ suitable permutations, print $$$-1$$$. Otherwise, print the $$$k$$$-th suitable permutation. Examples: input: 6 3 2 3 3 4 11 4 6 6 39 7 34 output: 1 3 2 2 3 1 -1 2 4 3 1 -1 2 3 4 5 7 6 1 Note: Let us calculate the required sum for all permutations of length $$$3$$$ (ordered lexicographically): PermutationValue of $$$S(p)$$$$$$[1, 2, 3]$$$$$$10$$$$$$[1, 3, 2]$$$$$$10$$$$$$[2, 1, 3]$$$$$$9$$$$$$[2, 3, 1]$$$$$$10$$$$$$[3, 1, 2]$$$$$$9$$$$$$[3, 2, 1]$$$$$$10$$$ In the first test case, you have to print the second suitable permutation of length $$$3$$$. Looking at the table, we see that it is the permutation $$$[1, 3, 2]$$$. In the second test case, you have to print the third suitable permutation of length $$$3$$$. Looking at the table, we see that it is the permutation $$$[2, 3, 1]$$$.
2040E
https://codeforces.com/problemset/problem/2040/E
Control of Randomness
2,100
[ "dp", "graphs", "math", "dfs and similar", "probabilities", "combinatorics", "greedy", "trees" ]
Div. 2
2,000
256
You are given a tree with $$$n$$$ vertices. Let's place a robot in some vertex $$$v \ne 1$$$, and suppose we initially have $$$p$$$ coins. Consider the following process, where in the $$$i$$$-th step (starting from $$$i = 1$$$): - If $$$i$$$ is odd, the robot moves to an adjacent vertex in the direction of vertex $$$1$$$; - Else, $$$i$$$ is even. You can either pay one coin (if there are some left) and then the robot moves to an adjacent vertex in the direction of vertex $$$1$$$, or not pay, and then the robot moves to an adjacent vertex chosen uniformly at random. The process stops as soon as the robot reaches vertex $$$1$$$. Let $$$f(v, p)$$$ be the minimum possible expected number of steps in the process above if we spend our coins optimally. Answer $$$q$$$ queries, in the $$$i$$$-th of which you have to find the value of $$$f(v_i, p_i)$$$, modulo$$$^{\text{∗}}$$$ $$$998\,244\,353$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 2 \cdot 10^3$$$; $$$1 \le q \le 2 \cdot 10^3$$$) — the number of vertices in the tree and the number of queries. The next $$$n - 1$$$ lines contain the edges of the tree, one edge per line. The $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$; $$$u_i \neq v_i$$$), denoting the edge between the nodes $$$u_i$$$ and $$$v_i$$$. The next $$$q$$$ lines contain two integers $$$v_i$$$ and $$$p_i$$$ ($$$2 \le v_i \le n$$$; $$$0 \le p_i \le n$$$). It's guaranteed that the given edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 3$$$. It is guaranteed that the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 3$$$.
For each test case, print $$$q$$$ integers: the values of $$$f(v_i, p_i)$$$ modulo $$$998\,244\,353$$$. Formally, let $$$M = 998\,244\,353$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x < M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.
[ [ "2\n4 4\n1 2\n2 3\n2 4\n2 0\n3 0\n4 0\n3 1\n12 10\n1 2\n2 3\n2 4\n1 5\n5 6\n6 7\n6 8\n6 9\n8 10\n10 11\n10 12\n6 0\n9 0\n10 0\n11 0\n3 1\n7 1\n10 1\n12 1\n12 2\n11 12", "1\n6\n6\n2\n4\n9\n8\n15\n2\n3\n6\n9\n5\n5" ] ]
The tree in the first test case: In the first query, the expected value is equal to $$$1$$$, since the robot starts moving from vertex $$$2$$$ to vertex $$$1$$$ in the first step and the process stops. Let's calculate the expected value in the second query ($$$x$$$ is the number of steps): - $$$P(x < 2) = 0$$$, the distance to vertex $$$1$$$ is $$$2$$$ and the robot cannot reach it in fewer steps. - $$$P(x = 2) = \frac{1}{3}$$$, since there is only one sequence of steps leading to $$$x = 2$$$. This is $$$3 \rightarrow_{1} 2 \rightarrow_{0.33} 1$$$ with probability $$$1 \cdot \frac{1}{3}$$$. - $$$P(x \bmod 2 = 1) = 0$$$, since the robot can reach vertex $$$1$$$ by only taking an even number of steps. - $$$P(x = 4) = \frac{2}{9}$$$: possible paths $$$3 \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.33} 1$$$. - $$$P(x = 6) = \frac{4}{27}$$$: possible paths $$$3 \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.33} 1$$$. - $$$P(x = i \cdot 2) = \frac{2^{i - 1}}{3^i}$$$ in the general case. As a result, $$$f(v, p) = \sum\limits_{i=1}^{\infty}{i \cdot 2 \cdot \frac{2^{i - 1}}{3^i}} = 6$$$. The tree in the second test case:
Title: Control of Randomness time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a tree with $$$n$$$ vertices. Let's place a robot in some vertex $$$v \ne 1$$$, and suppose we initially have $$$p$$$ coins. Consider the following process, where in the $$$i$$$-th step (starting from $$$i = 1$$$): - If $$$i$$$ is odd, the robot moves to an adjacent vertex in the direction of vertex $$$1$$$; - Else, $$$i$$$ is even. You can either pay one coin (if there are some left) and then the robot moves to an adjacent vertex in the direction of vertex $$$1$$$, or not pay, and then the robot moves to an adjacent vertex chosen uniformly at random. The process stops as soon as the robot reaches vertex $$$1$$$. Let $$$f(v, p)$$$ be the minimum possible expected number of steps in the process above if we spend our coins optimally. Answer $$$q$$$ queries, in the $$$i$$$-th of which you have to find the value of $$$f(v_i, p_i)$$$, modulo$$$^{\text{∗}}$$$ $$$998\,244\,353$$$. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 2 \cdot 10^3$$$; $$$1 \le q \le 2 \cdot 10^3$$$) — the number of vertices in the tree and the number of queries. The next $$$n - 1$$$ lines contain the edges of the tree, one edge per line. The $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$; $$$u_i \neq v_i$$$), denoting the edge between the nodes $$$u_i$$$ and $$$v_i$$$. The next $$$q$$$ lines contain two integers $$$v_i$$$ and $$$p_i$$$ ($$$2 \le v_i \le n$$$; $$$0 \le p_i \le n$$$). It's guaranteed that the given edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 3$$$. It is guaranteed that the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 3$$$. Output: For each test case, print $$$q$$$ integers: the values of $$$f(v_i, p_i)$$$ modulo $$$998\,244\,353$$$. Formally, let $$$M = 998\,244\,353$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x < M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$. Examples: input: 2 4 4 1 2 2 3 2 4 2 0 3 0 4 0 3 1 12 10 1 2 2 3 2 4 1 5 5 6 6 7 6 8 6 9 8 10 10 11 10 12 6 0 9 0 10 0 11 0 3 1 7 1 10 1 12 1 12 2 11 12 output: 1 6 6 2 4 9 8 15 2 3 6 9 5 5 Note: The tree in the first test case: In the first query, the expected value is equal to $$$1$$$, since the robot starts moving from vertex $$$2$$$ to vertex $$$1$$$ in the first step and the process stops. Let's calculate the expected value in the second query ($$$x$$$ is the number of steps): - $$$P(x < 2) = 0$$$, the distance to vertex $$$1$$$ is $$$2$$$ and the robot cannot reach it in fewer steps. - $$$P(x = 2) = \frac{1}{3}$$$, since there is only one sequence of steps leading to $$$x = 2$$$. This is $$$3 \rightarrow_{1} 2 \rightarrow_{0.33} 1$$$ with probability $$$1 \cdot \frac{1}{3}$$$. - $$$P(x \bmod 2 = 1) = 0$$$, since the robot can reach vertex $$$1$$$ by only taking an even number of steps. - $$$P(x = 4) = \frac{2}{9}$$$: possible paths $$$3 \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.33} 1$$$. - $$$P(x = 6) = \frac{4}{27}$$$: possible paths $$$3 \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.33} 1$$$. - $$$P(x = i \cdot 2) = \frac{2^{i - 1}}{3^i}$$$ in the general case. As a result, $$$f(v, p) = \sum\limits_{i=1}^{\infty}{i \cdot 2 \cdot \frac{2^{i - 1}}{3^i}} = 6$$$. The tree in the second test case:
2022D1
https://codeforces.com/problemset/problem/2022/D1
Asesino (Easy Version)
1,900
[ "brute force", "binary search", "implementation", "constructive algorithms", "interactive" ]
Div. 2
2,500
256
This is the easy version of the problem. In this version, you can ask at most $$$n+69$$$ questions. You can make hacks only if both versions of the problem are solved. This is an interactive problem. It is a tradition in Mexico's national IOI trainings to play the game "Asesino", which is similar to "Among Us" or "Mafia". Today, $$$n$$$ players, numbered from $$$1$$$ to $$$n$$$, will play "Asesino" with the following three roles: - Knight: a Knight is someone who always tells the truth. - Knave: a Knave is someone who always lies. - Impostor: an Impostor is someone everybody thinks is a Knight, but is secretly a Knave. Each player will be assigned a role in the game. There will be exactly one Impostor but there can be any (possible zero) number of Knights and Knaves. As the game moderator, you have accidentally forgotten the roles of everyone, but you need to determine the player who is the Impostor. To determine the Impostor, you will ask some questions. In each question, you will pick two players $$$i$$$ and $$$j$$$ ($$$1 \leq i, j \leq n$$$; $$$i \neq j$$$) and ask if player $$$i$$$ thinks that player $$$j$$$ is a Knight. The results of the question is shown in the table below. KnightKnaveImpostorKnightYesNoYesKnaveNoYesNoImpostorNoYes— The response of the cell in row $$$a$$$ and column $$$b$$$ is the result of asking a question when $$$i$$$ has role $$$a$$$ and $$$j$$$ has row $$$b$$$. For example, the "Yes" in the top right cell belongs to row "Knight" and column "Impostor", so it is the response when $$$i$$$ is a Knight and $$$j$$$ is an Impostor. Find the Impostor in at most $$$n + 69$$$ questions. Note: the grader is adaptive: the roles of the players are not fixed in the beginning and may change depending on your questions. However, it is guaranteed that there exists an assignment of roles that is consistent with all previously asked questions under the constraints of this problem.
The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 10^5$$$) — the number of people playing the game. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
null
[ [ "2\n7\n\n1\n\n0\n\n0\n\n1\n\n1\n\n0\n\n0\n\n1\n\n4\n\n0\n\n1\n\n1\n\n1", "? 1 3\n\n? 7 6\n\n? 2 5\n\n? 6 2\n\n? 4 5\n\n? 4 6\n\n? 1 4\n\n? 2 4\n\n! 4\n\n? 1 2\n\n? 2 3\n\n? 3 4\n\n? 4 1\n\n! 3" ] ]
Note that the example test cases do not represent an optimal strategy for asking questions and are only shown for the sake of demonstrating the interaction format. Specifically, we cannot determine which player is the Impostor from the questions asked in the examples. In the first test case of the example, players at indices $$$2$$$ and $$$6$$$ are Knights, players at indices $$$1$$$, $$$3$$$, $$$5$$$, and $$$7$$$ are Knaves, and the Impostor is at index $$$4$$$. The following is an explanation of the questions asked: - In the first query, player $$$i$$$ is a Knave and player $$$j$$$ is a Knave. The answer is "yes" since Knaves always lie. - In the second query, player $$$i$$$ is a Knave and player $$$j$$$ is a Knight. The answer is "no" since Knaves always lie. - In the third query, player $$$i$$$ is a Knight and player $$$j$$$ is a Knave. The answer is "no" since Knights always tell the truth. - In the fourth query, player $$$i$$$ is a Knight and player $$$j$$$ is a Knight. The answer is "yes" since Knights always tell the truth. - In the fifth query, player $$$i$$$ is a Impostor and player $$$j$$$ is a Knave. The answer is "yes" since the Impostor always lies. - In the sixth query, player $$$i$$$ is a Impostor and player $$$j$$$ is a Knight. The answer is "no" since the Impostor always lies. - In the seventh query, player $$$i$$$ is a Knave and player $$$j$$$ is a Impostor. The answer is "no" since Knaves always lie and Knaves thinks that the Impostor is a Knight. - In the eighth query, player $$$i$$$ is a Knight and player $$$j$$$ is a Impostor. The answer is "yes" since Knights always tell the truth and Knights think that the Impostor is a Knight.
Title: Asesino (Easy Version) time_limit_ms: 2500 memory_limit_mb: 256 Description: This is the easy version of the problem. In this version, you can ask at most $$$n+69$$$ questions. You can make hacks only if both versions of the problem are solved. This is an interactive problem. It is a tradition in Mexico's national IOI trainings to play the game "Asesino", which is similar to "Among Us" or "Mafia". Today, $$$n$$$ players, numbered from $$$1$$$ to $$$n$$$, will play "Asesino" with the following three roles: - Knight: a Knight is someone who always tells the truth. - Knave: a Knave is someone who always lies. - Impostor: an Impostor is someone everybody thinks is a Knight, but is secretly a Knave. Each player will be assigned a role in the game. There will be exactly one Impostor but there can be any (possible zero) number of Knights and Knaves. As the game moderator, you have accidentally forgotten the roles of everyone, but you need to determine the player who is the Impostor. To determine the Impostor, you will ask some questions. In each question, you will pick two players $$$i$$$ and $$$j$$$ ($$$1 \leq i, j \leq n$$$; $$$i \neq j$$$) and ask if player $$$i$$$ thinks that player $$$j$$$ is a Knight. The results of the question is shown in the table below. KnightKnaveImpostorKnightYesNoYesKnaveNoYesNoImpostorNoYes— The response of the cell in row $$$a$$$ and column $$$b$$$ is the result of asking a question when $$$i$$$ has role $$$a$$$ and $$$j$$$ has row $$$b$$$. For example, the "Yes" in the top right cell belongs to row "Knight" and column "Impostor", so it is the response when $$$i$$$ is a Knight and $$$j$$$ is an Impostor. Find the Impostor in at most $$$n + 69$$$ questions. Note: the grader is adaptive: the roles of the players are not fixed in the beginning and may change depending on your questions. However, it is guaranteed that there exists an assignment of roles that is consistent with all previously asked questions under the constraints of this problem. Input: The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 10^5$$$) — the number of people playing the game. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output: None Examples: input: 2 7 1 0 0 1 1 0 0 1 4 0 1 1 1 output: ? 1 3 ? 7 6 ? 2 5 ? 6 2 ? 4 5 ? 4 6 ? 1 4 ? 2 4 ! 4 ? 1 2 ? 2 3 ? 3 4 ? 4 1 ! 3 Note: Note that the example test cases do not represent an optimal strategy for asking questions and are only shown for the sake of demonstrating the interaction format. Specifically, we cannot determine which player is the Impostor from the questions asked in the examples. In the first test case of the example, players at indices $$$2$$$ and $$$6$$$ are Knights, players at indices $$$1$$$, $$$3$$$, $$$5$$$, and $$$7$$$ are Knaves, and the Impostor is at index $$$4$$$. The following is an explanation of the questions asked: - In the first query, player $$$i$$$ is a Knave and player $$$j$$$ is a Knave. The answer is "yes" since Knaves always lie. - In the second query, player $$$i$$$ is a Knave and player $$$j$$$ is a Knight. The answer is "no" since Knaves always lie. - In the third query, player $$$i$$$ is a Knight and player $$$j$$$ is a Knave. The answer is "no" since Knights always tell the truth. - In the fourth query, player $$$i$$$ is a Knight and player $$$j$$$ is a Knight. The answer is "yes" since Knights always tell the truth. - In the fifth query, player $$$i$$$ is a Impostor and player $$$j$$$ is a Knave. The answer is "yes" since the Impostor always lies. - In the sixth query, player $$$i$$$ is a Impostor and player $$$j$$$ is a Knight. The answer is "no" since the Impostor always lies. - In the seventh query, player $$$i$$$ is a Knave and player $$$j$$$ is a Impostor. The answer is "no" since Knaves always lie and Knaves thinks that the Impostor is a Knight. - In the eighth query, player $$$i$$$ is a Knight and player $$$j$$$ is a Impostor. The answer is "yes" since Knights always tell the truth and Knights think that the Impostor is a Knight.
2013C
https://codeforces.com/problemset/problem/2013/C
Password Cracking
1,400
[ "constructive algorithms", "interactive", "strings" ]
Div. 2
2,000
256
Dimash learned that Mansur wrote something very unpleasant about him to a friend, so he decided to find out his password at all costs and discover what exactly he wrote. Believing in the strength of his password, Mansur stated that his password — is a binary string of length $$$n$$$. He is also ready to answer Dimash's questions of the following type: Dimash says a binary string $$$t$$$, and Mansur replies whether it is true that $$$t$$$ is a substring of his password. Help Dimash find out the password in no more than $$$2n$$$ operations; otherwise, Mansur will understand the trick and stop communicating with him.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.
null
[ [ "4\n3\n\n0\n\n0\n\n1\n\n4\n\n4\n\n2", "? 00\n\n? 000\n\n? 010\n\n! 010\n\n! 1100\n\n! 0110\n\n! 10" ] ]
In the first example, the string $$$010$$$ is given. Therefore, the answers to the queries are as follows: "? 00" $$$00$$$ is not a substring of $$$010$$$, so the answer is $$$0$$$. "? 000" $$$000$$$ is not a substring, so the answer is $$$0$$$. "? 010" $$$010$$$ is a substring, so the answer is $$$1$$$. In the second example, the string is $$$1100$$$, in the third $$$0110$$$, and in the fourth $$$10$$$.
Title: Password Cracking time_limit_ms: 2000 memory_limit_mb: 256 Description: Dimash learned that Mansur wrote something very unpleasant about him to a friend, so he decided to find out his password at all costs and discover what exactly he wrote. Believing in the strength of his password, Mansur stated that his password — is a binary string of length $$$n$$$. He is also ready to answer Dimash's questions of the following type: Dimash says a binary string $$$t$$$, and Mansur replies whether it is true that $$$t$$$ is a substring of his password. Help Dimash find out the password in no more than $$$2n$$$ operations; otherwise, Mansur will understand the trick and stop communicating with him. Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. Output: None Examples: input: 4 3 0 0 1 4 4 2 output: ? 00 ? 000 ? 010 ! 010 ! 1100 ! 0110 ! 10 Note: In the first example, the string $$$010$$$ is given. Therefore, the answers to the queries are as follows: "? 00" $$$00$$$ is not a substring of $$$010$$$, so the answer is $$$0$$$. "? 000" $$$000$$$ is not a substring, so the answer is $$$0$$$. "? 010" $$$010$$$ is a substring, so the answer is $$$1$$$. In the second example, the string is $$$1100$$$, in the third $$$0110$$$, and in the fourth $$$10$$$.
2044A
https://codeforces.com/problemset/problem/2044/A
Easy Problem
800
[ "brute force", "math" ]
Div. 4
1,000
256
Cube is given an integer $$$n$$$. She wants to know how many ordered pairs of positive integers $$$(a,b)$$$ there are such that $$$a=n-b$$$. Since Cube is not very good at math, please help her!
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 99$$$) — the number of test cases. The only line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 100$$$).
For each test case, output the number of ordered pairs $$$(a, b)$$$ on a new line.
[ [ "3\n2\n4\n6", "1\n3\n5" ] ]
In the first test case, the only ordered pair that works is $$$(a,b)=(1,1)$$$. In the second test case, the three ordered pairs of $$$(a,b)$$$ that work are $$$(3,1), (2,2), (1,3)$$$.
Title: Easy Problem time_limit_ms: 1000 memory_limit_mb: 256 Description: Cube is given an integer $$$n$$$. She wants to know how many ordered pairs of positive integers $$$(a,b)$$$ there are such that $$$a=n-b$$$. Since Cube is not very good at math, please help her! Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 99$$$) — the number of test cases. The only line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 100$$$). Output: For each test case, output the number of ordered pairs $$$(a, b)$$$ on a new line. Examples: input: 3 2 4 6 output: 1 3 5 Note: In the first test case, the only ordered pair that works is $$$(a,b)=(1,1)$$$. In the second test case, the three ordered pairs of $$$(a,b)$$$ that work are $$$(3,1), (2,2), (1,3)$$$.
2044B
https://codeforces.com/problemset/problem/2044/B
Normal Problem
800
[ "strings", "implementation" ]
Div. 4
1,000
256
A string consisting of only characters 'p', 'q', and 'w' is painted on a glass window of a store. Ship walks past the store, standing directly in front of the glass window, and observes string $$$a$$$. Ship then heads inside the store, looks directly at the same glass window, and observes string $$$b$$$. Ship gives you string $$$a$$$. Your job is to find and output $$$b$$$.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The only line of each test case contains a string $$$a$$$ ($$$1 \leq |a| \leq 100$$$) — the string Ship observes from outside the store. It is guaranteed that $$$a$$$ only contains characters 'p', 'q', and 'w'.
For each test case, output string $$$b$$$, the string Ship observes from inside the store, on a new line.
[ [ "5\nqwq\nppppp\npppwwwqqq\nwqpqwpqwwqp\npqpqpqpq", "pwp\nqqqqq\npppwwwqqq\nqpwwpqwpqpw\npqpqpqpq" ] ]
Title: Normal Problem time_limit_ms: 1000 memory_limit_mb: 256 Description: A string consisting of only characters 'p', 'q', and 'w' is painted on a glass window of a store. Ship walks past the store, standing directly in front of the glass window, and observes string $$$a$$$. Ship then heads inside the store, looks directly at the same glass window, and observes string $$$b$$$. Ship gives you string $$$a$$$. Your job is to find and output $$$b$$$. Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The only line of each test case contains a string $$$a$$$ ($$$1 \leq |a| \leq 100$$$) — the string Ship observes from outside the store. It is guaranteed that $$$a$$$ only contains characters 'p', 'q', and 'w'. Output: For each test case, output string $$$b$$$, the string Ship observes from inside the store, on a new line. Examples: input: 5 qwq ppppp pppwwwqqq wqpqwpqwwqp pqpqpqpq output: pwp qqqqq pppwwwqqq qpwwpqwpqpw pqpqpqpq Note:
2044C
https://codeforces.com/problemset/problem/2044/C
Hard Problem
800
[ "greedy", "math" ]
Div. 4
1,000
256
Ball is the teacher in Paperfold University. The seats of his classroom are arranged in $$$2$$$ rows with $$$m$$$ seats each. Ball is teaching $$$a + b + c$$$ monkeys, and he wants to assign as many monkeys to a seat as possible. Ball knows that $$$a$$$ of them only want to sit in row $$$1$$$, $$$b$$$ of them only want to sit in row $$$2$$$, and $$$c$$$ of them have no preference. Only one monkey may sit in each seat, and each monkey's preference must be followed if it is seated. What is the maximum number of monkeys that Ball can seat?
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case contains four integers $$$m$$$, $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 \leq m, a, b, c \leq 10^8$$$).
For each test case, output the maximum number of monkeys you can seat.
[ [ "5\n10 5 5 10\n3 6 1 1\n15 14 12 4\n1 1 1 1\n420 6 9 69", "20\n5\n30\n2\n84" ] ]
In the second test case, $$$6$$$ monkeys want to sit in the front row, but only $$$3$$$ seats are available. The monkeys that have no preference and the monkeys who prefer sitting in the second row can sit in the second row together. Thus, the answer is $$$3+2=5$$$.
Title: Hard Problem time_limit_ms: 1000 memory_limit_mb: 256 Description: Ball is the teacher in Paperfold University. The seats of his classroom are arranged in $$$2$$$ rows with $$$m$$$ seats each. Ball is teaching $$$a + b + c$$$ monkeys, and he wants to assign as many monkeys to a seat as possible. Ball knows that $$$a$$$ of them only want to sit in row $$$1$$$, $$$b$$$ of them only want to sit in row $$$2$$$, and $$$c$$$ of them have no preference. Only one monkey may sit in each seat, and each monkey's preference must be followed if it is seated. What is the maximum number of monkeys that Ball can seat? Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each test case contains four integers $$$m$$$, $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 \leq m, a, b, c \leq 10^8$$$). Output: For each test case, output the maximum number of monkeys you can seat. Examples: input: 5 10 5 5 10 3 6 1 1 15 14 12 4 1 1 1 1 420 6 9 69 output: 20 5 30 2 84 Note: In the second test case, $$$6$$$ monkeys want to sit in the front row, but only $$$3$$$ seats are available. The monkeys that have no preference and the monkeys who prefer sitting in the second row can sit in the second row together. Thus, the answer is $$$3+2=5$$$.
2044E
https://codeforces.com/problemset/problem/2044/E
Insane Problem
1,300
[ "math", "binary search", "implementation", "number theory", "greedy" ]
Div. 4
2,000
256
Wave is given five integers $$$k$$$, $$$l_1$$$, $$$r_1$$$, $$$l_2$$$, and $$$r_2$$$. Wave wants you to help her count the number of ordered pairs $$$(x, y)$$$ such that all of the following are satisfied: - $$$l_1 \leq x \leq r_1$$$. - $$$l_2 \leq y \leq r_2$$$. - There exists a non-negative integer $$$n$$$ such that $$$\frac{y}{x} = k^n$$$.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The only line of each test case contains five integers $$$k$$$, $$$l_1$$$, $$$r_1$$$, $$$l_2$$$, and $$$r_2$$$ ($$$2 \leq k \leq 10^9, 1 \leq l_1 \leq r_1 \leq 10^9, 1 \leq l_2 \leq r_2 \leq 10^9$$$).
For each test case, output the number of matching ordered pairs $$$(x, y)$$$ on a new line.
[ [ "5\n2 2 6 2 12\n2 1 1000000000 1 1000000000\n3 5 7 15 63\n1000000000 1 5 6 1000000000\n15 17 78 2596 20914861", "12\n1999999987\n6\n1\n197" ] ]
In the third test case, the matching ordered pairs are the following: - $$$(5,15)$$$ - $$$(5,45)$$$ - $$$(6,18)$$$ - $$$(6,54)$$$ - $$$(7,21)$$$ - $$$(7,63)$$$ In the fourth test case, the only valid ordered pair is $$$(1,1\,000\,000\,000)$$$
Title: Insane Problem time_limit_ms: 2000 memory_limit_mb: 256 Description: Wave is given five integers $$$k$$$, $$$l_1$$$, $$$r_1$$$, $$$l_2$$$, and $$$r_2$$$. Wave wants you to help her count the number of ordered pairs $$$(x, y)$$$ such that all of the following are satisfied: - $$$l_1 \leq x \leq r_1$$$. - $$$l_2 \leq y \leq r_2$$$. - There exists a non-negative integer $$$n$$$ such that $$$\frac{y}{x} = k^n$$$. Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The only line of each test case contains five integers $$$k$$$, $$$l_1$$$, $$$r_1$$$, $$$l_2$$$, and $$$r_2$$$ ($$$2 \leq k \leq 10^9, 1 \leq l_1 \leq r_1 \leq 10^9, 1 \leq l_2 \leq r_2 \leq 10^9$$$). Output: For each test case, output the number of matching ordered pairs $$$(x, y)$$$ on a new line. Examples: input: 5 2 2 6 2 12 2 1 1000000000 1 1000000000 3 5 7 15 63 1000000000 1 5 6 1000000000 15 17 78 2596 20914861 output: 12 1999999987 6 1 197 Note: In the third test case, the matching ordered pairs are the following: - $$$(5,15)$$$ - $$$(5,45)$$$ - $$$(6,18)$$$ - $$$(6,54)$$$ - $$$(7,21)$$$ - $$$(7,63)$$$ In the fourth test case, the only valid ordered pair is $$$(1,1\,000\,000\,000)$$$
2044F
https://codeforces.com/problemset/problem/2044/F
Easy Demon Problem
1,900
[ "brute force", "binary search", "math", "data structures", "number theory" ]
Div. 4
4,000
256
For an arbitrary grid, Robot defines its beauty to be the sum of elements in the grid. Robot gives you an array $$$a$$$ of length $$$n$$$ and an array $$$b$$$ of length $$$m$$$. You construct a $$$n$$$ by $$$m$$$ grid $$$M$$$ such that $$$M_{i,j}=a_i\cdot b_j$$$ for all $$$1 \leq i \leq n$$$ and $$$1 \leq j \leq m$$$. Then, Robot gives you $$$q$$$ queries, each consisting of a single integer $$$x$$$. For each query, determine whether or not it is possible to perform the following operation exactly once so that $$$M$$$ has a beauty of $$$x$$$: 1. Choose integers $$$r$$$ and $$$c$$$ such that $$$1 \leq r \leq n$$$ and $$$1 \leq c \leq m$$$ 2. Set $$$M_{i,j}$$$ to be $$$0$$$ for all ordered pairs $$$(i,j)$$$ such that $$$i=r$$$, $$$j=c$$$, or both. Note that queries are not persistent, meaning that you do not actually set any elements to $$$0$$$ in the process — you are only required to output if it is possible to find $$$r$$$ and $$$c$$$ such that if the above operation is performed, the beauty of the grid will be $$$x$$$. Also, note that you must perform the operation for each query, even if the beauty of the original grid is already $$$x$$$.
The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$1 \leq n,m \leq 2\cdot 10^5, 1 \leq q \leq 5\cdot 10^4$$$) — the length of $$$a$$$, the length of $$$b$$$, and the number of queries respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq |a_i| \leq n$$$). The third line contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$0 \leq |b_i| \leq m$$$). The following $$$q$$$ lines each contain a single integer $$$x$$$ ($$$1 \leq |x| \leq 2\cdot 10^5$$$), the beauty of the grid you wish to achieve by setting all elements in a row and a column to $$$0$$$.
For each testcase, output "YES" (without quotes) if there is a way to perform the aforementioned operation such that the beauty is $$$x$$$, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).
[ [ "3 3 6\n-2 3 -3\n-2 2 -1\n-1\n1\n-2\n2\n-3\n3", "NO\nYES\nNO\nNO\nYES\nNO" ], [ "5 5 6\n1 -2 3 0 0\n0 -2 5 0 -3\n4\n-3\n5\n2\n-1\n2", "YES\nYES\nYES\nYES\nNO\nYES" ] ]
In the second example, the grid is 0 -2 5 0 -3 0 4 -10 0 6 0 -6 15 0 -9 0 0 0 0 0 0 0 0 0 0 By performing the operation with $$$r=4$$$ and $$$c=2$$$, we create the following grid: 0 0 5 0 -3 0 0 -10 0 6 0 0 15 0 -9 0 0 0 0 0 0 0 0 0 0 which has beauty $$$4$$$. Thus, we output YES. In the second query, selecting $$$r=3$$$ and $$$c=5$$$ creates a grid with beauty $$$-3$$$. In the third query, selecting $$$r=3$$$ and $$$c=3$$$ creates a grid with beauty $$$5$$$.
Title: Easy Demon Problem time_limit_ms: 4000 memory_limit_mb: 256 Description: For an arbitrary grid, Robot defines its beauty to be the sum of elements in the grid. Robot gives you an array $$$a$$$ of length $$$n$$$ and an array $$$b$$$ of length $$$m$$$. You construct a $$$n$$$ by $$$m$$$ grid $$$M$$$ such that $$$M_{i,j}=a_i\cdot b_j$$$ for all $$$1 \leq i \leq n$$$ and $$$1 \leq j \leq m$$$. Then, Robot gives you $$$q$$$ queries, each consisting of a single integer $$$x$$$. For each query, determine whether or not it is possible to perform the following operation exactly once so that $$$M$$$ has a beauty of $$$x$$$: 1. Choose integers $$$r$$$ and $$$c$$$ such that $$$1 \leq r \leq n$$$ and $$$1 \leq c \leq m$$$ 2. Set $$$M_{i,j}$$$ to be $$$0$$$ for all ordered pairs $$$(i,j)$$$ such that $$$i=r$$$, $$$j=c$$$, or both. Note that queries are not persistent, meaning that you do not actually set any elements to $$$0$$$ in the process — you are only required to output if it is possible to find $$$r$$$ and $$$c$$$ such that if the above operation is performed, the beauty of the grid will be $$$x$$$. Also, note that you must perform the operation for each query, even if the beauty of the original grid is already $$$x$$$. Input: The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$1 \leq n,m \leq 2\cdot 10^5, 1 \leq q \leq 5\cdot 10^4$$$) — the length of $$$a$$$, the length of $$$b$$$, and the number of queries respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq |a_i| \leq n$$$). The third line contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$0 \leq |b_i| \leq m$$$). The following $$$q$$$ lines each contain a single integer $$$x$$$ ($$$1 \leq |x| \leq 2\cdot 10^5$$$), the beauty of the grid you wish to achieve by setting all elements in a row and a column to $$$0$$$. Output: For each testcase, output "YES" (without quotes) if there is a way to perform the aforementioned operation such that the beauty is $$$x$$$, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response). Examples: input: 3 3 6 -2 3 -3 -2 2 -1 -1 1 -2 2 -3 3 output: NO YES NO NO YES NO input: 5 5 6 1 -2 3 0 0 0 -2 5 0 -3 4 -3 5 2 -1 2 output: YES YES YES YES NO YES Note: In the second example, the grid is 0 -2 5 0 -3 0 4 -10 0 6 0 -6 15 0 -9 0 0 0 0 0 0 0 0 0 0 By performing the operation with $$$r=4$$$ and $$$c=2$$$, we create the following grid: 0 0 5 0 -3 0 0 -10 0 6 0 0 15 0 -9 0 0 0 0 0 0 0 0 0 0 which has beauty $$$4$$$. Thus, we output YES. In the second query, selecting $$$r=3$$$ and $$$c=5$$$ creates a grid with beauty $$$-3$$$. In the third query, selecting $$$r=3$$$ and $$$c=3$$$ creates a grid with beauty $$$5$$$.
2044G1
https://codeforces.com/problemset/problem/2044/G1
Medium Demon Problem (easy version)
1,700
[ "graphs", "implementation", "graph matchings", "dfs and similar", "trees" ]
Div. 4
2,000
256
This is the easy version of the problem. The key difference between the two versions is highlighted in bold. A group of $$$n$$$ spiders has come together to exchange plushies. Initially, each spider has $$$1$$$ plushie. Every year, if spider $$$i$$$ has at least one plushie, he will give exactly one plushie to spider $$$r_i$$$. Otherwise, he will do nothing. Note that all plushie transfers happen at the same time. In this version, if any spider has more than $$$1$$$ plushie at any point in time, they will throw all but $$$1$$$ away. The process is stable in the current year if each spider has the same number of plushies (before the current year's exchange) as he did the previous year (before the previous year's exchange). Note that year $$$1$$$ can never be stable. Find the first year in which the process becomes stable.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the number of spiders. The following line contains $$$n$$$ integers $$$r_1, r_2, \ldots, r_n$$$ ($$$1 \leq r_i \leq n, r_i \neq i$$$) — the recipient of the plushie of each spider. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output an integer on a new line, the first year in which the process becomes stable.
[ [ "5\n2\n2 1\n5\n2 3 4 5 1\n5\n2 1 4 2 3\n5\n4 1 1 5 4\n10\n4 3 9 1 6 7 9 10 10 3", "2\n2\n5\n4\n5" ] ]
For the second test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Since this array is the same as the previous year, this year is stable. For the third test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 0]$$$. Then, year $$$2$$$'s exchange happens. Note that even though two spiders gave spider $$$2$$$ plushies, spider $$$2$$$ may only keep one plushie. - At year $$$3$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 0, 1, 0]$$$. Then, year $$$3$$$'s exchange happens. - At year $$$4$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 0, 0, 0]$$$. Then, year $$$4$$$'s exchange happens. - At year $$$5$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 0, 0, 0]$$$. Since this array is the same as the previous year, this year is stable.
Title: Medium Demon Problem (easy version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the easy version of the problem. The key difference between the two versions is highlighted in bold. A group of $$$n$$$ spiders has come together to exchange plushies. Initially, each spider has $$$1$$$ plushie. Every year, if spider $$$i$$$ has at least one plushie, he will give exactly one plushie to spider $$$r_i$$$. Otherwise, he will do nothing. Note that all plushie transfers happen at the same time. In this version, if any spider has more than $$$1$$$ plushie at any point in time, they will throw all but $$$1$$$ away. The process is stable in the current year if each spider has the same number of plushies (before the current year's exchange) as he did the previous year (before the previous year's exchange). Note that year $$$1$$$ can never be stable. Find the first year in which the process becomes stable. Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the number of spiders. The following line contains $$$n$$$ integers $$$r_1, r_2, \ldots, r_n$$$ ($$$1 \leq r_i \leq n, r_i \neq i$$$) — the recipient of the plushie of each spider. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: For each test case, output an integer on a new line, the first year in which the process becomes stable. Examples: input: 5 2 2 1 5 2 3 4 5 1 5 2 1 4 2 3 5 4 1 1 5 4 10 4 3 9 1 6 7 9 10 10 3 output: 2 2 5 4 5 Note: For the second test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Since this array is the same as the previous year, this year is stable. For the third test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 0]$$$. Then, year $$$2$$$'s exchange happens. Note that even though two spiders gave spider $$$2$$$ plushies, spider $$$2$$$ may only keep one plushie. - At year $$$3$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 0, 1, 0]$$$. Then, year $$$3$$$'s exchange happens. - At year $$$4$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 0, 0, 0]$$$. Then, year $$$4$$$'s exchange happens. - At year $$$5$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 0, 0, 0]$$$. Since this array is the same as the previous year, this year is stable.
2044G2
https://codeforces.com/problemset/problem/2044/G2
Medium Demon Problem (hard version)
1,900
[ "dp", "graphs", "implementation", "dfs and similar", "dsu", "trees" ]
Div. 4
2,000
256
This is the hard version of the problem. The key difference between the two versions is highlighted in bold. A group of $$$n$$$ spiders has come together to exchange plushies. Initially, each spider has $$$1$$$ plushie. Every year, if spider $$$i$$$ has at least one plushie, he will give exactly one plushie to spider $$$r_i$$$. Otherwise, he will do nothing. Note that all plushie transfers happen at the same time. In this version, each spider is allowed to have more than 1 plushie at any point in time. The process is stable in the current year if each spider has the same number of plushies (before the current year's exchange) as he did the previous year (before the previous year's exchange). Note that year $$$1$$$ can never be stable. Find the first year in which the process becomes stable.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the number of spiders. The following line contains $$$n$$$ integers $$$r_1, r_2, \ldots, r_n$$$ ($$$1 \leq r_i \leq n, r_i \neq i$$$) — the recipient of the plushie of each spider. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output an integer on a new line, the first year in which the process becomes stable.
[ [ "5\n2\n2 1\n5\n2 3 4 5 1\n5\n2 1 4 2 3\n5\n4 1 1 5 4\n10\n4 3 9 1 6 7 9 10 10 3", "2\n2\n5\n5\n5" ] ]
For the second test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Since this array is the same as the previous year, this year is stable. For the third test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 2, 1, 1, 0]$$$. Then, year $$$2$$$'s exchange happens. - At year $$$3$$$, the following array shows the number of plushies each spider has: $$$[1, 3, 0, 1, 0]$$$. Then, year $$$3$$$'s exchange happens. - At year $$$4$$$, the following array shows the number of plushies each spider has: $$$[1, 4, 0, 0, 0]$$$. Then, year $$$4$$$'s exchange happens. - At year $$$5$$$, the following array shows the number of plushies each spider has: $$$[1, 4, 0, 0, 0]$$$. Since this array is the same as the previous year, this year is stable.
Title: Medium Demon Problem (hard version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the hard version of the problem. The key difference between the two versions is highlighted in bold. A group of $$$n$$$ spiders has come together to exchange plushies. Initially, each spider has $$$1$$$ plushie. Every year, if spider $$$i$$$ has at least one plushie, he will give exactly one plushie to spider $$$r_i$$$. Otherwise, he will do nothing. Note that all plushie transfers happen at the same time. In this version, each spider is allowed to have more than 1 plushie at any point in time. The process is stable in the current year if each spider has the same number of plushies (before the current year's exchange) as he did the previous year (before the previous year's exchange). Note that year $$$1$$$ can never be stable. Find the first year in which the process becomes stable. Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the number of spiders. The following line contains $$$n$$$ integers $$$r_1, r_2, \ldots, r_n$$$ ($$$1 \leq r_i \leq n, r_i \neq i$$$) — the recipient of the plushie of each spider. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. Output: For each test case, output an integer on a new line, the first year in which the process becomes stable. Examples: input: 5 2 2 1 5 2 3 4 5 1 5 2 1 4 2 3 5 4 1 1 5 4 10 4 3 9 1 6 7 9 10 10 3 output: 2 2 5 5 5 Note: For the second test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Since this array is the same as the previous year, this year is stable. For the third test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 2, 1, 1, 0]$$$. Then, year $$$2$$$'s exchange happens. - At year $$$3$$$, the following array shows the number of plushies each spider has: $$$[1, 3, 0, 1, 0]$$$. Then, year $$$3$$$'s exchange happens. - At year $$$4$$$, the following array shows the number of plushies each spider has: $$$[1, 4, 0, 0, 0]$$$. Then, year $$$4$$$'s exchange happens. - At year $$$5$$$, the following array shows the number of plushies each spider has: $$$[1, 4, 0, 0, 0]$$$. Since this array is the same as the previous year, this year is stable.
2044H
https://codeforces.com/problemset/problem/2044/H
Hard Demon Problem
2,100
[ "dp", "implementation", "math", "data structures", "constructive algorithms" ]
Div. 4
3,500
512
Swing is opening a pancake factory! A good pancake factory must be good at flattening things, so Swing is going to test his new equipment on 2D matrices. Swing is given an $$$n \times n$$$ matrix $$$M$$$ containing positive integers. He has $$$q$$$ queries to ask you. For each query, he gives you four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ and asks you to flatten the submatrix bounded by $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ into an array $$$A$$$. Formally, $$$A = [M_{(x1,y1)}, M_{(x1,y1+1)}, \ldots, M_{(x1,y2)}, M_{(x1+1,y1)}, M_{(x1+1,y1+1)}, \ldots, M_{(x2,y2)}]$$$. The following image depicts the flattening of a submatrix bounded by the red dotted lines. The orange arrows denote the direction that the elements of the submatrix are appended to the back of $$$A$$$, and $$$A$$$ is shown at the bottom of the image. Afterwards, he asks you for the value of $$$\sum_{i=1}^{|A|} A_i \cdot i$$$ (sum of $$$A_i \cdot i$$$ over all $$$i$$$).
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The first line of each test contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n \leq 2000, 1 \leq q \leq 10^6$$$) — the length of $$$M$$$ and the number of queries. The following $$$n$$$ lines contain $$$n$$$ integers each, the $$$i$$$'th of which contains $$$M_{(i,1)}, M_{(i,2)}, \ldots, M_{(i,n)}$$$ ($$$1 \leq M_{(i, j)} \leq 10^6$$$). The following $$$q$$$ lines contain four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, and $$$y_2$$$ ($$$1 \leq x_1 \leq x_2 \leq n, 1 \leq y_1 \leq y_2 \leq n$$$) — the bounds of the query. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output the results of the $$$q$$$ queries on a new line.
[ [ "2\n4 3\n1 5 2 4\n4 9 5 3\n4 5 2 3\n1 5 5 2\n1 1 4 4\n2 2 3 3\n1 2 4 3\n3 3\n1 2 3\n4 5 6\n7 8 9\n1 1 1 3\n1 3 3 3\n2 2 2 2", "500 42 168 \n14 42 5" ] ]
In the second query of the first test case, $$$A = [9, 5, 5, 2]$$$. Therefore, the sum is $$$1 \cdot 9 + 2 \cdot 5 + 3 \cdot 5 + 4 \cdot 2 = 42$$$.
Title: Hard Demon Problem time_limit_ms: 3500 memory_limit_mb: 512 Description: Swing is opening a pancake factory! A good pancake factory must be good at flattening things, so Swing is going to test his new equipment on 2D matrices. Swing is given an $$$n \times n$$$ matrix $$$M$$$ containing positive integers. He has $$$q$$$ queries to ask you. For each query, he gives you four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ and asks you to flatten the submatrix bounded by $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ into an array $$$A$$$. Formally, $$$A = [M_{(x1,y1)}, M_{(x1,y1+1)}, \ldots, M_{(x1,y2)}, M_{(x1+1,y1)}, M_{(x1+1,y1+1)}, \ldots, M_{(x2,y2)}]$$$. The following image depicts the flattening of a submatrix bounded by the red dotted lines. The orange arrows denote the direction that the elements of the submatrix are appended to the back of $$$A$$$, and $$$A$$$ is shown at the bottom of the image. Afterwards, he asks you for the value of $$$\sum_{i=1}^{|A|} A_i \cdot i$$$ (sum of $$$A_i \cdot i$$$ over all $$$i$$$). Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The first line of each test contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n \leq 2000, 1 \leq q \leq 10^6$$$) — the length of $$$M$$$ and the number of queries. The following $$$n$$$ lines contain $$$n$$$ integers each, the $$$i$$$'th of which contains $$$M_{(i,1)}, M_{(i,2)}, \ldots, M_{(i,n)}$$$ ($$$1 \leq M_{(i, j)} \leq 10^6$$$). The following $$$q$$$ lines contain four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, and $$$y_2$$$ ($$$1 \leq x_1 \leq x_2 \leq n, 1 \leq y_1 \leq y_2 \leq n$$$) — the bounds of the query. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$10^6$$$. Output: For each test case, output the results of the $$$q$$$ queries on a new line. Examples: input: 2 4 3 1 5 2 4 4 9 5 3 4 5 2 3 1 5 5 2 1 1 4 4 2 2 3 3 1 2 4 3 3 3 1 2 3 4 5 6 7 8 9 1 1 1 3 1 3 3 3 2 2 2 2 output: 500 42 168 14 42 5 Note: In the second query of the first test case, $$$A = [9, 5, 5, 2]$$$. Therefore, the sum is $$$1 \cdot 9 + 2 \cdot 5 + 3 \cdot 5 + 4 \cdot 2 = 42$$$.