| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array of n integers a0, a1, …, an − 1, some of which may be negative. Among all non-empty contiguous blocks al, al + 1, …, ar (with 0 ≤ l ≤ r ≤ n − 1), find the largest possible value of the sum al + al + 1 + … + ar.
If every element is negative, the answer is the single largest element, because the block must be non-empty.
The first line contains one integer n — the length of the array. The second line contains n space-separated integers a0… an − 1.
Print one integer — the maximum sum of a non-empty contiguous block.
1 ≤ n ≤ 2 ⋅ 105
− 109 ≤ ai ≤ 109
The answer fits into a signed 64-bit integer.
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 5 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array of n positive integers a0, a1, …, an − 1 and an integer k. Cut the array into exactly k non-empty contiguous parts: choose k − 1 split points so that every element belongs to exactly one part and the parts appear in the original order.
The cost of a cut is the largest part sum among the k parts. Find the minimum possible cost over all valid cuts.
The first line contains two integers n and k. The second line contains n space-separated integers a0… an − 1.
Print one integer — the minimum possible value of the largest part sum.
1 ≤ k ≤ n ≤ 2 ⋅ 105
1 ≤ ai ≤ 109
The answer fits into a signed 64-bit integer.
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array of n integers a0, a1, …, an − 1, some of which may be negative, and q independent queries. Each query is an index p (0 ≤ p ≤ n − 1).
For a query p, consider every non-empty contiguous block al, …, ar that contains position p, that is l ≤ p ≤ r. Output the largest possible value of al + al + 1 + … + ar among those blocks.
The first line contains two integers n and q. The second line contains n space-separated integers a0… an − 1. The third line contains q space-separated integers — the query indices.
Print q lines; line j is the answer to the j-th query.
1 ≤ n ≤ 2 ⋅ 105
1 ≤ q ≤ 2 ⋅ 105
− 109 ≤ ai ≤ 109
Every answer fits into a signed 64-bit integer.
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 3 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array of n integers a0, a1, …, an − 1. For every index i, compute ci = \#{ j: j > i and aj < ai }, the number of elements to the right of position i that are strictly smaller than ai.
The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.
Print n space-separated integers c0 c1 … cn − 1.
1 ≤ n ≤ 2 ⋅ 105
− 109 ≤ ai ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 3 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array of n integers a0, a1, …, an − 1, possibly negative. A significant reverse pair is a pair of indices i < j with ai > 2 ⋅ aj.
Count the significant reverse pairs.
The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.
Print one integer — the number of pairs i < j with ai > 2 ⋅ aj.
1 ≤ n ≤ 2 ⋅ 105
− 109 ≤ ai ≤ 109
The answer fits into a signed 64-bit integer.
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 3 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array of n integers a0, a1, …, an − 1. An inversion is a pair of indices i < j with ai > aj — a pair that appears out of order.
Count the inversions. A sorted array has 0; a strictly decreasing array has binomn2; the count equals the number of adjacent swaps bubble sort would make.
The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.
Print one integer — the number of inversions.
1 ≤ n ≤ 2 ⋅ 105
− 109 ≤ ai ≤ 109
The answer fits into a signed 64-bit integer.
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
There are n stalls at distinct integer positions a0, a1, …, an − 1 on a line, and C cows. Place the cows into C distinct stalls so that the minimum distance between any two cows is as large as possible.
Output that largest possible minimum distance.
The first line contains two integers n and C. The second line contains n distinct integers a0… an − 1 — the stall positions, in arbitrary order.
Print one integer — the maximum achievable minimum distance between two cows.
2 ≤ C ≤ n ≤ 105
0 ≤ ai ≤ 109, all ai distinct
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array of n integers a0, a1, …, an − 1 and q queries. Each query is an integer x; answer with the number of array elements that are strictly less than x.
The first line contains two integers n and q. The second line contains n integers a0… an − 1. The third line contains q integers — the queries.
Print q lines; line j is the answer to the j-th query.
1 ≤ n ≤ 105
1 ≤ q ≤ 105
− 109 ≤ ai ≤ 109, − 109 ≤ x ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
There are n piles of bananas; pile i has ai bananas. There are H hours available. If the eating speed is k bananas per hour, then in one hour one pile is chosen and min(k, remaining in that pile) bananas are eaten; a pile that is finished early does not carry the spare time over. Pile i therefore takes ⌈ ai / k⌉ hours.
Find the smallest integer speed k ≥ 1 for which all piles can be finished within H hours, i.e. ∑i⌈ ai / k⌉ ≤ H.
The first line contains two integers n and H. The second line contains n integers a0… an − 1.
Print one integer — the minimum eating speed.
1 ≤ n ≤ 105
n ≤ H ≤ 109
1 ≤ ai ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are simulating a browser's history. The browser starts on the homepage, page 0. You are given n operations, each one of:
After every BACK and FORWARD operation, output the page you land on. VISIT produces no output.
The first line contains one integer n — the number of operations. Each of the next n lines is one operation in the format above.
For every BACK or FORWARD operation, print the resulting current page on its own line, in order.
1 ≤ n ≤ 2 ⋅ 105
1 ≤ x ≤ 109 for VISIT x
1 ≤ k ≤ 109 for BACK k / FORWARD k
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
Implement a FIFO queue supporting n operations of two kinds:
It is guaranteed that POP is never called on an empty queue. You must use only a Stack ADT (push / pop / top from one end) as your underlying storage — no deque, no list indexing from the front, no built-in queue.
The first line contains one integer n — the number of operations. Each of the next n lines is either PUSH x or POP.
For each POP, print the removed value on its own line, in order.
1 ≤ n ≤ 2 ⋅ 105
1 ≤ x ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
A stream of n integers a0, a1, …, an − 1 arrives one at a time, together with a fixed window size k (1 ≤ k ≤ n), known in advance. For every position i with i ≥ k − 1, report the sum of the last k values seen so far: ai − k + 1 + ai − k + 2 + … + ai.
The first line contains two integers n and k. The second line contains n space-separated integers a0… an − 1.
Print n − k + 1 lines: the sliding-window sum for i = k − 1,k,…,n − 1, in order.
1 ≤ k ≤ n ≤ 2 ⋅ 105
− 109 ≤ ai ≤ 109
Every printed sum fits into a signed 64-bit integer.
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array a0, a1, …, an − 1 of integers and q queries. Each query is a pair (l, r) with 0 ≤ l ≤ r ≤ n − 1; answer with al + al + 1 + … + ar.
The first line contains two integers n and q. The second line contains n space-separated integers a0… an − 1. Each of the next q lines contains two integers l and r.
Print q lines: the answer to each query, in order.
1 ≤ n, q ≤ 2 ⋅ 105
− 109 ≤ ai ≤ 109
Every answer fits into a signed 64-bit integer.
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an n × m grid of integers and q queries. Each query is a rectangle given by its top-left corner (r1, c1) and bottom-right corner (r2, c2) (0 ≤ r1 ≤ r2 ≤ n − 1, 0 ≤ c1 ≤ c2 ≤ m − 1); answer with the sum of every cell in that rectangle, inclusive.
The first line contains three integers n, m and q. Each of the next n lines contains m space-separated integers — the grid. Each of the next q lines contains four integers r1, c1, r2, c2.
Print q lines: the sum of each query rectangle, in order.
1 ≤ n, m, \ n ⋅ m ≤ 4 ⋅ 105
1 ≤ q ≤ 2 ⋅ 105
− 109 ≤ grid[i][j] ≤ 109
Every answer fits into a signed 64-bit integer.
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array a0, a1, …, an − 1 of integers, which may include zero and negative values. For every index i, output the product of every element except ai (the product of an empty set of factors, when n = 1, is 1).
You may not use division.
The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.
Print n space-separated integers: the answer for each index, in order.
1 ≤ n ≤ 800
− 50 ≤ ai ≤ 50
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given n non-negative integers h0, h1, …, hn − 1, where hi is the height of a vertical line drawn at position i. Choose two lines i < j; together with the x-axis they form a container that can hold (j − i) ⋅ min(hi,hj) units of water. Find the maximum amount of water any pair of lines can hold.
The first line contains one integer n. The second line contains n space-separated integers h0… hn − 1.
Print one integer — the maximum water any pair of lines can hold.
2 ≤ n ≤ 2 ⋅ 105
0 ≤ hi ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array a0, a1, …, an − 1 of integers. For every index i, find the first index j > i with aj > ai, and report aj. If no such j exists, report − 1.
The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.
Print n space-separated integers: the answer for each index, in order.
1 ≤ n ≤ 2 ⋅ 105
1 ≤ ai ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given n non-negative integers h0, h1, …, hn − 1 representing an elevation map, each bar having width 1. Compute how much water it can trap after raining.
The first line contains one integer n. The second line contains n space-separated integers h0… hn − 1.
Print one integer — the total trapped water.
1 ≤ n ≤ 2 ⋅ 105
0 ≤ hi ≤ 106
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array a0, a1, …, an − 1 of integers and a window size k. For every contiguous window of size k, output the maximum value in that window. There are n − k + 1 windows in total.
The first line contains two integers n and k. The second line contains n space-separated integers a0… an − 1.
Print n − k + 1 space-separated integers: the maximum of each window, in order.
1 ≤ k ≤ n ≤ 2 ⋅ 105
− 109 ≤ ai ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array a0, a1, …, an − 1 of integers and a window size k. For every contiguous window of size k, output max(window) − min(window).
The first line contains two integers n and k. The second line contains n space-separated integers a0… an − 1.
Print n − k + 1 space-separated integers: the range of each window, in order.
1 ≤ k ≤ n ≤ 2 ⋅ 105
− 109 ≤ ai ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given an array a0, a1, …, an − 1 of integers (may include negatives) and an integer k. Find the length of the shortest non-empty contiguous subarray whose sum is ≥ k, or report − 1 if no such subarray exists.
The first line contains two integers n and k. The second line contains n space-separated integers a0… an − 1.
Print one integer: the shortest length, or − 1.
1 ≤ n ≤ 2 ⋅ 105
− 1000 ≤ ai ≤ 1000
− 109 ≤ k ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
Values arrive one at a time (n of them), and you are given k. After each arrival, once at least k values have arrived, report the k-th largest value seen so far. Arrivals before the k-th produce no output.
The first line contains two integers n and k. The second line contains n space-separated integers — the values, in arrival order.
Print one line per arrival from the k-th onward: the k-th largest value seen so far at that point.
1 ≤ k ≤ n ≤ 2 ⋅ 105
− 109 ≤ value ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
You are given k arrays, each already sorted in non-decreasing order (an array may be empty). Output all elements from all arrays, merged into one non-decreasing sequence.
The first line contains one integer k. Then, for each of the k arrays: one line with its length m, followed by a line with m space-separated integers (the line may be empty if m = 0).
Print all elements, merged and sorted, space-separated on one line (an empty line if there are none).
1 ≤ k ≤ 2 ⋅ 105
0 ≤ total number of elements across all arrays ≤ 2 ⋅ 105
− 109 ≤ element ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| Author: | Pavel Mesenev | Time limit: | 2 sec | |
| Input file: | Standard input | Memory limit: | 256 Mb | |
| Output file: | Standard output |
Values arrive one at a time (n of them). After each arrival, report the median of all values seen so far. If the count so far is even, report the lower of the two middle values (this keeps every answer an integer).
The first line contains one integer n. The second line contains n space-separated integers — the values, in arrival order.
Print n lines: the median after each arrival.
1 ≤ n ≤ 2 ⋅ 105
− 109 ≤ value ≤ 109
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
虚拟学生会决定举行一场反对言论自由的集会。 因此产生了一个问题 — 选出主席。 学生会里人很多,但没有合适的人选。 经过商议,虚拟角色给自己编了号,并为编号为 i 的角色定义了以下数值:
当且仅当不存在角色 j,使得同时满足 mj > mi 且 pj > pi 时, 第 i 个角色才被认为是主席候选人。
编写一个程序,列出所有主席候选人。
输入文件包含一个自然数 n — 角色的数量。 接下来是 n 对自然数 mi pi。
需要将被选中角色的编号按升序输出到输出文件中。
1 ≤ n ≤ 105
1 ≤ mi, pi ≤ 109
(input.txt) |
(output.txt) | |
|---|---|---|
| 1 |
|
|
虚拟学生会决定举行一场反对言论自由的集会。 因此产生了一个问题 — 选出主席。 学生会里人很多,但没有合适的人选。 经过商议,虚拟角色给自己编了号,并为编号为 i 的角色定义了以下数值:
当且仅当不存在角色 j,使得同时满足 mj > mi、pj > pi 且 tj > ti 时, 第 i 个角色才被认为是主席候选人。
编写一个程序,列出所有主席候选人。
输入文件包含一个自然数 n — 角色的数量。 接下来是 n 组自然数三元组 mi pi ti。
需要将被选中角色的编号按升序输出到输出文件中。
1 ≤ n ≤ 105
1 ≤ mi, pi, ti ≤ 109
(input.txt) |
(output.txt) | |
|---|---|---|
| 1 |
|
|
| Author: | Pavel Mesenev | Time limit: | 3 sec | |
| Input file: | Standard input | Memory limit: | 512 Mb | |
| Output file: | Standard output |
The same farmer as before, the same aggressive cows — but this time the n pastures are not stalls on a tidy line. They are connected by a tangle of fences, ditches and informal cow gossip, so all the farmer can tell you is a number di,j for every pair of pastures: "how far apart I'd call i and j". These numbers need not come from any map or line, and need not satisfy the triangle inequality.
Place C cows into C distinct pastures so that the minimum distance between any two cows is as large as possible.
Output that largest possible minimum distance.
The first line contains two integers n and C. Each of the next n lines contains n integers — row i of the distance matrix di,0… di,n − 1. The matrix is symmetric (di,j = dj,i); the diagonal entries di,i are present but irrelevant and may be ignored.
Print one integer — the maximum achievable minimum distance between two cows.
2 ≤ C ≤ n ≤ 3000
0 ≤ di,j ≤ 109 for i ≠ j, di,j = dj,i
the matrix need not satisfy the triangle inequality
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| Author: | П.Р. Месенёв | Time limit: | 6 sec | |
| Input file: | Standard input | Memory limit: | 512 Mb | |
| Output file: | Standard output |
No Message May Ever Go Undelivered.
Blazing Stars — the Judgements — incessantly erect the law of the universe with each deliverance, prescribing the possibility of existence for all through Correspondence.
This incinerating language is so hazardous to living witnesses that messages written in it must be insulated upon the hard carapace of the postmen — Messengers, whose holy destiny is to promulgate edicts of the Stars.
The Sacred Message must reach its audience in undimmed brightness. Every Correspondence sigil must preserve its distinctness within the shared immanence of the whole. Thus each postman is endowed with a palette fitted to its burden: Violant, Irrigo, Apocyan, Peligin, Viric, Cosmogone, Gant, and others with no distinctive names. Every additional color demands another commitment, one hardly spared in the service of the Gods.
Two sigils inscribed upon touching regions of the carapace must never share a color: should their hues merge, their meanings will merge also, and the law delivered will not be the law ordained.
Choose a sigil to change its hue. Violant and Apocyan answer this.In the first example, two colors suffice.
The Messenger knows which message its carapace carries. The incarnation of the ink is an immediate problem — one it will gladly transfer to the algorithm you are about to write — because No Message May Ever Go Undelivered.
The first line contains n and m: the number of carapace regions and the number of touching pairs. Each of the following m lines contains a b, 1 ≤ a,b ≤ n: the regions with these numbers touch. No region touches itself, and no pair is listed twice.
Print one integer: the minimum number of colors required to deliver the Sacred Message unchanged.
1 ≤ n ≤ 24, 0 ≤ m ≤ n(n − 1) / 2.
| No. | Standard input | Standard output |
|---|---|---|
| 1 |
|
|
| 2 |
|
|