contestId
int64
0
1.01k
name
stringlengths
2
58
tags
listlengths
0
11
title
stringclasses
523 values
time-limit
stringclasses
8 values
memory-limit
stringclasses
8 values
problem-description
stringlengths
0
7.15k
input-specification
stringlengths
0
2.05k
output-specification
stringlengths
0
1.5k
demo-input
listlengths
0
7
demo-output
listlengths
0
7
note
stringlengths
0
5.24k
test_cases
listlengths
0
402
timeConsumedMillis
int64
0
8k
memoryConsumedBytes
int64
0
537M
score
float64
-1
3.99
__index_level_0__
int64
0
621k
0
none
[ "none" ]
null
null
Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1<=×<=*n*. Your task is help Appleman with folding of such a sheet. Actually, you need to perform *q* queries. Each query will have one of the following types: 1. Fold the sheet of paper at position *p**i*. After this query the leftmost part of the paper with dimensions 1<=×<=*p**i* must be above the rightmost part of the paper with dimensions 1<=×<=([*current* *width* *of* *sheet*]<=-<=*p**i*). 1. Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance *l**i* from the left border of the current sheet of paper and the other at distance *r**i* from the left border of the current sheet of paper. Please look at the explanation of the first test example for better understanding of the problem.
The first line contains two integers: *n* and *q* (1<=<=≤<=*n*<=≤<=105; 1<=≤<=*q*<=≤<=105) — the width of the paper and the number of queries. Each of the following *q* lines contains one of the described queries in the following format: - "1 *p**i*" (1<=≤<=*p**i*<=&lt;<=[*current* *width* *of* *sheet*]) — the first type query. - "2 *l**i* *r**i*" (0<=≤<=*l**i*<=&lt;<=*r**i*<=≤<=[*current* *width* *of* *sheet*]) — the second type query.
For each query of the second type, output the answer.
[ "7 4\n1 3\n1 2\n2 0 1\n2 1 2\n", "10 9\n2 2 9\n1 1\n2 0 1\n1 8\n2 0 8\n1 2\n2 1 3\n1 4\n2 2 4\n" ]
[ "4\n3\n", "7\n2\n10\n4\n5\n" ]
The pictures below show the shapes of the paper during the queries of the first example: After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2.
[]
62
0
0
12,849
863
Turn Off The TV
[ "data structures", "sortings" ]
null
null
Luba needs your help again! Luba has *n* TV sets. She knows that *i*-th TV set will be working from moment of time *l**i* till moment *r**i*, inclusive. Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set. Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.
The first line contains one integer number *n* (1<=≤<=*n*<=≤<=2·105) — the number of TV sets. Then *n* lines follow, each of them containing two integer numbers *l**i*,<=*r**i* (0<=≤<=*l**i*<=≤<=*r**i*<=≤<=109) denoting the working time of *i*-th TV set.
If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to *n*). If there are multiple answers, print any of them.
[ "3\n1 3\n4 6\n1 7\n", "2\n0 10\n0 10\n", "3\n1 2\n3 4\n6 8\n", "3\n1 2\n2 3\n3 4\n" ]
[ "1\n", "1\n", "-1\n", "2\n" ]
Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one). Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].
[ { "input": "3\n1 3\n4 6\n1 7", "output": "1" }, { "input": "2\n0 10\n0 10", "output": "1" }, { "input": "3\n1 2\n3 4\n6 8", "output": "-1" }, { "input": "3\n1 2\n2 3\n3 4", "output": "2" }, { "input": "3\n0 500000000\n500000001 1000000000\n0 1000000000", "outp...
0
0
-1
12,865
0
none
[ "none" ]
null
null
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice. Applying the simplest variant of median smoothing to the sequence of numbers *a*1,<=*a*2,<=...,<=*a**n* will result a new sequence *b*1,<=*b*2,<=...,<=*b**n* obtained by the following algorithm: - *b*1<==<=*a*1, *b**n*<==<=*a**n*, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence. - For *i*<==<=2,<=...,<=*n*<=-<=1 value *b**i* is equal to the median of three values *a**i*<=-<=1, *a**i* and *a**i*<=+<=1. The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1. In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only. Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it. Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.
The first input line of the input contains a single integer *n* (3<=≤<=*n*<=≤<=500<=000) — the length of the initial sequence. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (*a**i*<==<=0 or *a**i*<==<=1), giving the initial sequence itself.
If the sequence will never become stable, print a single number <=-<=1. Otherwise, first print a single integer — the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print *n* numbers separated by a space  — the resulting sequence itself.
[ "4\n0 0 1 1\n", "5\n0 1 0 1 0\n" ]
[ "0\n0 0 1 1\n", "2\n0 0 0 0 0\n" ]
In the second sample the stabilization occurs in two steps: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5a983e7baab048cbe43812cb997c15e9d7100231.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and the sequence 00000 is obviously stable.
[ { "input": "4\n0 0 1 1", "output": "0\n0 0 1 1" }, { "input": "5\n0 1 0 1 0", "output": "2\n0 0 0 0 0" }, { "input": "3\n1 0 0", "output": "0\n1 0 0" }, { "input": "4\n1 0 0 1", "output": "0\n1 0 0 1" }, { "input": "7\n1 0 1 1 1 0 1", "output": "1\n1 1 1 1 1 1...
62
0
0
12,874
1,003
Coins and Queries
[ "greedy" ]
null
null
Polycarp has $n$ coins, the value of the $i$-th coin is $a_i$. It is guaranteed that all the values are integer powers of $2$ (i.e. $a_i = 2^d$ for some non-negative integer number $d$). Polycarp wants to know answers on $q$ queries. The $j$-th query is described as integer number $b_j$. The answer to the query is the minimum number of coins that is necessary to obtain the value $b_j$ using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value $b_j$, the answer to the $j$-th query is -1. The queries are independent (the answer on the query doesn't affect Polycarp's coins).
The first line of the input contains two integers $n$ and $q$ ($1 \le n, q \le 2 \cdot 10^5$) — the number of coins and the number of queries. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ — values of coins ($1 \le a_i \le 2 \cdot 10^9$). It is guaranteed that all $a_i$ are integer powers of $2$ (i.e. $a_i = 2^d$ for some non-negative integer number $d$). The next $q$ lines contain one integer each. The $j$-th line contains one integer $b_j$ — the value of the $j$-th query ($1 \le b_j \le 10^9$).
Print $q$ integers $ans_j$. The $j$-th integer must be equal to the answer on the $j$-th query. If Polycarp can't obtain the value $b_j$ the answer to the $j$-th query is -1.
[ "5 4\n2 4 8 2 4\n8\n5\n14\n10\n" ]
[ "1\n-1\n3\n2\n" ]
none
[ { "input": "5 4\n2 4 8 2 4\n8\n5\n14\n10", "output": "1\n-1\n3\n2" }, { "input": "3 3\n1 1 1\n1\n2\n3", "output": "1\n2\n3" }, { "input": "4 1\n2 4 16 32\n14", "output": "-1" }, { "input": "1 10\n8\n1\n2\n3\n4\n5\n6\n7\n8\n9\n16", "output": "-1\n-1\n-1\n-1\n-1\n-1\n-1\n1\...
2,000
13,414,400
0
12,890
579
Finding Team Member
[ "brute force", "implementation", "sortings" ]
null
null
There is a programing contest named SnakeUp, 2*n* people want to compete for it. In order to attend this contest, people need to form teams of exactly two people. You are given the strength of each possible combination of two people. All the values of the strengths are distinct. Every contestant hopes that he can find a teammate so that their team’s strength is as high as possible. That is, a contestant will form a team with highest strength possible by choosing a teammate from ones who are willing to be a teammate with him/her. More formally, two people *A* and *B* may form a team if each of them is the best possible teammate (among the contestants that remain unpaired) for the other one. Can you determine who will be each person’s teammate?
There are 2*n* lines in the input. The first line contains an integer *n* (1<=≤<=*n*<=≤<=400) — the number of teams to be formed. The *i*-th line (*i*<=&gt;<=1) contains *i*<=-<=1 numbers *a**i*1, *a**i*2, ... , *a**i*(*i*<=-<=1). Here *a**ij* (1<=≤<=*a**ij*<=≤<=106, all *a**ij* are distinct) denotes the strength of a team consisting of person *i* and person *j* (people are numbered starting from 1.)
Output a line containing 2*n* numbers. The *i*-th number should represent the number of teammate of *i*-th person.
[ "2\n6\n1 2\n3 4 5\n", "3\n487060\n3831 161856\n845957 794650 976977\n83847 50566 691206 498447\n698377 156232 59015 382455 626960\n" ]
[ "2 1 4 3\n", "6 5 4 3 2 1\n" ]
In the first sample, contestant 1 and 2 will be teammates and so do contestant 3 and 4, so the teammate of contestant 1, 2, 3, 4 will be 2, 1, 4, 3 respectively.
[ { "input": "2\n6\n1 2\n3 4 5", "output": "2 1 4 3" }, { "input": "3\n487060\n3831 161856\n845957 794650 976977\n83847 50566 691206 498447\n698377 156232 59015 382455 626960", "output": "6 5 4 3 2 1" }, { "input": "3\n8\n1 6\n14 13 15\n4 2 11 9\n12 5 3 7 10", "output": "6 5 4 3 2 1" ...
327
35,840,000
3
12,915
812
Sagheer, the Hausmeister
[ "bitmasks", "brute force", "dp" ]
null
null
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of *n* floors with stairs at the left and the right sides. Each floor has *m* rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with *n* rows and *m*<=+<=2 columns, where the first and the last columns represent the stairs, and the *m* columns in the middle represent rooms. Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights. Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=15 and 1<=≤<=*m*<=≤<=100) — the number of floors and the number of rooms in each floor, respectively. The next *n* lines contains the building description. Each line contains a binary string of length *m*<=+<=2 representing a floor (the left stairs, then *m* rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
Print a single integer — the minimum total time needed to turn off all the lights.
[ "2 2\n0010\n0100\n", "3 4\n001000\n000010\n000010\n", "4 3\n01110\n01110\n01110\n01110\n" ]
[ "5\n", "12\n", "18\n" ]
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs. In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor. In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
[ { "input": "2 2\n0010\n0100", "output": "5" }, { "input": "3 4\n001000\n000010\n000010", "output": "12" }, { "input": "4 3\n01110\n01110\n01110\n01110", "output": "18" }, { "input": "3 2\n0000\n0100\n0100", "output": "4" }, { "input": "1 89\n0000000000000000000000...
139
0
0
12,932
457
Golden System
[ "math", "meet-in-the-middle" ]
null
null
Piegirl got bored with binary, decimal and other integer based counting systems. Recently she discovered some interesting properties about number , in particular that *q*2<==<=*q*<=+<=1, and she thinks it would make a good base for her new unique system. She called it "golden system". In golden system the number is a non-empty string containing 0's and 1's as digits. The decimal value of expression *a*0*a*1...*a**n* equals to . Soon Piegirl found out that this system doesn't have same properties that integer base systems do and some operations can not be performed on it. She wasn't able to come up with a fast way of comparing two numbers. She is asking for your help. Given two numbers written in golden system notation, determine which of them has larger decimal value.
Input consists of two lines — one for each number. Each line contains non-empty string consisting of '0' and '1' characters. The length of each string does not exceed 100000.
Print "&gt;" if the first number is larger, "&lt;" if it is smaller and "=" if they are equal.
[ "1000\n111\n", "00100\n11\n", "110\n101\n" ]
[ "&lt;\n", "=\n", "&gt;\n" ]
In the first example first number equals to <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9c955eec678d6e7dcdc7c94fb203e922d2ad19ad.png" style="max-width: 100.0%;max-height: 100.0%;"/>, while second number is approximately 1.618033988<sup class="upper-index">2</sup> + 1.618033988 + 1 ≈ 5.236, which is clearly a bigger number. In the second example numbers are equal. Each of them is  ≈ 2.618.
[ { "input": "1000\n111", "output": "<" }, { "input": "00100\n11", "output": "=" }, { "input": "110\n101", "output": ">" }, { "input": "0\n0", "output": "=" }, { "input": "1\n10", "output": "<" }, { "input": "11\n10", "output": ">" }, { "inpu...
1,000
1,945,600
0
12,934
219
Special Offer! Super Price 999 Bourles!
[ "implementation" ]
null
null
Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors. Polycaprus calculated that the optimal celling price for such scissors would be *p* bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect. Polycarpus agrees to lower the price by no more than *d* bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price. Note, Polycarpus counts only the trailing nines in a price.
The first line contains two integers *p* and *d* (1<=≤<=*p*<=≤<=1018; 0<=≤<=*d*<=&lt;<=*p*) — the initial price of scissors and the maximum possible price reduction. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Print the required price — the maximum price that ends with the largest number of nines and that is less than *p* by no more than *d*. The required number shouldn't have leading zeroes.
[ "1029 102\n", "27191 17\n" ]
[ "999\n", "27189\n" ]
none
[ { "input": "1029 102", "output": "999" }, { "input": "27191 17", "output": "27189" }, { "input": "1 0", "output": "1" }, { "input": "9 0", "output": "9" }, { "input": "20 1", "output": "19" }, { "input": "100 23", "output": "99" }, { "input...
278
512,000
-1
12,962
88
Chord
[ "brute force", "implementation" ]
A. Chord
2
256
Vasya studies music. He has learned lots of interesting stuff. For example, he knows that there are 12 notes: C, C#, D, D#, E, F, F#, G, G#, A, B, H. He also knows that the notes are repeated cyclically: after H goes C again, and before C stands H. We will consider the C note in the row's beginning and the C note after the H similar and we will identify them with each other. The distance between the notes along the musical scale is measured in tones: between two consecutive notes there's exactly one semitone, that is, 0.5 tone. The distance is taken from the lowest tone to the uppest one, that is, the distance between C and E is 4 semitones and between E and C is 8 semitones Vasya also knows what a chord is. A chord is an unordered set of no less than three notes. However, for now Vasya only works with triads, that is with the chords that consist of exactly three notes. He can already distinguish between two types of triads — major and minor. Let's define a major triad. Let the triad consist of notes *X*, *Y* and *Z*. If we can order the notes so as the distance along the musical scale between *X* and *Y* equals 4 semitones and the distance between *Y* and *Z* is 3 semitones, then the triad is major. The distance between *X* and *Z*, accordingly, equals 7 semitones. A minor triad is different in that the distance between *X* and *Y* should be 3 semitones and between *Y* and *Z* — 4 semitones. For example, the triad "C E G" is major: between C and E are 4 semitones, and between E and G are 3 semitones. And the triplet "C# B F" is minor, because if we order the notes as "B C# F", than between B and C# will be 3 semitones, and between C# and F — 4 semitones. Help Vasya classify the triad the teacher has given to him.
The only line contains 3 space-separated notes in the above-given notation.
Print "major" if the chord is major, "minor" if it is minor, and "strange" if the teacher gave Vasya some weird chord which is neither major nor minor. Vasya promises you that the answer will always be unambiguous. That is, there are no chords that are both major and minor simultaneously.
[ "C E G\n", "C# B F\n", "A B H\n" ]
[ "major\n", "minor\n", "strange\n" ]
none
[ { "input": "C E G", "output": "major" }, { "input": "C# B F", "output": "minor" }, { "input": "A B H", "output": "strange" }, { "input": "G H E", "output": "minor" }, { "input": "D# B G", "output": "major" }, { "input": "D# B F#", "output": "minor"...
310
0
3.9225
12,972
958
Guard Duty (easy)
[ "brute force", "geometry", "greedy", "math" ]
null
null
The Rebel fleet is afraid that the Empire might want to strike back again. Princess Heidi needs to know if it is possible to assign *R* Rebel spaceships to guard *B* bases so that every base has exactly one guardian and each spaceship has exactly one assigned base (in other words, the assignment is a perfect matching). Since she knows how reckless her pilots are, she wants to be sure that any two (straight) paths – from a base to its assigned spaceship – do not intersect in the galaxy plane (that is, in 2D), and so there is no risk of collision.
The first line contains two space-separated integers *R*,<=*B*(1<=≤<=*R*,<=*B*<=≤<=10). For 1<=≤<=*i*<=≤<=*R*, the *i*<=+<=1-th line contains two space-separated integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=≤<=10000) denoting the coordinates of the *i*-th Rebel spaceship. The following *B* lines have the same format, denoting the position of bases. It is guaranteed that no two points coincide and that no three points are on the same line.
If it is possible to connect Rebel spaceships and bases so as satisfy the constraint, output Yes, otherwise output No (without quote).
[ "3 3\n0 0\n2 0\n3 1\n-2 1\n0 3\n2 2\n", "2 1\n1 0\n2 2\n3 1\n" ]
[ "Yes\n", "No\n" ]
For the first example, one possible way is to connect the Rebels and bases in order. For the second example, there is no perfect matching between Rebels and bases.
[ { "input": "3 3\n0 0\n2 0\n3 1\n-2 1\n0 3\n2 2", "output": "Yes" }, { "input": "2 1\n1 0\n2 2\n3 1", "output": "No" }, { "input": "1 1\n3686 4362\n-7485 5112", "output": "Yes" }, { "input": "1 2\n1152 -7324\n-5137 -35\n-6045 -5271", "output": "No" }, { "input": "1...
46
0
0
12,994
107
Dorm Water Supply
[ "dfs and similar", "graphs" ]
A. Dorm Water Supply
1
256
The German University in Cairo (GUC) dorm houses are numbered from 1 to *n*. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle). For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house. In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
The first line contains two space-separated integers *n* and *p* (1<=≤<=*n*<=≤<=1000,<=0<=≤<=*p*<=≤<=*n*) — the number of houses and the number of pipes correspondingly. Then *p* lines follow — the description of *p* pipes. The *i*-th line contains three integers *a**i* *b**i* *d**i*, indicating a pipe of diameter *d**i* going from house *a**i* to house *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*,<=*a**i*<=≠<=*b**i*,<=1<=≤<=*d**i*<=≤<=106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.
Print integer *t* in the first line — the number of tank-tap pairs of houses. For the next *t* lines, print 3 integers per line, separated by spaces: *tank**i*, *tap**i*, and *diameter**i*, where *tank**i*<=≠<=*tap**i* (1<=≤<=*i*<=≤<=*t*). Here *tank**i* and *tap**i* are indexes of tank and tap houses respectively, and *diameter**i* is the maximum amount of water that can be conveyed. All the *t* lines should be ordered (increasingly) by *tank**i*.
[ "3 2\n1 2 10\n2 3 20\n", "3 3\n1 2 20\n2 3 10\n3 1 5\n", "4 2\n1 2 60\n3 4 50\n" ]
[ "1\n1 3 10\n", "0\n", "2\n1 2 60\n3 4 50\n" ]
none
[ { "input": "3 2\n1 2 10\n2 3 20", "output": "1\n1 3 10" }, { "input": "3 3\n1 2 20\n2 3 10\n3 1 5", "output": "0" }, { "input": "4 2\n1 2 60\n3 4 50", "output": "2\n1 2 60\n3 4 50" }, { "input": "10 10\n10 3 70\n1 9 98\n9 10 67\n5 2 78\n8 6 71\n4 8 95\n7 1 10\n2 5 73\n6 7 94\...
264
30,208,000
0
13,007
555
Case of Computer Network
[ "dfs and similar", "graphs", "trees" ]
null
null
Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible attack by hackers on a major computer network. In this network are *n* vertices, some pairs of vertices are connected by *m* undirected channels. It is planned to transfer *q* important messages via this network, the *i*-th of which must be sent from vertex *s**i* to vertex *d**i* via one or more channels, perhaps through some intermediate vertices. To protect against attacks a special algorithm was developed. Unfortunately it can be applied only to the network containing directed channels. Therefore, as new channels can't be created, it was decided for each of the existing undirected channels to enable them to transmit data only in one of the two directions. Your task is to determine whether it is possible so to choose the direction for each channel so that each of the *q* messages could be successfully transmitted.
The first line contains three integers *n*, *m* and *q* (1<=≤<=*n*,<=*m*,<=*q*<=≤<=2·105) — the number of nodes, channels and important messages. Next *m* lines contain two integers each, *v**i* and *u**i* (1<=≤<=*v**i*,<=*u**i*<=≤<=*n*, *v**i*<=≠<=*u**i*), that means that between nodes *v**i* and *u**i* is a channel. Between a pair of nodes can exist more than one channel. Next *q* lines contain two integers *s**i* and *d**i* (1<=≤<=*s**i*,<=*d**i*<=≤<=*n*, *s**i*<=≠<=*d**i*) — the numbers of the nodes of the source and destination of the corresponding message. It is not guaranteed that in it initially possible to transmit all the messages.
If a solution exists, print on a single line "Yes" (without the quotes). Otherwise, print "No" (without the quotes).
[ "4 4 2\n1 2\n1 3\n2 3\n3 4\n1 3\n4 2\n", "3 2 2\n1 2\n3 2\n1 3\n2 1\n", "3 3 2\n1 2\n1 2\n3 2\n1 3\n2 1\n" ]
[ "Yes\n", "No\n", "Yes\n" ]
In the first sample test you can assign directions, for example, as follows: 1 → 2, 1 → 3, 3 → 2, 4 → 3. Then the path for for the first message will be 1 → 3, and for the second one — 4 → 3 → 2. In the third sample test you can assign directions, for example, as follows: 1 → 2, 2 → 1, 2 → 3. Then the path for the first message will be 1 → 2 → 3, and for the second one — 2 → 1.
[]
0
0
-1
13,087
549
Degenerate Matrix
[ "binary search", "math" ]
null
null
The determinant of a matrix 2<=×<=2 is defined as follows: A matrix is called degenerate if its determinant is equal to zero. The norm ||*A*|| of a matrix *A* is defined as a maximum of absolute values of its elements. You are given a matrix . Consider any degenerate matrix *B* such that norm ||*A*<=-<=*B*|| is minimum possible. Determine ||*A*<=-<=*B*||.
The first line contains two integers *a* and *b* (|*a*|,<=|*b*|<=≤<=109), the elements of the first row of matrix *A*. The second line contains two integers *c* and *d* (|*c*|,<=|*d*|<=≤<=109) the elements of the second row of matrix *A*.
Output a single real number, the minimum possible value of ||*A*<=-<=*B*||. Your answer is considered to be correct if its absolute or relative error does not exceed 10<=-<=9.
[ "1 2\n3 4\n", "1 0\n0 1\n" ]
[ "0.2000000000\n", "0.5000000000\n" ]
In the first sample matrix *B* is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ce214ad27bde5d77f87492eedd74d34c745f72a1.png" style="max-width: 100.0%;max-height: 100.0%;"/> In the second sample matrix *B* is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a07c34fbc6e9328bcb519d3f780eea6e02e5dc87.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "1 2\n3 4", "output": "0.2000000000" }, { "input": "1 0\n0 1", "output": "0.5000000000" }, { "input": "1000000000 0\n0 1000000000", "output": "500000000.0000000000" }, { "input": "8205 9482\n11 -63", "output": "35.0198432832" }, { "input": "0 0\n0 0", ...
156
5,632,000
3
13,105
673
Problems for Round
[ "greedy", "implementation" ]
null
null
There are *n* problems prepared for the next Codeforces round. They are arranged in ascending order by their difficulty, and no two problems have the same difficulty. Moreover, there are *m* pairs of similar problems. Authors want to split problems between two division according to the following rules: - Problemset of each division should be non-empty. - Each problem should be used in exactly one division (yes, it is unusual requirement). - Each problem used in division 1 should be harder than any problem used in division 2. - If two problems are similar, they should be used in different divisions. Your goal is count the number of ways to split problem between two divisions and satisfy all the rules. Two ways to split problems are considered to be different if there is at least one problem that belongs to division 1 in one of them and to division 2 in the other. Note, that the relation of similarity is not transitive. That is, if problem *i* is similar to problem *j* and problem *j* is similar to problem *k*, it doesn't follow that *i* is similar to *k*.
The first line of the input contains two integers *n* and *m* (2<=≤<=*n*<=≤<=100<=000, 0<=≤<=*m*<=≤<=100<=000) — the number of problems prepared for the round and the number of pairs of similar problems, respectively. Each of the following *m* lines contains a pair of similar problems *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*,<=*u**i*<=≠<=*v**i*). It's guaranteed, that no pair of problems meets twice in the input.
Print one integer — the number of ways to split problems in two divisions.
[ "5 2\n1 4\n5 2\n", "3 3\n1 2\n2 3\n1 3\n", "3 2\n3 1\n3 2\n" ]
[ "2\n", "0\n", "1\n" ]
In the first sample, problems 1 and 2 should be used in division 2, while problems 4 and 5 in division 1. Problem 3 may be used either in division 1 or in division 2. In the second sample, all pairs of problems are similar and there is no way to split problem between two divisions without breaking any rules. Third sample reminds you that the similarity relation is not transitive. Problem 3 is similar to both 1 and 2, but 1 is not similar to 2, so they may be used together.
[ { "input": "5 2\n1 4\n5 2", "output": "2" }, { "input": "3 3\n1 2\n2 3\n1 3", "output": "0" }, { "input": "3 2\n3 1\n3 2", "output": "1" }, { "input": "2 0", "output": "1" }, { "input": "2 1\n1 2", "output": "1" }, { "input": "3 0", "output": "2" ...
61
4,608,000
0
13,114
36
Bowls
[ "geometry", "implementation" ]
C. Bowls
2
64
Once Petya was in such a good mood that he decided to help his mum with the washing-up. There were *n* dirty bowls in the sink. From the geometrical point of view each bowl looks like a blunted cone. We can disregard the width of the walls and bottom. Petya puts the clean bowls one on another naturally, i. e. so that their vertical axes coincide (see the picture). You will be given the order in which Petya washes the bowls. Determine the height of the construction, i.e. the distance from the bottom of the lowest bowl to the top of the highest one.
The first input line contains integer *n* (1<=≤<=*n*<=≤<=3000). Each of the following *n* lines contains 3 integers *h*, *r* and *R* (1<=≤<=*h*<=≤<=10000,<=1<=≤<=*r*<=&lt;<=*R*<=≤<=10000). They are the height of a bowl, the radius of its bottom and the radius of its top. The plates are given in the order Petya puts them on the table.
Output the height of the plate pile accurate to at least 10<=-<=6.
[ "2\n40 10 50\n60 20 30\n", "3\n50 30 80\n35 25 70\n40 10 90\n" ]
[ "70.00000000\n", "55.00000000\n" ]
none
[ { "input": "2\n40 10 50\n60 20 30", "output": "70.00000000" }, { "input": "3\n50 30 80\n35 25 70\n40 10 90", "output": "55.00000000" }, { "input": "1\n5 3 10", "output": "5.00000000" }, { "input": "3\n1 1 2\n2 2 3\n3 3 4", "output": "6.00000000" }, { "input": "3\n...
92
0
0
13,158
802
Fake News (medium)
[ "constructive algorithms", "strings" ]
null
null
Thanks to your help, Heidi is confident that no one can fool her. She has now decided to post some fake news on the HC2 Facebook page. However, she wants to be able to communicate to the HC2 committee that the post is fake, using some secret phrase hidden in the post as a subsequence. To make this method foolproof, she wants the phrase to appear *n* times in the post. She is asking you to design a post (string) *s* and a hidden phrase *p* such that *p* appears in *s* as a subsequence exactly *n* times.
The first and only line of input contains a single integer *n* (1<=≤<=*n*<=≤<=1<=000<=000).
The output should contain two nonempty strings *s* and *p* separated by a single space. Each string should be composed of letters (a-z and A-Z: both lowercase and uppercase are allowed) and have length at most 200. The number of occurrences of *p* in *s* as a subsequence should be exactly *n*. If there are many possible solutions, output any of them. It is guaranteed that at least one solution exists.
[ "2\n", "4\n", "6\n" ]
[ "hHheidi Hei", "bbbba ba", "aaabb ab" ]
An occurrence of *p* as a subsequence in *s* should be thought of as a set of positions in *s* such that the letters at these positions, in order, form *p*. The number of occurences is thus the number of such sets. For example, ab appears 6 times as a subsequence in aaabb, for the following sets of positions: {1, 4}, {1, 5}, {2, 4}, {2, 5}, {3, 4}, {3, 5} (that is, we should choose one of the a's and one of the b's).
[ { "input": "2", "output": "aa a" }, { "input": "4", "output": "bbbba ba" }, { "input": "6", "output": "bbabba ba" }, { "input": "1", "output": "a a" }, { "input": "3", "output": "bbba ba" }, { "input": "5", "output": "bbaba ba" }, { "input"...
31
0
-1
13,184
911
Inversion Counting
[ "brute force", "math" ]
null
null
A permutation of size *n* is an array of size *n* such that each integer from 1 to *n* occurs exactly once in this array. An inversion in a permutation *p* is a pair of indices (*i*,<=*j*) such that *i*<=&gt;<=*j* and *a**i*<=&lt;<=*a**j*. For example, a permutation [4,<=1,<=3,<=2] contains 4 inversions: (2,<=1), (3,<=1), (4,<=1), (4,<=3). You are given a permutation *a* of size *n* and *m* queries to it. Each query is represented by two indices *l* and *r* denoting that you have to reverse the segment [*l*,<=*r*] of the permutation. For example, if *a*<==<=[1,<=2,<=3,<=4] and a query *l*<==<=2, *r*<==<=4 is applied, then the resulting permutation is [1,<=4,<=3,<=2]. After each query you have to determine whether the number of inversions is odd or even.
The first line contains one integer *n* (1<=≤<=*n*<=≤<=1500) — the size of the permutation. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=*n*) — the elements of the permutation. These integers are pairwise distinct. The third line contains one integer *m* (1<=≤<=*m*<=≤<=2·105) — the number of queries to process. Then *m* lines follow, *i*-th line containing two integers *l**i*, *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*) denoting that *i*-th query is to reverse a segment [*l**i*,<=*r**i*] of the permutation. All queries are performed one after another.
Print *m* lines. *i*-th of them must be equal to odd if the number of inversions in the permutation after *i*-th query is odd, and even otherwise.
[ "3\n1 2 3\n2\n1 2\n2 3\n", "4\n1 2 4 3\n4\n1 1\n1 4\n1 4\n2 3\n" ]
[ "odd\neven\n", "odd\nodd\nodd\neven\n" ]
The first example: 1. after the first query *a* = [2, 1, 3], inversion: (2, 1); 1. after the second query *a* = [2, 3, 1], inversions: (3, 1), (3, 2). The second example: 1. *a* = [1, 2, 4, 3], inversion: (4, 3); 1. *a* = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3); 1. *a* = [1, 2, 4, 3], inversion: (4, 3); 1. *a* = [1, 4, 2, 3], inversions: (3, 2), (4, 2).
[ { "input": "3\n1 2 3\n2\n1 2\n2 3", "output": "odd\neven" }, { "input": "4\n1 2 4 3\n4\n1 1\n1 4\n1 4\n2 3", "output": "odd\nodd\nodd\neven" }, { "input": "7\n2 6 1 7 4 5 3\n5\n4 5\n7 7\n5 6\n4 5\n4 5", "output": "odd\nodd\neven\nodd\neven" }, { "input": "3\n2 1 3\n3\n2 3\n1 ...
451
13,312,000
3
13,194
156
Cipher
[ "combinatorics", "dp" ]
null
null
Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But there is a problem! The correspondence turned out to be encrypted. The detective tried really hard to decipher the correspondence, but he couldn't understand anything. At last, after some thought, he thought of something. Let's say there is a word *s*, consisting of |*s*| lowercase Latin letters. Then for one operation you can choose a certain position *p* (1<=≤<=*p*<=&lt;<=|*s*|) and perform one of the following actions: - either replace letter *s**p* with the one that alphabetically follows it and replace letter *s**p*<=+<=1 with the one that alphabetically precedes it; - or replace letter *s**p* with the one that alphabetically precedes it and replace letter *s**p*<=+<=1 with the one that alphabetically follows it. Let us note that letter "z" doesn't have a defined following letter and letter "a" doesn't have a defined preceding letter. That's why the corresponding changes are not acceptable. If the operation requires performing at least one unacceptable change, then such operation cannot be performed. Two words coincide in their meaning iff one of them can be transformed into the other one as a result of zero or more operations. Sherlock Holmes needs to learn to quickly determine the following for each word: how many words can exist that coincide in their meaning with the given word, but differs from the given word in at least one character? Count this number for him modulo 1000000007 (109<=+<=7).
The input data contains several tests. The first line contains the only integer *t* (1<=≤<=*t*<=≤<=104) — the number of tests. Next *t* lines contain the words, one per line. Each word consists of lowercase Latin letters and has length from 1 to 100, inclusive. Lengths of words can differ.
For each word you should print the number of different other words that coincide with it in their meaning — not from the words listed in the input data, but from all possible words. As the sought number can be very large, print its value modulo 1000000007 (109<=+<=7).
[ "1\nab\n", "1\naaaaaaaaaaa\n", "2\nya\nklmbfxzb\n" ]
[ "1\n", "0\n", "24\n320092793\n" ]
Some explanations about the operation: - Note that for each letter, we can clearly define the letter that follows it. Letter "b" alphabetically follows letter "a", letter "c" follows letter "b", ..., "z" follows letter "y". - Preceding letters are defined in the similar manner: letter "y" precedes letter "z", ..., "a" precedes letter "b". - Note that the operation never changes a word's length. In the first sample you can obtain the only other word "ba". In the second sample you cannot obtain any other word, so the correct answer is 0. Consider the third sample. One operation can transform word "klmbfxzb" into word "klmcexzb": we should choose *p* = 4, and replace the fourth letter with the following one ("b"  →  "c"), and the fifth one — with the preceding one ("f"  →  "e"). Also, we can obtain many other words from this one. An operation can transform word "ya" only into one other word "xb". Word "ya" coincides in its meaning with words "xb", "wc", "vd", ..., "ay" (overall there are 24 other words). The word "klmbfxzb has many more variants — there are 3320092814 other words that coincide with in the meaning. So the answer for the first word equals 24 and for the second one equals 320092793 — the number 3320092814 modulo 10<sup class="upper-index">9</sup> + 7
[ { "input": "1\nab", "output": "1" }, { "input": "1\naaaaaaaaaaa", "output": "0" }, { "input": "2\nya\nklmbfxzb", "output": "24\n320092793" }, { "input": "1\na", "output": "0" }, { "input": "1\nz", "output": "0" }, { "input": "1\naaaaaaaaaaaaaaaaaaaaaaa...
964
8,601,600
3
13,250
316
EKG
[ "dfs and similar", "dp" ]
null
null
In the rush of modern life, people often forget how beautiful the world is. The time to enjoy those around them is so little that some even stand in queues to several rooms at the same time in the clinic, running from one queue to another. (Cultural note: standing in huge and disorganized queues for hours is a native tradition in Russia, dating back to the Soviet period. Queues can resemble crowds rather than lines. Not to get lost in such a queue, a person should follow a strict survival technique: you approach the queue and ask who the last person is, somebody answers and you join the crowd. Now you're the last person in the queue till somebody else shows up. You keep an eye on the one who was last before you as he is your only chance to get to your destination) I'm sure many people have had the problem when a stranger asks who the last person in the queue is and even dares to hint that he will be the last in the queue and then bolts away to some unknown destination. These are the representatives of the modern world, in which the ratio of lack of time is so great that they do not even watch foreign top-rated TV series. Such people often create problems in queues, because the newcomer does not see the last person in the queue and takes a place after the "virtual" link in this chain, wondering where this legendary figure has left. The Smart Beaver has been ill and he's made an appointment with a therapist. The doctor told the Beaver the sad news in a nutshell: it is necessary to do an electrocardiogram. The next day the Smart Beaver got up early, put on the famous TV series on download (three hours till the download's complete), clenched his teeth and bravely went to join a queue to the electrocardiogram room, which is notorious for the biggest queues at the clinic. Having stood for about three hours in the queue, the Smart Beaver realized that many beavers had not seen who was supposed to stand in the queue before them and there was a huge mess. He came up to each beaver in the ECG room queue and asked who should be in front of him in the queue. If the beaver did not know his correct position in the queue, then it might be his turn to go get an ECG, or maybe he should wait for a long, long time... As you've guessed, the Smart Beaver was in a hurry home, so he gave you all the necessary information for you to help him to determine what his number in the queue can be.
The first line contains two integers *n* (1<=≤<=*n*<=≤<=103) and *x* (1<=≤<=*x*<=≤<=*n*) — the number of beavers that stand in the queue and the Smart Beaver's number, correspondingly. All willing to get to the doctor are numbered from 1 to *n*. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=*n*) — the number of the beaver followed by the *i*-th beaver. If *a**i*<==<=0, then the *i*-th beaver doesn't know who is should be in front of him. It is guaranteed that values *a**i* are correct. That is there is no cycles in the dependencies. And any beaver is followed by at most one beaver in the queue. The input limits for scoring 30 points are (subproblem B1): - It is guaranteed that the number of zero elements *a**i* doesn't exceed 20. The input limits for scoring 100 points are (subproblems B1+B2): - The number of zero elements *a**i* is arbitrary.
Print all possible positions of the Smart Beaver in the line in the increasing order.
[ "6 1\n2 0 4 0 6 0\n", "6 2\n2 3 0 5 6 0\n", "4 1\n0 0 0 0\n", "6 2\n0 0 1 0 4 5\n" ]
[ "2\n4\n6\n", "2\n5\n", "1\n2\n3\n4\n", "1\n3\n4\n6\n" ]
[ { "input": "6 1\n2 0 4 0 6 0", "output": "2\n4\n6" }, { "input": "6 2\n2 3 0 5 6 0", "output": "2\n5" }, { "input": "4 1\n0 0 0 0", "output": "1\n2\n3\n4" }, { "input": "6 2\n0 0 1 0 4 5", "output": "1\n3\n4\n6" }, { "input": "10 7\n10 8 6 5 0 0 0 4 3 9", "out...
140
2,764,800
3
13,256
291
Parallel Programming
[ "*special", "greedy" ]
null
null
Polycarpus has a computer with *n* processors. Also, his computer has *n* memory cells. We'll consider the processors numbered by integers from 1 to *n* and that the memory cells are consecutively numbered by integers from 1 to *n*. Polycarpus needs to come up with a parallel program model. For each memory cell number *i* this program must record the value *n*<=-<=*i* to this cell. In other words, for each cell you've got to find the distance to cell *n*. Let's denote the value that is written in the *i*-th cell as *a**i*. Initially, *a**i*<==<=1 (1<=≤<=*i*<=&lt;<=*n*) and *a**n*<==<=0. We will consider that only processor *i* can write values in the memory cell number *i*. All processors can read an information from some cell (several processors can read an information from some cell simultaneously). The parallel program is executed in several steps. During each step we execute the parallel version of the increment operation. Executing the parallel version of the increment operation goes as follows: 1. Each processor independently of the other ones chooses some memory cell. Let's say that processor *i* has chosen a cell with number *c**i* (1<=≤<=*c**i*<=≤<=*n*). 1. All processors simultaneously execute operation *a**i*<==<=*a**i*<=+<=*a**c**i*. Help Polycarpus come up with the parallel program model that is executed in exactly *k* steps. Calculate the operations that need to be executed. Note that after *k* steps for all *i*'s value *a**i* must be equal *n*<=-<=*i*.
The first line contains two space-separated integers *n* and *k* (1<=≤<=*n*<=≤<=104,<=1<=≤<=*k*<=≤<=20). It is guaranteed that at the given *n* and *k* the required sequence of operations exists.
Print exactly *n*·*k* integers in *k* lines. In the first line print numbers *c*1,<=*c*2,<=...,<=*c**n* (1<=≤<=*c**i*<=≤<=*n*) for the first increment operation. In the second line print the numbers for the second increment operation. In the *k*-th line print the numbers for the *k*-th increment operation. As a result of the printed operations for any *i* value *a**i* must equal *n*<=-<=*i*.
[ "1 1\n", "3 2\n" ]
[ "1\n", "2 3 3\n3 3 3\n" ]
none
[ { "input": "1 1", "output": "1" }, { "input": "3 2", "output": "2 3 3\n3 3 3" }, { "input": "4 2", "output": "2 3 4 4\n3 4 4 4" }, { "input": "2 1", "output": "2 2" }, { "input": "2 20", "output": "2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 ...
92
0
0
13,263
396
On Sum of Fractions
[ "math", "number theory" ]
null
null
Let's assume that - *v*(*n*) is the largest prime number, that does not exceed *n*;- *u*(*n*) is the smallest prime number strictly greater than *n*. Find .
The first line contains integer *t* (1<=≤<=*t*<=≤<=500) — the number of testscases. Each of the following *t* lines of the input contains integer *n* (2<=≤<=*n*<=≤<=109).
Print *t* lines: the *i*-th of them must contain the answer to the *i*-th test as an irreducible fraction "*p*/*q*", where *p*,<=*q* are integers, *q*<=&gt;<=0.
[ "2\n2\n3\n" ]
[ "1/6\n7/30\n" ]
none
[ { "input": "2\n2\n3", "output": "1/6\n7/30" }, { "input": "1\n1000000000", "output": "999999941999999673/1999999887999999118" }, { "input": "5\n3\n6\n9\n10\n5", "output": "7/30\n5/14\n61/154\n9/22\n23/70" }, { "input": "5\n5\n8\n18\n17\n17", "output": "23/70\n59/154\n17/3...
46
307,200
0
13,273
290
Mysterious strings
[ "*special", "implementation" ]
null
null
The input contains a single integer *a* (1<=≤<=*a*<=≤<=40). Output a single string.
The input contains a single integer *a* (1<=≤<=*a*<=≤<=40).
Output a single string.
[ "2\n", "8\n", "29\n" ]
[ "Adams\n", "Van Buren\n", "Harding\n" ]
none
[ { "input": "2", "output": "Adams" }, { "input": "8", "output": "Van Buren" }, { "input": "29", "output": "Harding" }, { "input": "1", "output": "Washington" }, { "input": "3", "output": "Jefferson" }, { "input": "4", "output": "Madison" }, { ...
62
0
0
13,329
567
One-Dimensional Battle Ships
[ "binary search", "data structures", "greedy", "sortings" ]
null
null
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of *n* square cells (that is, on a 1<=×<=*n* table). At the beginning of the game Alice puts *k* ships on the field without telling their positions to Bob. Each ship looks as a 1<=×<=*a* rectangle (that is, it occupies a sequence of *a* consecutive squares of the field). The ships cannot intersect and even touch each other. After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit"). But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss". Help Bob catch Alice cheating — find Bob's first move, such that after it you can be sure that Alice cheated.
The first line of the input contains three integers: *n*, *k* and *a* (1<=≤<=*n*,<=*k*,<=*a*<=≤<=2·105) — the size of the field, the number of the ships and the size of each ship. It is guaranteed that the *n*, *k* and *a* are such that you can put *k* ships of size *a* on the field, so that no two ships intersect or touch each other. The second line contains integer *m* (1<=≤<=*m*<=≤<=*n*) — the number of Bob's moves. The third line contains *m* distinct integers *x*1,<=*x*2,<=...,<=*x**m*, where *x**i* is the number of the cell where Bob made the *i*-th shot. The cells are numbered from left to right from 1 to *n*.
Print a single integer — the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to *m* in the order the were made. If the sought move doesn't exist, then print "-1".
[ "11 3 3\n5\n4 8 6 1 11\n", "5 1 3\n2\n1 5\n", "5 1 3\n1\n3\n" ]
[ "3\n", "-1\n", "1\n" ]
none
[ { "input": "11 3 3\n5\n4 8 6 1 11", "output": "3" }, { "input": "5 1 3\n2\n1 5", "output": "-1" }, { "input": "5 1 3\n1\n3", "output": "1" }, { "input": "1 1 1\n1\n1", "output": "1" }, { "input": "5000 1660 2\n20\n1 100 18 102 300 81 19 25 44 88 1337 4999 1054 120...
124
0
0
13,336
883
Photo Processing
[ "binary search", "dp" ]
null
null
Evlampiy has found one more cool application to process photos. However the application has certain limitations. Each photo *i* has a contrast *v**i*. In order for the processing to be truly of high quality, the application must receive at least *k* photos with contrasts which differ as little as possible. Evlampiy already knows the contrast *v**i* for each of his *n* photos. Now he wants to split the photos into groups, so that each group contains at least *k* photos. As a result, each photo must belong to exactly one group. He considers a processing time of the *j*-th group to be the difference between the maximum and minimum values of *v**i* in the group. Because of multithreading the processing time of a division into groups is the maximum processing time among all groups. Split *n* photos into groups in a such way that the processing time of the division is the minimum possible, i.e. that the the maximum processing time over all groups as least as possible.
The first line contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=3·105) — number of photos and minimum size of a group. The second line contains *n* integers *v*1,<=*v*2,<=...,<=*v**n* (1<=≤<=*v**i*<=≤<=109), where *v**i* is the contrast of the *i*-th photo.
Print the minimal processing time of the division into groups.
[ "5 2\n50 110 130 40 120\n", "4 1\n2 3 4 1\n" ]
[ "20\n", "0\n" ]
In the first example the photos should be split into 2 groups: [40, 50] and [110, 120, 130]. The processing time of the first group is 10, and the processing time of the second group is 20. Maximum among 10 and 20 is 20. It is impossible to split the photos into groups in a such way that the processing time of division is less than 20. In the second example the photos should be split into four groups, each containing one photo. So the minimal possible processing time of a division is 0.
[ { "input": "5 2\n50 110 130 40 120", "output": "20" }, { "input": "4 1\n2 3 4 1", "output": "0" }, { "input": "1 1\n4", "output": "0" }, { "input": "2 2\n7 5", "output": "2" }, { "input": "3 2\n34 3 75", "output": "72" }, { "input": "5 2\n932 328 886 9...
3,000
22,118,400
0
13,405
0
none
[ "none" ]
null
null
There are *n* animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number *i* in the queue will have to visit his office exactly *a**i* times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving *k* animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out.
The first line of input data contains two space-separated integers *n* and *k* (1<=≤<=*n*<=≤<=105, 0<=≤<=*k*<=≤<=1014). In the second line are given space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
If the doctor will overall carry out less than *k* examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.
[ "3 3\n1 2 1\n", "4 10\n3 3 2 1\n", "7 10\n1 3 3 1 2 3 1\n" ]
[ "2 ", "-1\n", "6 2 3 " ]
In the first sample test: - Before examination: {1, 2, 3} - After the first examination: {2, 3} - After the second examination: {3, 2} - After the third examination: {2} In the second sample test: - Before examination: {1, 2, 3, 4, 5, 6, 7} - After the first examination: {2, 3, 4, 5, 6, 7} - After the second examination: {3, 4, 5, 6, 7, 2} - After the third examination: {4, 5, 6, 7, 2, 3} - After the fourth examination: {5, 6, 7, 2, 3} - After the fifth examination: {6, 7, 2, 3, 5} - After the sixth examination: {7, 2, 3, 5, 6} - After the seventh examination: {2, 3, 5, 6} - After the eighth examination: {3, 5, 6, 2} - After the ninth examination: {5, 6, 2, 3} - After the tenth examination: {6, 2, 3}
[ { "input": "3 3\n1 2 1", "output": "2 " }, { "input": "4 10\n3 3 2 1", "output": "-1" }, { "input": "7 10\n1 3 3 1 2 3 1", "output": "6 2 3 " }, { "input": "1 0\n1", "output": "1 " }, { "input": "6 101\n9 78 54 62 2 91", "output": "4 6 2 3 " }, { "inpu...
186
0
0
13,411
225
Barcode
[ "dp", "matrices" ]
null
null
You've got an *n*<=×<=*m* pixel picture. Each pixel can be white or black. Your task is to change the colors of as few pixels as possible to obtain a barcode picture. A picture is a barcode if the following conditions are fulfilled: - All pixels in each column are of the same color. - The width of each monochrome vertical line is at least *x* and at most *y* pixels. In other words, if we group all neighbouring columns of the pixels with equal color, the size of each group can not be less than *x* or greater than *y*.
The first line contains four space-separated integers *n*, *m*, *x* and *y* (1<=≤<=*n*,<=*m*,<=*x*,<=*y*<=≤<=1000; *x*<=≤<=*y*). Then follow *n* lines, describing the original image. Each of these lines contains exactly *m* characters. Character "." represents a white pixel and "#" represents a black pixel. The picture description doesn't have any other characters besides "." and "#".
In the first line print the minimum number of pixels to repaint. It is guaranteed that the answer exists.
[ "6 5 1 2\n##.#.\n.###.\n###..\n#...#\n.##.#\n###..\n", "2 5 1 1\n#####\n.....\n" ]
[ "11\n", "5\n" ]
In the first test sample the picture after changing some colors can looks as follows: In the second test sample the picture after changing some colors can looks as follows:
[ { "input": "6 5 1 2\n##.#.\n.###.\n###..\n#...#\n.##.#\n###..", "output": "11" }, { "input": "10 5 3 7\n.####\n###..\n##.##\n#..#.\n.#...\n#.##.\n.##..\n.#.##\n#.#..\n.#..#", "output": "24" }, { "input": "6 3 1 4\n##.\n#..\n#..\n..#\n.#.\n#.#", "output": "6" }, { "input": "5 ...
92
512,000
0
13,412
0
none
[ "none" ]
null
null
Codeforces is a wonderful platform and one its feature shows how much someone contributes to the community. Every registered user has contribution — an integer number, not necessarily positive. There are *n* registered users and the *i*-th of them has contribution *t**i*. Limak is a little polar bear and he's new into competitive programming. He doesn't even have an account in Codeforces but he is able to upvote existing blogs and comments. We assume that every registered user has infinitely many blogs and comments. - Limak can spend *b* minutes to read one blog and upvote it. Author's contribution will be increased by 5. - Limak can spend *c* minutes to read one comment and upvote it. Author's contribution will be increased by 1. Note that it's possible that Limak reads blogs faster than comments. Limak likes ties. He thinks it would be awesome to see a tie between at least *k* registered users. To make it happen he is going to spend some time on reading and upvoting. After that, there should exist an integer value *x* that at least *k* registered users have contribution exactly *x*. How much time does Limak need to achieve his goal?
The first line contains four integers *n*, *k*, *b* and *c* (2<=≤<=*k*<=≤<=*n*<=≤<=200<=000,<=1<=≤<=*b*,<=*c*<=≤<=1000) — the number of registered users, the required minimum number of users with the same contribution, time needed to read and upvote a blog, and time needed to read and upvote a comment, respectively. The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (|*t**i*|<=≤<=109) where *t**i* denotes contribution of the *i*-th registered user.
Print the minimum number of minutes Limak will spend to get a tie between at least *k* registered users.
[ "4 3 100 30\n12 2 6 1\n", "4 3 30 100\n12 2 6 1\n", "6 2 987 789\n-8 42 -4 -65 -8 -8\n" ]
[ "220\n", "190\n", "0\n" ]
In the first sample, there are 4 registered users and Limak wants a tie between at least 3 of them. Limak should behave as follows. - He spends 100 minutes to read one blog of the 4-th user and increase his contribution from 1 to 6. - Then he spends 4·30 = 120 minutes to read four comments of the 2-nd user and increase his contribution from 2 to 6 (four times it was increaded by 1). In the given scenario, Limak spends 100 + 4·30 = 220 minutes and after that each of users 2, 3, 4 has contribution 6. In the second sample, Limak needs 30 minutes to read a blog and 100 minutes to read a comment. This time he can get 3 users with contribution equal to 12 by spending 100 + 3·30 = 190 minutes: - Spend 2·30 = 60 minutes to read two blogs of the 1-st user to increase his contribution from 2 to 12. - Spend 30 + 100 minutes to read one blog and one comment of the 3-rd user. His contribution will change from 6 to 6 + 5 + 1 = 12.
[ { "input": "4 3 100 30\n12 2 6 1", "output": "220" }, { "input": "4 3 30 100\n12 2 6 1", "output": "190" }, { "input": "6 2 987 789\n-8 42 -4 -65 -8 -8", "output": "0" }, { "input": "5 5 1 1000\n1 1 1 1 2", "output": "4000" }, { "input": "6 6 1 1000\n1 1 1 1 1 2",...
62
0
0
13,432
842
Vitya and Strange Lesson
[ "binary search", "data structures" ]
null
null
Today at the lesson Vitya learned a very interesting function — mex. Mex of a sequence of numbers is the minimum non-negative number that is not present in the sequence as element. For example, *mex*([4,<=33,<=0,<=1,<=1,<=5])<==<=2 and *mex*([1,<=2,<=3])<==<=0. Vitya quickly understood all tasks of the teacher, but can you do the same? You are given an array consisting of *n* non-negative integers, and *m* queries. Each query is characterized by one number *x* and consists of the following consecutive steps: - Perform the bitwise addition operation modulo 2 (xor) of each array element with the number *x*. - Find mex of the resulting array. Note that after each query the array changes.
First line contains two integer numbers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=3·105) — number of elements in array and number of queries. Next line contains *n* integer numbers *a**i* (0<=≤<=*a**i*<=≤<=3·105) — elements of then array. Each of next *m* lines contains query — one integer number *x* (0<=≤<=*x*<=≤<=3·105).
For each query print the answer on a separate line.
[ "2 2\n1 3\n1\n3\n", "4 3\n0 1 5 6\n1\n2\n4\n", "5 4\n0 1 5 6 7\n1\n1\n4\n5\n" ]
[ "1\n0\n", "2\n0\n0\n", "2\n2\n0\n2\n" ]
none
[ { "input": "2 2\n1 3\n1\n3", "output": "1\n0" }, { "input": "4 3\n0 1 5 6\n1\n2\n4", "output": "2\n0\n0" }, { "input": "5 4\n0 1 5 6 7\n1\n1\n4\n5", "output": "2\n2\n0\n2" }, { "input": "5 5\n1 2 3 4 5\n1\n2\n3\n4\n5", "output": "1\n3\n0\n2\n1" }, { "input": "9 3\...
2,000
20,275,200
0
13,439
999
Equalize the Remainders
[ "data structures", "greedy", "implementation" ]
null
null
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$. In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$. Let's calculate $c_r$ ($0 \le r \le m-1)$ — the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder. Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$. Find the minimum number of moves to satisfy the above requirement.
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$. The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
In the first line, print a single integer — the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$. In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.
[ "6 3\n3 2 0 6 10 12\n", "4 2\n0 1 2 3\n" ]
[ "3\n3 2 0 7 10 14 \n", "0\n0 1 2 3 \n" ]
none
[ { "input": "6 3\n3 2 0 6 10 12", "output": "3\n3 2 0 7 10 14 " }, { "input": "4 2\n0 1 2 3", "output": "0\n0 1 2 3 " }, { "input": "1 1\n1000000000", "output": "0\n1000000000 " }, { "input": "6 3\n3 2 0 6 10 11", "output": "1\n3 2 0 7 10 11 " }, { "input": "100 25...
780
29,491,200
3
13,457
712
Memory and Scores
[ "combinatorics", "dp", "math" ]
null
null
Memory and his friend Lexa are competing to get higher score in one popular computer game. Memory starts with score *a* and Lexa starts with score *b*. In a single turn, both Memory and Lexa get some integer in the range [<=-<=*k*;*k*] (i.e. one integer among <=-<=*k*,<=<=-<=*k*<=+<=1,<=<=-<=*k*<=+<=2,<=...,<=<=-<=2,<=<=-<=1,<=0,<=1,<=2,<=...,<=*k*<=-<=1,<=*k*) and add them to their current scores. The game has exactly *t* turns. Memory and Lexa, however, are not good at this game, so they both always get a random integer at their turn. Memory wonders how many possible games exist such that he ends with a strictly higher score than Lexa. Two games are considered to be different if in at least one turn at least one player gets different score. There are (2*k*<=+<=1)2*t* games in total. Since the answer can be very large, you should print it modulo 109<=+<=7. Please solve this problem for Memory.
The first and only line of input contains the four integers *a*, *b*, *k*, and *t* (1<=≤<=*a*,<=*b*<=≤<=100, 1<=≤<=*k*<=≤<=1000, 1<=≤<=*t*<=≤<=100) — the amount Memory and Lexa start with, the number *k*, and the number of turns respectively.
Print the number of possible games satisfying the conditions modulo 1<=000<=000<=007 (109<=+<=7) in one line.
[ "1 2 2 1\n", "1 1 1 2\n", "2 12 3 1\n" ]
[ "6\n", "31\n", "0\n" ]
In the first sample test, Memory starts with 1 and Lexa starts with 2. If Lexa picks  - 2, Memory can pick 0, 1, or 2 to win. If Lexa picks  - 1, Memory can pick 1 or 2 to win. If Lexa picks 0, Memory can pick 2 to win. If Lexa picks 1 or 2, Memory cannot win. Thus, there are 3 + 2 + 1 = 6 possible games in which Memory wins.
[ { "input": "1 2 2 1", "output": "6" }, { "input": "1 1 1 2", "output": "31" }, { "input": "2 12 3 1", "output": "0" }, { "input": "4 6 2 1", "output": "3" }, { "input": "4 6 2 2", "output": "122" }, { "input": "6 4 2 2", "output": "435" }, { ...
2,000
11,673,600
0
13,499
51
Caterpillar
[ "dfs and similar", "dp", "graphs", "trees" ]
F. Caterpillar
2
256
An undirected graph is called a caterpillar if it is a connected graph without cycles and it has such a path *p* that any vertex is located at a distance of at most 1 from the path *p*. The caterpillar can contain loops (edges from a vertex to itself) but cannot contain multiple (parallel) edges. The picture contains an example of a caterpillar: You are given an undirected graph *G*. You are allowed to do a merging operations, each such operation merges two vertices into one vertex. For that two any vertices *a* and *b* (*a*<=≠<=*b*) are chosen. These verteces are deleted together with their edges (which are incident to at least one of the vertices *a* or *b*) but a new vertex *w* is added together with edges (*x*,<=*w*) for each edge (*a*,<=*w*) and/or (*b*,<=*w*). If there was the edge (*a*,<=*b*) it transforms to the loop (*w*,<=*w*). The resulting graph (after the merging operation) may contain multiple (parallel) edges between pairs of vertices and loops. Let us note that this operation decreases the number of vertices of graph by 1 but leaves the number of edges in the graph unchanged. The merging operation can be informally described as a unity of two vertices of the graph into one with the natural transformation of the graph edges. You may apply this operation consecutively and make the given graph to be a caterpillar. Write a program that will print the minimal number of merging operations required to make the given graph a caterpillar.
The first line contains a pair of integers *n*, *m* (1<=≤<=*n*<=≤<=2000;0<=≤<=*m*<=≤<=105), where *n* represents the number of vertices in the graph and *m* is the number of edges in it. Then the following *m* lines contain edge descriptions, one edge description per line. Every line contains a pair of integers *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*;*a**i*<=≠<=*b**i*), *a**i*,<=*b**i* which represent the indices of the vertices connected by the edge. The vertices are numbered from 1 to *n*. In the given graph it will be no more than one edge between any pair of vertices. The given graph is not necessarily connected.
Print the minimal required number of operations.
[ "4 4\n1 2\n2 3\n3 4\n4 2\n", "6 3\n1 2\n3 4\n5 6\n", "7 6\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7\n" ]
[ "2\n", "2\n", "1\n" ]
none
[]
92
0
0
13,514
577
Modulo Sum
[ "combinatorics", "data structures", "dp", "two pointers" ]
null
null
You are given a sequence of numbers *a*1,<=*a*2,<=...,<=*a**n*, and a number *m*. Check if it is possible to choose a non-empty subsequence *a**i**j* such that the sum of numbers in this subsequence is divisible by *m*.
The first line contains two numbers, *n* and *m* (1<=≤<=*n*<=≤<=106, 2<=≤<=*m*<=≤<=103) — the size of the original sequence and the number such that sum should be divisible by it. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=109).
In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist.
[ "3 5\n1 2 3\n", "1 6\n5\n", "4 6\n3 1 1 3\n", "6 6\n5 5 5 5 5 5\n" ]
[ "YES\n", "NO\n", "YES\n", "YES\n" ]
In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5. In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist. In the third sample test you need to choose two numbers 3 on the ends. In the fourth sample test you can take the whole subsequence.
[ { "input": "3 5\n1 2 3", "output": "YES" }, { "input": "1 6\n5", "output": "NO" }, { "input": "4 6\n3 1 1 3", "output": "YES" }, { "input": "6 6\n5 5 5 5 5 5", "output": "YES" }, { "input": "4 5\n1 1 1 1", "output": "NO" }, { "input": "5 5\n1 1 1 1 1",...
0
0
-1
13,529
917
The Monster
[ "dp", "greedy", "implementation", "math" ]
null
null
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem. A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally: - Empty string is a correct bracket sequence. - if *s* is a correct bracket sequence, then (*s*) is also a correct bracket sequence. - if *s* and *t* are correct bracket sequences, then *st* (concatenation of *s* and *t*) is also a correct bracket sequence. A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence. Will gave his mom a string *s* consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (*l*,<=*r*) such that 1<=≤<=*l*<=≤<=*r*<=≤<=|*s*| and the string *s**l**s**l*<=+<=1... *s**r* is pretty, where *s**i* is *i*-th character of *s*. Joyce doesn't know anything about bracket sequences, so she asked for your help.
The first and only line of input contains string *s*, consisting only of characters '(', ')' and '?' (2<=≤<=|*s*|<=≤<=5000).
Print the answer to Will's puzzle in the first and only line of output.
[ "((?))\n", "??()??\n" ]
[ "4\n", "7\n" ]
For the first sample testcase, the pretty substrings of *s* are: 1. "(?" which can be transformed to "()". 1. "?)" which can be transformed to "()". 1. "((?)" which can be transformed to "(())". 1. "(?))" which can be transformed to "(())". For the second sample testcase, the pretty substrings of *s* are: 1. "??" which can be transformed to "()". 1. "()". 1. "??()" which can be transformed to "()()". 1. "?()?" which can be transformed to "(())". 1. "??" which can be transformed to "()". 1. "()??" which can be transformed to "()()". 1. "??()??" which can be transformed to "()()()".
[ { "input": "((?))", "output": "4" }, { "input": "??()??", "output": "7" }, { "input": "?????)(???", "output": "21" }, { "input": "()()((?(()(((()()(())(((()((())))(()))(()(((((())))()))(((()()()))))))(((((()))))))))", "output": "62" }, { "input": "))((()(()((((())...
46
0
0
13,533
590
Three States
[ "dfs and similar", "graphs", "shortest paths" ]
null
null
The famous global economic crisis is approaching rapidly, so the states of Berman, Berance and Bertaly formed an alliance and allowed the residents of all member states to freely pass through the territory of any of them. In addition, it was decided that a road between the states should be built to guarantee so that one could any point of any country can be reached from any point of any other State. Since roads are always expensive, the governments of the states of the newly formed alliance asked you to help them assess the costs. To do this, you have been issued a map that can be represented as a rectangle table consisting of *n* rows and *m* columns. Any cell of the map either belongs to one of three states, or is an area where it is allowed to build a road, or is an area where the construction of the road is not allowed. A cell is called passable, if it belongs to one of the states, or the road was built in this cell. From any passable cells you can move up, down, right and left, if the cell that corresponds to the movement exists and is passable. Your task is to construct a road inside a minimum number of cells, so that it would be possible to get from any cell of any state to any cell of any other state using only passable cells. It is guaranteed that initially it is possible to reach any cell of any state from any cell of this state, moving only along its cells. It is also guaranteed that for any state there is at least one cell that belongs to it.
The first line of the input contains the dimensions of the map *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1000) — the number of rows and columns respectively. Each of the next *n* lines contain *m* characters, describing the rows of the map. Digits from 1 to 3 represent the accessory to the corresponding state. The character '.' corresponds to the cell where it is allowed to build a road and the character '#' means no construction is allowed in this cell.
Print a single integer — the minimum number of cells you need to build a road inside in order to connect all the cells of all states. If such a goal is unachievable, print -1.
[ "4 5\n11..2\n#..22\n#.323\n.#333", "1 5\n1#2#3\n" ]
[ "2", "-1\n" ]
none
[ { "input": "4 5\n11..2\n#..22\n#.323\n.#333", "output": "2" }, { "input": "1 5\n1#2#3", "output": "-1" }, { "input": "3 4\n.2..\n...3\n.1#.", "output": "2" }, { "input": "10 10\n##.#..#.#2\n...###....\n#..#....##\n.....#....\n.#........\n.....#####\n...#..#...\n....###...\n##...
1,762
100,556,800
3
13,560
523
Name Quest
[ "*special", "greedy" ]
null
null
A Martian boy is named *s* — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if *s*=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not. However rather than being happy once, he loves twice as much being happy twice! So, when he got string *t* as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy. Help *s* determine the number of distinct ways to cut the given string *t* into two parts in the required manner.
The first line contains string *s*, consisting of lowercase English letters. The length of string *s* is from 1 to 1000 letters. The second line contains string *t*, that also consists of lowercase English letters. The length of string *t* is from 1 to 106 letters.
Print the sought number of ways to cut string *t* in two so that each part made *s* happy.
[ "aba\nbaobababbah\n", "mars\nsunvenusearthmarsjupitersaturnuranusneptune\n" ]
[ "2\n", "0\n" ]
none
[ { "input": "aba\nbaobababbah", "output": "2" }, { "input": "mars\nsunvenusearthmarsjupitersaturnuranusneptune", "output": "0" }, { "input": "rry\nsorrymercuryismissedabove", "output": "3" }, { "input": "a\naaaaaaaaaaaaaa", "output": "13" }, { "input": "abcaba\nabc...
77
3,379,200
3
13,561
864
Cities Excursions
[ "dfs and similar", "graphs", "trees" ]
null
null
There are *n* cities in Berland. Some pairs of them are connected with *m* directed roads. One can use only these roads to move from one city to another. There are no roads that connect a city to itself. For each pair of cities (*x*,<=*y*) there is at most one road from *x* to *y*. A path from city *s* to city *t* is a sequence of cities *p*1, *p*2, ... , *p**k*, where *p*1<==<=*s*, *p**k*<==<=*t*, and there is a road from city *p**i* to city *p**i*<=+<=1 for each *i* from 1 to *k*<=-<=1. The path can pass multiple times through each city except *t*. It can't pass through *t* more than once. A path *p* from *s* to *t* is ideal if it is the lexicographically minimal such path. In other words, *p* is ideal path from *s* to *t* if for any other path *q* from *s* to *t* *p**i*<=&lt;<=*q**i*, where *i* is the minimum integer such that *p**i*<=≠<=*q**i*. There is a tourist agency in the country that offers *q* unusual excursions: the *j*-th excursion starts at city *s**j* and ends in city *t**j*. For each pair *s**j*, *t**j* help the agency to study the ideal path from *s**j* to *t**j*. Note that it is possible that there is no ideal path from *s**j* to *t**j*. This is possible due to two reasons: - there is no path from *s**j* to *t**j*; - there are paths from *s**j* to *t**j*, but for every such path *p* there is another path *q* from *s**j* to *t**j*, such that *p**i*<=&gt;<=*q**i*, where *i* is the minimum integer for which *p**i*<=≠<=*q**i*. The agency would like to know for the ideal path from *s**j* to *t**j* the *k**j*-th city in that path (on the way from *s**j* to *t**j*). For each triple *s**j*, *t**j*, *k**j* (1<=≤<=*j*<=≤<=*q*) find if there is an ideal path from *s**j* to *t**j* and print the *k**j*-th city in that path, if there is any.
The first line contains three integers *n*, *m* and *q* (2<=≤<=*n*<=≤<=3000,0<=≤<=*m*<=≤<=3000, 1<=≤<=*q*<=≤<=4·105) — the number of cities, the number of roads and the number of excursions. Each of the next *m* lines contains two integers *x**i* and *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*, *x**i*<=≠<=*y**i*), denoting that the *i*-th road goes from city *x**i* to city *y**i*. All roads are one-directional. There can't be more than one road in each direction between two cities. Each of the next *q* lines contains three integers *s**j*, *t**j* and *k**j* (1<=≤<=*s**j*,<=*t**j*<=≤<=*n*, *s**j*<=≠<=*t**j*, 1<=≤<=*k**j*<=≤<=3000).
In the *j*-th line print the city that is the *k**j*-th in the ideal path from *s**j* to *t**j*. If there is no ideal path from *s**j* to *t**j*, or the integer *k**j* is greater than the length of this path, print the string '-1' (without quotes) in the *j*-th line.
[ "7 7 5\n1 2\n2 3\n1 3\n3 4\n4 5\n5 3\n4 6\n1 4 2\n2 6 1\n1 7 3\n1 3 2\n1 3 5\n" ]
[ "2\n-1\n-1\n2\n-1\n" ]
none
[ { "input": "7 7 5\n1 2\n2 3\n1 3\n3 4\n4 5\n5 3\n4 6\n1 4 2\n2 6 1\n1 7 3\n1 3 2\n1 3 5", "output": "2\n-1\n-1\n2\n-1" }, { "input": "3 4 5\n1 3\n2 1\n3 1\n3 2\n1 2 1\n2 3 2\n2 3 3\n1 3 1\n3 2 1", "output": "-1\n1\n3\n1\n-1" }, { "input": "2 0 2\n2 1 2\n2 1 1", "output": "-1\n-1" }...
0
0
-1
13,566
660
Number of Parallelograms
[ "geometry" ]
null
null
You are given *n* points on a plane. All the points are distinct and no three of them lie on the same line. Find the number of parallelograms with the vertices at the given points.
The first line of the input contains integer *n* (1<=≤<=*n*<=≤<=2000) — the number of points. Each of the next *n* lines contains two integers (*x**i*,<=*y**i*) (0<=≤<=*x**i*,<=*y**i*<=≤<=109) — the coordinates of the *i*-th point.
Print the only integer *c* — the number of parallelograms with the vertices at the given points.
[ "4\n0 1\n1 0\n1 1\n2 0\n" ]
[ "1\n" ]
none
[ { "input": "4\n0 1\n1 0\n1 1\n2 0", "output": "1" }, { "input": "1\n0 0", "output": "0" }, { "input": "1\n6 6", "output": "0" }, { "input": "5\n1 5\n4 2\n4 4\n8 1\n8 2", "output": "0" }, { "input": "10\n1 7\n2 14\n3 7\n3 13\n5 13\n13 10\n15 12\n17 1\n18 8\n19 0", ...
4,000
98,918,400
0
13,608
0
none
[ "none" ]
null
null
Stepan is a very experienced olympiad participant. He has *n* cups for Physics olympiads and *m* cups for Informatics olympiads. Each cup is characterized by two parameters — its significance *c**i* and width *w**i*. Stepan decided to expose some of his cups on a shelf with width *d* in such a way, that: - there is at least one Physics cup and at least one Informatics cup on the shelf, - the total width of the exposed cups does not exceed *d*, - from each subjects (Physics and Informatics) some of the most significant cups are exposed (i. e. if a cup for some subject with significance *x* is exposed, then all the cups for this subject with significance greater than *x* must be exposed too). Your task is to determine the maximum possible total significance, which Stepan can get when he exposes cups on the shelf with width *d*, considering all the rules described above. The total significance is the sum of significances of all the exposed cups.
The first line contains three integers *n*, *m* and *d* (1<=≤<=*n*,<=*m*<=≤<=100<=000, 1<=≤<=*d*<=≤<=109) — the number of cups for Physics olympiads, the number of cups for Informatics olympiads and the width of the shelf. Each of the following *n* lines contains two integers *c**i* and *w**i* (1<=≤<=*c**i*,<=*w**i*<=≤<=109) — significance and width of the *i*-th cup for Physics olympiads. Each of the following *m* lines contains two integers *c**j* and *w**j* (1<=≤<=*c**j*,<=*w**j*<=≤<=109) — significance and width of the *j*-th cup for Informatics olympiads.
Print the maximum possible total significance, which Stepan can get exposing cups on the shelf with width *d*, considering all the rules described in the statement. If there is no way to expose cups on the shelf, then print 0.
[ "3 1 8\n4 2\n5 5\n4 2\n3 2\n", "4 3 12\n3 4\n2 4\n3 5\n3 4\n3 5\n5 2\n3 4\n", "2 2 2\n5 3\n6 3\n4 2\n8 1\n" ]
[ "8\n", "11\n", "0\n" ]
In the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals 3 and width equals 2, so after Stepan exposes it, the width of free space on the shelf becomes equal to 6. Also, Stepan must expose the second Physics cup (which has width 5), because it is the most significant cup for Physics (its significance equals 5). After that Stepan can not expose more cups on the shelf, because there is no enough free space. Thus, the maximum total significance of exposed cups equals to 8.
[ { "input": "3 1 8\n4 2\n5 5\n4 2\n3 2", "output": "8" }, { "input": "4 3 12\n3 4\n2 4\n3 5\n3 4\n3 5\n5 2\n3 4", "output": "11" }, { "input": "2 2 2\n5 3\n6 3\n4 2\n8 1", "output": "0" }, { "input": "10 10 229\n15 17\n5 4\n4 15\n4 17\n15 11\n7 6\n5 19\n14 8\n4 1\n10 12\n20 13...
1,949
23,756,800
0
13,665
321
Ciel and Robot
[ "binary search", "implementation", "math" ]
null
null
Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string *s*. Each character of *s* is one move operation. There are four move operations at all: - 'U': go up, (x, y) <=→<= (x, y+1); - 'D': go down, (x, y) <=→<= (x, y-1); - 'L': go left, (x, y) <=→<= (x-1, y); - 'R': go right, (x, y) <=→<= (x+1, y). The robot will do the operations in *s* from left to right, and repeat it infinite times. Help Fox Ciel to determine if after some steps the robot will located in (*a*,<=*b*).
The first line contains two integers *a* and *b*, (<=-<=109<=≤<=*a*,<=*b*<=≤<=109). The second line contains a string *s* (1<=≤<=|*s*|<=≤<=100, *s* only contains characters 'U', 'D', 'L', 'R') — the command.
Print "Yes" if the robot will be located at (*a*,<=*b*), and "No" otherwise.
[ "2 2\nRU\n", "1 2\nRU\n", "-1 1000000000\nLRRLU\n", "0 0\nD\n" ]
[ "Yes\n", "No\n", "Yes\n", "Yes\n" ]
In the first and second test case, command string is "RU", so the robot will go right, then go up, then right, and then up and so on. The locations of its moves are (0, 0)  →  (1, 0)  →  (1, 1)  →  (2, 1)  →  (2, 2)  →  ... So it can reach (2, 2) but not (1, 2).
[ { "input": "2 2\nRU", "output": "Yes" }, { "input": "1 2\nRU", "output": "No" }, { "input": "-1 1000000000\nLRRLU", "output": "Yes" }, { "input": "0 0\nD", "output": "Yes" }, { "input": "0 0\nUURRDL", "output": "Yes" }, { "input": "987654321 987654321\...
248
0
0
13,693
91
Queue
[ "binary search", "data structures" ]
B. Queue
2
256
There are *n* walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the *n*-th walrus stands at the beginning of the queue. The *i*-th walrus has the age equal to *a**i*. The *i*-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such *j* (*i*<=&lt;<=*j*), that *a**i*<=&gt;<=*a**j*. The displeasure of the *i*-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the *i*-th one. That is, the further that young walrus stands from him, the stronger the displeasure is. The airport manager asked you to count for each of *n* walruses in the queue his displeasure.
The first line contains an integer *n* (2<=≤<=*n*<=≤<=105) — the number of walruses in the queue. The second line contains integers *a**i* (1<=≤<=*a**i*<=≤<=109). Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.
Print *n* numbers: if the *i*-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the *i*-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
[ "6\n10 8 5 3 50 45\n", "7\n10 4 6 3 2 8 15\n", "5\n10 3 1 10 11\n" ]
[ "2 1 0 -1 0 -1 ", "4 2 1 0 -1 -1 -1 ", "1 0 -1 -1 -1 " ]
none
[ { "input": "6\n10 8 5 3 50 45", "output": "2 1 0 -1 0 -1 " }, { "input": "7\n10 4 6 3 2 8 15", "output": "4 2 1 0 -1 -1 -1 " }, { "input": "5\n10 3 1 10 11", "output": "1 0 -1 -1 -1 " }, { "input": "13\n18 9 8 9 23 20 18 18 33 25 31 37 36", "output": "2 0 -1 -1 2 1 -1 -1 ...
748
12,288,000
3.790112
13,700
55
Beautiful numbers
[ "dp", "number theory" ]
D. Beautiful numbers
4
256
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.
The first line of the input contains the number of cases *t* (1<=≤<=*t*<=≤<=10). Each of the next *t* lines contains two natural numbers *l**i* and *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=9<=·1018). Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
Output should contain *t* numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from *l**i* to *r**i*, inclusively).
[ "1\n1 9\n", "1\n12 15\n" ]
[ "9\n", "2\n" ]
none
[ { "input": "1\n1 9", "output": "9" }, { "input": "1\n12 15", "output": "2" }, { "input": "1\n25 53", "output": "7" }, { "input": "1\n1 1000", "output": "138" }, { "input": "1\n1 100000", "output": "4578" }, { "input": "2\n234 59843\n46 3243", "outp...
60
0
0
13,717
0
none
[ "none" ]
null
null
Карта звёздного неба представляет собой прямоугольное поле, состоящее из *n* строк по *m* символов в каждой строке. Каждый символ — это либо «.» (означает пустой участок неба), либо «*» (означает то, что в этом месте на небе есть звезда). Новое издание карты звёздного неба будет напечатано на квадратных листах, поэтому требуется найти минимально возможную сторону квадрата, в который могут поместиться все звезды. Границы искомого квадрата должны быть параллельны сторонам заданного прямоугольного поля.
В первой строке входных данных записаны два числа *n* и *m* (1<=≤<=*n*,<=*m*<=≤<=1000) — количество строк и столбцов на карте звездного неба. В следующих *n* строках задано по *m* символов. Каждый символ — это либо «.» (пустой участок неба), либо «*» (звезда). Гарантируется, что на небе есть хотя бы одна звезда.
Выведите одно число — минимально возможную сторону квадрата, которым можно накрыть все звезды.
[ "4 4\n....\n..*.\n...*\n..**\n", "1 3\n*.*\n", "2 1\n.\n*\n" ]
[ "3\n", "3\n", "1\n" ]
Один из возможных ответов на первый тестовый пример: Один из возможных ответов на второй тестовый пример (обратите внимание, что покрывающий квадрат выходит за пределы карты звездного неба): Ответ на третий тестовый пример:
[ { "input": "4 4\n....\n..*.\n...*\n..**", "output": "3" }, { "input": "1 3\n*.*", "output": "3" }, { "input": "2 1\n.\n*", "output": "1" }, { "input": "1 1\n*", "output": "1" }, { "input": "1 2\n.*", "output": "1" }, { "input": "1 2\n*.", "output":...
77
6,144,000
3
13,724
777
Hanoi Factory
[ "brute force", "data structures", "dp", "greedy", "sortings" ]
null
null
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings. There are *n* rings in factory's stock. The *i*-th ring has inner radius *a**i*, outer radius *b**i* and height *h**i*. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied: - Outer radiuses form a non-increasing sequence, i.e. one can put the *j*-th ring on the *i*-th ring only if *b**j*<=≤<=*b**i*. - Rings should not fall one into the the other. That means one can place ring *j* on the ring *i* only if *b**j*<=&gt;<=*a**i*. - The total height of all rings used should be maximum possible.
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of rings in factory's stock. The *i*-th of the next *n* lines contains three integers *a**i*, *b**i* and *h**i* (1<=≤<=*a**i*,<=*b**i*,<=*h**i*<=≤<=109, *b**i*<=&gt;<=*a**i*) — inner radius, outer radius and the height of the *i*-th ring respectively.
Print one integer — the maximum height of the tower that can be obtained.
[ "3\n1 5 1\n2 6 2\n3 7 3\n", "4\n1 2 1\n1 3 3\n4 6 2\n5 7 1\n" ]
[ "6\n", "4\n" ]
In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1. In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
[ { "input": "3\n1 5 1\n2 6 2\n3 7 3", "output": "6" }, { "input": "4\n1 2 1\n1 3 3\n4 6 2\n5 7 1", "output": "4" }, { "input": "1\n1 2 1", "output": "1" }, { "input": "5\n6 10 4\n9 20 19\n8 11 18\n18 20 1\n19 20 8", "output": "50" }, { "input": "5\n21 25 26\n14 30 ...
62
0
0
13,730
731
80-th Level Archeology
[ "brute force", "data structures", "greedy", "sortings" ]
null
null
Archeologists have found a secret pass in the dungeon of one of the pyramids of Cycleland. To enter the treasury they have to open an unusual lock on the door. The lock consists of *n* words, each consisting of some hieroglyphs. The wall near the lock has a round switch. Each rotation of this switch changes the hieroglyphs according to some rules. The instruction nearby says that the door will open only if words written on the lock would be sorted in lexicographical order (the definition of lexicographical comparison in given in notes section). The rule that changes hieroglyphs is the following. One clockwise rotation of the round switch replaces each hieroglyph with the next hieroglyph in alphabet, i.e. hieroglyph *x* (1<=≤<=*x*<=≤<=*c*<=-<=1) is replaced with hieroglyph (*x*<=+<=1), and hieroglyph *c* is replaced with hieroglyph 1. Help archeologist determine, how many clockwise rotations they should perform in order to open the door, or determine that this is impossible, i.e. no cyclic shift of the alphabet will make the sequence of words sorted lexicographically.
The first line of the input contains two integers *n* and *c* (2<=≤<=*n*<=≤<=500<=000, 1<=≤<=*c*<=≤<=106) — the number of words, written on the lock, and the number of different hieroglyphs. Each of the following *n* lines contains the description of one word. The *i*-th of these lines starts with integer *l**i* (1<=≤<=*l**i*<=≤<=500<=000), that denotes the length of the *i*-th word, followed by *l**i* integers *w**i*,<=1, *w**i*,<=2, ..., *w**i*,<=*l**i* (1<=≤<=*w**i*,<=*j*<=≤<=*c*) — the indices of hieroglyphs that make up the *i*-th word. Hieroglyph with index 1 is the smallest in the alphabet and with index *c* — the biggest. It's guaranteed, that the total length of all words doesn't exceed 106.
If it is possible to open the door by rotating the round switch, print integer *x* (0<=≤<=*x*<=≤<=*c*<=-<=1) that defines the required number of clockwise rotations. If there are several valid *x*, print any of them. If it is impossible to open the door by this method, print <=-<=1.
[ "4 3\n2 3 2\n1 1\n3 2 3 1\n4 2 3 1 2\n", "2 5\n2 4 2\n2 4 2\n", "4 4\n1 2\n1 3\n1 4\n1 2\n" ]
[ "1\n", "0\n", "-1\n" ]
Word *a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*m*</sub> of length *m* is lexicographically not greater than word *b*<sub class="lower-index">1</sub>, *b*<sub class="lower-index">2</sub>, ..., *b*<sub class="lower-index">*k*</sub> of length *k*, if one of two conditions hold: - at first position *i*, such that *a*<sub class="lower-index">*i*</sub> ≠ *b*<sub class="lower-index">*i*</sub>, the character *a*<sub class="lower-index">*i*</sub> goes earlier in the alphabet than character *b*<sub class="lower-index">*i*</sub>, i.e. *a* has smaller character in the first position where they differ; - if there is no such position *i* and *m* ≤ *k*, i.e. the first word is a prefix of the second or two words are equal. The sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word. In the first sample, after the round switch is rotated 1 position clockwise the words look as follows: In the second sample, words are already sorted in lexicographical order. In the last sample, one can check that no shift of the alphabet will work.
[ { "input": "4 3\n2 3 2\n1 1\n3 2 3 1\n4 2 3 1 2", "output": "1" }, { "input": "2 5\n2 4 2\n2 4 2", "output": "0" }, { "input": "4 4\n1 2\n1 3\n1 4\n1 2", "output": "-1" }, { "input": "3 1\n2 1 1\n2 1 1\n3 1 1 1", "output": "0" }, { "input": "2 10\n14 9 6 7 1 6 9 3...
77
0
0
13,805
439
Devu and his Brother
[ "binary search", "sortings", "ternary search", "two pointers" ]
null
null
Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays *a* and *b* by their father. The array *a* is given to Devu and *b* to his brother. As Devu is really a naughty kid, he wants the minimum value of his array *a* should be at least as much as the maximum value of his brother's array *b*. Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation on any index of the array multiple times. You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting.
The first line contains two space-separated integers *n*, *m* (1<=≤<=*n*,<=*m*<=≤<=105). The second line will contain *n* space-separated integers representing content of the array *a* (1<=≤<=*a**i*<=≤<=109). The third line will contain *m* space-separated integers representing content of the array *b* (1<=≤<=*b**i*<=≤<=109).
You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.
[ "2 2\n2 3\n3 5\n", "3 2\n1 2 3\n3 4\n", "3 2\n4 5 6\n1 2\n" ]
[ "3\n", "4\n", "0\n" ]
In example 1, you can increase *a*<sub class="lower-index">1</sub> by 1 and decrease *b*<sub class="lower-index">2</sub> by 1 and then again decrease *b*<sub class="lower-index">2</sub> by 1. Now array *a* will be [3; 3] and array *b* will also be [3; 3]. Here minimum element of *a* is at least as large as maximum element of *b*. So minimum number of operations needed to satisfy Devu's condition are 3. In example 3, you don't need to do any operation, Devu's condition is already satisfied.
[ { "input": "2 2\n2 3\n3 5", "output": "3" }, { "input": "3 2\n1 2 3\n3 4", "output": "4" }, { "input": "3 2\n4 5 6\n1 2", "output": "0" }, { "input": "10 10\n23 100 38 38 73 54 59 69 44 86\n100 100 100 100 100 100 100 100 100 100", "output": "416" }, { "input": "1...
140
20,172,800
0
13,814
977
Cyclic Components
[ "dfs and similar", "dsu", "graphs" ]
null
null
You are given an undirected graph consisting of $n$ vertices and $m$ edges. Your task is to find the number of connected components which are cycles. Here are some definitions of graph theory. An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex $a$ is connected with a vertex $b$, a vertex $b$ is also connected with a vertex $a$). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices. Two vertices $u$ and $v$ belong to the same connected component if and only if there is at least one path along edges connecting $u$ and $v$. A connected component is a cycle if and only if its vertices can be reordered in such a way that: - the first vertex is connected with the second vertex by an edge, - the second vertex is connected with the third vertex by an edge, - ... - the last vertex is connected with the first vertex by an edge, - all the described edges of a cycle are distinct. A cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices.
The first line contains two integer numbers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $0 \le m \le 2 \cdot 10^5$) — number of vertices and edges. The following $m$ lines contains edges: edge $i$ is given as a pair of vertices $v_i$, $u_i$ ($1 \le v_i, u_i \le n$, $u_i \ne v_i$). There is no multiple edges in the given graph, i.e. for each pair ($v_i, u_i$) there no other pairs ($v_i, u_i$) and ($u_i, v_i$) in the list of edges.
Print one integer — the number of connected components which are also cycles.
[ "5 4\n1 2\n3 4\n5 4\n3 5\n", "17 15\n1 8\n1 12\n5 11\n11 9\n9 15\n15 5\n4 13\n3 13\n4 3\n10 16\n7 10\n16 7\n14 3\n14 4\n17 6\n" ]
[ "1\n", "2\n" ]
In the first example only component $[3, 4, 5]$ is also a cycle. The illustration above corresponds to the second example.
[ { "input": "5 4\n1 2\n3 4\n5 4\n3 5", "output": "1" }, { "input": "17 15\n1 8\n1 12\n5 11\n11 9\n9 15\n15 5\n4 13\n3 13\n4 3\n10 16\n7 10\n16 7\n14 3\n14 4\n17 6", "output": "2" }, { "input": "4 4\n1 2\n2 3\n1 3\n1 4", "output": "0" }, { "input": "5 5\n1 2\n2 3\n3 4\n4 1\n3 5...
1,887
99,840,000
3
13,815
869
The Intriguing Obsession
[ "combinatorics", "dp", "math" ]
null
null
— This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice itself, Onii-chan! With hands joined, go everywhere at a speed faster than our thoughts! This time, the Fire Sisters — Karen and Tsukihi — is heading for somewhere they've never reached — water-surrounded islands! There are three clusters of islands, conveniently coloured red, blue and purple. The clusters consist of *a*, *b* and *c* distinct islands respectively. Bridges have been built between some (possibly all or none) of the islands. A bridge bidirectionally connects two different islands and has length 1. For any two islands of the same colour, either they shouldn't be reached from each other through bridges, or the shortest distance between them is at least 3, apparently in order to prevent oddities from spreading quickly inside a cluster. The Fire Sisters are ready for the unknown, but they'd also like to test your courage. And you're here to figure out the number of different ways to build all bridges under the constraints, and give the answer modulo 998<=244<=353. Two ways are considered different if a pair of islands exist, such that there's a bridge between them in one of them, but not in the other.
The first and only line of input contains three space-separated integers *a*, *b* and *c* (1<=≤<=*a*,<=*b*,<=*c*<=≤<=5<=000) — the number of islands in the red, blue and purple clusters, respectively.
Output one line containing an integer — the number of different ways to build bridges, modulo 998<=244<=353.
[ "1 1 1\n", "1 2 2\n", "1 3 5\n", "6 2 9\n" ]
[ "8\n", "63\n", "3264\n", "813023575\n" ]
In the first example, there are 3 bridges that can possibly be built, and no setup of bridges violates the restrictions. Thus the answer is 2<sup class="upper-index">3</sup> = 8. In the second example, the upper two structures in the figure below are instances of valid ones, while the lower two are invalid due to the blue and purple clusters, respectively.
[ { "input": "1 1 1", "output": "8" }, { "input": "1 2 2", "output": "63" }, { "input": "1 3 5", "output": "3264" }, { "input": "6 2 9", "output": "813023575" }, { "input": "7 3 7", "output": "807577560" }, { "input": "135 14 39", "output": "41484950...
171
1,536,000
0
13,834
303
Lucky Permutation Triple
[ "constructive algorithms", "implementation", "math" ]
null
null
Bike is interested in permutations. A permutation of length *n* is an integer sequence such that each integer from 0 to (*n*<=-<=1) appears exactly once in it. For example, [0,<=2,<=1] is a permutation of length 3 while both [0,<=2,<=2] and [1,<=2,<=3] is not. A permutation triple of permutations of length *n* (*a*,<=*b*,<=*c*) is called a Lucky Permutation Triple if and only if . The sign *a**i* denotes the *i*-th element of permutation *a*. The modular equality described above denotes that the remainders after dividing *a**i*<=+<=*b**i* by *n* and dividing *c**i* by *n* are equal. Now, he has an integer *n* and wants to find a Lucky Permutation Triple. Could you please help him?
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105).
If no Lucky Permutation Triple of length *n* exists print -1. Otherwise, you need to print three lines. Each line contains *n* space-seperated integers. The first line must contain permutation *a*, the second line — permutation *b*, the third — permutation *c*. If there are multiple solutions, print any of them.
[ "5\n", "2\n" ]
[ "1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3\n", "-1\n" ]
In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a6bf1b9b57809dbec5021f65f89616f259587c07.png" style="max-width: 100.0%;max-height: 100.0%;"/>; - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/48cc13134296b68f459f69d78e0240859aaec702.png" style="max-width: 100.0%;max-height: 100.0%;"/>; - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ac44412de7b46833e90348a6b3298f9796e3977c.png" style="max-width: 100.0%;max-height: 100.0%;"/>; - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3825b0bb758208dda2ead1c5224c05d89ad9ab55.png" style="max-width: 100.0%;max-height: 100.0%;"/>; - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0a72e2da40048a507839927a211267ac01c9bf89.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In Sample 2, you can easily notice that no lucky permutation triple exists.
[ { "input": "5", "output": "1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3" }, { "input": "2", "output": "-1" }, { "input": "8", "output": "-1" }, { "input": "9", "output": "0 1 2 3 4 5 6 7 8 \n0 1 2 3 4 5 6 7 8 \n0 2 4 6 8 1 3 5 7 " }, { "input": "2", "output": "-1" }, {...
748
10,240,000
3
13,879
583
Robot's Task
[ "greedy", "implementation" ]
null
null
Robot Doc is located in the hall, with *n* computers stand in a line, numbered from left to right from 1 to *n*. Each computer contains exactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the *i*-th of them, the robot needs to collect at least *a**i* any pieces of information from the other computers. Doc can hack the computer only if he is right next to it. The robot is assembled using modern technologies and can move along the line of computers in either of the two possible directions, but the change of direction requires a large amount of resources from Doc. Tell the minimum number of changes of direction, which the robot will have to make to collect all *n* parts of information if initially it is next to computer with number 1. It is guaranteed that there exists at least one sequence of the robot's actions, which leads to the collection of all information. Initially Doc doesn't have any pieces of information.
The first line contains number *n* (1<=≤<=*n*<=≤<=1000). The second line contains *n* non-negative integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=&lt;<=*n*), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information.
Print a single number — the minimum number of changes in direction that the robot will have to make in order to collect all *n* parts of information.
[ "3\n0 2 0\n", "5\n4 2 3 0 1\n", "7\n0 3 1 0 5 2 6\n" ]
[ "1\n", "3\n", "2\n" ]
In the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece. In the second sample to collect all the pieces of information in the optimal manner, Doc can go to the fourth computer and get the piece of information, then go to the fifth computer with one piece and get another one, then go to the second computer in the same manner, then to the third one and finally, to the first one. Changes of direction will take place before moving from the fifth to the second computer, then from the second to the third computer, then from the third to the first computer. In the third sample the optimal order of collecting parts from computers can look like that: 1-&gt;3-&gt;4-&gt;6-&gt;2-&gt;5-&gt;7.
[ { "input": "3\n0 2 0", "output": "1" }, { "input": "5\n4 2 3 0 1", "output": "3" }, { "input": "7\n0 3 1 0 5 2 6", "output": "2" }, { "input": "1\n0", "output": "0" }, { "input": "2\n0 1", "output": "0" }, { "input": "10\n0 0 0 0 0 0 0 0 0 0", "out...
264
204,800
3
13,926
68
Energy exchange
[ "binary search" ]
B. Energy exchange
2
256
It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the *i*-th accumulator has *a**i* units of energy. Energy can be transferred from one accumulator to the other. Every time *x* units of energy are transferred (*x* is not necessarily an integer) *k* percent of it is lost. That is, if *x* units were transferred from one accumulator to the other, amount of energy in the first one decreased by *x* units and in other increased by units. Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.
First line of the input contains two integers *n* and *k* (1<=≤<=*n*<=≤<=10000,<=0<=≤<=*k*<=≤<=99) — number of accumulators and the percent of energy that is lost during transfers. Next line contains *n* integers *a*1,<=*a*2,<=... ,<=*a**n* — amounts of energy in the first, second, .., *n*-th accumulator respectively (0<=≤<=*a**i*<=≤<=1000,<=1<=≤<=*i*<=≤<=*n*).
Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy. The absolute or relative error in the answer should not exceed 10<=-<=6.
[ "3 50\n4 2 1\n", "2 90\n1 11\n" ]
[ "2.000000000\n", "1.909090909\n" ]
none
[ { "input": "3 50\n4 2 1", "output": "2.000000000" }, { "input": "2 90\n1 11", "output": "1.909090909" }, { "input": "5 26\n42 65 23 43 64", "output": "45.415178571" }, { "input": "5 45\n964 515 454 623 594", "output": "594.109756098" }, { "input": "1 20\n784", ...
310
22,323,200
3.88092
13,984
958
Encryption (easy)
[ "brute force" ]
null
null
Rebel spy Heidi has just obtained the plans for the Death Star from the Empire and, now on her way to safety, she is trying to break the encryption of the plans (of course they are encrypted – the Empire may be evil, but it is not stupid!). The encryption has several levels of security, and here is how the first one looks. Heidi is presented with a screen that shows her a sequence of integers *A* and a positive integer *p*. She knows that the encryption code is a single number *S*, which is defined as follows: Define the score of *X* to be the sum of the elements of *X* modulo *p*. Heidi is given a sequence *A* that consists of *N* integers, and also given an integer *p*. She needs to split *A* into 2 parts such that: - Each part contains at least 1 element of *A*, and each part consists of contiguous elements of *A*. - The two parts do not overlap. - The total sum *S* of the scores of those two parts is maximized. This is the encryption code. Output the sum *S*, which is the encryption code.
The first line of the input contains two space-separated integer *N* and *p* (2<=≤<=*N*<=≤<=100<=000, 2<=≤<=*p*<=≤<=10<=000) – the number of elements in *A*, and the modulo for computing scores, respectively. The second line contains *N* space-separated integers which are the elements of *A*. Each integer is from the interval [1,<=1<=000<=000].
Output the number *S* as described in the problem statement.
[ "4 10\n3 4 7 2\n", "10 12\n16 3 24 13 9 8 7 5 12 12\n" ]
[ "16\n", "13\n" ]
In the first example, the score is maximized if the input sequence is split into two parts as (3, 4), (7, 2). It gives the total score of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fc1f9aecea625f3be9a56917d3ba448d669ff278.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the second example, the score is maximized if the first part consists of the first three elements, and the second part consists of the rest. Then, the score is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ffd0a0f4c65b0578601ae2fe11a80aec5192f53d.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "4 10\n3 4 7 2", "output": "16" }, { "input": "10 12\n16 3 24 13 9 8 7 5 12 12", "output": "13" }, { "input": "2 2\n9 9", "output": "2" }, { "input": "2 2\n8 8", "output": "0" }, { "input": "5 50\n1 1 1 1 1", "output": "5" }, { "input": "5 5...
1,000
921,600
0
14,012
690
The Wall (easy)
[]
null
null
"The zombies are lurking outside. Waiting. Moaning. And when they come..." "When they come?" "I hope the Wall is high enough." Zombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segments. We call on you for help. Go forth and explore the wall! Report how many disconnected segments there are. The wall is a two-dimensional structure made of bricks. Each brick is one unit wide and one unit high. Bricks are stacked on top of each other to form columns that are up to *R* bricks high. Each brick is placed either on the ground or directly on top of another brick. Consecutive non-empty columns form a wall segment. The entire wall, all the segments and empty columns in-between, is *C* columns wide.
The first line of the input consists of two space-separated integers *R* and *C*, 1<=≤<=*R*,<=*C*<=≤<=100. The next *R* lines provide a description of the columns as follows: - each of the *R* lines contains a string of length *C*, - the *c*-th character of line *r* is B if there is a brick in column *c* and row *R*<=-<=*r*<=+<=1, and . otherwise.
The number of wall segments in the input configuration.
[ "3 7\n.......\n.......\n.BB.B..\n", "4 5\n..B..\n..B..\nB.B.B\nBBB.B\n", "4 6\n..B...\nB.B.BB\nBBB.BB\nBBBBBB\n", "1 1\nB\n", "10 7\n.......\n.......\n.......\n.......\n.......\n.......\n.......\n.......\n...B...\nB.BB.B.\n", "8 8\n........\n........\n........\n........\n.B......\n.B.....B\n.B.....B\n.BB....
[ "2\n", "2\n", "1\n", "1\n", "3\n", "2\n" ]
In the first sample case, the 2nd and 3rd columns define the first wall segment, and the 5th column defines the second.
[ { "input": "3 7\n.......\n.......\n.BB.B..", "output": "2" }, { "input": "4 5\n..B..\n..B..\nB.B.B\nBBB.B", "output": "2" }, { "input": "4 6\n..B...\nB.B.BB\nBBB.BB\nBBBBBB", "output": "1" }, { "input": "1 1\nB", "output": "1" }, { "input": "10 7\n.......\n..........
93
6,758,400
3
14,038
592
Super M
[ "dfs and similar", "dp", "graphs", "trees" ]
null
null
Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ superhero. Byteforces is a country that consists of *n* cities, connected by *n*<=-<=1 bidirectional roads. Every road connects exactly two distinct cities, and the whole road system is designed in a way that one is able to go from any city to any other city using only the given roads. There are *m* cities being attacked by humans. So Ari... we meant Super M have to immediately go to each of the cities being attacked to scare those bad humans. Super M can pass from one city to another only using the given roads. Moreover, passing through one road takes her exactly one kron - the time unit used in Byteforces. However, Super M is not on Byteforces now - she is attending a training camp located in a nearby country Codeforces. Fortunately, there is a special device in Codeforces that allows her to instantly teleport from Codeforces to any city of Byteforces. The way back is too long, so for the purpose of this problem teleportation is used exactly once. You are to help Super M, by calculating the city in which she should teleport at the beginning in order to end her job in the minimum time (measured in krons). Also, provide her with this time so she can plan her way back to Codeforces.
The first line of the input contains two integers *n* and *m* (1<=≤<=*m*<=≤<=*n*<=≤<=123456) - the number of cities in Byteforces, and the number of cities being attacked respectively. Then follow *n*<=-<=1 lines, describing the road system. Each line contains two city numbers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*) - the ends of the road *i*. The last line contains *m* distinct integers - numbers of cities being attacked. These numbers are given in no particular order.
First print the number of the city Super M should teleport to. If there are many possible optimal answers, print the one with the lowest city number. Then print the minimum possible time needed to scare all humans in cities being attacked, measured in Krons. Note that the correct answer is always unique.
[ "7 2\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n2 7\n", "6 4\n1 2\n2 3\n2 4\n4 5\n4 6\n2 4 5 6\n" ]
[ "2\n3\n", "2\n4\n" ]
In the first sample, there are two possibilities to finish the Super M's job in 3 krons. They are: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/93d3c0306b529e9c2324f68158ca2156587473a2.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/df80aa84591eaa7b9f52c88cc43b5f7da5bfead3.png" style="max-width: 100.0%;max-height: 100.0%;"/>. However, you should choose the first one as it starts in the city with the lower number.
[ { "input": "7 2\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n2 7", "output": "2\n3" }, { "input": "6 4\n1 2\n2 3\n2 4\n4 5\n4 6\n2 4 5 6", "output": "2\n4" }, { "input": "2 1\n2 1\n1", "output": "1\n0" }, { "input": "1 1\n1", "output": "1\n0" }, { "input": "10 2\n6 9\n6 2\n1 6\...
1,778
268,390,400
0
14,074
847
Sum of Nestings
[ "constructive algorithms" ]
null
null
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression insering symbols '+' and '1': "((1+1)+(1+1))". Also the following sequences are regular: "()()()", "(())" and "()". The following sequences are not regular bracket sequences: ")(", "(()" and "())(()". In this problem you are given two integers *n* and *k*. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·*n* with total sum of nesting of all opening brackets equals to exactly *k*. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded. For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=3·105, 0<=≤<=*k*<=≤<=1018) — the number of opening brackets and needed total nesting.
Print the required regular bracket sequence consisting of round brackets. If there is no solution print "Impossible" (without quotes).
[ "3 1\n", "4 6\n", "2 5\n" ]
[ "()(())", "(((())))", "Impossible\n" ]
The first example is examined in the statement. In the second example the answer is "(((())))". The nesting of the first opening bracket is 0, the nesting of the second is 1, the nesting of the third is 2, the nesting of fourth is 3. So the total sum of nestings equals to 0 + 1 + 2 + 3 = 6. In the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())".
[ { "input": "3 1", "output": "()(())" }, { "input": "4 6", "output": "(((())))" }, { "input": "2 5", "output": "Impossible" }, { "input": "1 0", "output": "()" }, { "input": "2 0", "output": "()()" }, { "input": "2 1", "output": "(())" }, { ...
2,000
6,451,200
0
14,090
250
Building Bridge
[ "geometry", "ternary search", "two pointers" ]
null
null
Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages. The river banks can be assumed to be vertical straight lines *x*<==<=*a* and *x*<==<=*b* (0<=&lt;<=*a*<=&lt;<=*b*). The west village lies in a steppe at point *O*<==<=(0,<=0). There are *n* pathways leading from the village to the river, they end at points *A**i*<==<=(*a*,<=*y**i*). The villagers there are plain and simple, so their pathways are straight segments as well. The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are *m* twisted paths leading from this village to the river and ending at points *B**i*<==<=(*b*,<=*y*'*i*). The lengths of all these paths are known, the length of the path that leads from the eastern village to point *B**i*, equals *l**i*. The villagers want to choose exactly one point on the left bank of river *A**i*, exactly one point on the right bank *B**j* and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |*OA**i*|<=+<=|*A**i**B**j*|<=+<=*l**j*, where |*XY*| is the Euclidean distance between points *X* and *Y*) were minimum. The Euclidean distance between points (*x*1,<=*y*1) and (*x*2,<=*y*2) equals . Help them and find the required pair of points.
The first line contains integers *n*, *m*, *a*, *b* (1<=≤<=*n*,<=*m*<=≤<=105, 0<=&lt;<=*a*<=&lt;<=*b*<=&lt;<=106). The second line contains *n* integers in the ascending order: the *i*-th integer determines the coordinate of point *A**i* and equals *y**i* (|*y**i*|<=≤<=106). The third line contains *m* integers in the ascending order: the *i*-th integer determines the coordinate of point *B**i* and equals *y*'*i* (|*y*'*i*|<=≤<=106). The fourth line contains *m* more integers: the *i*-th of them determines the length of the path that connects the eastern village and point *B**i*, and equals *l**i* (1<=≤<=*l**i*<=≤<=106). It is guaranteed, that there is such a point *C* with abscissa at least *b*, that |*B**i**C*|<=≤<=*l**i* for all *i* (1<=≤<=*i*<=≤<=*m*). It is guaranteed that no two points *A**i* coincide. It is guaranteed that no two points *B**i* coincide.
Print two integers — the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to *n*, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to *m* in the order in which they are given in the input. If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10<=-<=6 in absolute or relative value.
[ "3 2 3 5\n-2 -1 4\n-1 2\n7 3\n" ]
[ "2 2" ]
none
[ { "input": "3 2 3 5\n-2 -1 4\n-1 2\n7 3", "output": "2 2" }, { "input": "1 1 10 20\n5\n-5\n1", "output": "1 1" }, { "input": "2 2 1 2\n-1 10\n8 9\n3 7", "output": "1 1" }, { "input": "10 20 50 60\n-96 -75 32 37 42 43 44 57 61 65\n-95 -90 -86 -79 -65 -62 -47 -11 -8 -6 1 8 23 2...
78
307,200
0
14,114
798
Mike and gcd problem
[ "dp", "greedy", "number theory" ]
null
null
Mike has a sequence *A*<==<=[*a*1,<=*a*2,<=...,<=*a**n*] of length *n*. He considers the sequence *B*<==<=[*b*1,<=*b*2,<=...,<=*b**n*] beautiful if the *gcd* of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index *i* (1<=≤<=*i*<=&lt;<=*n*), delete numbers *a**i*,<=*a**i*<=+<=1 and put numbers *a**i*<=-<=*a**i*<=+<=1,<=*a**i*<=+<=*a**i*<=+<=1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence *A* beautiful if it's possible, or tell him that it is impossible to do so. is the biggest non-negative number *d* such that *d* divides *b**i* for every *i* (1<=≤<=*i*<=≤<=*n*).
The first line contains a single integer *n* (2<=≤<=*n*<=≤<=100<=000) — length of sequence *A*. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — elements of sequence *A*.
Output on the first line "YES" (without quotes) if it is possible to make sequence *A* beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence *A* beautiful.
[ "2\n1 1\n", "3\n6 2 4\n", "2\n1 3\n" ]
[ "YES\n1\n", "YES\n0\n", "YES\n1\n" ]
In the first example you can simply make one move to obtain sequence [0, 2] with <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/6ddb490b8e898198d30c56c6edce2cf15e6f998a.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the second example the *gcd* of the sequence is already greater than 1.
[ { "input": "2\n1 1", "output": "YES\n1" }, { "input": "3\n6 2 4", "output": "YES\n0" }, { "input": "2\n1 3", "output": "YES\n1" }, { "input": "9\n57 30 28 81 88 32 3 42 25", "output": "YES\n8" }, { "input": "2\n8 77", "output": "YES\n2" }, { "input": "...
46
0
0
14,145
522
Chicken or Fish?
[ "greedy" ]
null
null
Polycarp is flying in the airplane. Finally, it is his favorite time — the lunchtime. The BerAvia company stewardess is giving food consecutively to all the passengers from the 1-th one to the last one. Polycarp is sitting on seat *m*, that means, he will be the *m*-th person to get food. The flight menu has *k* dishes in total and when Polycarp boarded the flight, he had time to count the number of portions of each dish on board. Thus, he knows values *a*1,<=*a*2,<=...,<=*a**k*, where *a**i* is the number of portions of the *i*-th dish. The stewardess has already given food to *m*<=-<=1 passengers, gave Polycarp a polite smile and asked him what he would prefer. That's when Polycarp realized that they might have run out of some dishes by that moment. For some of the *m*<=-<=1 passengers ahead of him, he noticed what dishes they were given. Besides, he's heard some strange mumbling from some of the *m*<=-<=1 passengers ahead of him, similar to phrase 'I'm disappointed'. That happened when a passenger asked for some dish but the stewardess gave him a polite smile and said that they had run out of that dish. In that case the passenger needed to choose some other dish that was available. If Polycarp heard no more sounds from a passenger, that meant that the passenger chose his dish at the first try. Help Polycarp to find out for each dish: whether they could have run out of the dish by the moment Polyarp was served or that dish was definitely available.
Each test in this problem consists of one or more input sets. First goes a string that contains a single integer *t* (1<=≤<=*t*<=≤<=100<=000) — the number of input data sets in the test. Then the sets follow, each set is preceded by an empty line. The first line of each set of the input contains integers *m*, *k* (2<=≤<=*m*<=≤<=100<=000, 1<=≤<=*k*<=≤<=100<=000) — the number of Polycarp's seat and the number of dishes, respectively. The second line contains a sequence of *k* integers *a*1,<=*a*2,<=...,<=*a**k* (1<=≤<=*a**i*<=≤<=100<=000), where *a**i* is the initial number of portions of the *i*-th dish. Then *m*<=-<=1 lines follow, each line contains the description of Polycarp's observations about giving food to a passenger sitting in front of him: the *j*-th line contains a pair of integers *t**j*,<=*r**j* (0<=≤<=*t**j*<=≤<=*k*,<=0<=≤<=*r**j*<=≤<=1), where *t**j* is the number of the dish that was given to the *j*-th passenger (or 0, if Polycarp didn't notice what dish was given to the passenger), and *r**j* — a 1 or a 0, depending on whether the *j*-th passenger was or wasn't disappointed, respectively. We know that sum *a**i* equals at least *m*, that is,Polycarp will definitely get some dish, even if it is the last thing he wanted. It is guaranteed that the data is consistent. Sum *m* for all input sets doesn't exceed 100<=000. Sum *k* for all input sets doesn't exceed 100<=000.
For each input set print the answer as a single line. Print a string of *k* letters "Y" or "N". Letter "Y" in position *i* should be printed if they could have run out of the *i*-th dish by the time the stewardess started serving Polycarp.
[ "2\n\n3 4\n2 3 2 1\n1 0\n0 0\n\n5 5\n1 2 1 3 1\n3 0\n0 0\n2 1\n4 0\n" ]
[ "YNNY\nYYYNY\n" ]
In the first input set depending on the choice of the second passenger the situation could develop in different ways: - If he chose the first dish, then by the moment the stewardess reaches Polycarp, they will have run out of the first dish; - If he chose the fourth dish, then by the moment the stewardess reaches Polycarp, they will have run out of the fourth dish; - Otherwise, Polycarp will be able to choose from any of the four dishes. Thus, the answer is "YNNY". In the second input set there is, for example, the following possible scenario. First, the first passenger takes the only third dish, then the second passenger takes the second dish. Then, the third passenger asks for the third dish, but it is not available, so he makes disappointed muttering and ends up with the second dish. Then the fourth passenger takes the fourth dish, and Polycarp ends up with the choice between the first, fourth and fifth dish. Likewise, another possible scenario is when by the time the stewardess comes to Polycarp, they will have run out of either the first or the fifth dish (this can happen if one of these dishes is taken by the second passenger). It is easy to see that there is more than enough of the fourth dish, so Polycarp can always count on it. Thus, the answer is "YYYNY".
[ { "input": "2\n\n3 4\n2 3 2 1\n1 0\n0 0\n\n5 5\n1 2 1 3 1\n3 0\n0 0\n2 1\n4 0", "output": "YNNY\nYYYNY" }, { "input": "4\n\n2 1\n42\n0 0\n\n2 1\n2\n0 0\n\n2 1\n42\n1 0\n\n2 1\n2\n1 0", "output": "N\nN\nN\nN" }, { "input": "5\n\n3 3\n1 1 1\n0 0\n0 1\n\n3 3\n1 1 1\n1 0\n2 1\n\n3 3\n1 1 1\n...
61
5,632,000
-1
14,174
246
Colorful Graph
[ "brute force", "dfs and similar", "graphs" ]
null
null
You've got an undirected graph, consisting of *n* vertices and *m* edges. We will consider the graph's vertices numbered with integers from 1 to *n*. Each vertex of the graph has a color. The color of the *i*-th vertex is an integer *c**i*. Let's consider all vertices of the graph, that are painted some color *k*. Let's denote a set of such as *V*(*k*). Let's denote the value of the neighbouring color diversity for color *k* as the cardinality of the set *Q*(*k*)<==<={*c**u* :<= *c**u*<=≠<=*k* and there is vertex *v* belonging to set *V*(*k*) such that nodes *v* and *u* are connected by an edge of the graph}. Your task is to find such color *k*, which makes the cardinality of set *Q*(*k*) maximum. In other words, you want to find the color that has the most diverse neighbours. Please note, that you want to find such color *k*, that the graph has at least one vertex with such color.
The first line contains two space-separated integers *n*,<=*m* (1<=≤<=*n*,<=*m*<=≤<=105) — the number of vertices end edges of the graph, correspondingly. The second line contains a sequence of integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≤<=*c**i*<=≤<=105) — the colors of the graph vertices. The numbers on the line are separated by spaces. Next *m* lines contain the description of the edges: the *i*-th line contains two space-separated integers *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*) — the numbers of the vertices, connected by the *i*-th edge. It is guaranteed that the given graph has no self-loops or multiple edges.
Print the number of the color which has the set of neighbours with the maximum cardinality. It there are multiple optimal colors, print the color with the minimum number. Please note, that you want to find such color, that the graph has at least one vertex with such color.
[ "6 6\n1 1 2 3 5 8\n1 2\n3 2\n1 4\n4 3\n4 5\n4 6\n", "5 6\n4 2 5 2 4\n1 2\n2 3\n3 1\n5 3\n5 4\n3 4\n" ]
[ "3\n", "2\n" ]
none
[ { "input": "6 6\n1 1 2 3 5 8\n1 2\n3 2\n1 4\n4 3\n4 5\n4 6", "output": "3" }, { "input": "5 6\n4 2 5 2 4\n1 2\n2 3\n3 1\n5 3\n5 4\n3 4", "output": "2" }, { "input": "3 1\n13 13 4\n1 2", "output": "4" }, { "input": "2 1\n500 300\n1 2", "output": "300" }, { "input":...
218
5,632,000
0
14,187
28
DravDe saves the world
[ "geometry", "math" ]
E. DravDe saves the world
1
256
How horrible! The empire of galactic chickens tries to conquer a beautiful city "Z", they have built a huge incubator that produces millions of chicken soldiers a day, and fenced it around. The huge incubator looks like a polygon on the plane *Oxy* with *n* vertices. Naturally, DravDe can't keep still, he wants to destroy the chicken empire. For sure, he will start with the incubator. DravDe is strictly outside the incubator's territory in point *A*(*x**a*,<=*y**a*), and wants to get inside and kill all the chickens working there. But it takes a lot of doing! The problem is that recently DravDe went roller skating and has broken both his legs. He will get to the incubator's territory in his jet airplane LEVAP-41. LEVAP-41 flies at speed *V*(*x**v*,<=*y**v*,<=*z**v*). DravDe can get on the plane in point *A*, fly for some time, and then air drop himself. DravDe is very heavy, that's why he falls vertically at speed *F**down*, but in each point of his free fall DravDe can open his parachute, and from that moment he starts to fall at the wind speed *U*(*x**u*,<=*y**u*,<=*z**u*) until he lands. Unfortunately, DravDe isn't good at mathematics. Would you help poor world's saviour find such an air dropping plan, that allows him to land on the incubator's territory? If the answer is not unique, DravDe wants to find the plan with the minimum time of his flight on the plane. If the answers are still multiple, he wants to find the one with the minimum time of his free fall before opening his parachute
The first line contains the number *n* (3<=≤<=*n*<=≤<=104) — the amount of vertices of the fence. Then there follow *n* lines containing the coordinates of these vertices (two integer numbers *x**i*,<=*y**i*) in clockwise or counter-clockwise order. It's guaranteed, that the fence does not contain self-intersections. The following four lines contain coordinates of point *A*(*x**a*,<=*y**a*), speeds *V*(*x**v*,<=*y**v*,<=*z**v*), *F**down* and speed *U*(*x**u*,<=*y**u*,<=*z**u*). All the input numbers are integer. All the coordinates don't exceed 104 in absolute value. It's guaranteed, that *z**v*<=&gt;<=0 and *F**down*,<=*z**u*<=&lt;<=0, and point *A* is strictly outside the incubator's territory.
In the first line output two numbers *t*1,<=*t*2 such, that if DravDe air drops at time *t*1 (counting from the beginning of the flight), he lands on the incubator's territory (landing on the border is regarder as landing on the territory). If DravDe doesn't open his parachute, the second number should be equal to the duration of DravDe's falling down. If it's impossible for DravDe to get to the incubator's territory, output -1 -1. If the answer is not unique, output the answer with the minimum *t*1. If the answers are still multiple, output the answer with the minimum *t*2. Your answer must have an absolute or relative error less than 10<=-<=6.
[ "4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 0 1\n-1\n0 1 -1\n", "4\n0 0\n0 1\n1 1\n1 0\n0 -1\n-1 -1 1\n-1\n0 1 -1\n", "4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 1 1\n-1\n1 1 -1\n" ]
[ "1.00000000 0.00000000\n", "-1.00000000 -1.00000000\n", "0.50000000 0.00000000\n" ]
none
[ { "input": "4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 0 1\n-1\n0 1 -1", "output": "1.00000000 0.00000000" }, { "input": "4\n0 0\n0 1\n1 1\n1 0\n0 -1\n-1 -1 1\n-1\n0 1 -1", "output": "-1.00000000 -1.00000000" }, { "input": "4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 1 1\n-1\n1 1 -1", "output": "0.50000000 0...
30
0
0
14,207
400
Inna and Huge Candy Matrix
[ "implementation", "math" ]
null
null
Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to *n* from top to bottom and the columns — from 1 to *m*, from left to right. We'll represent the cell on the intersection of the *i*-th row and *j*-th column as (*i*,<=*j*). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has *p* candies: the *k*-th candy is at cell (*x**k*,<=*y**k*). The time moved closer to dinner and Inna was already going to eat *p* of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix *x* times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix *y* times. And then he rotated the matrix *z* times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!
The first line of the input contains fix integers *n*, *m*, *x*, *y*, *z*, *p* (1<=≤<=*n*,<=*m*<=≤<=109; 0<=≤<=*x*,<=*y*,<=*z*<=≤<=109; 1<=≤<=*p*<=≤<=105). Each of the following *p* lines contains two integers *x**k*, *y**k* (1<=≤<=*x**k*<=≤<=*n*; 1<=≤<=*y**k*<=≤<=*m*) — the initial coordinates of the *k*-th candy. Two candies can lie on the same cell.
For each of the *p* candies, print on a single line its space-separated new coordinates.
[ "3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3\n" ]
[ "1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1\n" ]
Just for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix:
[ { "input": "3 3 3 1 1 9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3", "output": "1 3\n1 2\n1 1\n2 3\n2 2\n2 1\n3 3\n3 2\n3 1" }, { "input": "5 5 0 0 0 1\n1 4", "output": "1 4" }, { "input": "14 76 376219315 550904689 16684615 24\n11 21\n1 65\n5 25\n14 63\n11 30\n1 19\n5 7\n9 51\n2 49\n1...
140
0
0
14,254
57
Array
[ "combinatorics", "math" ]
C. Array
2
256
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of *n*, containing only integers from 1 to *n*. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions: - each elements, starting from the second one, is no more than the preceding one - each element, starting from the second one, is no less than the preceding one Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
The single line contains an integer *n* which is the size of the array (1<=≤<=*n*<=≤<=105).
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
[ "2\n", "3\n" ]
[ "4\n", "17\n" ]
none
[ { "input": "2", "output": "4" }, { "input": "3", "output": "17" }, { "input": "12", "output": "2704144" }, { "input": "19", "output": "345263536" }, { "input": "20", "output": "846527841" }, { "input": "26", "output": "529476652" }, { "inpu...
1,838
0
3.5405
14,261
491
Deciphering
[ "flows", "graph matchings" ]
null
null
One day Maria Ivanovna found a Sasha's piece of paper with a message dedicated to Olya. Maria Ivanovna wants to know what is there in a message, but unfortunately the message is ciphered. Maria Ivanovna knows that her students usually cipher their messages by replacing each letter of an original message by some another letter. Replacement works in such way that same letters are always replaced with some fixed letter, and different letters are always replaced by different letters. Maria Ivanovna supposed that the message contains answers to the final exam (since its length is equal to the number of final exam questions). On the other hand she knows that Sasha's answer are not necessary correct. There are *K* possible answers for each questions. Of course, Maria Ivanovna knows correct answers. Maria Ivanovna decided to decipher message in such way that the number of Sasha's correct answers is maximum possible. She is very busy now, so your task is to help her.
First line contains length of both strings *N* (1<=≤<=*N*<=≤<=2<=000<=000) and an integer *K* — number of possible answers for each of the questions (1<=≤<=*K*<=≤<=52). Answers to the questions are denoted as Latin letters abcde...xyzABCDE...XYZ in the order. For example for *K*<==<=6, possible answers are abcdef and for *K*<==<=30 possible answers are abcde...xyzABCD. Second line contains a ciphered message string consisting of Latin letters. Third line contains a correct answers string consisting of Latin letters.
In the first line output maximum possible number of correct Sasha's answers. In the second line output cipher rule as the string of length *K* where for each letter from the students' cipher (starting from 'a' as mentioned above) there is specified which answer does it correspond to. If there are several ways to produce maximum answer, output any of them.
[ "10 2\naaabbbaaab\nbbbbabbbbb\n", "10 2\naaaaaaabbb\nbbbbaaabbb\n", "9 4\ndacbdacbd\nacbdacbda\n" ]
[ "7\nba\n", "6\nab\n", "9\ncdba\n" ]
none
[]
30
0
0
14,296
158
Cd and pwd commands
[ "*special", "data structures", "implementation" ]
null
null
Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory). Directories in Vasya's operating system form a traditional hierarchical tree structure. There is a single root directory, denoted by the slash character "/". Every other directory has a name — a non-empty string consisting of lowercase Latin letters. Each directory (except for the root) has a parent directory — the one that contains the given directory. It is denoted as "..". The command cd takes a single parameter, which is a path in the file system. The command changes the current directory to the directory specified by the path. The path consists of the names of directories separated by slashes. The name of the directory can be "..", which means a step up to the parent directory. «..» can be used in any place of the path, maybe several times. If the path begins with a slash, it is considered to be an absolute path, that is, the directory changes to the specified one, starting from the root. If the parameter begins with a directory name (or ".."), it is considered to be a relative path, that is, the directory changes to the specified directory, starting from the current one. The command pwd should display the absolute path to the current directory. This path must not contain "..". Initially, the current directory is the root. All directories mentioned explicitly or passed indirectly within any command cd are considered to exist. It is guaranteed that there is no attempt of transition to the parent directory of the root directory.
The first line of the input data contains the single integer *n* (1<=≤<=*n*<=≤<=50) — the number of commands. Then follow *n* lines, each contains one command. Each of these lines contains either command pwd, or command cd, followed by a space-separated non-empty parameter. The command parameter cd only contains lower case Latin letters, slashes and dots, two slashes cannot go consecutively, dots occur only as the name of a parent pseudo-directory. The command parameter cd does not end with a slash, except when it is the only symbol that points to the root directory. The command parameter has a length from 1 to 200 characters, inclusive. Directories in the file system can have the same names.
For each command pwd you should print the full absolute path of the given directory, ending with a slash. It should start with a slash and contain the list of slash-separated directories in the order of being nested from the root to the current folder. It should contain no dots.
[ "7\npwd\ncd /home/vasya\npwd\ncd ..\npwd\ncd vasya/../petya\npwd\n", "4\ncd /a/b\npwd\ncd ../a/b\npwd\n" ]
[ "/\n/home/vasya/\n/home/\n/home/petya/\n", "/a/b/\n/a/a/b/\n" ]
none
[ { "input": "7\npwd\ncd /home/vasya\npwd\ncd ..\npwd\ncd vasya/../petya\npwd", "output": "/\n/home/vasya/\n/home/\n/home/petya/" }, { "input": "4\ncd /a/b\npwd\ncd ../a/b\npwd", "output": "/a/b/\n/a/a/b/" }, { "input": "1\npwd", "output": "/" }, { "input": "2\ncd /test/../test...
92
0
0
14,309
26
Parquet
[ "combinatorics", "constructive algorithms", "greedy", "implementation" ]
C. Parquet
2
256
Once Bob decided to lay a parquet floor in his living room. The living room is of size *n*<=×<=*m* metres. Bob had planks of three types: *a* planks 1<=×<=2 meters, *b* planks 2<=×<=1 meters, and *c* planks 2<=×<=2 meters. Help Bob find out, if it is possible to parquet the living room with such a set of planks, and if it is possible, find one of the possible ways to do so. Bob doesn't have to use all the planks.
The first input line contains 5 space-separated integer numbers *n*, *m*, *a*, *b*, *c* (1<=≤<=*n*,<=*m*<=≤<=100,<=0<=≤<=*a*,<=*b*,<=*c*<=≤<=104), *n* and *m* — the living room dimensions, *a*, *b* and *c* — amount of planks 1<=×<=2, 2<=×<=1 и 2<=×<=2 respectively. It's not allowed to turn the planks.
If it is not possible to parquet the room with such a set of planks, output IMPOSSIBLE. Otherwise output one of the possible ways to parquet the room — output *n* lines with *m* lower-case Latin letters each. Two squares with common sides should contain the same letters, if they belong to one and the same plank, and different letters otherwise. Different planks can be marked with one and the same letter (see examples). If the answer is not unique, output any.
[ "2 6 2 2 1\n", "1 1 100 100 100\n", "4 4 10 10 10\n" ]
[ "aabcca\naabdda\n", "IMPOSSIBLE\n", "aabb\naabb\nbbaa\nbbaa\n" ]
none
[ { "input": "2 6 2 2 1", "output": "aaccab\naaddab" }, { "input": "1 1 100 100 100", "output": "IMPOSSIBLE" }, { "input": "4 4 10 10 10", "output": "aacc\naacc\nccaa\nccaa" }, { "input": "2 2 0 0 1", "output": "aa\naa" }, { "input": "2 3 2 1 0", "output": "aac\...
310
1,126,400
3.920402
14,347
0
none
[ "none" ]
null
null
Olya wants to buy a custom wardrobe. It should have *n* boxes with heights *a*1,<=*a*2,<=...,<=*a**n*, stacked one on another in some order. In other words, we can represent each box as a vertical segment of length *a**i*, and all these segments should form a single segment from 0 to without any overlaps. Some of the boxes are important (in this case *b**i*<==<=1), others are not (then *b**i*<==<=0). Olya defines the convenience of the wardrobe as the number of important boxes such that their bottom edge is located between the heights *l* and *r*, inclusive. You are given information about heights of the boxes and their importance. Compute the maximum possible convenience of the wardrobe if you can reorder the boxes arbitrarily.
The first line contains three integers *n*, *l* and *r* (1<=≤<=*n*<=≤<=10<=000, 0<=≤<=*l*<=≤<=*r*<=≤<=10<=000) — the number of boxes, the lowest and the highest heights for a bottom edge of an important box to be counted in convenience. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=10<=000) — the heights of the boxes. It is guaranteed that the sum of height of all boxes (i. e. the height of the wardrobe) does not exceed 10<=000: Olya is not very tall and will not be able to reach any higher. The second line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (0<=≤<=*b**i*<=≤<=1), where *b**i* equals 1 if the *i*-th box is important, and 0 otherwise.
Print a single integer — the maximum possible convenience of the wardrobe.
[ "5 3 6\n3 2 5 1 2\n1 1 0 1 0\n", "2 2 5\n3 6\n1 1\n" ]
[ "2\n", "1\n" ]
In the first example you can, for example, first put an unimportant box of height 2, then put an important boxes of sizes 1, 3 and 2, in this order, and then the remaining unimportant boxes. The convenience is equal to 2, because the bottom edges of important boxes of sizes 3 and 2 fall into the range [3, 6]. In the second example you have to put the short box under the tall box.
[]
46
0
0
14,396
356
Xenia and Hamming
[ "implementation", "math" ]
null
null
Xenia is an amateur programmer. Today on the IT lesson she learned about the Hamming distance. The Hamming distance between two strings *s*<==<=*s*1*s*2... *s**n* and *t*<==<=*t*1*t*2... *t**n* of equal length *n* is value . Record [*s**i*<=≠<=*t**i*] is the Iverson notation and represents the following: if *s**i*<=≠<=*t**i*, it is one, otherwise — zero. Now Xenia wants to calculate the Hamming distance between two long strings *a* and *b*. The first string *a* is the concatenation of *n* copies of string *x*, that is, . The second string *b* is the concatenation of *m* copies of string *y*. Help Xenia, calculate the required Hamming distance, given *n*,<=*x*,<=*m*,<=*y*.
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1012). The second line contains a non-empty string *x*. The third line contains a non-empty string *y*. Both strings consist of at most 106 lowercase English letters. It is guaranteed that strings *a* and *b* that you obtain from the input have the same length.
Print a single integer — the required Hamming distance. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "100 10\na\naaaaaaaaaa\n", "1 1\nabacaba\nabzczzz\n", "2 3\nrzr\naz\n" ]
[ "0\n", "4\n", "5\n" ]
In the first test case string *a* is the same as string *b* and equals 100 letters a. As both strings are equal, the Hamming distance between them is zero. In the second test case strings *a* and *b* differ in their 3-rd, 5-th, 6-th and 7-th characters. Thus, the Hamming distance equals 4. In the third test case string *a* is rzrrzr and string *b* is azazaz. The strings differ in all characters apart for the second one, the Hamming distance between them equals 5.
[]
46
204,800
-1
14,418
33
String Problem
[ "shortest paths" ]
B. String Problem
2
256
Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character *A**i* in one of the strings into arbitrary character *B**i*, but he has to pay for every move a particular sum of money, equal to *W**i*. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings.
The first input line contains two initial non-empty strings *s* and *t*, consisting of lower case Latin letters. The length of each string doesn't exceed 105. The following line contains integer *n* (0<=≤<=*n*<=≤<=500) — amount of possible changings. Then follow *n* lines, each containing characters *A**i* and *B**i* (lower case Latin letters) and integer *W**i* (0<=≤<=*W**i*<=≤<=100), saying that it's allowed to change character *A**i* into character *B**i* in any of the strings and spend sum of money *W**i*.
If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any.
[ "uayd\nuxxd\n3\na x 8\nx y 13\nd c 3\n", "a\nb\n3\na b 2\na b 3\nb a 5\n", "abc\nab\n6\na b 4\na b 7\nb a 8\nc b 11\nc a 3\na c 0\n" ]
[ "21\nuxyd\n", "2\nb\n", "-1\n" ]
none
[ { "input": "uayd\nuxxd\n3\na x 8\nx y 13\nd c 3", "output": "21\nuxyd" }, { "input": "a\nb\n3\na b 2\na b 3\nb a 5", "output": "2\nb" }, { "input": "abc\nab\n6\na b 4\na b 7\nb a 8\nc b 11\nc a 3\na c 0", "output": "-1" }, { "input": "xhtuopq\nrtutbz\n10\nh x 10\nx d 3\nr u 4...
154
2,457,600
0
14,455
490
Treeland Tour
[ "data structures", "dfs and similar", "dp", "trees" ]
null
null
The "Road Accident" band is planning an unprecedented tour around Treeland. The RA fans are looking forward to the event and making bets on how many concerts their favorite group will have. Treeland consists of *n* cities, some pairs of cities are connected by bidirectional roads. Overall the country has *n*<=-<=1 roads. We know that it is possible to get to any city from any other one. The cities are numbered by integers from 1 to *n*. For every city we know its value *r**i* — the number of people in it. We know that the band will travel along some path, having concerts in some cities along the path. The band's path will not pass one city twice, each time they move to the city that hasn't been previously visited. Thus, the musicians will travel along some path (without visiting any city twice) and in some (not necessarily all) cities along the way they will have concerts. The band plans to gather all the big stadiums and concert halls during the tour, so every time they will perform in a city which population is larger than the population of the previously visited with concert city. In other words, the sequence of population in the cities where the concerts will be held is strictly increasing. In a recent interview with the leader of the "road accident" band promised to the fans that the band will give concert in the largest possible number of cities! Thus the band will travel along some chain of cities of Treeland and have concerts in some of these cities, so that the population number will increase, and the number of concerts will be the largest possible. The fans of Treeland are frantically trying to figure out how many concerts the group will have in Treeland. Looks like they can't manage without some help from a real programmer! Help the fans find the sought number of concerts.
The first line of the input contains integer *n* (2<=≤<=*n*<=≤<=6000) — the number of cities in Treeland. The next line contains *n* integers *r*1,<=*r*2,<=...,<=*r**n* (1<=≤<=*r**i*<=≤<=106), where *r**i* is the population of the *i*-th city. The next *n*<=-<=1 lines contain the descriptions of the roads, one road per line. Each road is defined by a pair of integers *a**j*, *b**j* (1<=≤<=*a**j*,<=*b**j*<=≤<=*n*) — the pair of the numbers of the cities that are connected by the *j*-th road. All numbers in the lines are separated by spaces.
Print the number of cities where the "Road Accident" band will have concerts.
[ "6\n1 2 3 4 5 1\n1 2\n2 3\n3 4\n3 5\n3 6\n", "5\n1 2 3 4 5\n1 2\n1 3\n2 4\n3 5\n" ]
[ "4\n", "3\n" ]
none
[]
46
0
0
14,470
630
Cracking the Code
[ "implementation", "math" ]
null
null
The protection of a popular program developed by one of IT City companies is organized the following way. After installation it outputs a random five digit number which should be sent in SMS to a particular phone number. In response an SMS activation code arrives. A young hacker Vasya disassembled the program and found the algorithm that transforms the shown number into the activation code. Note: it is clear that Vasya is a law-abiding hacker, and made it for a noble purpose — to show the developer the imperfection of their protection. The found algorithm looks the following way. At first the digits of the number are shuffled in the following order &lt;first digit&gt;&lt;third digit&gt;&lt;fifth digit&gt;&lt;fourth digit&gt;&lt;second digit&gt;. For example the shuffle of 12345 should lead to 13542. On the second stage the number is raised to the fifth power. The result of the shuffle and exponentiation of the number 12345 is 455 422 043 125 550 171 232. The answer is the 5 last digits of this result. For the number 12345 the answer should be 71232. Vasya is going to write a keygen program implementing this algorithm. Can you do the same?
The only line of the input contains a positive integer five digit number for which the activation code should be found.
Output exactly 5 digits without spaces between them — the found activation code of the program.
[ "12345\n" ]
[ "71232" ]
none
[ { "input": "12345", "output": "71232" }, { "input": "13542", "output": "84443" }, { "input": "71232", "output": "10151" }, { "input": "11111", "output": "36551" }, { "input": "10000", "output": "00000" }, { "input": "99999", "output": "99999" }, ...
46
0
3
14,476
799
Fountains
[ "binary search", "data structures", "implementation" ]
null
null
Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are *n* available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cost can be either in coins or diamonds. No money changes between the types are allowed. Help Arkady to find two fountains with maximum total beauty so that he can buy both at the same time.
The first line contains three integers *n*, *c* and *d* (2<=≤<=*n*<=≤<=100<=000, 0<=≤<=*c*,<=*d*<=≤<=100<=000) — the number of fountains, the number of coins and diamonds Arkady has. The next *n* lines describe fountains. Each of these lines contain two integers *b**i* and *p**i* (1<=≤<=*b**i*,<=*p**i*<=≤<=100<=000) — the beauty and the cost of the *i*-th fountain, and then a letter "C" or "D", describing in which type of money is the cost of fountain *i*: in coins or in diamonds, respectively.
Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.
[ "3 7 6\n10 8 C\n4 3 C\n5 6 D\n", "2 4 5\n2 5 C\n2 1 D\n", "3 10 10\n5 5 C\n5 5 C\n10 11 D\n" ]
[ "9\n", "0\n", "10\n" ]
In the first example Arkady should build the second fountain with beauty 4, which costs 3 coins. The first fountain he can't build because he don't have enough coins. Also Arkady should build the third fountain with beauty 5 which costs 6 diamonds. Thus the total beauty of built fountains is 9. In the second example there are two fountains, but Arkady can't build both of them, because he needs 5 coins for the first fountain, and Arkady has only 4 coins.
[ { "input": "3 7 6\n10 8 C\n4 3 C\n5 6 D", "output": "9" }, { "input": "2 4 5\n2 5 C\n2 1 D", "output": "0" }, { "input": "3 10 10\n5 5 C\n5 5 C\n10 11 D", "output": "10" }, { "input": "6 68 40\n1 18 D\n6 16 D\n11 16 D\n7 23 D\n16 30 D\n2 20 D", "output": "18" }, { ...
61
7,168,000
0
14,506
295
Yaroslav and Points
[ "data structures" ]
null
null
Yaroslav has *n* points that lie on the *Ox* axis. The coordinate of the first point is *x*1, the coordinate of the second point is *x*2, ..., the coordinate of the *n*-th point is — *x**n*. Now Yaroslav wants to execute *m* queries, each of them is of one of the two following types: 1. Move the *p**j*-th point from position *x**p**j* to position *x**p**j*<=+<=*d**j*. At that, it is guaranteed that after executing such query all coordinates of the points will be distinct. 1. Count the sum of distances between all pairs of points that lie on the segment [*l**j*,<=*r**j*] (*l**j*<=≤<=*r**j*). In other words, you should count the sum of: . Help Yaroslav.
The first line contains integer *n* — the number of points (1<=≤<=*n*<=≤<=105). The second line contains distinct integers *x*1,<=*x*2,<=...,<=*x**n* — the coordinates of points (|*x**i*|<=≤<=109). The third line contains integer *m* — the number of queries (1<=≤<=*m*<=≤<=105). The next *m* lines contain the queries. The *j*-th line first contains integer *t**j* (1<=≤<=*t**j*<=≤<=2) — the query type. If *t**j*<==<=1, then it is followed by two integers *p**j* and *d**j* (1<=≤<=*p**j*<=≤<=*n*,<=|*d**j*|<=≤<=1000). If *t**j*<==<=2, then it is followed by two integers *l**j* and *r**j* (<=-<=109<=≤<=*l**j*<=≤<=*r**j*<=≤<=109). It is guaranteed that at any moment all the points have distinct coordinates.
For each type 2 query print the answer on a single line. Print the answers in the order, in which the queries follow in the input. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
[ "8\n36 50 28 -75 40 -60 -95 -48\n20\n2 -61 29\n1 5 -53\n1 1 429\n1 5 130\n2 -101 -71\n2 -69 53\n1 1 404\n1 5 518\n2 -101 53\n2 50 872\n1 1 -207\n2 -99 -40\n1 7 -389\n1 6 -171\n1 2 464\n1 7 -707\n1 1 -730\n1 1 560\n2 635 644\n1 7 -677\n" ]
[ "176\n20\n406\n1046\n1638\n156\n0\n" ]
none
[]
60
0
0
14,509
8
Looking for Order
[ "bitmasks", "dp" ]
C. Looking for Order
4
512
Girl Lena likes it when everything is in order, and looks for order everywhere. Once she was getting ready for the University and noticed that the room was in a mess — all the objects from her handbag were thrown about the room. Of course, she wanted to put them back into her handbag. The problem is that the girl cannot carry more than two objects at a time, and cannot move the handbag. Also, if he has taken an object, she cannot put it anywhere except her handbag — her inherent sense of order does not let her do so. You are given the coordinates of the handbag and the coordinates of the objects in some Сartesian coordinate system. It is known that the girl covers the distance between any two objects in the time equal to the squared length of the segment between the points of the objects. It is also known that initially the coordinates of the girl and the handbag are the same. You are asked to find such an order of actions, that the girl can put all the objects back into her handbag in a minimum time period.
The first line of the input file contains the handbag's coordinates *x**s*,<=*y**s*. The second line contains number *n* (1<=≤<=*n*<=≤<=24) — the amount of objects the girl has. The following *n* lines contain the objects' coordinates. All the coordinates do not exceed 100 in absolute value. All the given positions are different. All the numbers are integer.
In the first line output the only number — the minimum time the girl needs to put the objects into her handbag. In the second line output the possible optimum way for Lena. Each object in the input is described by its index number (from 1 to *n*), the handbag's point is described by number 0. The path should start and end in the handbag's point. If there are several optimal paths, print any of them.
[ "0 0\n2\n1 1\n-1 1\n", "1 1\n3\n4 3\n3 4\n0 0\n" ]
[ "8\n0 1 2 0 \n", "32\n0 1 2 0 3 0 \n" ]
none
[ { "input": "0 0\n2\n1 1\n-1 1", "output": "8\n0 1 2 0 " }, { "input": "1 1\n3\n4 3\n3 4\n0 0", "output": "32\n0 1 2 0 3 0 " }, { "input": "-3 4\n1\n2 2", "output": "58\n0 1 0 " }, { "input": "7 -7\n2\n3 1\n-3 8", "output": "490\n0 1 2 0 " }, { "input": "3 -9\n3\n0...
4,000
278,118,400
0
14,514
159
String Manipulation 1.0
[ "*special", "binary search", "brute force", "data structures", "strings" ]
null
null
One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name *s*, a user can pick number *p* and character *c* and delete the *p*-th occurrence of character *c* from the name. After the user changed his name, he can't undo the change. For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc". Polycarpus learned that some user initially registered under nickname *t*, where *t* is a concatenation of *k* copies of string *s*. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name.
The first line contains an integer *k* (1<=≤<=*k*<=≤<=2000). The second line contains a non-empty string *s*, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer *n* (0<=≤<=*n*<=≤<=20000) — the number of username changes. Each of the next *n* lines contains the actual changes, one per line. The changes are written as "*p**i* *c**i*" (without the quotes), where *p**i* (1<=≤<=*p**i*<=≤<=200000) is the number of occurrences of letter *c**i*, *c**i* is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1.
Print a single string — the user's final name after all changes are applied to it.
[ "2\nbac\n3\n2 a\n1 b\n2 c\n", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b\n" ]
[ "acb\n", "baa\n" ]
Let's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb".
[ { "input": "2\nbac\n3\n2 a\n1 b\n2 c", "output": "acb" }, { "input": "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b", "output": "baa" }, { "input": "1\naabbabbb\n7\n2 a\n1 a\n1 a\n2 b\n1 b\n3 b\n1 b", "output": "b" }, { "input": "1\na\n0", "output": "a" }, { "input": "4\ndb\n...
3,000
11,673,600
0
14,554
755
PolandBall and Many Other Balls
[ "combinatorics", "divide and conquer", "dp", "fft", "math", "number theory" ]
null
null
PolandBall is standing in a row with Many Other Balls. More precisely, there are exactly *n* Balls. Balls are proud of their home land — and they want to prove that it's strong. The Balls decided to start with selecting exactly *m* groups of Balls, each consisting either of single Ball or two neighboring Balls. Each Ball can join no more than one group. The Balls really want to impress their Enemies. They kindly asked you to calculate number of such divisions for all *m* where 1<=≤<=*m*<=≤<=*k*. Output all these values modulo 998244353, the Enemies will be impressed anyway.
There are exactly two numbers *n* and *k* (1<=≤<=*n*<=≤<=109, 1<=≤<=*k*<=&lt;<=215), denoting the number of Balls and the maximim number of groups, respectively.
You should output a sequence of *k* values. The *i*-th of them should represent the sought number of divisions into exactly *i* groups, according to PolandBall's rules.
[ "3 3\n", "1 1\n", "5 10\n" ]
[ "5 5 1 ", "1 ", "9 25 25 9 1 0 0 0 0 0 " ]
In the first sample case we can divide Balls into groups as follows: {1}, {2}, {3}, {12}, {23}. {12}{3}, {1}{23}, {1}{2}, {1}{3}, {2}{3}. {1}{2}{3}. Therefore, output is: 5 5 1.
[]
46
0
0
14,558
120
Three Sons
[ "brute force" ]
null
null
Three sons inherited from their father a rectangular corn fiend divided into *n*<=×<=*m* squares. For each square we know how many tons of corn grows on it. The father, an old farmer did not love all three sons equally, which is why he bequeathed to divide his field into three parts containing *A*, *B* and *C* tons of corn. The field should be divided by two parallel lines. The lines should be parallel to one side of the field and to each other. The lines should go strictly between the squares of the field. Each resulting part of the field should consist of at least one square. Your task is to find the number of ways to divide the field as is described above, that is, to mark two lines, dividing the field in three parts so that on one of the resulting parts grew *A* tons of corn, *B* on another one and *C* on the remaining one.
The first line contains space-separated integers *n* and *m* — the sizes of the original (1<=≤<=*n*,<=*m*<=≤<=50,<=*max*(*n*,<=*m*)<=≥<=3). Then the field's description follows: *n* lines, each containing *m* space-separated integers *c**ij*, (0<=≤<=*c**ij*<=≤<=100) — the number of tons of corn each square contains. The last line contains space-separated integers *A*,<=*B*,<=*C* (0<=≤<=*A*,<=*B*,<=*C*<=≤<=106).
Print the answer to the problem: the number of ways to divide the father's field so that one of the resulting parts contained *A* tons of corn, another one contained *B* tons, and the remaining one contained *C* tons. If no such way exists, print 0.
[ "3 3\n1 1 1\n1 1 1\n1 1 1\n3 3 3\n", "2 5\n1 1 1 1 1\n2 2 2 2 2\n3 6 6\n", "3 3\n1 2 3\n3 1 2\n2 3 1\n5 6 7\n" ]
[ "2\n", "3\n", "0\n" ]
The lines dividing the field can be horizontal or vertical, but they should be parallel to each other.
[ { "input": "3 3\n1 1 1\n1 1 1\n1 1 1\n3 3 3", "output": "2" }, { "input": "2 5\n1 1 1 1 1\n2 2 2 2 2\n3 6 6", "output": "3" }, { "input": "3 3\n1 2 3\n3 1 2\n2 3 1\n5 6 7", "output": "0" }, { "input": "3 3\n0 0 0\n0 0 1\n1 1 0\n2 1 0", "output": "1" }, { "input": ...
186
2,457,600
3
14,608
923
Picking Strings
[ "constructive algorithms", "implementation", "strings" ]
null
null
Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: - A BC - B AC - C AB - AAA empty string Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.
The first line contains a string *S* (1<=≤<=|*S*|<=≤<=105). The second line contains a string *T* (1<=≤<=|*T*|<=≤<=105), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'. The third line contains the number of queries *Q* (1<=≤<=*Q*<=≤<=105). The following *Q* lines describe queries. The *i*-th of these lines contains four space separated integers *a**i*, *b**i*, *c**i*, *d**i*. These represent the *i*-th query: is it possible to create *T*[*c**i*..*d**i*] from *S*[*a**i*..*b**i*] by applying the above transitions finite amount of times? Here, *U*[*x*..*y*] is a substring of *U* that begins at index *x* (indexed from 1) and ends at index *y*. In particular, *U*[1..|*U*|] is the whole string *U*. It is guaranteed that 1<=≤<=*a*<=≤<=*b*<=≤<=|*S*| and 1<=≤<=*c*<=≤<=*d*<=≤<=|*T*|.
Print a string of *Q* characters, where the *i*-th character is '1' if the answer to the *i*-th query is positive, and '0' otherwise.
[ "AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3\n" ]
[ "10011\n" ]
In the first query we can achieve the result, for instance, by using transitions <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/2c164f8b6e335aa51b97bbd019ca0d7326927314.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The third query asks for changing AAB to A — but in this case we are not able to get rid of the character 'B'.
[ { "input": "AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3", "output": "10011" }, { "input": "AAAAAA\nAAAAAA\n30\n3 4 1 2\n3 3 4 4\n5 6 3 4\n3 3 2 3\n6 6 1 5\n2 4 4 6\n1 6 2 5\n6 6 3 4\n3 5 1 4\n4 5 3 6\n2 3 2 4\n3 4 4 4\n6 6 4 6\n3 3 2 5\n1 5 3 3\n4 6 1 2\n6 6 6 6\n3 3 3 4\n6 6 6 6...
685
18,534,400
3
14,646
0
none
[ "none" ]
null
null
Natasha travels around Mars in the Mars rover. But suddenly it broke down, namely — the logical scheme inside it. The scheme is an undirected tree (connected acyclic graph) with a root in the vertex $1$, in which every leaf (excluding root) is an input, and all other vertices are logical elements, including the root, which is output. One bit is fed to each input. One bit is returned at the output. There are four types of logical elements: [AND](https://en.wikipedia.org/wiki/Logical_conjunction) ($2$ inputs), [OR](https://en.wikipedia.org/wiki/Logical_disjunction) ($2$ inputs), [XOR](https://en.wikipedia.org/wiki/Exclusive_or) ($2$ inputs), [NOT](https://en.wikipedia.org/wiki/Negation) ($1$ input). Logical elements take values from their direct descendants (inputs) and return the result of the function they perform. Natasha knows the logical scheme of the Mars rover, as well as the fact that only one input is broken. In order to fix the Mars rover, she needs to change the value on this input. For each input, determine what the output will be if Natasha changes this input.
The first line contains a single integer $n$ ($2 \le n \le 10^6$) — the number of vertices in the graph (both inputs and elements). The $i$-th of the next $n$ lines contains a description of $i$-th vertex: the first word "AND", "OR", "XOR", "NOT" or "IN" (means the input of the scheme) is the vertex type. If this vertex is "IN", then the value of this input follows ($0$ or $1$), otherwise follow the indices of input vertices of this element: "AND", "OR", "XOR" have $2$ inputs, whereas "NOT" has $1$ input. The vertices are numbered from one. It is guaranteed that input data contains a correct logical scheme with an output produced by the vertex $1$.
Print a string of characters '0' and '1' (without quotes) — answers to the problem for each input in the ascending order of their vertex indices.
[ "10\nAND 9 4\nIN 1\nIN 1\nXOR 6 5\nAND 3 7\nIN 0\nNOT 10\nIN 1\nIN 1\nAND 2 8\n" ]
[ "10110" ]
The original scheme from the example (before the input is changed): <img class="tex-graphics" src="https://espresso.codeforces.com/4507113582d40356e140857daa04318b91197f46.png" style="max-width: 100.0%;max-height: 100.0%;"/> Green indicates bits '1', yellow indicates bits '0'. If Natasha changes the input bit $2$ to $0$, then the output will be $1$. If Natasha changes the input bit $3$ to $0$, then the output will be $0$. If Natasha changes the input bit $6$ to $1$, then the output will be $1$. If Natasha changes the input bit $8$ to $0$, then the output will be $1$. If Natasha changes the input bit $9$ to $0$, then the output will be $0$.
[ { "input": "10\nAND 9 4\nIN 1\nIN 1\nXOR 6 5\nAND 3 7\nIN 0\nNOT 10\nIN 1\nIN 1\nAND 2 8", "output": "10110" }, { "input": "3\nAND 2 3\nIN 0\nIN 0", "output": "00" }, { "input": "3\nAND 2 3\nIN 1\nIN 0", "output": "01" }, { "input": "3\nAND 2 3\nIN 0\nIN 1", "output": "10...
5,000
216,780,800
0
14,673
27
Unordered Subsequence
[ "constructive algorithms", "greedy" ]
C. Unordered Subsequence
2
256
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
The first line of the input contains one integer *n* (1<=≤<=*n*<=≤<=105). The second line contains *n* space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length *k* of the shortest such subsequence. Then output *k* integers from the range [1..*n*] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
[ "5\n67 499 600 42 23\n", "3\n1 2 3\n", "3\n2 3 1\n" ]
[ "3\n1 3 5\n", "0\n", "3\n1 2 3\n" ]
none
[ { "input": "3\n3 1 2", "output": "3\n1 2 3" }, { "input": "1\n-895376", "output": "0" }, { "input": "2\n166442 61629", "output": "0" }, { "input": "3\n-771740 -255752 -300809", "output": "3\n1 2 3" }, { "input": "4\n-227347 -573134 -671045 11011", "output": "3...
778
7,680,000
0
14,693
996
World Cup
[ "binary search", "math" ]
null
null
Allen wants to enter a fan zone that occupies a round square and has $n$ entrances. There already is a queue of $a_i$ people in front of the $i$-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute. Allen uses the following strategy to enter the fan zone: - Initially he stands in the end of the queue in front of the first entrance. - Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance). Determine the entrance through which Allen will finally enter the fan zone.
The first line contains a single integer $n$ ($2 \le n \le 10^5$) — the number of entrances. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the number of people in queues. These numbers do not include Allen.
Print a single integer — the number of entrance that Allen will use.
[ "4\n2 3 2 0\n", "2\n10 10\n", "6\n5 2 6 5 7 4\n" ]
[ "3\n", "1\n", "6\n" ]
In the first example the number of people (not including Allen) changes as follows: $[\textbf{2}, 3, 2, 0] \to [1, \textbf{2}, 1, 0] \to [0, 1, \textbf{0}, 0]$. The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance. In the second example the number of people (not including Allen) changes as follows: $[\textbf{10}, 10] \to [9, \textbf{9}] \to [\textbf{8}, 8] \to [7, \textbf{7}] \to [\textbf{6}, 6] \to \\ [5, \textbf{5}] \to [\textbf{4}, 4] \to [3, \textbf{3}] \to [\textbf{2}, 2] \to [1, \textbf{1}] \to [\textbf{0}, 0]$. In the third example the number of people (not including Allen) changes as follows: $[\textbf{5}, 2, 6, 5, 7, 4] \to [4, \textbf{1}, 5, 4, 6, 3] \to [3, 0, \textbf{4}, 3, 5, 2] \to \\ [2, 0, 3, \textbf{2}, 4, 1] \to [1, 0, 2, 1, \textbf{3}, 0] \to [0, 0, 1, 0, 2, \textbf{0}]$.
[ { "input": "4\n2 3 2 0", "output": "3" }, { "input": "2\n10 10", "output": "1" }, { "input": "6\n5 2 6 5 7 4", "output": "6" }, { "input": "2\n483544186 940350702", "output": "1" }, { "input": "10\n3 3 3 5 6 9 3 1 7 3", "output": "7" }, { "input": "10\...
15
0
-1
14,700
63
Dividing Island
[ "constructive algorithms" ]
D. Dividing Island
2
256
A revolution took place on the Buka Island. New government replaced the old one. The new government includes *n* parties and each of them is entitled to some part of the island according to their contribution to the revolution. However, they can't divide the island. The island can be conventionally represented as two rectangles *a*<=×<=*b* and *c*<=×<=*d* unit squares in size correspondingly. The rectangles are located close to each other. At that, one of the sides with the length of *a* and one of the sides with the length of *c* lie on one line. You can see this in more details on the picture. The *i*-th party is entitled to a part of the island equal to *x**i* unit squares. Every such part should fully cover several squares of the island (it is not allowed to cover the squares partially) and be a connected figure. A "connected figure" presupposes that from any square of this party one can move to any other square of the same party moving through edge-adjacent squares also belonging to that party. Your task is to divide the island between parties.
The first line contains 5 space-separated integers — *a*, *b*, *c*, *d* and *n* (1<=≤<=*a*,<=*b*,<=*c*,<=*d*<=≤<=50, *b*<=≠<=*d*, 1<=≤<=*n*<=≤<=26). The second line contains *n* space-separated numbers. The *i*-th of them is equal to number *x**i* (1<=≤<=*x**i*<=≤<=*a*<=×<=*b*<=+<=*c*<=×<=*d*). It is guaranteed that .
If dividing the island between parties in the required manner is impossible, print "NO" (without the quotes). Otherwise, print "YES" (also without the quotes) and, starting from the next line, print *max*(*b*,<=*d*) lines each containing *a*<=+<=*c* characters. To mark what square should belong to what party, use lowercase Latin letters. For the party that is first in order in the input data, use "a", for the second one use "b" and so on. Use "." for the squares that belong to the sea. The first symbol of the second line of the output data should correspond to the square that belongs to the rectangle *a*<=×<=*b*. The last symbol of the second line should correspond to the square that belongs to the rectangle *c*<=×<=*d*. If there are several solutions output any.
[ "3 4 2 2 3\n5 8 3\n", "3 2 1 4 4\n1 2 3 4\n" ]
[ "YES\naaabb\naabbb\ncbb..\nccb..\n", "YES\nabbd\ncccd\n...d\n...d\n" ]
none
[ { "input": "3 4 2 2 3\n5 8 3", "output": "YES\nbbbbc\nbbbcc\naab..\naaa.." }, { "input": "3 2 1 4 4\n1 2 3 4", "output": "YES\ncccd\nbbad\n...d\n...d" }, { "input": "1 2 1 1 1\n3", "output": "YES\naa\na." }, { "input": "1 2 1 3 2\n3 2", "output": "YES\naa\nab\n.b" }, ...
216
512,000
0
14,717
42
Guilty --- to the kitchen!
[ "greedy", "implementation" ]
A. Guilty — to the kitchen!
2
256
It's a very unfortunate day for Volodya today. He got bad mark in algebra and was therefore forced to do some work in the kitchen, namely to cook borscht (traditional Russian soup). This should also improve his algebra skills. According to the borscht recipe it consists of *n* ingredients that have to be mixed in proportion litres (thus, there should be *a*1<=·*x*,<=...,<=*a**n*<=·*x* litres of corresponding ingredients mixed for some non-negative *x*). In the kitchen Volodya found out that he has *b*1,<=...,<=*b**n* litres of these ingredients at his disposal correspondingly. In order to correct his algebra mistakes he ought to cook as much soup as possible in a *V* litres volume pan (which means the amount of soup cooked can be between 0 and *V* litres). What is the volume of borscht Volodya will cook ultimately?
The first line of the input contains two space-separated integers *n* and *V* (1<=≤<=*n*<=≤<=20,<=1<=≤<=*V*<=≤<=10000). The next line contains *n* space-separated integers *a**i* (1<=≤<=*a**i*<=≤<=100). Finally, the last line contains *n* space-separated integers *b**i* (0<=≤<=*b**i*<=≤<=100).
Your program should output just one real number — the volume of soup that Volodya will cook. Your answer must have a relative or absolute error less than 10<=-<=4.
[ "1 100\n1\n40\n", "2 100\n1 1\n25 30\n", "2 100\n1 1\n60 60\n" ]
[ "40.0\n", "50.0\n", "100.0\n" ]
none
[ { "input": "1 100\n1\n40", "output": "40.0" }, { "input": "2 100\n1 1\n25 30", "output": "50.0" }, { "input": "2 100\n1 1\n60 60", "output": "100.0" }, { "input": "2 100\n1 1\n50 50", "output": "100.0" }, { "input": "2 100\n1 2\n33 66", "output": "99.0" }, ...
0
0
-1
14,735
954
Runner's Problem
[ "dp", "matrices", "sortings" ]
null
null
You are running through a rectangular field. This field can be represented as a matrix with 3 rows and *m* columns. (*i*,<=*j*) denotes a cell belonging to *i*-th row and *j*-th column. You start in (2,<=1) and have to end your path in (2,<=*m*). From the cell (*i*,<=*j*) you may advance to: - (*i*<=-<=1,<=*j*<=+<=1) — only if *i*<=&gt;<=1, - (*i*,<=*j*<=+<=1), or - (*i*<=+<=1,<=*j*<=+<=1) — only if *i*<=&lt;<=3. However, there are *n* obstacles blocking your path. *k*-th obstacle is denoted by three integers *a**k*, *l**k* and *r**k*, and it forbids entering any cell (*a**k*,<=*j*) such that *l**k*<=≤<=*j*<=≤<=*r**k*. You have to calculate the number of different paths from (2,<=1) to (2,<=*m*), and print it modulo 109<=+<=7.
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=104, 3<=≤<=*m*<=≤<=1018) — the number of obstacles and the number of columns in the matrix, respectively. Then *n* lines follow, each containing three integers *a**k*, *l**k* and *r**k* (1<=≤<=*a**k*<=≤<=3, 2<=≤<=*l**k*<=≤<=*r**k*<=≤<=*m*<=-<=1) denoting an obstacle blocking every cell (*a**k*,<=*j*) such that *l**k*<=≤<=*j*<=≤<=*r**k*. Some cells may be blocked by multiple obstacles.
Print the number of different paths from (2,<=1) to (2,<=*m*), taken modulo 109<=+<=7. If it is impossible to get from (2,<=1) to (2,<=*m*), then the number of paths is 0.
[ "2 5\n1 3 4\n2 2 3\n" ]
[ "2\n" ]
none
[ { "input": "2 5\n1 3 4\n2 2 3", "output": "2" }, { "input": "50 100\n3 24 49\n2 10 12\n1 87 92\n2 19 60\n2 53 79\n3 65 82\n3 10 46\n1 46 86\n2 55 84\n1 50 53\n3 80 81\n3 66 70\n2 35 52\n1 63 69\n2 65 87\n3 68 75\n1 33 42\n1 56 90\n3 73 93\n2 20 26\n2 42 80\n2 83 87\n3 99 99\n1 14 79\n2 94 97\n1 66 8...
62
0
0
14,737
245
Queries for Number of Palindromes
[ "dp", "hashing", "strings" ]
null
null
You've got a string *s*<==<=*s*1*s*2... *s*|*s*| of length |*s*|, consisting of lowercase English letters. There also are *q* queries, each query is described by two integers *l**i*,<=*r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=|*s*|). The answer to the query is the number of substrings of string *s*[*l**i*... *r**i*], which are palindromes. String *s*[*l*... *r*]<==<=*s**l**s**l*<=+<=1... *s**r* (1<=≤<=*l*<=≤<=*r*<=≤<=|*s*|) is a substring of string *s*<==<=*s*1*s*2... *s*|*s*|. String *t* is called a palindrome, if it reads the same from left to right and from right to left. Formally, if *t*<==<=*t*1*t*2... *t*|*t*|<==<=*t*|*t*|*t*|*t*|<=-<=1... *t*1.
The first line contains string *s* (1<=≤<=|*s*|<=≤<=5000). The second line contains a single integer *q* (1<=≤<=*q*<=≤<=106) — the number of queries. Next *q* lines contain the queries. The *i*-th of these lines contains two space-separated integers *l**i*,<=*r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=|*s*|) — the description of the *i*-th query. It is guaranteed that the given string consists only of lowercase English letters.
Print *q* integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces.
[ "caaaba\n5\n1 1\n1 4\n2 3\n4 6\n4 5\n" ]
[ "1\n7\n3\n4\n2\n" ]
Consider the fourth query in the first test case. String *s*[4... 6] = «aba». Its palindrome substrings are: «a», «b», «a», «aba».
[ { "input": "caaaba\n5\n1 1\n1 4\n2 3\n4 6\n4 5", "output": "1\n7\n3\n4\n2" }, { "input": "a\n100\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 ...
592
307,200
0
14,757
404
Minesweeper 1D
[ "dp", "implementation" ]
null
null
Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is *n* squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 — the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of *n* cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end.
The first line contains sequence of characters without spaces *s*1*s*2... *s**n* (1<=≤<=*n*<=≤<=106), containing only characters "*", "?" and digits "0", "1" or "2". If character *s**i* equals "*", then the *i*-th cell of the field contains a bomb. If character *s**i* equals "?", then Valera hasn't yet decided what to put in the *i*-th cell. Character *s**i*, that is equal to a digit, represents the digit written in the *i*-th square.
Print a single integer — the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109<=+<=7).
[ "?01???\n", "?\n", "**12\n", "1\n" ]
[ "4\n", "2\n", "0\n", "0\n" ]
In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10.
[ { "input": "?01???", "output": "4" }, { "input": "?", "output": "2" }, { "input": "**12", "output": "0" }, { "input": "1", "output": "0" }, { "input": "?01*??****", "output": "4" }, { "input": "0", "output": "1" }, { "input": "2", "outp...
46
0
0
14,762
750
New Year and North Pole
[ "geometry", "implementation" ]
null
null
In this problem we assume the Earth to be a completely round ball and its surface a perfect sphere. The length of the equator and any meridian is considered to be exactly 40<=000 kilometers. Thus, travelling from North Pole to South Pole or vice versa takes exactly 20<=000 kilometers. Limak, a polar bear, lives on the North Pole. Close to the New Year, he helps somebody with delivering packages all around the world. Instead of coordinates of places to visit, Limak got a description how he should move, assuming that he starts from the North Pole. The description consists of *n* parts. In the *i*-th part of his journey, Limak should move *t**i* kilometers in the direction represented by a string *dir**i* that is one of: "North", "South", "West", "East". Limak isn’t sure whether the description is valid. You must help him to check the following conditions: - If at any moment of time (before any of the instructions or while performing one of them) Limak is on the North Pole, he can move only to the South. - If at any moment of time (before any of the instructions or while performing one of them) Limak is on the South Pole, he can move only to the North. - The journey must end on the North Pole. Check if the above conditions are satisfied and print "YES" or "NO" on a single line.
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=50). The *i*-th of next *n* lines contains an integer *t**i* and a string *dir**i* (1<=≤<=*t**i*<=≤<=106, ) — the length and the direction of the *i*-th part of the journey, according to the description Limak got.
Print "YES" if the description satisfies the three conditions, otherwise print "NO", both without the quotes.
[ "5\n7500 South\n10000 East\n3500 North\n4444 West\n4000 North\n", "2\n15000 South\n4000 East\n", "5\n20000 South\n1000 North\n1000000 West\n9000 North\n10000 North\n", "3\n20000 South\n10 East\n20000 North\n", "2\n1000 North\n1000 South\n", "4\n50 South\n50 North\n15000 South\n15000 North\n" ]
[ "YES\n", "NO\n", "YES\n", "NO\n", "NO\n", "YES\n" ]
Drawings below show how Limak's journey would look like in first two samples. In the second sample the answer is "NO" because he doesn't end on the North Pole.
[ { "input": "5\n7500 South\n10000 East\n3500 North\n4444 West\n4000 North", "output": "YES" }, { "input": "2\n15000 South\n4000 East", "output": "NO" }, { "input": "5\n20000 South\n1000 North\n1000000 West\n9000 North\n10000 North", "output": "YES" }, { "input": "3\n20000 Sout...
30
0
0
14,820
379
New Year Ratings Change
[ "greedy", "sortings" ]
null
null
One very well-known internet resource site (let's call it X) has come up with a New Year adventure. Specifically, they decided to give ratings to all visitors. There are *n* users on the site, for each user we know the rating value he wants to get as a New Year Present. We know that user *i* wants to get at least *a**i* rating units as a present. The X site is administered by very creative and thrifty people. On the one hand, they want to give distinct ratings and on the other hand, the total sum of the ratings in the present must be as small as possible. Help site X cope with the challenging task of rating distribution. Find the optimal distribution.
The first line contains integer *n* (1<=≤<=*n*<=≤<=3·105) — the number of users on the site. The next line contains integer sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
Print a sequence of integers *b*1,<=*b*2,<=...,<=*b**n*. Number *b**i* means that user *i* gets *b**i* of rating as a present. The printed sequence must meet the problem conditions. If there are multiple optimal solutions, print any of them.
[ "3\n5 1 1\n", "1\n1000000000\n" ]
[ "5 1 2\n", "1000000000\n" ]
none
[ { "input": "3\n5 1 1", "output": "5 1 2" }, { "input": "1\n1000000000", "output": "1000000000" }, { "input": "10\n1 1 1 1 1 1 1 1 1 1", "output": "1 2 3 4 5 6 7 8 9 10" }, { "input": "10\n1 10 1 10 1 1 7 8 6 7", "output": "1 10 2 11 3 4 7 9 6 8" }, { "input": "10\...
1,000
29,593,600
0
14,863
409
000001
[ "*special" ]
null
null
The input contains a single integer *a* (1<=≤<=*a*<=≤<=64). Output a single integer.
The input contains a single integer *a* (1<=≤<=*a*<=≤<=64).
Output a single integer.
[ "2\n", "4\n", "27\n", "42\n" ]
[ "1\n", "2\n", "5\n", "6\n" ]
none
[ { "input": "2", "output": "1" }, { "input": "4", "output": "2" }, { "input": "27", "output": "5" }, { "input": "42", "output": "6" }, { "input": "1", "output": "1" }, { "input": "3", "output": "1" }, { "input": "64", "output": "267" }...
46
0
3
14,879
453
Little Pony and Lord Tirek
[ "data structures" ]
null
null
Lord Tirek is a centaur and the main antagonist in the season four finale episodes in the series "My Little Pony: Friendship Is Magic". In "Twilight's Kingdom" (Part 1), Tirek escapes from Tartarus and drains magic from ponies to grow stronger. The core skill of Tirek is called Absorb Mana. It takes all mana from a magic creature and gives them to the caster. Now to simplify the problem, assume you have *n* ponies (numbered from 1 to *n*). Each pony has three attributes: - *s**i* : amount of mana that the pony has at time 0; - *m**i* : maximum mana that the pony can have; - *r**i* : mana regeneration per unit time. Lord Tirek will do *m* instructions, each of them can be described with three integers: *t**i*,<=*l**i*,<=*r**i*. The instruction means that at time *t**i*, Tirek will use Absorb Mana on ponies with numbers from *l**i* to *r**i* (both borders inclusive). We'll give you all the *m* instructions in order, count how much mana Tirek absorbs for each instruction.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=105) — the number of ponies. Each of the next *n* lines contains three integers *s**i*,<=*m**i*,<=*r**i* (0<=≤<=*s**i*<=≤<=*m**i*<=≤<=105; 0<=≤<=*r**i*<=≤<=105), describing a pony. The next line contains an integer *m* (1<=≤<=*m*<=≤<=105) — the number of instructions. Each of the next *m* lines contains three integers *t**i*,<=*l**i*,<=*r**i* (0<=≤<=*t**i*<=≤<=109; 1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*), describing an instruction of Lord Tirek. The instructions are given in strictly increasing order of *t**i* (all *t**i* are distinct).
For each instruction, output a single line which contains a single integer, the total mana absorbed in this instruction.
[ "5\n0 10 1\n0 12 1\n0 20 1\n0 12 1\n0 10 1\n2\n5 1 5\n19 1 5\n" ]
[ "25\n58\n" ]
Every pony starts with zero mana. For the first instruction, each pony has 5 mana, so you get 25 mana in total and each pony has 0 mana after the first instruction. For the second instruction, pony 3 has 14 mana and other ponies have mana equal to their *m*<sub class="lower-index">*i*</sub>.
[]
46
0
0
14,883
48
The Race
[ "math" ]
C. The Race
2
256
Every year a race takes place on the motorway between cities A and B. This year Vanya decided to take part in the race and drive his own car that has been around and bears its own noble name — The Huff-puffer. So, Vasya leaves city A on the Huff-puffer, besides, at the very beginning he fills the petrol tank with α liters of petrol (α<=≥<=10 is Vanya's favorite number, it is not necessarily integer). Petrol stations are located on the motorway at an interval of 100 kilometers, i.e. the first station is located 100 kilometers away from the city A, the second one is 200 kilometers away from the city A, the third one is 300 kilometers away from the city A and so on. The Huff-puffer spends 10 liters of petrol every 100 kilometers. Vanya checks the petrol tank every time he passes by a petrol station. If the petrol left in the tank is not enough to get to the next station, Vanya fills the tank with α liters of petrol. Otherwise, he doesn't stop at the station and drives on. For example, if α<==<=43.21, then the car will be fuelled up for the first time at the station number 4, when there'll be 3.21 petrol liters left. After the fuelling up the car will have 46.42 liters. Then Vanya stops at the station number 8 and ends up with 6.42<=+<=43.21<==<=49.63 liters. The next stop is at the station number 12, 9.63<=+<=43.21<==<=52.84. The next stop is at the station number 17 and so on. You won't believe this but the Huff-puffer has been leading in the race! Perhaps it is due to unexpected snow. Perhaps it is due to video cameras that have been installed along the motorway which register speed limit breaking. Perhaps it is due to the fact that Vanya threatened to junk the Huff-puffer unless the car wins. Whatever the reason is, the Huff-puffer is leading, and jealous people together with other contestants wrack their brains trying to think of a way to stop that outrage. One way to do this is to mine the next petrol station where Vanya will stop. Your task is to calculate at which station this will happen and warn Vanya. You don't know the α number, however, you are given the succession of the numbers of the stations where Vanya has stopped. Find the number of the station where the next stop will be.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=1000) which represents the number of petrol stations where Vanya has stopped. The next line has *n* space-separated integers which represent the numbers of the stations. The numbers are positive and do not exceed 106, they are given in the increasing order. No two numbers in the succession match. It is guaranteed that there exists at least one number α<=≥<=10, to which such a succession of stops corresponds.
Print in the first line "unique" (without quotes) if the answer can be determined uniquely. In the second line print the number of the station where the next stop will take place. If the answer is not unique, print in the first line "not unique".
[ "3\n1 2 4\n", "2\n1 2\n" ]
[ "unique\n5\n", "not unique\n" ]
In the second example the answer is not unique. For example, if α = 10, we'll have such a sequence as 1, 2, 3, and if α = 14, the sequence will be 1, 2, 4.
[ { "input": "3\n1 2 4", "output": "unique\n5" }, { "input": "2\n1 2", "output": "not unique" }, { "input": "1\n5", "output": "not unique" }, { "input": "3\n1 3 4", "output": "unique\n6" }, { "input": "5\n1 2 3 5 6", "output": "unique\n7" }, { "input": "...
1,340
307,200
3.664428
14,884
963
Cutting Rectangle
[ "brute force", "math", "number theory" ]
null
null
A rectangle with sides $A$ and $B$ is cut into rectangles with cuts parallel to its sides. For example, if $p$ horizontal and $q$ vertical cuts were made, $(p + 1) \cdot (q + 1)$ rectangles were left after the cutting. After the cutting, rectangles were of $n$ different types. Two rectangles are different if at least one side of one rectangle isn't equal to the corresponding side of the other. Note that the rectangle can't be rotated, this means that rectangles $a \times b$ and $b \times a$ are considered different if $a \neq b$. For each type of rectangles, lengths of the sides of rectangles are given along with the amount of the rectangles of this type that were left after cutting the initial rectangle. Calculate the amount of pairs $(A; B)$ such as the given rectangles could be created by cutting the rectangle with sides of lengths $A$ and $B$. Note that pairs $(A; B)$ and $(B; A)$ are considered different when $A \neq B$.
The first line consists of a single integer $n$ ($1 \leq n \leq 2 \cdot 10^{5}$) — amount of different types of rectangles left after cutting the initial rectangle. The next $n$ lines each consist of three integers $w_{i}, h_{i}, c_{i}$ $(1 \leq w_{i}, h_{i}, c_{i} \leq 10^{12})$ — the lengths of the sides of the rectangles of this type and the amount of the rectangles of this type. It is guaranteed that the rectangles of the different types are different.
Output one integer — the answer to the problem.
[ "1\n1 1 9\n", "2\n2 3 20\n2 4 40\n", "2\n1 2 5\n2 3 5\n" ]
[ "3\n", "6\n", "0\n" ]
In the first sample there are three suitable pairs: $(1; 9)$, $(3; 3)$ and $(9; 1)$. In the second sample case there are 6 suitable pairs: $(2; 220)$, $(4; 110)$, $(8; 55)$, $(10; 44)$, $(20; 22)$ and $(40; 11)$. Here the sample of cut for $(20; 22)$. The third sample has no suitable pairs.
[ { "input": "1\n1 1 9", "output": "3" }, { "input": "2\n2 3 20\n2 4 40", "output": "6" }, { "input": "2\n1 2 5\n2 3 5", "output": "0" } ]
0
0
-1
14,885
23
Oranges and Apples
[ "constructive algorithms", "sortings" ]
C. Oranges and Apples
1
256
In 2*N*<=-<=1 boxes there are apples and oranges. Your task is to choose *N* boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges.
The first input line contains one number *T* — amount of tests. The description of each test starts with a natural number *N* — amount of boxes. Each of the following 2*N*<=-<=1 lines contains numbers *a**i* and *o**i* — amount of apples and oranges in the *i*-th box (0<=≤<=*a**i*,<=*o**i*<=≤<=109). The sum of *N* in all the tests in the input doesn't exceed 105. All the input numbers are integer.
For each test output two lines. In the first line output YES, if it's possible to choose *N* boxes, or NO otherwise. If the answer is positive output in the second line *N* numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers with one space.
[ "2\n2\n10 15\n5 7\n20 18\n1\n0 0\n" ]
[ "YES\n1 3\nYES\n1\n" ]
none
[ { "input": "2\n2\n10 15\n5 7\n20 18\n1\n0 0", "output": "YES\n3 1\nYES\n1" } ]
2,000
24,064,000
0
14,907
187
Permutations
[ "greedy" ]
null
null
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot. One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through *n* and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation. Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=2·105) — the quantity of the numbers in the both given permutations. Next line contains *n* space-separated integers — the first permutation. Each number between 1 to *n* will appear in the permutation exactly once. Next line describe the second permutation in the same format.
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
[ "3\n3 2 1\n1 2 3\n", "5\n1 2 3 4 5\n1 5 2 3 4\n", "5\n1 5 2 3 4\n1 2 3 4 5\n" ]
[ "2\n", "1\n", "3\n" ]
In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3. In the second sample, he removes number 5 and inserts it after 1. In the third sample, the sequence of changes are like this: - 1 5 2 3 4 - 1 4 5 2 3 - 1 3 4 5 2 - 1 2 3 4 5
[ { "input": "3\n3 2 1\n1 2 3", "output": "2" }, { "input": "5\n1 2 3 4 5\n1 5 2 3 4", "output": "1" }, { "input": "5\n1 5 2 3 4\n1 2 3 4 5", "output": "3" }, { "input": "1\n1\n1", "output": "0" }, { "input": "7\n6 1 7 3 4 5 2\n6 1 7 3 4 5 2", "output": "0" },...
186
23,142,400
0
14,908
366
Dima and Magic Guitar
[ "brute force", "implementation", "math" ]
null
null
Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with *n* strings and *m* frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the *i*-th string holding it on the *j*-th fret the guitar produces a note, let's denote it as *a**ij*. We know that Dima's guitar can produce *k* distinct notes. It is possible that some notes can be produced in multiple ways. In other words, it is possible that *a**ij*<==<=*a**pq* at (*i*,<=*j*)<=≠<=(*p*,<=*q*). Dima has already written a song — a sequence of *s* notes. In order to play the song, you need to consecutively produce the notes from the song on the guitar. You can produce each note in any available way. Dima understood that there are many ways to play a song and he wants to play it so as to make the song look as complicated as possible (try to act like Cobein). We'll represent a way to play a song as a sequence of pairs (*x**i*,<=*y**i*) (1<=≤<=*i*<=≤<=*s*), such that the *x**i*-th string on the *y**i*-th fret produces the *i*-th note from the song. The complexity of moving between pairs (*x*1,<=*y*1) and (*x*2,<=*y*2) equals + . The complexity of a way to play a song is the maximum of complexities of moving between adjacent pairs. Help Dima determine the maximum complexity of the way to play his song! The guy's gotta look cool!
The first line of the input contains four integers *n*, *m*, *k* and *s* (1<=≤<=*n*,<=*m*<=≤<=2000,<=1<=≤<=*k*<=≤<=9,<=2<=≤<=*s*<=≤<=105). Then follow *n* lines, each containing *m* integers *a**ij* (1<=≤<=*a**ij*<=≤<=*k*). The number in the *i*-th row and the *j*-th column (*a**ij*) means a note that the guitar produces on the *i*-th string and the *j*-th fret. The last line of the input contains *s* integers *q**i* (1<=≤<=*q**i*<=≤<=*k*) — the sequence of notes of the song.
In a single line print a single number — the maximum possible complexity of the song.
[ "4 6 5 7\n3 1 2 2 3 1\n3 2 2 2 5 5\n4 2 2 2 5 3\n3 2 2 1 4 3\n2 3 1 4 1 5 1\n", "4 4 9 5\n4 7 9 5\n1 2 1 7\n8 3 4 9\n5 7 7 2\n7 1 9 2 5\n" ]
[ "8\n", "4\n" ]
none
[ { "input": "4 6 5 7\n3 1 2 2 3 1\n3 2 2 2 5 5\n4 2 2 2 5 3\n3 2 2 1 4 3\n2 3 1 4 1 5 1", "output": "8" }, { "input": "4 4 9 5\n4 7 9 5\n1 2 1 7\n8 3 4 9\n5 7 7 2\n7 1 9 2 5", "output": "4" }, { "input": "5 5 2 2\n2 2 2 1 2\n2 1 2 2 2\n2 2 2 2 2\n1 2 2 2 2\n2 2 2 2 1\n1 1", "output": ...
61
0
0
14,917
175
Geometry Horse
[ "greedy", "implementation", "sortings", "two pointers" ]
null
null
Vasya plays the Geometry Horse. The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value. There are *n* types of geometric figures. The number of figures of type *k**i* and figure cost *c**i* is known for each figure type. A player gets *c**i*·*f* points for destroying one figure of type *i*, where *f* is the current factor. The factor value can be an integer number from 1 to *t*<=+<=1, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to *i*<=+<=1 after destruction of *p**i* (1<=≤<=*i*<=≤<=*t*) figures, so the (*p**i*<=+<=1)-th figure to be destroyed is considered with factor equal to *i*<=+<=1. Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.
The first line contains the only integer number *n* (1<=≤<=*n*<=≤<=100) — the number of figure types. Each of the following *n* lines contains two integer numbers *k**i* and *c**i* (1<=≤<=*k**i*<=≤<=109,<=0<=≤<=*c**i*<=≤<=1000), separated with space — the number of figures of the *i*-th type and the cost of one *i*-type figure, correspondingly. The next line contains the only integer number *t* (1<=≤<=*t*<=≤<=100) — the number that describe the factor's changes. The next line contains *t* integer numbers *p**i* (1<=≤<=*p*1<=&lt;<=*p*2<=&lt;<=...<=&lt;<=*p**t*<=≤<=1012), separated with spaces. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
Print the only number — the maximum number of points Vasya can get.
[ "1\n5 10\n2\n3 6\n", "2\n3 8\n5 10\n1\n20\n" ]
[ "70", "74" ]
In the first example Vasya destroys three figures first and gets 3·1·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2·2·10 = 40 points. As a result Vasya will get 70 points. In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3·8 + 5·10)·1 = 74 points.
[ { "input": "1\n5 10\n2\n3 6", "output": "70" }, { "input": "2\n3 8\n5 10\n1\n20", "output": "74" }, { "input": "3\n10 3\n20 2\n30 1\n3\n30 50 60", "output": "200" }, { "input": "1\n100 1000\n1\n1", "output": "199000" }, { "input": "1\n1 1000\n1\n1", "output": ...
0
0
-1
14,919
840
Destiny
[ "data structures", "probabilities" ]
null
null
Once, Leha found in the left pocket an array consisting of *n* integers, and in the right pocket *q* queries of the form *l* *r* *k*. If there are queries, then they must be answered. Answer for the query is minimal *x* such that *x* occurs in the interval *l* *r* strictly more than times or <=-<=1 if there is no such number. Help Leha with such a difficult task.
First line of input data contains two integers *n* and *q* (1<=≤<=*n*,<=*q*<=≤<=3·105) — number of elements in the array and number of queries respectively. Next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*n*) — Leha's array. Each of next *q* lines contains three integers *l*, *r* and *k* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*,<=2<=≤<=*k*<=≤<=5) — description of the queries.
Output answer for each query in new line.
[ "4 2\n1 1 2 2\n1 3 2\n1 4 2\n", "5 3\n1 2 1 3 2\n2 5 3\n1 2 3\n5 5 2\n" ]
[ "1\n-1\n", "2\n1\n2\n" ]
none
[]
2,500
213,299,200
0
14,925
980
The Number Games
[ "data structures", "greedy", "trees" ]
null
null
The nation of Panel holds an annual show called The Number Games, where each district in the nation will be represented by one contestant. The nation has $n$ districts numbered from $1$ to $n$, each district has exactly one path connecting it to every other district. The number of fans of a contestant from district $i$ is equal to $2^i$. This year, the president decided to reduce the costs. He wants to remove $k$ contestants from the games. However, the districts of the removed contestants will be furious and will not allow anyone to cross through their districts. The president wants to ensure that all remaining contestants are from districts that can be reached from one another. He also wishes to maximize the total number of fans of the participating contestants. Which contestants should the president remove?
The first line of input contains two integers $n$ and $k$ ($1 \leq k &lt; n \leq 10^6$) — the number of districts in Panel, and the number of contestants the president wishes to remove, respectively. The next $n-1$ lines each contains two integers $a$ and $b$ ($1 \leq a, b \leq n$, $a \ne b$), that describe a road that connects two different districts $a$ and $b$ in the nation. It is guaranteed that there is exactly one path between every two districts.
Print $k$ space-separated integers: the numbers of the districts of which the contestants should be removed, in increasing order of district number.
[ "6 3\n2 1\n2 6\n4 2\n5 6\n2 3\n", "8 4\n2 6\n2 7\n7 8\n1 2\n3 1\n2 4\n7 5\n" ]
[ "1 3 4\n", "1 3 4 5\n" ]
In the first sample, the maximum possible total number of fans is $2^2 + 2^5 + 2^6 = 100$. We can achieve it by removing the contestants of the districts 1, 3, and 4.
[ { "input": "6 3\n2 1\n2 6\n4 2\n5 6\n2 3", "output": "1 3 4" }, { "input": "8 4\n2 6\n2 7\n7 8\n1 2\n3 1\n2 4\n7 5", "output": "1 3 4 5" }, { "input": "2 1\n1 2", "output": "1" }, { "input": "3 1\n2 1\n2 3", "output": "1" }, { "input": "3 2\n1 3\n1 2", "output...
0
0
-1
14,954
138
Hellish Constraints
[ "brute force", "dp", "two pointers" ]
null
null
Katya recently started to invent programming tasks and prepare her own contests. What she does not like is boring and simple constraints. Katya is fed up with all those "*N* does not exceed a thousand" and "the sum of *a**i* does not exceed a million" and she decided to come up with something a little more complicated. The last problem written by Katya deals with strings. The input is a string of small Latin letters. To make the statement longer and strike terror into the people who will solve the contest, Katya came up with the following set of *k* restrictions of the same type (characters in restrictions can be repeated and some restrictions may contradict each other): - The number of characters *c*1 in a string is not less than *l*1 and not more than *r*1. - ... - The number of characters *c**i* in a string is not less than *l**i* and not more than *r**i*. - ... - The number of characters *c**k* in a string is not less than *l**k* and not more than *r**k*. However, having decided that it is too simple and obvious, Katya added the following condition: a string meets no less than *L* and not more than *R* constraints from the above given list. Katya does not like to compose difficult and mean tests, so she just took a big string *s* and wants to add to the tests all its substrings that meet the constraints. However, Katya got lost in her conditions and asked you to count the number of substrings of the string *s* that meet the conditions (each occurrence of the substring is counted separately).
The first line contains a non-empty string *s*, consisting of small Latin letters. The length of the string *s* does not exceed 105. The second line contains three space-separated integers *k*, *L* and *R* (0<=≤<=*L*<=≤<=*R*<=≤<=*k*<=≤<=500). Next *k* lines contain Katya's constrictions in the following form "*c**i* *l**i* *r**i*". All letters *c**i* are small Latin letters, *l**i* and *r**i* are integers (0<=≤<=*l**i*<=≤<=*r**i*<=≤<=|*s*|, where |*s*| is the length of string *s*). Letters *c**i* are not necessarily different.
Print a single number — the number of substrings that meet the constrictions. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cout stream or the %I64d specificator.
[ "codeforces\n2 0 0\no 1 2\ne 1 2\n", "codeforces\n2 1 1\no 1 2\no 1 2\n" ]
[ "7\n", "0\n" ]
In the first test we should count the number of strings that do not contain characters "e" and "o". All such strings are as follows (in the order of occurrence in the initial string from the left to the right): "c", "d"', "f", "r", "rc", "c", "s". In the second test we cannot achieve fulfilling exactly one of the two identical constrictions, so the answer is 0.
[]
186
2,252,800
-1
14,988
547
Mike and Foam
[ "bitmasks", "combinatorics", "dp", "math", "number theory" ]
null
null
Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are *n* kinds of beer at Rico's numbered from 1 to *n*. *i*-th kind of beer has *a**i* milliliters of foam on it. Maxim is Mike's boss. Today he told Mike to perform *q* queries. Initially the shelf is empty. In each request, Maxim gives him a number *x*. If beer number *x* is already in the shelf, then Mike should remove it from the shelf, otherwise he should put it in the shelf. After each query, Mike should tell him the score of the shelf. Bears are geeks. So they think that the score of a shelf is the number of pairs (*i*,<=*j*) of glasses in the shelf such that *i*<=&lt;<=*j* and where is the greatest common divisor of numbers *a* and *b*. Mike is tired. So he asked you to help him in performing these requests.
The first line of input contains numbers *n* and *q* (1<=≤<=*n*,<=*q*<=≤<=2<=×<=105), the number of different kinds of beer and number of queries. The next line contains *n* space separated integers, *a*1,<=*a*2,<=... ,<=*a**n* (1<=≤<=*a**i*<=≤<=5<=×<=105), the height of foam in top of each kind of beer. The next *q* lines contain the queries. Each query consists of a single integer integer *x* (1<=≤<=*x*<=≤<=*n*), the index of a beer that should be added or removed from the shelf.
For each query, print the answer for that query in one line.
[ "5 6\n1 2 3 4 6\n1\n2\n3\n4\n5\n1\n" ]
[ "0\n1\n3\n5\n6\n2\n" ]
none
[ { "input": "5 6\n1 2 3 4 6\n1\n2\n3\n4\n5\n1", "output": "0\n1\n3\n5\n6\n2" }, { "input": "3 3\n151790 360570 1\n2\n3\n3", "output": "0\n1\n0" }, { "input": "1 1\n1\n1", "output": "0" }, { "input": "5 10\n1 1 1 1 1\n1\n2\n3\n4\n5\n5\n4\n3\n2\n1", "output": "0\n1\n3\n6\n10...
2,000
14,745,600
0
14,999
472
Design Tutorial: Learn from Life
[]
null
null
One way to create a task is to learn from life. You can choose some experience in real life, formalize it and then you will get a new task. Let's think about a scene in real life: there are lots of people waiting in front of the elevator, each person wants to go to a certain floor. We can formalize it in the following way. We have *n* people standing on the first floor, the *i*-th person wants to go to the *f**i*-th floor. Unfortunately, there is only one elevator and its capacity equal to *k* (that is at most *k* people can use it simultaneously). Initially the elevator is located on the first floor. The elevator needs |*a*<=-<=*b*| seconds to move from the *a*-th floor to the *b*-th floor (we don't count the time the people need to get on and off the elevator). What is the minimal number of seconds that is needed to transport all the people to the corresponding floors and then return the elevator to the first floor?
The first line contains two integers *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=2000) — the number of people and the maximal capacity of the elevator. The next line contains *n* integers: *f*1,<=*f*2,<=...,<=*f**n* (2<=≤<=*f**i*<=≤<=2000), where *f**i* denotes the target floor of the *i*-th person.
Output a single integer — the minimal time needed to achieve the goal.
[ "3 2\n2 3 4\n", "4 2\n50 100 50 100\n", "10 3\n2 2 2 2 2 2 2 2 2 2\n" ]
[ "8\n", "296\n", "8\n" ]
In first sample, an optimal solution is: 1. The elevator takes up person #1 and person #2. 1. It goes to the 2nd floor. 1. Both people go out of the elevator. 1. The elevator goes back to the 1st floor. 1. Then the elevator takes up person #3. 1. And it goes to the 2nd floor. 1. It picks up person #2. 1. Then it goes to the 3rd floor. 1. Person #2 goes out. 1. Then it goes to the 4th floor, where person #3 goes out. 1. The elevator goes back to the 1st floor.
[ { "input": "3 2\n2 3 4", "output": "8" }, { "input": "4 2\n50 100 50 100", "output": "296" }, { "input": "10 3\n2 2 2 2 2 2 2 2 2 2", "output": "8" }, { "input": "1 1\n2", "output": "2" }, { "input": "2 1\n2 2", "output": "4" }, { "input": "2 2\n2 2", ...
62
1,536,000
3
15,003
989
A Shade of Moonlight
[ "binary search", "geometry", "math", "sortings", "two pointers" ]
null
null
"To curtain off the moonlight should be hardly possible; the shades present its mellow beauty and restful nature." Intonates Mino. "See? The clouds are coming." Kanno gazes into the distance. "That can't be better," Mino turns to Kanno. The sky can be seen as a one-dimensional axis. The moon is at the origin whose coordinate is $0$. There are $n$ clouds floating in the sky. Each cloud has the same length $l$. The $i$-th initially covers the range of $(x_i, x_i + l)$ (endpoints excluded). Initially, it moves at a velocity of $v_i$, which equals either $1$ or $-1$. Furthermore, no pair of clouds intersect initially, that is, for all $1 \leq i \lt j \leq n$, $\lvert x_i - x_j \rvert \geq l$. With a wind velocity of $w$, the velocity of the $i$-th cloud becomes $v_i + w$. That is, its coordinate increases by $v_i + w$ during each unit of time. Note that the wind can be strong and clouds can change their direction. You are to help Mino count the number of pairs $(i, j)$ ($i &lt; j$), such that with a proper choice of wind velocity $w$ not exceeding $w_\mathrm{max}$ in absolute value (possibly negative and/or fractional), the $i$-th and $j$-th clouds both cover the moon at the same future moment. This $w$ doesn't need to be the same across different pairs.
The first line contains three space-separated integers $n$, $l$, and $w_\mathrm{max}$ ($1 \leq n \leq 10^5$, $1 \leq l, w_\mathrm{max} \leq 10^8$) — the number of clouds, the length of each cloud and the maximum wind speed, respectively. The $i$-th of the following $n$ lines contains two space-separated integers $x_i$ and $v_i$ ($-10^8 \leq x_i \leq 10^8$, $v_i \in \{-1, 1\}$) — the initial position and the velocity of the $i$-th cloud, respectively. The input guarantees that for all $1 \leq i \lt j \leq n$, $\lvert x_i - x_j \rvert \geq l$.
Output one integer — the number of unordered pairs of clouds such that it's possible that clouds from each pair cover the moon at the same future moment with a proper choice of wind velocity $w$.
[ "5 1 2\n-2 1\n2 1\n3 -1\n5 -1\n7 -1\n", "4 10 1\n-20 1\n-10 -1\n0 1\n10 -1\n" ]
[ "4\n", "1\n" ]
In the first example, the initial positions and velocities of clouds are illustrated below. The pairs are: - $(1, 3)$, covering the moon at time $2.5$ with $w = -0.4$; - $(1, 4)$, covering the moon at time $3.5$ with $w = -0.6$; - $(1, 5)$, covering the moon at time $4.5$ with $w = -0.7$; - $(2, 5)$, covering the moon at time $2.5$ with $w = -2$. Below is the positions of clouds at time $2.5$ with $w = -0.4$. At this moment, the $1$-st and $3$-rd clouds both cover the moon. In the second example, the only pair is $(1, 4)$, covering the moon at time $15$ with $w = 0$. Note that all the times and wind velocities given above are just examples among infinitely many choices.
[ { "input": "5 1 2\n-2 1\n2 1\n3 -1\n5 -1\n7 -1", "output": "4" }, { "input": "4 10 1\n-20 1\n-10 -1\n0 1\n10 -1", "output": "1" }, { "input": "1 100000000 98765432\n73740702 1", "output": "0" }, { "input": "10 2 3\n-1 -1\n-4 1\n-6 -1\n1 1\n10 -1\n-8 -1\n6 1\n8 1\n4 -1\n-10 -1...
1,044
8,806,400
0
15,053