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
707
Pythagorean Triples
[ "math", "number theory" ]
null
null
Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, triples (3,<=4,<=5), (5,<=12,<=13) and (6,<=8,<=10) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same?
The only line of the input contains single integer *n* (1<=≀<=*n*<=≀<=109)Β β€” the length of some side of a right triangle.
Print two integers *m* and *k* (1<=≀<=*m*,<=*k*<=≀<=1018), such that *n*, *m* and *k* form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer *n*, print <=-<=1 in the only line. If there are many answers, print any of them.
[ "3\n", "6\n", "1\n", "17\n", "67\n" ]
[ "4 5", "8 10", "-1", "144 145", "2244 2245" ]
Illustration for the first sample.
[ { "input": "3", "output": "4 5" }, { "input": "6", "output": "8 10" }, { "input": "1", "output": "-1" }, { "input": "17", "output": "144 145" }, { "input": "67", "output": "2244 2245" }, { "input": "10", "output": "24 26" }, { "input": "14"...
46
0
3
83,651
798
Mike and distribution
[ "constructive algorithms", "sortings" ]
null
null
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers *A*<==<=[*a*1,<=*a*2,<=...,<=*a**n*] and *B*<==<=[*b*1,<=*b*2,<=...,<=*b**n*] of length *n* each which he uses to ask people some quite peculiar questions. To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select *k* numbers *P*<==<=[*p*1,<=*p*2,<=...,<=*p**k*] such that 1<=≀<=*p**i*<=≀<=*n* for 1<=≀<=*i*<=≀<=*k* and elements in *P* are distinct. Sequence *P* will represent indices of elements that you'll select from both sequences. He calls such a subset *P* "unfair" if and only if the following conditions are satisfied: 2Β·(*a**p*1<=+<=...<=+<=*a**p**k*) is greater than the sum of all elements from sequence *A*, and 2Β·(*b**p*1<=+<=...<=+<=*b**p**k*) is greater than the sum of all elements from the sequence *B*. Also, *k* should be smaller or equal to because it will be to easy to find sequence *P* if he allowed you to select too many elements! Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity!
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of elements in the sequences. On the second line there are *n* space-separated integers *a*1,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” elements of sequence *A*. On the third line there are also *n* space-separated integers *b*1,<=...,<=*b**n* (1<=≀<=*b**i*<=≀<=109) β€” elements of sequence *B*.
On the first line output an integer *k* which represents the size of the found subset. *k* should be less or equal to . On the next line print *k* integers *p*1,<=*p*2,<=...,<=*p**k* (1<=≀<=*p**i*<=≀<=*n*) β€” the elements of sequence *P*. You can print the numbers in any order you want. Elements in sequence *P* should be distinct.
[ "5\n8 7 4 8 3\n4 2 5 3 7\n" ]
[ "3\n1 4 5\n" ]
none
[ { "input": "5\n8 7 4 8 3\n4 2 5 3 7", "output": "3\n1 4 5" }, { "input": "27\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864\n67108864 33554432 16777216 8388608 4194304 2097152 1048576 524288 262144 1...
0
0
-1
83,787
559
Randomizer
[ "combinatorics", "geometry", "probabilities" ]
null
null
Gerald got tired of playing board games with the usual six-sided die, and he bought a toy called Randomizer. It functions as follows. A Randomizer has its own coordinate plane on which a strictly convex polygon is painted, the polygon is called a basic polygon. If you shake a Randomizer, it draws some nondegenerate (i.e. having a non-zero area) convex polygon with vertices at some vertices of the basic polygon. The result of the roll (more precisely, the result of the shaking) is considered to be the number of points with integer coordinates, which were strictly inside (the points on the border are not considered) the selected polygon. Now Gerald is wondering: what is the expected result of shaking the Randomizer? During the shaking the Randomizer considers all the possible non-degenerate convex polygons with vertices at the vertices of the basic polygon. Let's assume that there are *k* versions of the polygons. Then the Randomizer chooses each of them with probability .
The first line of the input contains a single integer *n* (3<=≀<=*n*<=≀<=100<=000) β€” the number of vertices of the basic polygon. Next *n* lines contain the coordinates of the vertices of the basic polygon. The *i*-th of these lines contain two integers *x**i* and *y**i* (<=-<=109<=≀<=*x**i*,<=*y**i*<=≀<=109) β€” the coordinates of the *i*-th vertex of the polygon. The vertices are given in the counter-clockwise order.
Print the sought expected value with absolute or relative error at most 10<=-<=9.
[ "4\n0 0\n2 0\n2 2\n0 2\n", "5\n0 0\n2 0\n2 2\n1 3\n0 2\n" ]
[ "0.2\n", "0.8125\n" ]
A polygon is called strictly convex if it is convex and no its vertices lie on the same line. Let's assume that a random variable takes values *x*<sub class="lower-index">1</sub>, ..., *x*<sub class="lower-index">*n*</sub> with probabilities *p*<sub class="lower-index">1</sub>, ..., *p*<sub class="lower-index">*n*</sub>, correspondingly. Then the expected value of this variable equals to <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e592ba0986f3722e82626df9d579add61ddc5d36.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "4\n0 0\n2 0\n2 2\n0 2", "output": "0.2" }, { "input": "5\n0 0\n2 0\n2 2\n1 3\n0 2", "output": "0.8125" }, { "input": "4\n0 0\n3 0\n2 2\n0 3", "output": "1.2" }, { "input": "4\n0 0\n100 0\n67 69\n0 100", "output": "3999.6" }, { "input": "20\n89100 45399...
77
8,396,800
0
83,789
201
Thoroughly Bureaucratic Organization
[ "binary search", "combinatorics" ]
null
null
Once *n* people simultaneously signed in to the reception at the recently opened, but already thoroughly bureaucratic organization (abbreviated TBO). As the organization is thoroughly bureaucratic, it can accept and cater for exactly one person per day. As a consequence, each of *n* people made an appointment on one of the next *n* days, and no two persons have an appointment on the same day. However, the organization workers are very irresponsible about their job, so none of the signed in people was told the exact date of the appointment. The only way to know when people should come is to write some requests to TBO. The request form consists of *m* empty lines. Into each of these lines the name of a signed in person can be written (it can be left blank as well). Writing a person's name in the same form twice is forbidden, such requests are ignored. TBO responds very quickly to written requests, but the reply format is of very poor quality β€” that is, the response contains the correct appointment dates for all people from the request form, but the dates are in completely random order. Responds to all requests arrive simultaneously at the end of the day (each response specifies the request that it answers). Fortunately, you aren't among these *n* lucky guys. As an observer, you have the following task β€” given *n* and *m*, determine the minimum number of requests to submit to TBO to clearly determine the appointment date for each person.
The first line contains a single integer *t* (1<=≀<=*t*<=≀<=1000) β€” the number of test cases. Each of the following *t* lines contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=109) β€” the number of people who have got an appointment at TBO and the number of empty lines in the request form, correspondingly.
Print *t* lines, each containing an answer for the corresponding test case (in the order they are given in the input) β€” the minimum number of requests to submit to TBO.
[ "5\n4 1\n4 2\n7 3\n1 1\n42 7\n" ]
[ "3\n2\n3\n0\n11\n" ]
In the first sample, you need to submit three requests to TBO with three different names. When you learn the appointment dates of three people out of four, you can find out the fourth person's date by elimination, so you do not need a fourth request. In the second sample you need only two requests. Let's number the persons from 1 to 4 and mention persons 1 and 2 in the first request and persons 1 and 3 in the second request. It is easy to see that after that we can clearly determine each person's appointment date regardless of the answers obtained from TBO. In the fourth sample only one person signed up for an appointment. He doesn't need to submit any requests β€” his appointment date is tomorrow.
[ { "input": "5\n4 1\n4 2\n7 3\n1 1\n42 7", "output": "3\n2\n3\n0\n11" }, { "input": "12\n11 4\n9 2\n10000 100\n1000000000 2345\n123456 1234567\n123456 65536\n5 55\n5 3\n2323 10\n999111000 232323\n999888777 777888999\n999888777 777", "output": "4\n6\n198\n852515\n17\n17\n3\n3\n423\n12562\n30\n2570...
92
0
0
83,851
687
Dividing Kingdom II
[ "brute force", "data structures", "dsu", "graphs", "sortings" ]
null
null
Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great. These two had some problems about the numbers they like, so they decided to divide the great kingdom between themselves. The great kingdom consisted of *n* cities numbered from 1 to *n* and *m* bidirectional roads between these cities, numbered from 1 to *m*. The *i*-th road had length equal to *w**i*. The Great Arya and Pari The Great were discussing about destructing some prefix (all road with numbers less than some *x*) and suffix (all roads with numbers greater than some *x*) of the roads so there will remain only the roads with numbers *l*,<=*l*<=+<=1,<=...,<=*r*<=-<=1 and *r*. After that they will divide the great kingdom into two pieces (with each city belonging to exactly one piece) such that the hardness of the division is minimized. The hardness of a division is the maximum length of a road such that its both endpoints are in the same piece of the kingdom. In case there is no such road, the hardness of the division is considered to be equal to <=-<=1. Historians found the map of the great kingdom, and they have *q* guesses about the *l* and *r* chosen by those great rulers. Given these data, for each guess *l**i* and *r**i* print the minimum possible hardness of the division of the kingdom.
The first line of the input contains three integers *n*, *m* and *q* (1<=≀<=*n*,<=*q*<=≀<=1000, )Β β€” the number of cities and roads in the great kingdom, and the number of guesses, respectively. The *i*-th line of the following *m* lines contains three integers *u**i*,<=*v**i* and *w**i* (1<=<=≀<=<=*u**i*,<=<=*v**i*<=<=≀<=<=*n*,<=0<=≀<=*w**i*<=≀<=109), denoting the road number *i* connects cities *u**i* and *v**i* and its length is equal *w**i*. It's guaranteed that no road connects the city to itself and no pair of cities is connected by more than one road. Each of the next *q* lines contains a pair of integers *l**i* and<=*r**i* (1<=<=≀<=*l**i*<=≀<=*r**i*<=≀<=*m*)Β β€” a guess from the historians about the remaining roads in the kingdom.
For each guess print the minimum possible hardness of the division in described scenario.
[ "5 6 5\n5 4 86\n5 1 0\n1 3 38\n2 1 33\n2 4 28\n2 3 40\n3 5\n2 6\n1 3\n2 3\n1 6\n" ]
[ "-1\n33\n-1\n-1\n33\n" ]
none
[]
46
0
0
83,968
494
Helping People
[ "dp", "probabilities" ]
null
null
Malek is a rich man. He also is very generous. That's why he decided to split his money between poor people. A charity institute knows *n* poor people numbered from 1 to *n*. The institute gave Malek *q* recommendations. A recommendation is a segment of people like [*l*,<=*r*] which means the institute recommended that Malek gives one dollar to every person whose number is in this segment. However this charity has very odd rules about the recommendations. Because of those rules the recommendations are given in such a way that for every two recommendation [*a*,<=*b*] and [*c*,<=*d*] one of the following conditions holds: - The two segments are completely disjoint. More formally either *a*<=≀<=*b*<=&lt;<=*c*<=≀<=*d* or *c*<=≀<=*d*<=&lt;<=*a*<=≀<=*b* - One of the two segments are inside another. More formally either *a*<=≀<=*c*<=≀<=*d*<=≀<=*b* or *c*<=≀<=*a*<=≀<=*b*<=≀<=*d*. The goodness of a charity is the value of maximum money a person has after Malek finishes giving his money. The institute knows for each recommendation what is the probability that Malek will accept it. They want to know the expected value of goodness of this charity. So they asked you for help. You have been given the list of recommendations and for each recommendation the probability of it being accepted by Malek. You have also been given how much money each person initially has. You must find the expected value of goodness.
In the first line two space-separated integers *n*,<=*q* (1<=≀<=*n*<=≀<=105, 1<=≀<=*q*<=≀<=5000) are given. In the second line *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109) are given meaning that person number *i* initially has *a**i* dollars. Each of the next *q* lines contains three space-separated numbers *l**i*,<=*r**i*,<=*p**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*, 0<=≀<=*p*<=≀<=1) where *l**i* and *r**i* are two integers describing the segment of recommendation and *p**i* is a real number given with exactly three digits after decimal point which is equal to probability of Malek accepting this recommendation. Note that a segment may appear several times in recommendations.
Output the sought value. Your answer will be considered correct if its absolute or relative error is less than 10<=-<=6.
[ "5 2\n1 7 2 4 3\n1 3 0.500\n2 2 0.500\n", "5 2\n281 280 279 278 282\n1 4 1.000\n1 4 0.000\n", "3 5\n1 2 3\n1 3 0.500\n2 2 0.250\n1 2 0.800\n1 1 0.120\n2 2 0.900\n" ]
[ "8.000000000\n", "282.000000000\n", "4.465000000\n" ]
none
[]
2,000
5,836,800
0
84,023
794
Labelling Cities
[ "dfs and similar", "graphs", "hashing" ]
null
null
Oleg the bank client lives in Bankopolia. There are *n* cities in Bankopolia and some pair of cities are connected directly by bi-directional roads. The cities are numbered from 1 to *n*. There are a total of *m* roads in Bankopolia, the *i*-th road connects cities *u**i* and *v**i*. It is guaranteed that from each city it is possible to travel to any other city using some of the roads. Oleg wants to give a label to each city. Suppose the label of city *i* is equal to *x**i*. Then, it must hold that for all pairs of cities (*u*,<=*v*) the condition |*x**u*<=-<=*x**v*|<=≀<=1 holds if and only if there is a road connecting *u* and *v*. Oleg wonders if such a labeling is possible. Find an example of such labeling if the task is possible and state that it is impossible otherwise.
The first line of input contains two space-separated integers *n* and *m* (2<=≀<=*n*<=≀<=3Β·105, 1<=≀<=*m*<=≀<=3Β·105)Β β€” the number of cities and the number of roads. Next, *m* lines follow. The *i*-th line contains two space-separated integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, *u**i*<=β‰ <=*v**i*)Β β€” the cities connected by the *i*-th road. It is guaranteed that there is at most one road between each pair of cities and it is possible to travel from any city to any other city using some roads.
If the required labeling is not possible, output a single line containing the string "NO" (without quotes). Otherwise, output the string "YES" (without quotes) on the first line. On the next line, output *n* space-separated integers, *x*1,<=*x*2,<=...,<=*x**n*. The condition 1<=≀<=*x**i*<=≀<=109 must hold for all *i*, and for all pairs of cities (*u*,<=*v*) the condition |*x**u*<=-<=*x**v*|<=≀<=1 must hold if and only if there is a road connecting *u* and *v*.
[ "4 4\n1 2\n1 3\n1 4\n3 4\n", "5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n5 4\n", "4 3\n1 2\n1 3\n1 4\n" ]
[ "YES\n2 3 1 1 \n", "YES\n1 1 1 1 1 \n", "NO\n" ]
For the first sample, *x*<sub class="lower-index">1</sub> = 2, *x*<sub class="lower-index">2</sub> = 3, *x*<sub class="lower-index">3</sub> = *x*<sub class="lower-index">4</sub> = 1 is a valid labeling. Indeed, (3, 4), (1, 2), (1, 3), (1, 4) are the only pairs of cities with difference of labels not greater than 1, and these are precisely the roads of Bankopolia. For the second sample, all pairs of cities have difference of labels not greater than 1 and all pairs of cities have a road connecting them. For the last sample, it is impossible to construct a labeling satisfying the given constraints.
[ { "input": "4 4\n1 2\n1 3\n1 4\n3 4", "output": "YES\n2 3 1 1 " }, { "input": "5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n5 4", "output": "YES\n1 1 1 1 1 " }, { "input": "4 3\n1 2\n1 3\n1 4", "output": "NO" }, { "input": "8 12\n1 2\n1 3\n1 4\n2 3\n2 4\n4 5\n5 6\n5 7\n...
62
0
0
84,082
533
Work Group
[ "dfs and similar", "dp", "graphs", "strings", "trees" ]
null
null
One Big Software Company has *n* employees numbered from 1 to *n*. The director is assigned number 1. Every employee of the company except the director has exactly one immediate superior. The director, of course, doesn't have a superior. We will call person *a* a subordinates of another person *b*, if either *b* is an immediate supervisor of *a*, or the immediate supervisor of *a* is a subordinate to person *b*. In particular, subordinates of the head are all other employees of the company. To solve achieve an Important Goal we need to form a workgroup. Every person has some efficiency, expressed by a positive integer *a**i*, where *i* is the person's number. The efficiency of the workgroup is defined as the total efficiency of all the people included in it. The employees of the big software company are obsessed with modern ways of work process organization. Today pair programming is at the peak of popularity, so the workgroup should be formed with the following condition. Each person entering the workgroup should be able to sort all of his subordinates who are also in the workgroup into pairs. In other words, for each of the members of the workgroup the number of his subordinates within the workgroup should be even. Your task is to determine the maximum possible efficiency of the workgroup formed at observing the given condition. Any person including the director of company can enter the workgroup.
The first line contains integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of workers of the Big Software Company. Then *n* lines follow, describing the company employees. The *i*-th line contains two integers *p**i*,<=*a**i* (1<=≀<=*a**i*<=≀<=105) β€” the number of the person who is the *i*-th employee's immediate superior and *i*-th employee's efficiency. For the director *p*1<==<=<=-<=1, for all other people the condition 1<=≀<=*p**i*<=&lt;<=*i* is fulfilled.
Print a single integer β€” the maximum possible efficiency of the workgroup.
[ "7\n-1 3\n1 2\n1 1\n1 4\n4 5\n4 3\n5 2\n" ]
[ "17\n" ]
In the sample test the most effective way is to make a workgroup from employees number 1, 2, 4, 5, 6.
[ { "input": "7\n-1 3\n1 2\n1 1\n1 4\n4 5\n4 3\n5 2", "output": "17" }, { "input": "1\n-1 42", "output": "42" }, { "input": "2\n-1 3\n1 2", "output": "3" }, { "input": "3\n-1 3\n1 1\n1 2", "output": "6" }, { "input": "3\n-1 1\n1 2\n1 3", "output": "6" }, { ...
0
0
-1
84,177
333
Lucky Tickets
[ "brute force", "constructive algorithms" ]
null
null
Gerald has a friend, Pollard. Pollard is interested in lucky tickets (ticket is a sequence of digits). At first he thought that a ticket is lucky if between some its digits we can add arithmetic signs and brackets so that the result obtained by the arithmetic expression was number 100. But he quickly analyzed all such tickets and moved on to a more general question. Now he explores *k*-lucky tickets. Pollard sais that a ticket is *k*-lucky if we can add arithmetic operation signs between its digits to the left or right of them (i.e., "+", "-", "<=Γ—<=") and brackets so as to obtain the correct arithmetic expression whose value would equal *k*. For example, ticket "224201016" is 1000-lucky as (<=-<=2<=-<=(2<=+<=4))<=Γ—<=(2<=+<=0)<=+<=1016<==<=1000. Pollard was so carried away by the lucky tickets that he signed up for a seminar on lucky tickets and, as far as Gerald knows, Pollard will attend it daily at 7 pm in some famous institute and will commute to it in the same tram for *m* days. In this tram tickets have eight digits. And Gerald wants to make a surprise for Pollard: each day Pollard will receive a tram *k*-lucky ticket. The conductor has already agreed to give Pollard certain tickets during all these *m* days and he only wants Gerald to tell him what kind of tickets to give out. In this regard, help Gerald pick exactly *m* distinct *k*-lucky tickets.
The single line contains two integers *k* and *m* (0<=≀<=*k*<=≀<=104, 1<=≀<=*m*<=≀<=3Β·105).
Print *m* lines. Each line must contain exactly 8 digits β€” the *k*-winning ticket. The tickets may begin with 0, all tickets must be distinct. If there are more than *m* distinct *k*-lucky tickets, print any *m* of them. It is guaranteed that at least *m* distinct *k*-lucky tickets exist. The tickets can be printed in any order.
[ "0 3\n", "7 4\n" ]
[ "00000000\n00000001\n00000002\n", "00000007\n00000016\n00000017\n00000018\n" ]
none
[ { "input": "0 3", "output": "00000000\n00000001\n00000002" }, { "input": "7 4", "output": "00000007\n00000016\n00000017\n00000018" }, { "input": "0 10000", "output": "00000000\n00000001\n00000002\n00000003\n00000004\n00000005\n00000006\n00000007\n00000008\n00000009\n00000010\n0000001...
4,000
204,800
0
84,208
82
Two out of Three
[ "dp" ]
D. Two out of Three
2
256
Vasya has recently developed a new algorithm to optimize the reception of customer flow and he considered the following problem. Let the queue to the cashier contain *n* people, at that each of them is characterized by a positive integer *a**i* β€” that is the time needed to work with this customer. What is special about this very cashier is that it can serve two customers simultaneously. However, if two customers need *a**i* and *a**j* of time to be served, the time needed to work with both of them customers is equal to *max*(*a**i*,<=*a**j*). Please note that working with customers is an uninterruptable process, and therefore, if two people simultaneously come to the cashier, it means that they begin to be served simultaneously, and will both finish simultaneously (it is possible that one of them will have to wait). Vasya used in his algorithm an ingenious heuristic β€” as long as the queue has more than one person waiting, then some two people of the first three standing in front of the queue are sent simultaneously. If the queue has only one customer number *i*, then he goes to the cashier, and is served within *a**i* of time. Note that the total number of phases of serving a customer will always be equal to ⌈*n*<=/<=2βŒ‰. Vasya thinks that this method will help to cope with the queues we all hate. That's why he asked you to work out a program that will determine the minimum time during which the whole queue will be served using this algorithm.
The first line of the input file contains a single number *n* (1<=≀<=*n*<=≀<=1000), which is the number of people in the sequence. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=106). The people are numbered starting from the cashier to the end of the queue.
Print on the first line a single number β€” the minimum time needed to process all *n* people. Then on ⌈*n*<=/<=2βŒ‰ lines print the order in which customers will be served. Each line (probably, except for the last one) must contain two numbers separated by a space β€” the numbers of customers who will be served at the current stage of processing. If *n* is odd, then the last line must contain a single number β€” the number of the last served customer in the queue. The customers are numbered starting from 1.
[ "4\n1 2 3 4\n", "5\n2 4 3 1 4\n" ]
[ "6\n1 2\n3 4\n", "8\n1 3\n2 5\n4\n" ]
none
[ { "input": "4\n1 2 3 4", "output": "6\n1 2\n3 4" }, { "input": "5\n2 4 3 1 4", "output": "8\n1 3\n2 5\n4" }, { "input": "1\n10", "output": "10\n1" }, { "input": "2\n3 5", "output": "5\n1 2" }, { "input": "3\n1 10 1", "output": "11\n1 2\n3" }, { "input"...
92
0
0
84,217
85
Sum of Medians
[ "binary search", "brute force", "data structures", "implementation" ]
D. Sum of Medians
3
256
In one well-known algorithm of finding the *k*-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance speed on a modern video card, you should be able to find a sum of medians in each five of the array. A sum of medians of a sorted *k*-element set *S*<==<={*a*1,<=*a*2,<=...,<=*a**k*}, where *a*1<=&lt;<=*a*2<=&lt;<=*a*3<=&lt;<=...<=&lt;<=*a**k*, will be understood by as The operator stands for taking the remainder, that is stands for the remainder of dividing *x* by *y*. To organize exercise testing quickly calculating the sum of medians for a changing set was needed.
The first line contains number *n* (1<=≀<=*n*<=≀<=105), the number of operations performed. Then each of *n* lines contains the description of one of the three operations: - add *x*Β β€” add the element *x* to the set; - del *x*Β β€” delete the element *x* from the set; - sumΒ β€” find the sum of medians of the set. For any add *x* operation it is true that the element *x* is not included in the set directly before the operation. For any del *x* operation it is true that the element *x* is included in the set directly before the operation. All the numbers in the input are positive integers, not exceeding 109.
For each operation sum print on the single line the sum of medians of the current set. If the set is empty, print 0. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator).
[ "6\nadd 4\nadd 5\nadd 1\nadd 2\nadd 3\nsum\n", "14\nadd 1\nadd 7\nadd 2\nadd 5\nsum\nadd 6\nadd 8\nadd 9\nadd 3\nadd 4\nadd 10\nsum\ndel 1\nsum\n" ]
[ "3\n", "5\n11\n13\n" ]
none
[ { "input": "6\nadd 4\nadd 5\nadd 1\nadd 2\nadd 3\nsum", "output": "3" }, { "input": "14\nadd 1\nadd 7\nadd 2\nadd 5\nsum\nadd 6\nadd 8\nadd 9\nadd 3\nadd 4\nadd 10\nsum\ndel 1\nsum", "output": "5\n11\n13" }, { "input": "11\nadd 1\nsum\nadd 2\nsum\nadd 3\nsum\nadd 4\nsum\nadd 5\nsum\nadd ...
3,000
28,569,600
0
84,333
798
Mike and code of a permutation
[ "constructive algorithms", "data structures", "graphs", "sortings" ]
null
null
Mike has discovered a new way to encode permutations. If he has a permutation *P*<==<=[*p*1,<=*p*2,<=...,<=*p**n*], he will encode it in the following way: Denote by *A*<==<=[*a*1,<=*a*2,<=...,<=*a**n*] a sequence of length *n* which will represent the code of the permutation. For each *i* from 1 to *n* sequentially, he will choose the smallest unmarked *j* (1<=≀<=*j*<=≀<=*n*) such that *p**i*<=&lt;<=*p**j* and will assign to *a**i* the number *j* (in other words he performs *a**i*<==<=*j*) and will mark *j*. If there is no such *j*, he'll assign to *a**i* the number <=-<=1 (he performs *a**i*<==<=<=-<=1). Mike forgot his original permutation but he remembers its code. Your task is simple: find any permutation such that its code is the same as the code of Mike's original permutation. You may assume that there will always be at least one valid permutation.
The first line contains single integer *n* (1<=≀<=*n*<=≀<=500<=000) β€” length of permutation. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n* or *a**i*<==<=<=-<=1) β€” the code of Mike's permutation. You may assume that all positive values from *A* are different.
In first and only line print *n* numbers *p*1,<=*p*2,<=...,<=*p**n* (1<=≀<=*p**i*<=≀<=*n*) β€” a permutation *P* which has the same code as the given one. Note that numbers in permutation are distinct.
[ "6\n2 -1 1 5 -1 4\n", "8\n2 -1 4 -1 6 -1 8 -1\n" ]
[ "2 6 1 4 5 3\n", "1 8 2 7 3 6 4 5\n" ]
For the permutation from the first example: *i* = 1, the smallest *j* is 2 because *p*<sub class="lower-index">2</sub> = 6 &gt; *p*<sub class="lower-index">1</sub> = 2. *i* = 2, there is no *j* because *p*<sub class="lower-index">2</sub> = 6 is the greatest element in the permutation. *i* = 3, the smallest *j* is 1 because *p*<sub class="lower-index">1</sub> = 2 &gt; *p*<sub class="lower-index">3</sub> = 1. *i* = 4, the smallest *j* is 5 (2 was already marked) because *p*<sub class="lower-index">5</sub> = 5 &gt; *p*<sub class="lower-index">4</sub> = 4. *i* = 5, there is no *j* because 2 is already marked. *i* = 6, the smallest *j* is 4 because *p*<sub class="lower-index">4</sub> = 4 &gt; *p*<sub class="lower-index">6</sub> = 3.
[]
0
0
-1
84,709
164
Polycarpus and Tasks
[]
null
null
Polycarpus has many tasks. Each task is characterized by three integers *l**i*, *r**i* and *t**i*. Three integers (*l**i*,<=*r**i*,<=*t**i*) mean that to perform task *i*, one needs to choose an integer *s**i* (*l**i*<=≀<=*s**i*;Β *s**i*<=+<=*t**i*<=-<=1<=≀<=*r**i*), then the task will be carried out continuously for *t**i* units of time, starting at time *s**i* and up to time *s**i*<=+<=*t**i*<=-<=1, inclusive. In other words, a task is performed for a continuous period of time lasting *t**i*, should be started no earlier than *l**i*, and completed no later than *r**i*. Polycarpus's tasks have a surprising property: for any task *j*,<=*k* (with *j*<=&lt;<=*k*) *l**j*<=&lt;<=*l**k* and *r**j*<=&lt;<=*r**k*. Let's suppose there is an ordered set of tasks *A*, containing |*A*| tasks. We'll assume that *a**j*<==<=(*l**j*,<=*r**j*,<=*t**j*) (1<=≀<=*j*<=≀<=|*A*|). Also, we'll assume that the tasks are ordered by increasing *l**j* with the increase in number. Let's consider the following recursive function *f*, whose argument is an ordered set of tasks *A*, and the result is an integer. The function *f*(*A*) is defined by the greedy algorithm, which is described below in a pseudo-language of programming. - Step 1. , *ans*<==<=0. - Step 2. We consider all tasks in the order of increasing of their numbers in the set *A*. Lets define the current task counter *i*<==<=0. - Step 3. Consider the next task: *i*<==<=*i*<=+<=1. If *i*<=&gt;<=|*A*| fulfilled, then go to the 8 step. - Step 4. If you can get the task done starting at time *s**i* = max(*ans*<=+<=1,<=*l**i*), then do the task *i*: *s**i* = max(*ans*<=+<=1, *l**i*), *ans*<==<=*s**i*<=+<=*t**i*<=-<=1, . Go to the next task (step 3). - Step 5. Otherwise, find such task , that first, task *a**i* can be done at time *s**i* = max, and secondly, the value of is positive and takes the maximum value among all *b**k* that satisfy the first condition. If you can choose multiple tasks as *b**k*, choose the one with the maximum number in set *A*. - Step 6. If you managed to choose task *b**k*, then , . Go to the next task (step 3). - Step 7. If you didn't manage to choose task *b**k*, then skip task *i*. Go to the next task (step 3). - Step 8. Return *ans* as a result of executing *f*(*A*). Polycarpus got entangled in all these formulas and definitions, so he asked you to simulate the execution of the function *f*, calculate the value of *f*(*A*).
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of tasks in set *A*. Then *n* lines describe the tasks. The *i*-th line contains three space-separated integers *l**i*, *r**i*, *t**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=109, 1<=≀<=*t**i*<=≀<=*r**i*<=-<=*l**i*<=+<=1) β€” the description of the *i*-th task. It is guaranteed that for any tasks *j*,<=*k* (considering that *j*<=&lt;<=*k*) the following is true: *l**j*<=&lt;<=*l**k* and *r**j*<=&lt;<=*r**k*.
For each task *i* print a single integer β€” the result of processing task *i* on the *i*-th iteration of the cycle (step 3) in function *f*(*A*). In the *i*-th line print: - 0 β€” if you managed to add task *i* on step 4. - -1 β€” if you didn't manage to add or replace task *i* (step 7). - *res**i* (1<=≀<=*res**i*<=≀<=*n*) β€” if you managed to replace the task (step 6): *res**i* equals the task number (in set *A*), that should be chosen as *b**k* and replaced by task *a**i*.
[ "5\n1 8 5\n2 9 3\n3 10 3\n8 11 4\n11 12 2\n", "13\n1 8 5\n2 9 4\n3 10 1\n4 11 3\n8 12 5\n9 13 5\n10 14 5\n11 15 1\n12 16 1\n13 17 1\n14 18 3\n15 19 3\n16 20 2\n" ]
[ "0 0 1 0 -1 ", "0 0 0 2 -1 -1 0 0 0 0 7 0 12 " ]
none
[]
60
0
0
84,765
720
Cipher
[ "implementation" ]
null
null
Borya has recently found a big electronic display. The computer that manages the display stores some integer number. The number has *n* decimal digits, the display shows the encoded version of the number, where each digit is shown using some lowercase letter of the English alphabet. There is a legend near the display, that describes how the number is encoded. For each digit position *i* and each digit *j* the character *c* is known, that encodes this digit at this position. Different digits can have the same code characters. Each second the number is increased by 1. And one second after a moment when the number reaches the value that is represented as *n* 9-s in decimal notation, the loud beep sounds. Andrew knows the number that is stored in the computer. Now he wants to know how many seconds must pass until Borya can definitely tell what was the original number encoded by the display. Assume that Borya can precisely measure time, and that the encoded number will first be increased exactly one second after Borya started watching at the display.
Input data contains multiple test cases. The first line of input contains *t* (1<=≀<=*t*<=≀<=100)Β β€” the number of test cases. Each test case is described as follows. The first line of the description contains *n* (1<=≀<=*n*<=≀<=18)Β β€” the number of digits in the number. The second line contains *n* decimal digits without spaces (but possibly with leading zeroes)Β β€” the number initially stored in the display computer. The following *n* lines contain 10 characters each. The *j*-th character of the *i*-th of these lines is the code character for a digit *j*<=-<=1 in position *i*, most significant digit positions are described first.
For each test case print an integer: the number of seconds until Borya definitely knows what was the initial number stored on the display of the computer. Do not print leading zeroes.
[ "3\n2\n42\nabcdefghij\njihgfedcba\n2\n42\naaaaaaaaaa\naaaaaaaaaa\n1\n2\nabcdabcdff\n" ]
[ "0\n58\n2\n" ]
none
[]
15
0
0
84,796
612
Simba on the Circle
[ "dp" ]
null
null
You are given a circular array with *n* elements. The elements are numbered from some element with values from 1 to *n* in clockwise order. The *i*-th cell contains the value *a**i*. The robot Simba is in cell *s*. Each moment of time the robot is in some of the *n* cells (at the begin he is in *s*). In one turn the robot can write out the number written in current cell or move to the adjacent cell in clockwise or counterclockwise direction. To write out the number from the cell Simba doesn't spend any time, but to move to adjacent cell Simba spends one unit of time. Simba wants to write the number from each cell one time, so the numbers will be written in a non decreasing order. Find the least number of time units to write out all numbers.
The first line contains two integers *n* and *s* (1<=≀<=*s*<=≀<=*n*<=≀<=2000) β€” the number of cells in the circular array and the starting position of Simba. The second line contains *n* integers *a**i* (<=-<=109<=≀<=*a**i*<=≀<=109) β€” the number written in the *i*-th cell. The numbers are given for cells in order from 1 to *n*. Some of numbers *a**i* can be equal.
In the first line print the number *t* β€” the least number of time units. Each of the next *n* lines should contain the direction of robot movement and the number of cells to move in that direction. After that movement the robot writes out the number from the cell in which it turns out. The direction and the number of cells should be printed in the form of +x in case of clockwise movement and -x in case of counterclockwise movement to *x* cells (0<=≀<=*x*<=≀<=*n*<=-<=1). Note that the sum of absolute values of *x* should be equal to *t*.
[ "9 1\n0 1 2 2 2 1 0 1 1\n", "8 1\n0 1 0 1 0 1 0 1\n", "8 1\n1 2 3 4 5 6 7 8\n", "8 1\n0 0 0 0 0 0 0 0\n" ]
[ "12\n+0\n-3\n-1\n+2\n+1\n+2\n+1\n+1\n+1\n", "13\n+0\n+2\n+2\n+2\n-1\n+2\n+2\n+2\n", "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1\n", "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1\n" ]
none
[ { "input": "9 1\n0 1 2 2 2 1 0 1 1", "output": "12\n+0\n-3\n-1\n+2\n+1\n+2\n+1\n+1\n+1" }, { "input": "8 1\n0 1 0 1 0 1 0 1", "output": "13\n+0\n+2\n+2\n+2\n-1\n+2\n+2\n+2" }, { "input": "8 1\n1 2 3 4 5 6 7 8", "output": "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1" }, { "input": "8 1\...
62
614,400
0
84,964
991
Concise and clear
[ "brute force", "greedy", "implementation", "math" ]
null
null
Vasya is a regular participant at programming contests and is already experienced in finding important sentences in long statements. Of course, numbers constraints are importantΒ β€” factorization of a number less than 1000000 is easier than of a number less than 1000000000. However, sometimes it's hard to understand the number at the first glance. Could it be shortened? For example, instead of 1000000 you could write $10^{6}$, instead of 1000000000 Β β€”$10^{9}$, instead of 1000000007Β β€” $10^{9}+7$. Vasya decided that, to be concise, the notation should follow several rules: - the notation should only consist of numbers, operations of addition ("+"), multiplication ("*") and exponentiation ("^"), in particular, the use of braces is forbidden; - the use of several exponentiation operations in a row is forbidden, for example, writing "2^3^4" is unacceptable; - the value of the resulting expression equals to the initial number; - the notation should consist of the minimal amount of symbols. Given $n$, find the equivalent concise notation for it.
The only line contains a single integer $n$ ($1 \leq n \leq 10\,000\,000\,000$).
Output a concise notation of the number $n$. If there are several concise notations, output any of them.
[ "2018\n", "1000000007\n", "10000000000\n", "2000000000\n" ]
[ "2018\n", "10^9+7\n", "100^5\n", "2*10^9\n" ]
The third sample allows the answer 10^10 also of the length $5$.
[]
30
0
0
85,024
309
Context Advertising
[ "dp", "two pointers" ]
null
null
Advertising has become part of our routine. And now, in the era of progressive technologies, we need your ideas to make advertising better! In this problem we'll look at a simplified version of context advertising. You've got a text, consisting of exactly *n* words. A standard advertising banner has exactly *r* lines, each line can contain at most *c* characters. The potential customer always likes it when they can see lots of advertising, so you should determine which maximum number of consecutive words from the text can be written on the banner. Single words in one line of the banner should be separated by spaces. You are allowed to insert more than one space at once. Note that you are not allowed to break the words, that is, each word in the text must occupy exactly one line in the banner. Besides, you cannot change the word order, that is, if you read the banner text consecutively, from top to bottom and from left to right, you should get some consecutive part of the advertisement text. More formally, the statement can be written like that. Let's say that all words are indexed from 1 to *n* in the order in which they occur in the advertisement text. Then you have to choose all words, starting from some *i*-th one and ending with some *j*-th one (1<=≀<=*i*<=≀<=*j*<=≀<=*n*), so that all of them could be written on the banner. There must be as many words as possible. See the samples for clarifications.
The first input line contains three integers *n*, *r*, *c* (1<=≀<=*n*,<=*r*,<=*c*<=≀<=106;Β *r*<=Γ—<=*c*<=≀<=106). The next line contains a text, consisting of *n* words. The words consist only of lowercase English letters and are not empty. The words in the lines are separated by single spaces. The total number of characters in all words doesn't exceed 5Β·106.
Print at most *r* lines, in each line print at most *c* characters β€” the optimal advertisement banner. If there are multiple advertisement banners, print any of them. Note that some lines of the banner can be empty. You are allowed not to print such lines.
[ "9 4 12\nthis is a sample text for croc final round\n", "9 1 9\nthis is a sample text for croc final round\n", "6 2 3\ncroc a a a croc a\n", "2 2 5\nfirst second\n" ]
[ "this is a\nsample text\nfor croc\nfinal round\n", "this is a\n", "a a\na\n", "first\n" ]
none
[]
30
0
0
85,737
566
Replicating Processes
[ "constructive algorithms", "greedy" ]
null
null
A Large Software Company develops its own social network. Analysts have found that during the holidays, major sporting events and other significant events users begin to enter the network more frequently, resulting in great load increase on the infrastructure. As part of this task, we assume that the social network is 4*n* processes running on the *n* servers. All servers are absolutely identical machines, each of which has a volume of RAM of 1 GB = 1024 MB (1). Each process takes 100 MB of RAM on the server. At the same time, the needs of maintaining the viability of the server takes about 100 more megabytes of RAM. Thus, each server may have up to 9 different processes of social network. Now each of the *n* servers is running exactly 4 processes. However, at the moment of peak load it is sometimes necessary to replicate the existing 4*n* processes by creating 8*n* new processes instead of the old ones. More formally, there is a set of replication rules, the *i*-th (1<=≀<=*i*<=≀<=4*n*) of which has the form of *a**i*<=β†’<=(*b**i*,<=*c**i*), where *a**i*, *b**i* and *c**i* (1<=≀<=*a**i*,<=*b**i*,<=*c**i*<=≀<=*n*) are the numbers of servers. This means that instead of an old process running on server *a**i*, there should appear two new copies of the process running on servers *b**i* and *c**i*. The two new replicated processes can be on the same server (i.e., *b**i* may be equal to *c**i*) or even on the same server where the original process was (i.e. *a**i* may be equal to *b**i* or *c**i*). During the implementation of the rule *a**i*<=β†’<=(*b**i*,<=*c**i*) first the process from the server *a**i* is destroyed, then appears a process on the server *b**i*, then appears a process on the server *c**i*. There is a set of 4*n* rules, destroying all the original 4*n* processes from *n* servers, and creating after their application 8*n* replicated processes, besides, on each of the *n* servers will be exactly 8 processes. However, the rules can only be applied consecutively, and therefore the amount of RAM of the servers imposes limitations on the procedure for the application of the rules. According to this set of rules determine the order in which you want to apply all the 4*n* rules so that at any given time the memory of each of the servers contained at most 9 processes (old and new together), or tell that it is impossible.
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=30<=000) β€” the number of servers of the social network. Next 4*n* lines contain the rules of replicating processes, the *i*-th (1<=≀<=*i*<=≀<=4*n*) of these lines as form *a**i*,<=*b**i*,<=*c**i* (1<=≀<=*a**i*,<=*b**i*,<=*c**i*<=≀<=*n*) and describes rule *a**i*<=β†’<=(*b**i*,<=*c**i*). It is guaranteed that each number of a server from 1 to *n* occurs four times in the set of all *a**i*, and eight times among a set that unites all *b**i* and *c**i*.
If the required order of performing rules does not exist, print "NO" (without the quotes). Otherwise, print in the first line "YES" (without the quotes), and in the second line β€” a sequence of 4*n* numbers from 1 to 4*n*, giving the numbers of the rules in the order they are applied. The sequence should be a permutation, that is, include each number from 1 to 4*n* exactly once. If there are multiple possible variants, you are allowed to print any of them.
[ "2\n1 2 2\n1 2 2\n1 2 2\n1 2 2\n2 1 1\n2 1 1\n2 1 1\n2 1 1\n", "3\n1 2 3\n1 1 1\n1 1 1\n1 1 1\n2 1 3\n2 2 2\n2 2 2\n2 2 2\n3 1 2\n3 3 3\n3 3 3\n3 3 3\n" ]
[ "YES\n1 2 5 6 3 7 4 8\n", "YES\n2 3 4 6 7 8 10 11 12 1 5 9\n" ]
<sup class="upper-index">(1)</sup> To be extremely accurate, we should note that the amount of server memory is 1 GiB = 1024 MiB and processes require 100 MiB RAM where a gibibyte (GiB) is the amount of RAM of 2<sup class="upper-index">30</sup> bytes and a mebibyte (MiB) is the amount of RAM of 2<sup class="upper-index">20</sup> bytes. In the first sample test the network uses two servers, each of which initially has four launched processes. In accordance with the rules of replication, each of the processes must be destroyed and twice run on another server. One of the possible answers is given in the statement: after applying rules 1 and 2 the first server will have 2 old running processes, and the second server will have 8 (4 old and 4 new) processes. After we apply rules 5 and 6, both servers will have 6 running processes (2 old and 4 new). After we apply rules 3 and 7, both servers will have 7 running processes (1 old and 6 new), and after we apply rules 4 and 8, each server will have 8 running processes. At no time the number of processes on a single server exceeds 9. In the second sample test the network uses three servers. On each server, three processes are replicated into two processes on the same server, and the fourth one is replicated in one process for each of the two remaining servers. As a result of applying rules 2, 3, 4, 6, 7, 8, 10, 11, 12 each server would have 7 processes (6 old and 1 new), as a result of applying rules 1, 5, 9 each server will have 8 processes. At no time the number of processes on a single server exceeds 9.
[ { "input": "2\n1 2 2\n1 2 2\n1 2 2\n1 2 2\n2 1 1\n2 1 1\n2 1 1\n2 1 1", "output": "YES\n1 2 5 3 6 7 4 8" }, { "input": "3\n1 2 3\n1 1 1\n1 1 1\n1 1 1\n2 1 3\n2 2 2\n2 2 2\n2 2 2\n3 1 2\n3 3 3\n3 3 3\n3 3 3", "output": "YES\n1 2 3 4 5 6 7 8 9 10 11 12" }, { "input": "3\n1 2 3\n2 3 1\n3 1 ...
124
409,600
0
85,777
394
Dominoes
[ "constructive algorithms", "greedy" ]
null
null
During the break, we decided to relax and play dominoes. Our box with Domino was empty, so we decided to borrow the teacher's dominoes. The teacher responded instantly at our request. He put *nm* dominoes on the table as an *n*<=Γ—<=2*m* rectangle so that each of the *n* rows contained *m* dominoes arranged horizontally. Each half of each domino contained number (0 or 1). We were taken aback, and the teacher smiled and said: "Consider some arrangement of dominoes in an *n*<=Γ—<=2*m* matrix. Let's count for each column of the matrix the sum of numbers in this column. Then among all such sums find the maximum one. Can you rearrange the dominoes in the matrix in such a way that the maximum sum will be minimum possible? Note that it is prohibited to change the orientation of the dominoes, they all need to stay horizontal, nevertheless dominoes are allowed to rotate by 180 degrees. As a reward I will give you all my dominoes". We got even more taken aback. And while we are wondering what was going on, help us make an optimal matrix of dominoes.
The first line contains integers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=103). In the next lines there is a description of the teachers' matrix. Each of next *n* lines contains *m* dominoes. The description of one domino is two integers (0 or 1), written without a space β€” the digits on the left and right half of the domino.
Print the resulting matrix of dominoes in the format: *n* lines, each of them contains *m* space-separated dominoes. If there are multiple optimal solutions, print any of them.
[ "2 3\n01 11 00\n00 01 11\n", "4 1\n11\n10\n01\n00\n" ]
[ "11 11 10\n00 00 01\n", "11\n10\n01\n00\n" ]
Consider the answer for the first sample. There, the maximum sum among all columns equals 1 (the number of columns is 6, and not 3). Obviously, this maximum can't be less than 1, then such matrix is optimal. Note that the dominoes can be rotated by 180 degrees.
[ { "input": "2 3\n01 11 00\n00 01 11", "output": "11 11 10\n00 00 01" }, { "input": "4 1\n11\n10\n01\n00", "output": "11\n10\n01\n00" }, { "input": "1 1\n00", "output": "00" }, { "input": "1 1\n01", "output": "10" }, { "input": "1 1\n11", "output": "11" }, ...
46
0
0
85,836
771
Bear and Isomorphic Points
[ "geometry", "two pointers" ]
null
null
Bearland is a big square on the plane. It contains all points with coordinates not exceeding 106 by the absolute value. There are *n* houses in Bearland. The *i*-th of them is located at the point (*x**i*,<=*y**i*). The *n* points are distinct, but some subsets of them may be collinear. Bear Limak lives in the first house. He wants to destroy his house and build a new one somewhere in Bearland. Bears don't like big changes. For every three points (houses) *p**i*, *p**j* and *p**k*, the sign of their cross product (*p**j*<=-<=*p**i*)<=Γ—<=(*p**k*<=-<=*p**i*) should be the same before and after the relocation. If it was negative/positive/zero, it should still be negative/positive/zero respectively. This condition should be satisfied for all triples of indices (*i*,<=*j*,<=*k*), possibly equal to each other or different than 1. Additionally, Limak isn't allowed to build the house at the point where some other house already exists (but it can be the point where his old house was). In the formula above, we define the difference and the cross product of points (*a**x*,<=*a**y*) and (*b**x*,<=*b**y*) as: Consider a set of possible new placements of Limak's house. Your task is to find the area of that set of points. Formally, let's say that Limak chooses the new placement randomly (each coordinate is chosen independently uniformly at random from the interval [<=-<=106,<=106]). Let *p* denote the probability of getting the allowed placement of new house. Let *S* denote the area of Bearland (*S*<==<=4Β·1012). Your task is to find *p*Β·*S*.
The first line of the input contains an integer *T* (1<=≀<=*T*<=≀<=500)Β β€” the number of test cases. The description of the test cases follows. The first line of the description of a test case contains an integer *n* (3<=≀<=*n*<=≀<=200<=000)Β β€” the number of houses. The *i*-th of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=106<=≀<=*x**i*,<=*y**i*<=≀<=106)Β β€” coordinates of the *i*-th house. No two houses are located at the same point in the same test case. Limak lives in the first house. The sum of *n* won't exceed 200<=000.
Print one real value, denoting the area of the set of points that are possible new placements of Limak's house. Your answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=6. More precisely, let the jury's answer be *b*, and your answer be *a*. Then your answer will be accepted if and only if .
[ "4\n4\n5 3\n0 1\n10 1\n3 51\n3\n-999123 700000\n-950000 123456\n-950000 987654\n3\n2 3\n10 -1\n-4 6\n5\n1 3\n5 2\n6 1\n4 4\n-3 3\n" ]
[ "250.000000000000\n100000000000.000000000000\n0.000000000000\n6.562500000000\n" ]
In the sample test, there are 4 test cases. In the first test case, there are four houses and Limak's one is in (5, 3). The set of valid new placements form a triangle with vertices in points (0, 1), (10, 1) and (3, 51), without its sides. The area of such a triangle is 250. In the second test case, the set of valid new placements form a rectangle of width 50 000 and height 2 000 000. Don't forget that the new placement must be inside the big square that represents Bearland. In the third test case, the three given points are collinear. Each cross product is equal to 0 and it should be 0 after the relocation as well. Hence, Limak's new house must lie on the line that goes through the given points. Since it must also be inside the big square, new possible placements are limited to some segment (excluding the two points where the other houses are). The area of any segment is 0.
[]
46
0
0
86,011
62
Wormhouse
[ "dfs and similar", "graphs" ]
D. Wormhouse
2
256
Arnie the Worm has finished eating an apple house yet again and decided to move. He made up his mind on the plan, the way the rooms are located and how they are joined by corridors. He numbered all the rooms from 1 to *n*. All the corridors are bidirectional. Arnie wants the new house to look just like the previous one. That is, it should have exactly *n* rooms and, if a corridor from room *i* to room *j* existed in the old house, it should be built in the new one. We know that during the house constructing process Arnie starts to eat an apple starting from some room and only stops when he eats his way through all the corridors and returns to the starting room. It is also known that Arnie eats without stopping. That is, until Arnie finishes constructing the house, he is busy every moment of his time gnawing a new corridor. Arnie doesn't move along the already built corridors. However, gnawing out corridors in one and the same order any time you change a house is a very difficult activity. That's why Arnie, knowing the order in which the corridors were located in the previous house, wants to gnaw corridors in another order. It is represented as a list of rooms in the order in which they should be visited. The new list should be lexicographically smallest, but it also should be strictly lexicographically greater than the previous one. Help the worm.
The first line contains two integers *n* and *m* (3<=≀<=*n*<=≀<=100,<=3<=≀<=*m*<=≀<=2000). It is the number of rooms and corridors in Arnie's house correspondingly. The next line contains *m*<=+<=1 positive integers that do not exceed *n*. They are the description of Arnie's old path represented as a list of rooms he visited during the gnawing. It is guaranteed that the last number in the list coincides with the first one. The first room described in the list is the main entrance, that's why Arnie should begin gnawing from it. You may assume that there is no room which is connected to itself and there is at most one corridor between any pair of rooms. However, it is possible to find some isolated rooms which are disconnected from others.
Print *m*<=+<=1 positive integers that do not exceed *n*. Those numbers are the description of the new path, according to which Arnie should gnaw out his new house. If it is impossible to find new path you should print out No solution. The first number in your answer should be equal to the last one. Also it should be equal to the main entrance.
[ "3 3\n1 2 3 1\n", "3 3\n1 3 2 1\n" ]
[ "1 3 2 1 ", "No solution" ]
none
[ { "input": "3 3\n1 2 3 1", "output": "1 3 2 1 " }, { "input": "3 3\n1 3 2 1", "output": "No solution" }, { "input": "4 4\n1 2 4 3 1", "output": "1 3 4 2 1 " }, { "input": "6 7\n3 2 4 1 6 5 1 3", "output": "No solution" }, { "input": "8 12\n4 6 5 1 4 3 1 8 3 7 8 5 ...
280
2,662,400
0
86,183
254
Dormitory
[ "dp", "implementation" ]
null
null
Student Vasya came to study in Berland State University from the country, so he is living in a dormitory. A semester has *n* days, and in each of those days his parents send him some food. In the morning of the *i*-th day he receives *a**i* kilograms of food that can be eaten on that day and on the next one (then the food goes bad and becomes unfit for consumption). Every day Vasya eats *v* kilograms of food. It is known that Vasya's parents do not allow him to starve, so there always is enough food for Vasya. Vasya has *m* friends who sometimes live with him. Let's index the friends from 1 to *m*. Friend number *j* lives with Vasya from day *l**j* to day *r**j*, inclusive. Also, the *j*-th friend requires *f**j* kilograms of food per day. Usually Vasya's friends eat in the canteen, but sometimes generous Vasya feeds some of them. Every day Vasya can feed some friends who live with him this day (or may feed nobody). Every time Vasya feeds his friend, he gives him as much food as the friend needs for the day, and Vasya's popularity rating at the University increases by one. Vasya cannot feed the same friend multiple times in one day. In addition, he knows that eating habits must be regular, so he always eats *v* kilograms of food per day. Vasya wants so choose whom he will feed each day of the semester to make his rating as high as possible. Originally Vasya's rating is 0 because he is a freshman.
The first line contains two integers *n* and *v* (1<=≀<=*n*,<=*v*<=≀<=400). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=400), separated by single spaces. Value *a**i* means that in the morning of the *i*-th day *a**i* kilograms of food come, the food is good for eating on day *i* and/or on day *i*<=+<=1 (then the food goes bad). It is guaranteed that if Vasya doesn't feed anyone, there is a way for him to eat so as to consume *v* kilograms of food every day. The third line contains integer *m* (1<=≀<=*m*<=≀<=400). Each of the following *m* lines describes one Vasya's friend: the *j*-th of these lines contains three integers *l**j*,<=*r**j*,<=*f**j* (1<=≀<=*l**j*<=≀<=*r**j*<=≀<=*n*,<=1<=≀<=*f**j*<=≀<=400), separated by single spaces.
In the first line print the highest rating Vasya can reach. In the next *n* lines print, which friends Vasya needs to feed on each day. In the *i*-th of these lines first print the number of friends to feed on the *i*-th day, and then list the indexes of these friends. Print the friends in these lists in any order. If there are multiple optimal solutions, print any of them.
[ "4 1\n3 2 5 4\n3\n1 3 2\n1 4 1\n3 4 2\n" ]
[ "7\n1 2\n1 2\n3 2 1 3\n2 2 3\n" ]
none
[]
124
1,843,200
0
86,391
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"...
374
0
0
86,432
853
Boredom
[ "data structures" ]
null
null
Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems. First Ilya has drawn a grid of size *n*<=Γ—<=*n* and marked *n* squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly *n*Β·(*n*<=-<=1)<=/<=2 beautiful rectangles. Ilya has chosen *q* query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one. Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles.
The first line of input contains two integers *n* and *q* (2<=≀<=*n*<=≀<=200<=000, 1<=≀<=*q*<=≀<=200<=000)Β β€” the size of the grid and the number of query rectangles. The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n*, separated by spaces (1<=≀<=*p**i*<=≀<=*n*, all *p**i* are different), they specify grid squares marked by Ilya: in column *i* he has marked a square at row *p**i*, rows are numbered from 1 to *n*, bottom to top, columns are numbered from 1 to *n*, left to right. The following *q* lines describe query rectangles. Each rectangle is described by four integers: *l*,<=*d*,<=*r*,<=*u* (1<=≀<=*l*<=≀<=*r*<=≀<=*n*, 1<=≀<=*d*<=≀<=*u*<=≀<=*n*), here *l* and *r* are the leftmost and the rightmost columns of the rectangle, *d* and *u* the bottommost and the topmost rows of the rectangle.
For each query rectangle output its beauty degree on a separate line.
[ "2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2\n", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3\n" ]
[ "1\n1\n1\n", "3\n5\n" ]
The first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1. In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: <img class="tex-graphics" src="https://espresso.codeforces.com/ab1c66ff4bb212f2eaa1df6a3126acc2bc79535c.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/e4576fb30a1da895a09193faaf3d92ff2bf16cc9.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/3d67c946457a83004c99989b9ccf062d66affd1f.png" style="max-width: 100.0%;max-height: 100.0%;"/> There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: <img class="tex-graphics" src="https://espresso.codeforces.com/56bf16b394d5a08e28d2fd8d5ad57392f1cc10e0.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/c7d7961cd0d50c64b5a1ec104f16bc95254199a2.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/fa6096a3000f51ae6f1d798801441540454f6b9f.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/c87e98c04601ad2d95d654f7f31371198d98d1d6.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/cab8b66423587e485b08708148a8ce2194ac9558.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[]
62
0
0
86,530
618
Robot Arm
[ "data structures", "geometry" ]
null
null
Roger is a robot. He has an arm that is a series of *n* segments connected to each other. The endpoints of the *i*-th segment are initially located at points (*i*<=-<=1,<=0) and (*i*,<=0). The endpoint at (*i*<=-<=1,<=0) is colored red and the endpoint at (*i*,<=0) is colored blue for all segments. Thus, the blue endpoint of the *i*-th segment is touching the red endpoint of the (*i*<=+<=1)-th segment for all valid *i*. Roger can move his arm in two different ways: 1. He can choose some segment and some value. This is denoted as choosing the segment number *i* and picking some positive *l*. This change happens as follows: the red endpoint of segment number *i* and segments from 1 to *i*<=-<=1 are all fixed in place. Imagine a ray from the red endpoint to the blue endpoint. The blue endpoint and segments *i*<=+<=1 through *n* are translated *l* units in the direction of this ray. In this picture, the red point labeled *A* and segments before *A* stay in place, while the blue point labeled *B* and segments after *B* gets translated.1. He can choose a segment and rotate it. This is denoted as choosing the segment number *i*, and an angle *a*. The red endpoint of the *i*-th segment will stay fixed in place. The blue endpoint of that segment and segments *i*<=+<=1 to *n* will rotate clockwise by an angle of *a* degrees around the red endpoint. In this picture, the red point labeled *A* and segments before *A* stay in place, while the blue point labeled *B* and segments after *B* get rotated around point *A*. Roger will move his arm *m* times. These transformations are a bit complicated, and Roger easily loses track of where the blue endpoint of the last segment is. Help him compute the coordinates of the blue endpoint of the last segment after applying each operation. Note that these operations are cumulative, and Roger's arm may intersect itself arbitrarily during the moves.
The first line of the input will contain two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=300<=000)Β β€” the number of segments and the number of operations to perform. Each of the next *m* lines contains three integers *x**i*, *y**i* and *z**i* describing a move. If *x**i*<==<=1, this line describes a move of type 1, where *y**i* denotes the segment number and *z**i* denotes the increase in the length. If *x**i*<==<=2, this describes a move of type 2, where *y**i* denotes the segment number, and *z**i* denotes the angle in degrees. (1<=≀<=*x**i*<=≀<=2,<=1<=≀<=*y**i*<=≀<=*n*,<=1<=≀<=*z**i*<=≀<=359)
Print *m* lines. The *i*-th line should contain two real values, denoting the coordinates of the blue endpoint of the last segment after applying operations 1,<=...,<=*i*. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=4. Namely, let's assume that your answer for a particular value of a coordinate is *a* and the answer of the jury is *b*. The checker program will consider your answer correct if for all coordinates.
[ "5 4\n1 1 3\n2 3 90\n2 5 48\n1 4 1\n" ]
[ "8.0000000000 0.0000000000\n5.0000000000 -3.0000000000\n4.2568551745 -2.6691306064\n4.2568551745 -3.6691306064\n" ]
The following pictures shows the state of the arm after each operation. The coordinates of point *F* are printed after applying each operation. For simplicity, we only show the blue endpoints of a segment (with the exception for the red endpoint of the first segment). For instance, the point labeled *B* is the blue endpoint for segment 1 and also the red endpoint for segment 2. Initial state:
[ { "input": "5 4\n1 1 3\n2 3 90\n2 5 48\n1 4 1", "output": "8.0000000000 0.0000000000\n5.0000000000 -3.0000000000\n4.2568551745 -2.6691306064\n4.2568551745 -3.6691306064" }, { "input": "1 1\n2 1 302", "output": "0.5299192642 0.8480480962" }, { "input": "50 50\n1 41 261\n2 47 324\n1 41 256...
8,000
48,025,600
0
86,695
354
Lucky Number Representation
[ "constructive algorithms", "dfs and similar", "dp" ]
null
null
We know that lucky digits are digits 4 and 7, however Vasya's got another favorite digit 0 and he assumes it also is lucky! Lucky numbers are such non-negative integers whose decimal record only contains lucky digits. For example, numbers 0,<=47,<=7074 are lucky, but 1,<=7377,<=895,<= -7 are not. Vasya has *t* important positive integers he needs to remember. Vasya is quite superstitious and he wants to remember lucky numbers only, so he is asking you for each important number to represent it as a sum of exactly six lucky numbers (Vasya just can't remember more numbers). Then Vasya can just remember these six numbers and calculate the important number at any moment. For each of *t* important integers represent it as the sum of six lucky numbers or state that this is impossible.
The first line contains a single integer *t* (1<=≀<=*t*<=≀<=5000). Next *t* lines contain a single positive integer *n**i* (1<=≀<=*n**i*<=≀<=1018) β€” the list of important numbers. Please, do not use the %lld to read or write 64-bit integers Π‘++. It is preferred to read the cin, cout streams or the %I64d specifier.
Print *t* lines. The *i*-th line must contain the answer for the *i*-th important number: if the solution exists, the line must contain exactly six lucky numbers the sum of which equals *n**i*, if the solution doesn't exist the string must contain a single integer -1. If there are multiple answers print any of them.
[ "5\n42\n17\n444\n7\n51\n" ]
[ "7 7 7 7 7 7\n-1\n400 0 40 0 4 0\n7 0 0 0 0 0\n47 4 0 0 0 0\n" ]
none
[]
30
0
0
86,851
93
End of Exams
[ "greedy" ]
B. End of Exams
1
256
Students love to celebrate their holidays. Especially if the holiday is the day of the end of exams! Despite the fact that Igor K., unlike his groupmates, failed to pass a programming test, he decided to invite them to go to a cafe so that each of them could drink a bottle of... fresh cow milk. Having entered the cafe, the *m* friends found *n* different kinds of milk on the menu, that's why they ordered *n* bottles β€” one bottle of each kind. We know that the volume of milk in each bottle equals *w*. When the bottles were brought in, they decided to pour all the milk evenly among the *m* cups, so that each got a cup. As a punishment for not passing the test Igor was appointed the person to pour the milk. He protested that he was afraid to mix something up and suggested to distribute the drink so that the milk from each bottle was in no more than two different cups. His friends agreed but they suddenly faced the following problem β€” and what is actually the way to do it? Help them and write the program that will help to distribute the milk among the cups and drink it as quickly as possible! Note that due to Igor K.'s perfectly accurate eye and unswerving hands, he can pour any fractional amount of milk from any bottle to any cup.
The only input data file contains three integers *n*, *w* and *m* (1<=≀<=*n*<=≀<=50, 100<=≀<=*w*<=≀<=1000, 2<=≀<=*m*<=≀<=50), where *n* stands for the number of ordered bottles, *w* stands for the volume of each of them and *m* stands for the number of friends in the company.
Print on the first line "YES" if it is possible to pour the milk so that the milk from each bottle was in no more than two different cups. If there's no solution, print "NO". If there is a solution, then print *m* more lines, where the *i*-th of them describes the content of the *i*-th student's cup. The line should consist of one or more pairs that would look like "*b* *v*". Each such pair means that *v* (*v*<=&gt;<=0) units of milk were poured into the *i*-th cup from bottle *b* (1<=≀<=*b*<=≀<=*n*). All numbers *b* on each line should be different. If there are several variants to solve the problem, print any of them. Print the real numbers with no less than 6 digits after the decimal point.
[ "2 500 3\n", "4 100 5\n", "4 100 7\n", "5 500 2\n" ]
[ "YES\n1 333.333333\n2 333.333333\n2 166.666667 1 166.666667\n", "YES\n3 20.000000 4 60.000000\n1 80.000000\n4 40.000000 2 40.000000\n3 80.000000\n2 60.000000 1 20.000000\n", "NO\n", "YES\n4 250.000000 5 500.000000 2 500.000000\n3 500.000000 1 500.000000 4 250.000000\n" ]
none
[ { "input": "2 500 3", "output": "YES\n1 333.333333\n2 333.333333\n2 166.666667 1 166.666667" }, { "input": "4 100 5", "output": "YES\n3 20.000000 4 60.000000\n1 80.000000\n4 40.000000 2 40.000000\n3 80.000000\n2 60.000000 1 20.000000" }, { "input": "4 100 7", "output": "NO" }, { ...
46
102,400
3.976809
87,018
173
Camping Groups
[ "data structures", "sortings" ]
null
null
A club wants to take its members camping. In order to organize the event better the club directors decided to partition the members into several groups. Club member *i* has a responsibility value *r**i* and an age value *a**i*. A group is a non-empty subset of club members with one member known as group leader. A group leader should be one of the most responsible members of the group (his responsibility value is not less than responsibility of any other group member) and his age absolute difference with any other group member should not exceed *k*. Some club members are friends and want to be in the same group. They also like their group to be as large as possible. Now you should write a program that answers a series of questions like "What's the largest size of a group containing club member *x* and club member *y*?". It's possible for *x* or *y* to be the group leader.
The first line contains two integers *n* and *k* (2<=≀<=*n*<=≀<=105,<=0<=≀<=*k*<=≀<=109) β€” the number of club members and the age restriction for one group. The next line contains integer numbers *r*1,<=*r*2,<=...,<=*r**n* (1<=≀<=*r**i*<=≀<=109) separated by space: *r**i* denotes the *i*-th club member's responsibility. In the same way there are integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) in the third line: *a**i* denotes the *i*-th club member's age. The next line contains an integer *q* denoting the number of questions that you should answer (1<=≀<=*q*<=≀<=105). The next *q* lines describe the questions. Each line contains two space-separated integers *x**i* and *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*,<=*x**i*<=β‰ <=*y**i*) β€” the indices of the club members that should end up in the same group.
For each question print the maximum size of the group in a line. If making such a group is impossible print -1 instead.
[ "5 1\n1 5 4 1 2\n4 4 3 2 2\n4\n5 3\n2 3\n2 5\n4 1\n" ]
[ "4\n3\n-1\n4\n" ]
In the first query the largest group with members 3 and 5 is {1, 3, 4, 5} where member 3 is the leader. In the second query member 2 should be the leader so the group will be {1, 2, 3}. In the third query the leader of the group should have age 3 so the only leader can be member 3, who is less responsible than member 2. So making a group is impossible. The group for the fourth query is the same as first query.
[]
0
0
-1
87,101
671
Ultimate Weirdness of an Array
[ "data structures", "number theory" ]
null
null
Yasin has an array *a* containing *n* integers. Yasin is a 5 year old, so he loves ultimate weird things. Yasin denotes weirdness of an array as maximum *gcd*(*a**i*,<=<=*a**j*) value among all 1<=≀<=*i*<=&lt;<=*j*<=≀<=*n*. For *n*<=≀<=1 weirdness is equal to 0, *gcd*(*x*,<=<=*y*) is the greatest common divisor of integers *x* and *y*. He also defines the ultimate weirdness of an array. Ultimate weirdness is where *f*(*i*,<=<=*j*) is weirdness of the new array *a* obtained by removing all elements between *i* and *j* inclusive, so new array is [*a*1... *a**i*<=-<=1,<=*a**j*<=+<=1... *a**n*]. Since 5 year old boys can't code, Yasin asks for your help to find the value of ultimate weirdness of the given array *a*!
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the number of elements in *a*. The next line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=200<=000), where the *i*-th number is equal to the *i*-th element of the array *a*. It is guaranteed that all *a**i* are distinct.
Print a single line containing the value of ultimate weirdness of the array *a*.
[ "3\n2 6 3\n" ]
[ "6\n" ]
Consider the first sample. - *f*(1,  1) is equal to 3. - *f*(2,  2) is equal to 1. - *f*(3,  3) is equal to 2. - *f*(1,  2), *f*(1,  3) and *f*(2,  3) are equal to 0.
[]
46
0
0
87,302
611
New Year and Ancient Prophecy
[ "dp", "hashing", "strings" ]
null
null
Limak is a little polar bear. In the snow he found a scroll with the ancient prophecy. Limak doesn't know any ancient languages and thus is unable to understand the prophecy. But he knows digits! One fragment of the prophecy is a sequence of *n* digits. The first digit isn't zero. Limak thinks that it's a list of some special years. It's hard to see any commas or spaces, so maybe ancient people didn't use them. Now Limak wonders what years are listed there. Limak assumes three things: - Years are listed in the strictly increasing order; - Every year is a positive integer number; - There are no leading zeros. Limak is going to consider all possible ways to split a sequence into numbers (years), satisfying the conditions above. He will do it without any help. However, he asked you to tell him the number of ways to do so. Since this number may be very large, you are only asked to calculate it modulo 109<=+<=7.
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=5000)Β β€” the number of digits. The second line contains a string of digits and has length equal to *n*. It's guaranteed that the first digit is not '0'.
Print the number of ways to correctly split the given sequence modulo 109<=+<=7.
[ "6\n123434\n", "8\n20152016\n" ]
[ "8\n", "4\n" ]
In the first sample there are 8 ways to split the sequence: - "123434" = "123434" (maybe the given sequence is just one big number) - "123434" = "1" + "23434" - "123434" = "12" + "3434" - "123434" = "123" + "434" - "123434" = "1" + "23" + "434" - "123434" = "1" + "2" + "3434" - "123434" = "1" + "2" + "3" + "434" - "123434" = "1" + "2" + "3" + "4" + "34" Note that we don't count a split "123434" = "12" + "34" + "34" because numbers have to be strictly increasing. In the second sample there are 4 ways: - "20152016" = "20152016" - "20152016" = "20" + "152016" - "20152016" = "201" + "52016" - "20152016" = "2015" + "2016"
[]
2,500
2,252,800
0
87,330
98
Help Victoria the Wise
[ "brute force", "implementation" ]
A. Help Victoria the Wise
1
256
Vasilisa the Wise from a far away kingdom got a present from her friend Helga the Wise from a farther away kingdom. The present is a surprise box, yet Vasilisa the Wise doesn't know yet what the surprise actually is because she cannot open the box. She hopes that you can help her in that. The box's lock is constructed like that. The box itself is represented by an absolutely perfect black cube with the identical deepening on each face (those are some foreign nanotechnologies that the far away kingdom scientists haven't dreamt of). The box is accompanied by six gems whose form matches the deepenings in the box's faces. The box can only be opened after it is correctly decorated by the gems, that is, when each deepening contains exactly one gem. Two ways of decorating the box are considered the same if they can be obtained one from the other one by arbitrarily rotating the box (note that the box is represented by a perfect nanotechnological cube) Now Vasilisa the Wise wants to know by the given set of colors the following: in how many ways would she decorate the box in the worst case to open it? To answer this question it is useful to know that two gems of one color are indistinguishable from each other. Help Vasilisa to solve this challenging problem.
The first line contains exactly 6 characters without spaces from the set {R, O, Y, G, B, V} β€” they are the colors of gems with which the box should be decorated.
Print the required number of different ways to decorate the box.
[ "YYYYYY\n", "BOOOOB\n", "ROYGBV\n" ]
[ "1\n", "2\n", "30\n" ]
none
[ { "input": "YYYYYY", "output": "1" }, { "input": "BOOOOB", "output": "2" }, { "input": "ROYGBV", "output": "30" }, { "input": "RRRRRR", "output": "1" }, { "input": "BOOOOO", "output": "1" }, { "input": "GOGGVG", "output": "2" }, { "input": ...
0
0
-1
87,396
571
Campus
[ "binary search", "data structures", "dsu", "trees" ]
null
null
Oscolcovo city has a campus consisting of *n* student dormitories, *n* universities and *n* military offices. Initially, the *i*-th dormitory belongs to the *i*-th university and is assigned to the *i*-th military office. Life goes on and the campus is continuously going through some changes. The changes can be of four types: 1. University *a**j* merges with university *b**j*. After that all the dormitories that belonged to university *b**j* are assigned to to university *a**j*, and university *b**j* disappears. 1. Military office *c**j* merges with military office *d**j*. After that all the dormitories that were assigned to military office *d**j*, are assigned to military office *c**j*, and military office *d**j* disappears. 1. Students of university *x**j* move in dormitories. Lets *k**x**j* is the number of dormitories that belong to this university at the time when the students move in. Then the number of students in each dormitory of university *x**j* increases by *k**x**j* (note that the more dormitories belong to the university, the more students move in each dormitory of the university). 1. Military office number *y**j* conducts raids on all the dormitories assigned to it and takes all students from there. Thus, at each moment of time each dormitory is assigned to exactly one university and one military office. Initially, all the dormitory are empty. Your task is to process the changes that take place in the campus and answer the queries, how many people currently live in dormitory *q**j*.
The first line contains two integers, *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=5Β·105) β€” the number of dormitories and the number of queries, respectively. Next *m* lines contain the queries, each of them is given in one of the following formats: - Β«U *a**j* *b**j*Β» β€” merging universities; - Β«M *c**j* *d**j*Β» β€” merging military offices; - Β«A *x**j*Β» β€” students of university *x**j* moving in the dormitories; - Β«Z *y**j*Β» β€” a raid in military office *y**j*; - Β«Q *q**j*Β» β€” a query asking the number of people in dormitory *q**j*.
In the *i*-th line print the answer to the *i*-th query asking the number of people in the dormitory.
[ "2 7\nA 1\nQ 1\nU 1 2\nA 1\nZ 1\nQ 1\nQ 2\n", "5 12\nU 1 2\nM 4 5\nA 1\nQ 1\nA 3\nA 4\nQ 3\nQ 4\nZ 4\nQ 4\nA 5\nQ 5\n" ]
[ "1\n0\n2\n", "2\n1\n1\n0\n1\n" ]
Consider the first sample test: - In the first query university 1 owns only dormitory 1, so after the query dormitory 1 will have 1 student. - After the third query university 1 owns dormitories 1 and 2. - The fourth query increases by 2 the number of students living in dormitories 1 and 2 that belong to university number 1. After that 3 students live in the first dormitory and 2 students live in the second dormitory. - At the fifth query the number of students living in dormitory 1, assigned to the military office 1, becomes zero.
[]
77
0
0
87,426
786
ALT
[ "data structures", "flows", "graphs", "trees" ]
null
null
ALT is a planet in a galaxy called "Encore". Humans rule this planet but for some reason there's no dog in their planet, so the people there are sad and depressed. Rick and Morty are universal philanthropists and they want to make people in ALT happy. ALT has *n* cities numbered from 1 to *n* and *n*<=-<=1 bidirectional roads numbered from 1 to *n*<=-<=1. One can go from any city to any other city using these roads. There are two types of people in ALT: 1. Guardians. A guardian lives in a house alongside a road and guards the road. 1. Citizens. A citizen lives in a house inside a city and works in an office in another city. Every person on ALT is either a guardian or a citizen and there's exactly one guardian alongside each road. Rick and Morty talked to all the people in ALT, and here's what they got: - There are *m* citizens living in ALT. - Citizen number *i* lives in city number *x**i* and works in city number *y**i*. - Every day each citizen will go through all roads along the shortest path from his home to his work. - A citizen will be happy if and only if either he himself has a puppy himself or all of guardians along his path to his work has a puppy (he sees the guardian's puppy in each road and will be happy). - A guardian is always happy. You need to tell Rick and Morty the minimum number of puppies they need in order to make all people in ALT happy, and also provide an optimal way to distribute these puppies.
The first line of input contains two integers *n* and *m* (2<=≀<=*n*<=≀<=2<=Γ—<=104, 1<=≀<=*m*<=≀<=104) β€” number of cities and number of citizens respectively. The next *n*<=-<=1 lines contain the roads, *i*-th line contains endpoint of *i*-th edge, *v* and *u* (1<=≀<=*v*,<=*u*<=≀<=*n*, *v*<=β‰ <=*u*). The next *m* lines contain the information about citizens. *i*-th line contains two integers *x**i* and *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*, *x**i*<=β‰ <=*y**i*).
In the first line of input print a single integer *k*, the total number of puppies they need (1<=≀<=*k*<=≀<=*n*). In the second line print an integer *q*, the number of puppies to give to citizens, followed by *q* distinct integers *a*1,<=*a*2,<=...,<=*a**q*, index of citizens to give puppy to (0<=≀<=*q*<=≀<=*min*(*m*,<=*k*), 1<=≀<=*a**i*<=≀<=*m*). In the third line print an integer *e*, the number of puppies to give to guardians, followed by *e* distinct integers *b*1,<=*b*2,<=...,<=*b**e*, index of road of guardians to give puppy to (0<=≀<=*e*<=≀<=*min*(*n*<=-<=1,<=*k*), 1<=≀<=*b**i*<=≀<=*n*<=-<=1). Sum of *q* and *e* should be equal to *k*.
[ "4 5\n2 4\n3 4\n1 4\n2 4\n2 1\n2 4\n1 2\n2 3\n", "4 7\n3 4\n1 4\n2 1\n4 2\n4 2\n2 4\n1 4\n2 1\n3 1\n4 2\n" ]
[ "3\n1 5 \n2 3 1 \n", "3\n1 6 \n2 2 3 \n" ]
Map of ALT in the first sample testcase (numbers written on a road is its index): Map of ALT in the second sample testcase (numbers written on a road is its index):
[]
46
5,529,600
0
87,580
983
Arkady and Rectangles
[ "data structures" ]
null
null
Arkady has got an infinite plane painted in color $0$. Then he draws $n$ rectangles filled with paint with sides parallel to the Cartesian coordinate axes, one after another. The color of the $i$-th rectangle is $i$ (rectangles are enumerated from $1$ to $n$ in the order he draws them). It is possible that new rectangles cover some of the previous ones completely or partially. Count the number of different colors on the plane after Arkady draws all the rectangles.
The first line contains a single integer $n$ ($1 \le n \le 100\,000$)Β β€” the number of rectangles. The $i$-th of the next $n$ lines contains $4$ integers $x_1$, $y_1$, $x_2$ and $y_2$ ($-10^9 \le x_1 &lt; x_2 \le 10^9$, $-10^9 \le y_1 &lt; y_2 \le 10^9$)Β β€” the coordinates of corners of the $i$-th rectangle.
In the single line print the number of different colors in the plane, including color $0$.
[ "5\n-1 -1 1 1\n-4 0 0 4\n0 0 4 4\n-4 -4 0 0\n0 -4 4 0\n", "4\n0 0 4 4\n-4 -4 0 0\n0 -4 4 0\n-2 -4 2 4\n" ]
[ "5", "5" ]
<img class="tex-graphics" src="https://espresso.codeforces.com/b84e29026ee49b5972ce2479ad7cb4e72b99e516.png" style="max-width: 100.0%;max-height: 100.0%;" width="454px"/> That's how the plane looks in the second sample $0$ = white, $1$ = cyan, $2$ = blue, $3$ = purple, $4$ = yellow, $5$ = red.
[]
0
0
-1
87,585
628
Zbazi in Zeydabad
[ "data structures", "implementation" ]
null
null
A tourist wants to visit country Zeydabad for Zbazi (a local game in Zeydabad). The country Zeydabad is a rectangular table consisting of *n* rows and *m* columns. Each cell on the country is either 'z' or '.'. The tourist knows this country is named Zeydabad because there are lots of ''Z-pattern"s in the country. A ''Z-pattern" is a square which anti-diagonal is completely filled with 'z' and its upper and lower rows are also completely filled with 'z'. All other cells of a square can be arbitrary. Note that a ''Z-pattern" can consist of only one cell (see the examples). So he wants to count the number of ''Z-pattern"s in the country (a necessary skill for Zbazi). Now your task is to help tourist with counting number of ''Z-pattern"s. As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
The first line contains two integers *n*,<=*m* (1<=≀<=*n*,<=*m*<=≀<=3000) β€” the number of rows and columns respectively. Each of the next *n* lines contains *m* characters 'z' or '.' β€” the description of Zeydabad.
Print the only integer *a* β€” the number of ''Z-pattern"s in Zeydabad.
[ "4 4\nzzzz\nzzz.\n.z..\nzzzz\n", "1 4\nz.z.\n", "2 2\nzz\nzz\n" ]
[ "16\n", "2\n", "5\n" ]
none
[ { "input": "4 4\nzzzz\nzzz.\n.z..\nzzzz", "output": "16" }, { "input": "1 4\nz.z.", "output": "2" }, { "input": "2 2\nzz\nzz", "output": "5" } ]
46
0
0
87,721
149
Martian Strings
[ "string suffix structures", "strings" ]
null
null
During the study of the Martians Petya clearly understood that the Martians are absolutely lazy. They like to sleep and don't like to wake up. Imagine a Martian who has exactly *n* eyes located in a row and numbered from the left to the right from 1 to *n*. When a Martian sleeps, he puts a patch on each eye (so that the Martian morning doesn't wake him up). The inner side of each patch has an uppercase Latin letter. So, when a Martian wakes up and opens all his eyes he sees a string *s* consisting of uppercase Latin letters. The string's length is *n*. "Ding dong!" β€” the alarm goes off. A Martian has already woken up but he hasn't opened any of his eyes. He feels that today is going to be a hard day, so he wants to open his eyes and see something good. The Martian considers only *m* Martian words beautiful. Besides, it is hard for him to open all eyes at once so early in the morning. So he opens two non-overlapping segments of consecutive eyes. More formally, the Martian chooses four numbers *a*, *b*, *c*, *d*, (1<=≀<=*a*<=≀<=*b*<=&lt;<=*c*<=≀<=*d*<=≀<=*n*) and opens all eyes with numbers *i* such that *a*<=≀<=*i*<=≀<=*b* or *c*<=≀<=*i*<=≀<=*d*. After the Martian opens the eyes he needs, he reads all the visible characters from the left to the right and thus, he sees some word. Let's consider all different words the Martian can see in the morning. Your task is to find out how many beautiful words are among them.
The first line contains a non-empty string *s* consisting of uppercase Latin letters. The strings' length is *n* (2<=≀<=*n*<=≀<=105). The second line contains an integer *m* (1<=≀<=*m*<=≀<=100) β€” the number of beautiful words. Next *m* lines contain the beautiful words *p**i*, consisting of uppercase Latin letters. Their length is from 1 to 1000. All beautiful strings are pairwise different.
Print the single integer β€” the number of different beautiful strings the Martian can see this morning.
[ "ABCBABA\n2\nBAAB\nABBA\n" ]
[ "1\n" ]
Let's consider the sample test. There the Martian can get only the second beautiful string if he opens segments of eyes *a* = 1, *b* = 2 and *c* = 4, *d* = 5 or of he opens segments of eyes *a* = 1, *b* = 2 and *c* = 6, *d* = 7.
[ { "input": "ABCBABA\n2\nBAAB\nABBA", "output": "1" }, { "input": "AAAAAAAAAA\n4\nAA\nAAAA\nA\nAAAAAAA", "output": "3" }, { "input": "AABABBAAAB\n4\nABBA\nBABAABBABA\nA\nAABBBAABABB", "output": "1" }, { "input": "AABBB\n1\nBBABAB", "output": "0" }, { "input": "BA\n...
92
0
0
87,924
859
Third Month Insanity
[ "dp", "probabilities", "trees" ]
null
null
The annual college sports-ball tournament is approaching, which for trademark reasons we'll refer to as Third Month Insanity. There are a total of 2*N* teams participating in the tournament, numbered from 1 to 2*N*. The tournament lasts *N* rounds, with each round eliminating half the teams. The first round consists of 2*N*<=-<=1 games, numbered starting from 1. In game *i*, team 2Β·*i*<=-<=1 will play against team 2Β·*i*. The loser is eliminated and the winner advances to the next round (there are no ties). Each subsequent round has half as many games as the previous round, and in game *i* the winner of the previous round's game 2Β·*i*<=-<=1 will play against the winner of the previous round's game 2Β·*i*. Every year the office has a pool to see who can create the best bracket. A bracket is a set of winner predictions for every game. For games in the first round you may predict either team to win, but for games in later rounds the winner you predict must also be predicted as a winner in the previous round. Note that the bracket is fully constructed before any games are actually played. Correct predictions in the first round are worth 1 point, and correct predictions in each subsequent round are worth twice as many points as the previous, so correct predictions in the final game are worth 2*N*<=-<=1 points. For every pair of teams in the league, you have estimated the probability of each team winning if they play against each other. Now you want to construct a bracket with the maximum possible expected score.
Input will begin with a line containing *N* (2<=≀<=*N*<=≀<=6). 2*N* lines follow, each with 2*N* integers. The *j*-th column of the *i*-th row indicates the percentage chance that team *i* will defeat team *j*, unless *i*<==<=*j*, in which case the value will be 0. It is guaranteed that the *i*-th column of the *j*-th row plus the *j*-th column of the *i*-th row will add to exactly 100.
Print the maximum possible expected score over all possible brackets. Your answer must be correct to within an absolute or relative error of 10<=-<=9. Formally, let your answer be *a*, and the jury's answer be *b*. Your answer will be considered correct, if .
[ "2\n0 40 100 100\n60 0 40 40\n0 60 0 45\n0 60 55 0\n", "3\n0 0 100 0 100 0 0 0\n100 0 100 0 0 0 100 100\n0 0 0 100 100 0 0 0\n100 100 0 0 0 0 100 100\n0 100 0 100 0 0 100 0\n100 100 100 100 100 0 0 0\n100 0 100 0 0 100 0 0\n100 0 100 0 100 100 100 0\n", "2\n0 21 41 26\n79 0 97 33\n59 3 0 91\n74 67 9 0\n" ]
[ "1.75\n", "12\n", "3.141592\n" ]
In the first example, you should predict teams 1 and 4 to win in round 1, and team 1 to win in round 2. Recall that the winner you predict in round 2 must also be predicted as a winner in round 1.
[ { "input": "2\n0 40 100 100\n60 0 40 40\n0 60 0 45\n0 60 55 0", "output": "1.75" }, { "input": "3\n0 0 100 0 100 0 0 0\n100 0 100 0 0 0 100 100\n0 0 0 100 100 0 0 0\n100 100 0 0 0 0 100 100\n0 100 0 100 0 0 100 0\n100 100 100 100 100 0 0 0\n100 0 100 0 0 100 0 0\n100 0 100 0 100 100 100 0", "out...
30
0
0
88,025
0
none
[ "none" ]
null
null
You have a rectangular *n*<=Γ—<=*m*-cell board. Some cells are already painted some of *k* colors. You need to paint each uncolored cell one of the *k* colors so that any path from the upper left square to the lower right one doesn't contain any two cells of the same color. The path can go only along side-adjacent cells and can only go down or right. Print the number of possible paintings modulo 1000000007 (109<=+<=7).
The first line contains three integers *n*,<=*m*,<=*k* (1<=≀<=*n*,<=*m*<=≀<=1000,<=1<=≀<=*k*<=≀<=10). The next *n* lines contain *m* integers each β€” the board. The first of them contains *m* uppermost cells of the board from the left to the right and the second one contains *m* cells from the second uppermost row and so on. If a number in a line equals 0, then the corresponding cell isn't painted. Otherwise, this number represents the initial color of the board cell β€” an integer from 1 to *k*. Consider all colors numbered from 1 to *k* in some manner.
Print the number of possible paintings modulo 1000000007 (109<=+<=7).
[ "2 2 4\n0 0\n0 0\n", "2 2 4\n1 2\n2 1\n", "5 6 10\n0 0 0 0 0 0\n0 0 0 0 0 0\n0 0 0 0 0 0\n0 0 0 0 0 0\n0 0 0 0 0 0\n", "2 6 10\n1 2 3 4 5 6\n0 0 0 0 0 0\n" ]
[ "48\n", "0\n", "3628800\n", "4096\n" ]
none
[]
92
0
0
88,178
798
Mike and strings
[ "brute force", "dp", "strings" ]
null
null
Mike has *n* strings *s*1,<=*s*2,<=...,<=*s**n* each consisting of lowercase English letters. In one move he can choose a string *s**i*, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec". Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?
The first line contains integer *n* (1<=≀<=*n*<=≀<=50) β€” the number of strings. This is followed by *n* lines which contain a string each. The *i*-th line corresponding to string *s**i*. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.
Print the minimal number of moves Mike needs in order to make all the strings equal or print <=-<=1 if there is no solution.
[ "4\nxzzwo\nzwoxz\nzzwox\nxzzwo\n", "2\nmolzv\nlzvmo\n", "3\nkc\nkc\nkc\n", "3\naa\naa\nab\n" ]
[ "5\n", "2\n", "0\n", "-1\n" ]
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
[ { "input": "4\nxzzwo\nzwoxz\nzzwox\nxzzwo", "output": "5" }, { "input": "2\nmolzv\nlzvmo", "output": "2" }, { "input": "3\nkc\nkc\nkc", "output": "0" }, { "input": "3\naa\naa\nab", "output": "-1" }, { "input": "3\nkwkb\nkbkw\nbkwk", "output": "3" }, { ...
93
1,740,800
3
88,179
412
Giving Awards
[ "dfs and similar" ]
null
null
The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO will invite employees into his office one by one, rewarding each one for the hard work this month. The CEO knows who owes money to whom. And he also understands that if he invites person *x* to his office for a reward, and then immediately invite person *y*, who has lent some money to person *x*, then they can meet. Of course, in such a situation, the joy of person *x* from his brand new money reward will be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation will not happen for any pair of employees invited one after another. However, there are a lot of employees in the company, and the CEO doesn't have a lot of time. Therefore, the task has been assigned to you. Given the debt relationships between all the employees, determine in which order they should be invited to the office of the R1 company CEO, or determine that the described order does not exist.
The first line contains space-separated integers *n* and *m* β€” the number of employees in R1 and the number of debt relations. Each of the following *m* lines contains two space-separated integers *a**i*, *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*;Β *a**i*<=β‰ <=*b**i*), these integers indicate that the person number *a**i* owes money to a person a number *b**i*. Assume that all the employees are numbered from 1 to *n*. It is guaranteed that each pair of people *p*, *q* is mentioned in the input data at most once. In particular, the input data will not contain pairs *p*, *q* and *q*, *p* simultaneously.
Print -1 if the described order does not exist. Otherwise, print the permutation of *n* distinct integers. The first number should denote the number of the person who goes to the CEO office first, the second number denote the person who goes second and so on. If there are multiple correct orders, you are allowed to print any of them.
[ "2 1\n1 2\n", "3 3\n1 2\n2 3\n3 1\n" ]
[ "2 1 \n", "2 1 3 \n" ]
none
[ { "input": "2 1\n1 2", "output": "2 1 " }, { "input": "3 3\n1 2\n2 3\n3 1", "output": "2 1 3 " }, { "input": "10 45\n10 5\n10 7\n6 1\n5 8\n3 5\n6 5\n1 2\n6 10\n2 9\n9 5\n4 1\n7 5\n1 8\n6 8\n10 9\n7 2\n7 9\n4 10\n7 3\n4 8\n10 3\n10 8\n2 10\n8 2\n4 2\n5 2\n9 1\n4 5\n1 3\n9 6\n3 8\n5 1\n6 4...
374
11,673,600
3
88,196
208
Prizes, Prizes, more Prizes
[ "implementation" ]
null
null
Vasya, like many others, likes to participate in a variety of sweepstakes and lotteries. Now he collects wrappings from a famous chocolate bar "Jupiter". According to the sweepstake rules, each wrapping has an integer written on it β€” the number of points that the participant adds to his score as he buys the bar. After a participant earns a certain number of points, he can come to the prize distribution center and exchange the points for prizes. When somebody takes a prize, the prize's cost is simply subtracted from the number of his points. Vasya didn't only bought the bars, he also kept a record of how many points each wrapping cost. Also, he remembers that he always stucks to the greedy strategy β€” as soon as he could take at least one prize, he went to the prize distribution centre and exchanged the points for prizes. Moreover, if he could choose between multiple prizes, he chose the most expensive one. If after an exchange Vasya had enough points left to get at least one more prize, then he continued to exchange points. The sweepstake has the following prizes (the prizes are sorted by increasing of their cost): - a mug (costs *a* points), - a towel (costs *b* points), - a bag (costs *c* points), - a bicycle (costs *d* points), - a car (costs *e* points). Now Vasya wants to recollect what prizes he has received. You know sequence *p*1,<=*p*2,<=...,<=*p**n*, where *p**i* is the number of points Vasya got for the *i*-th bar. The sequence of points is given in the chronological order. You also know numbers *a*, *b*, *c*, *d*, *e*. Your task is to find, how many prizes Vasya received, what prizes they are and how many points he's got left after all operations are completed.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=50) β€” the number of chocolate bar wrappings that brought points to Vasya. The second line contains space-separated integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≀<=*p**i*<=≀<=109). The third line contains 5 integers *a*, *b*, *c*, *d*, *e* (1<=≀<=*a*<=&lt;<=*b*<=&lt;<=*c*<=&lt;<=*d*<=&lt;<=*e*<=≀<=109) β€” the prizes' costs.
Print on the first line 5 integers, separated by a space β€” the number of mugs, towels, bags, bicycles and cars that Vasya has got, respectively. On the second line print a single integer β€” the number of points Vasya will have left after all operations of exchange are completed. 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.
[ "3\n3 10 4\n2 4 10 15 20\n", "4\n10 4 39 2\n3 5 10 11 12\n" ]
[ "1 1 1 0 0 \n1\n", "3 0 1 0 3 \n0\n" ]
In the first sample Vasya gets 3 points after eating the first chocolate bar. Then he exchanges 2 points and gets a mug. Vasya wins a bag after eating the second chocolate bar. Then he wins a towel after eating the third chocolate bar. After all chocolate bars 3 - 2 + 10 - 10 + 4 - 4 = 1 points remains.
[ { "input": "3\n3 10 4\n2 4 10 15 20", "output": "1 1 1 0 0 \n1" }, { "input": "4\n10 4 39 2\n3 5 10 11 12", "output": "3 0 1 0 3 \n0" }, { "input": "1\n45\n1 2 3 4 5", "output": "0 0 0 0 9 \n0" }, { "input": "1\n50\n1 2 4 5 6", "output": "0 1 0 0 8 \n0" }, { "inpu...
124
2,867,200
-1
88,448
250
Mad Joe
[ "brute force" ]
null
null
Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path. Joe's house has *n* floors, each floor is a segment of *m* cells. Each cell either contains nothing (it is an empty cell), or has a brick or a concrete wall (always something one of three). It is believed that each floor is surrounded by a concrete wall on the left and on the right. Now Joe is on the *n*-th floor and in the first cell, counting from left to right. At each moment of time, Joe has the direction of his gaze, to the right or to the left (always one direction of the two). Initially, Joe looks to the right. Joe moves by a particular algorithm. Every second he makes one of the following actions: - If the cell directly under Joe is empty, then Joe falls down. That is, he moves to this cell, the gaze direction is preserved. - Otherwise consider the next cell in the current direction of the gaze. If the cell is empty, then Joe moves into it, the gaze direction is preserved. - If this cell has bricks, then Joe breaks them with his forehead (the cell becomes empty), and changes the direction of his gaze to the opposite. - If this cell has a concrete wall, then Joe just changes the direction of his gaze to the opposite (concrete can withstand any number of forehead hits). Joe calms down as soon as he reaches any cell of the first floor. The figure below shows an example Joe's movements around the house. Determine how many seconds Joe will need to calm down.
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=100, 1<=≀<=*m*<=≀<=104). Next *n* lines contain the description of Joe's house. The *i*-th of these lines contains the description of the (*n*<=-<=*i*<=+<=1)-th floor of the house β€” a line that consists of *m* characters: "." means an empty cell, "+" means bricks and "#" means a concrete wall. It is guaranteed that the first cell of the *n*-th floor is empty.
Print a single number β€” the number of seconds Joe needs to reach the first floor; or else, print word "Never" (without the quotes), if it can never happen. 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.
[ "3 5\n..+.#\n#+..+\n+.#+.\n", "4 10\n...+.##+.+\n+#++..+++#\n++.#++++..\n.+##.++#.+\n", "2 2\n..\n++\n" ]
[ "14", "42\n", "Never" ]
none
[ { "input": "3 5\n..+.#\n#+..+\n+.#+.", "output": "14" }, { "input": "4 10\n...+.##+.+\n+#++..+++#\n++.#++++..\n.+##.++#.+", "output": "42" }, { "input": "2 2\n..\n++", "output": "Never" }, { "input": "5 1\n.\n.\n.\n.\n.", "output": "4" }, { "input": "20 20\n..+#+....
436
11,059,200
3
88,470
220
Little Elephant and Triangle
[ "geometry", "math" ]
null
null
The Little Elephant is playing with the Cartesian coordinates' system. Most of all he likes playing with integer points. The Little Elephant defines an integer point as a pair of integers (*x*;Β *y*), such that 0<=≀<=*x*<=≀<=*w* and 0<=≀<=*y*<=≀<=*h*. Thus, the Little Elephant knows only (*w*<=+<=1)Β·(*h*<=+<=1) distinct integer points. The Little Elephant wants to paint a triangle with vertexes at integer points, the triangle's area must be a positive integer. For that, he needs to find the number of groups of three points that form such triangle. At that, the order of points in a group matters, that is, the group of three points (0;0), (0;2), (2;2) isn't equal to the group (0;2), (0;0), (2;2). Help the Little Elephant to find the number of groups of three integer points that form a nondegenerate triangle with integer area.
A single line contains two integers *w* and *h* (1<=≀<=*w*,<=*h*<=≀<=4000).
In a single output line print an integer β€” the remainder of dividing the answer to the problem by 1000000007 (109<=+<=7).
[ "2 1\n", "2 2\n" ]
[ "36\n", "240\n" ]
none
[]
92
0
0
88,590
167
Wizards and Bets
[ "dfs and similar", "graphs", "math", "matrices" ]
null
null
In some country live wizards. They like to make weird bets. Two wizards draw an acyclic directed graph with *n* vertices and *m* edges (the graph's vertices are numbered from 1 to *n*). A source is a vertex with no incoming edges, and a sink is the vertex with no outgoing edges. Note that a vertex could be the sink and the source simultaneously. In the wizards' graph the number of the sinks and the sources is the same. Wizards numbered the sources in the order of increasing numbers of the vertices from 1 to *k*. The sinks are numbered from 1 to *k* in the similar way. To make a bet, they, as are real wizards, cast a spell, which selects a set of *k* paths from all sources to the sinks in such a way that no two paths intersect at the vertices. In this case, each sink has exactly one path going to it from exactly one source. Let's suppose that the *i*-th sink has a path going to it from the *a**i*'s source. Then let's call pair (*i*,<=*j*) an inversion if *i*<=&lt;<=*j* and *a**i*<=&gt;<=*a**j*. If the number of inversions among all possible pairs (*i*,<=*j*), such that (1<=≀<=*i*<=&lt;<=*j*<=≀<=*k*), is even, then the first wizard wins (the second one gives him one magic coin). Otherwise, the second wizard wins (he gets one magic coin from the first one). Our wizards are captured with feverish excitement, so they kept choosing new paths again and again for so long that eventually they have chosen every possible set of paths for exactly once. The two sets of non-intersecting pathes are considered to be different, if and only if there is an edge, which lies at some path in one set and doesn't lie at any path of another set. To check their notes, they asked you to count the total winnings of the first player for all possible sets of paths modulo a prime number *p*.
The first line contains three space-separated integers *n*, *m*, *p* (1<=≀<=*n*<=≀<=600, 0<=≀<=*m*<=≀<=105, 2<=≀<=*p*<=≀<=109<=+<=7). It is guaranteed that *p* is prime number. Next *m* lines contain edges of the graph. Each line contains a pair of space-separated integers, *a**i*Β *b**i* β€” an edge from vertex *a**i* to vertex *b**i*. It is guaranteed that the graph is acyclic and that the graph contains the same number of sources and sinks. Please note that the graph can have multiple edges.
Print the answer to the problem β€” the total winnings of the first player modulo a prime number *p*. Please note that the winnings may be negative, but the modulo residue must be non-negative (see the sample).
[ "4 2 1000003\n1 3\n2 4\n", "4 2 1000003\n4 1\n3 2\n", "4 4 1000003\n2 1\n2 4\n3 1\n3 4\n", "6 5 1000003\n1 4\n1 5\n1 6\n2 6\n3 6\n", "5 2 1000003\n5 1\n3 4\n" ]
[ "1\n", "1000002\n", "0\n", "0\n", "1\n" ]
In the first sample, there is exactly one set of paths β€” <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4c100300071b70505c9eb8fd6f97456068e38f93.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The number of inversions is 0, which is an even number. Therefore, the first wizard gets 1 coin. In the second sample there is exactly one set of paths β€” <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/83cf8c3e6763396a8ead42b4762ff62e4b01f917.png" style="max-width: 100.0%;max-height: 100.0%;"/>. There is exactly one inversion. Therefore, the first wizard gets -1 coin. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fa579688f149cd6ab8f4c3c99c47c5a65e52e87c.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the third sample, there are two sets of paths, which are counted with opposite signs. In the fourth sample there are no set of paths at all. In the fifth sample, there are three sources β€” the vertices with the numbers (2, 3, 5) and three sinks β€” the vertices with numbers (1, 2, 4). For a single set of paths <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5ef1a2c8d476f3fea4b49124c0c23f371be35bb9.png" style="max-width: 100.0%;max-height: 100.0%;"/> are 2 inversions, that is, their number is even.
[]
124
0
0
88,829
920
List Of Integers
[ "binary search", "bitmasks", "brute force", "combinatorics", "math", "number theory" ]
null
null
Let's denote as *L*(*x*,<=*p*) an infinite sequence of integers *y* such that *gcd*(*p*,<=*y*)<==<=1 and *y*<=&gt;<=*x* (where *gcd* is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of *L*(*x*,<=*p*) are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of *L*(7,<=22), respectively. You have to process *t* queries. Each query is denoted by three integers *x*, *p* and *k*, and the answer to this query is *k*-th element of *L*(*x*,<=*p*).
The first line contains one integer *t* (1<=≀<=*t*<=≀<=30000) β€” the number of queries to process. Then *t* lines follow. *i*-th line contains three integers *x*, *p* and *k* for *i*-th query (1<=≀<=*x*,<=*p*,<=*k*<=≀<=106).
Print *t* integers, where *i*-th integer is the answer to *i*-th query.
[ "3\n7 22 1\n7 22 2\n7 22 3\n", "5\n42 42 42\n43 43 43\n44 44 44\n45 45 45\n46 46 46\n" ]
[ "9\n13\n15\n", "187\n87\n139\n128\n141\n" ]
none
[ { "input": "3\n7 22 1\n7 22 2\n7 22 3", "output": "9\n13\n15" }, { "input": "5\n42 42 42\n43 43 43\n44 44 44\n45 45 45\n46 46 46", "output": "187\n87\n139\n128\n141" } ]
0
0
-1
89,419
797
Mice and Holes
[ "data structures", "dp", "greedy", "sortings" ]
null
null
One day Masha came home and noticed *n* mice in the corridor of her flat. Of course, she shouted loudly, so scared mice started to run to the holes in the corridor. The corridor can be represeted as a numeric axis with *n* mice and *m* holes on it. *i*th mouse is at the coordinate *x**i*, and *j*th hole β€” at coordinate *p**j*. *j*th hole has enough room for *c**j* mice, so not more than *c**j* mice can enter this hole. What is the minimum sum of distances that mice have to go through so that they all can hide in the holes? If *i*th mouse goes to the hole *j*, then its distance is |*x**i*<=-<=*p**j*|. Print the minimum sum of distances.
The first line contains two integer numbers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=5000) β€” the number of mice and the number of holes, respectively. The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (<=-<=109<=≀<=*x**i*<=≀<=109), where *x**i* is the coordinate of *i*th mouse. Next *m* lines contain pairs of integer numbers *p**j*,<=*c**j* (<=-<=109<=≀<=*p**j*<=≀<=109,<=1<=≀<=*c**j*<=≀<=5000), where *p**j* is the coordinate of *j*th hole, and *c**j* is the maximum number of mice that can hide in the hole *j*.
Print one integer number β€” the minimum sum of distances. If there is no solution, print -1 instead.
[ "4 5\n6 2 8 9\n3 6\n2 1\n3 6\n4 7\n4 7\n", "7 2\n10 20 30 40 50 45 35\n-1000000000 10\n1000000000 1\n" ]
[ "11\n", "7000000130\n" ]
none
[ { "input": "4 5\n6 2 8 9\n3 6\n2 1\n3 6\n4 7\n4 7", "output": "11" }, { "input": "7 2\n10 20 30 40 50 45 35\n-1000000000 10\n1000000000 1", "output": "7000000130" }, { "input": "1 1\n1\n1 1", "output": "0" }, { "input": "1 1\n1\n1 5000", "output": "0" }, { "input"...
1,500
11,673,600
0
89,551
382
Ksenia and Combinatorics
[ "combinatorics", "dp" ]
null
null
Ksenia has her winter exams. Today she is learning combinatorics. Here's one of the problems she needs to learn to solve. How many distinct trees are there consisting of *n* vertices, each with the following properties: - the tree is marked, that is, the vertices of the tree are numbered from 1 to *n*; - each vertex of the tree is connected with at most three other vertices, and at the same moment the vertex with number 1 is connected with at most two other vertices; - the size of the tree's maximum matching equals *k*. Two trees are considered distinct if there are such two vertices *u* and *v*, that in one tree they are connected by an edge and in the other tree they are not. Help Ksenia solve the problem for the given *n* and *k*. As the answer to the problem can be very huge you should output it modulo 1000000007Β (109<=+<=7).
The first line contains two integers *n*,<=*k* (1<=≀<=*n*,<=*k*<=≀<=50).
Print a single integer β€” the answer to the problem modulo 1000000007Β (109<=+<=7).
[ "1 1\n", "2 1\n", "3 1\n", "4 2\n" ]
[ "0\n", "1\n", "3\n", "12\n" ]
If you aren't familiar with matchings, please, read the following link: http://en.wikipedia.org/wiki/Matching_(graph_theory).
[]
265
4,300,800
0
89,629
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...
280
1,331,200
0
89,644
48
Black and White
[ "constructive algorithms" ]
H. Black and White
2
256
According to the legends the king of Berland Berl I was noted for his love of beauty and order. One day he ordered to tile the palace hall's floor where balls and receptions used to take place with black and white tiles according to a regular geometrical pattern invented by him. However, as is after the case, due to low financing there were only *a* black and *b* white tiles delivered to the palace. The other *c* tiles were black and white (see the picture). The initial plan failed! Having learned of that, the king gave a new command: tile the floor with the available tiles so that no black side of a tile touched a white one. The tiles are squares of one size 1<=Γ—<=1, every black and white tile can be rotated in one of the four ways. The court programmer was given the task to work out the plan of tiling and he coped with the task and didn't suffer the consequences of disobedience. And can you cope with it?
The first line contains given integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100) which represent the sizes of the rectangle that needs to be tiled. The next line contains non-negative numbers *a*, *b* and *c*, *a*<=+<=*b*<=+<=*c*<==<=*nm*, *c*<=β‰₯<=*m*.
Print 2*n* lines containing 2*m* characters each β€” the tiling scheme. Every tile is represented by a square 2<=Γ—<=2 in the following manner (the order corresponds to the order of the picture above):
[ "2 2\n0 0 4\n", "2 3\n1 2 3\n" ]
[ "\\../\n#\\/#\n\\##/\n.\\/.\n", "###/\\#\n##/..\\\n#/....\n/.....\n" ]
none
[]
92
0
0
90,081
567
Mausoleum
[ "dp" ]
null
null
King of Berland Berl IV has recently died. Hail Berl V! As a sign of the highest achievements of the deceased king the new king decided to build a mausoleum with Berl IV's body on the main square of the capital. The mausoleum will be constructed from 2*n* blocks, each of them has the shape of a cuboid. Each block has the bottom base of a 1<=Γ—<=1 meter square. Among the blocks, exactly two of them have the height of one meter, exactly two have the height of two meters, ..., exactly two have the height of *n* meters. The blocks are arranged in a row without spacing one after the other. Of course, not every arrangement of blocks has the form of a mausoleum. In order to make the given arrangement in the form of the mausoleum, it is necessary that when you pass along the mausoleum, from one end to the other, the heights of the blocks first were non-decreasing (i.e., increasing or remained the same), and then β€” non-increasing (decrease or remained unchanged). It is possible that any of these two areas will be omitted. For example, the following sequences of block height meet this requirement: - [1,<=2,<=2,<=3,<=4,<=4,<=3,<=1]; - [1,<=1]; - [2,<=2,<=1,<=1]; - [1,<=2,<=3,<=3,<=2,<=1]. Suddenly, *k* more requirements appeared. Each of the requirements has the form: "*h*[*x**i*] sign*i* *h*[*y**i*]", where *h*[*t*] is the height of the *t*-th block, and a sign*i* is one of the five possible signs: '=' (equals), '&lt;' (less than), '&gt;' (more than), '&lt;=' (less than or equals), '&gt;=' (more than or equals). Thus, each of the *k* additional requirements is given by a pair of indexes *x**i*, *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=2*n*) and sign sign*i*. Find the number of possible ways to rearrange the blocks so that both the requirement about the shape of the mausoleum (see paragraph 3) and the *k* additional requirements were met.
The first line of the input contains integers *n* and *k* (1<=≀<=*n*<=≀<=35, 0<=≀<=*k*<=≀<=100) β€” the number of pairs of blocks and the number of additional requirements. Next *k* lines contain listed additional requirements, one per line in the format "*x**i* sign*i* *y**i*" (1<=≀<=*x**i*,<=*y**i*<=≀<=2*n*), and the sign is on of the list of the five possible signs.
Print the sought number of ways.
[ "3 0\n", "3 1\n2 &gt; 3\n", "4 1\n3 = 6\n" ]
[ "9\n", "1\n", "3\n" ]
none
[ { "input": "3 0", "output": "9" }, { "input": "3 1\n2 > 3", "output": "1" }, { "input": "4 1\n3 = 6", "output": "3" }, { "input": "5 2\n1 < 2\n9 > 10", "output": "27" }, { "input": "35 0", "output": "16677181699666569" }, { "input": "10 5\n17 <= 10\n16...
92
1,536,000
0
90,226
279
Ladder
[ "dp", "implementation", "two pointers" ]
null
null
You've got an array, consisting of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Also, you've got *m* queries, the *i*-th query is described by two integers *l**i*,<=*r**i*. Numbers *l**i*,<=*r**i* define a subsegment of the original array, that is, the sequence of numbers *a**l**i*,<=*a**l**i*<=+<=1,<=*a**l**i*<=+<=2,<=...,<=*a**r**i*. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers *b*1,<=*b*2,<=...,<=*b**k*, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer *x* (1<=≀<=*x*<=≀<=*k*), that the following inequation fulfills: *b*1<=≀<=*b*2<=≀<=...<=≀<=*b**x*<=β‰₯<=*b**x*<=+<=1<=β‰₯<=*b**x*<=+<=2...<=β‰₯<=*b**k*. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the number of array elements and the number of queries. The second line contains the sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109), where number *a**i* stands for the *i*-th array element. The following *m* lines contain the description of the queries. The *i*-th line contains the description of the *i*-th query, consisting of two integers *l**i*, *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*) β€” the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces.
Print *m* lines, in the *i*-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the *i*-th query is the ladder, or word "No" (without the quotes) otherwise.
[ "8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8\n" ]
[ "Yes\nYes\nNo\nYes\nNo\nYes\n" ]
none
[ { "input": "8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8", "output": "Yes\nYes\nNo\nYes\nNo\nYes" }, { "input": "1 1\n6\n1 1", "output": "Yes" }, { "input": "2 5\n1 1\n1 2\n2 2\n2 2\n1 2\n1 2", "output": "Yes\nYes\nYes\nYes\nYes" }, { "input": "10 10\n4 7 2 6 4 8 1 2 1 ...
92
0
0
90,264
878
Teams Formation
[ "data structures", "implementation" ]
null
null
This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has *n* passenger seats, seat *i* can be occupied only by a participant from the city *a**i*. Today the bus has completed *m* trips, each time bringing *n* participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i.Β e. if we write down the cities where the participants came from, we get the sequence *a*1,<=*a*2,<=...,<=*a**n* repeated *m* times). After that some teams were formed, each consisting of *k* participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no *k* neighboring participants from the same city. Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected.
The first line contains three integers *n*,<=*k* and *m* (1<=≀<=*n*<=≀<=105, 2<=≀<=*k*<=≀<=109, 1<=≀<=*m*<=≀<=109). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=105), where *a**i* is the number of city, person from which must take seat *i* in the bus.
Output the number of remaining participants in the line.
[ "4 2 5\n1 2 3 1\n", "1 9 10\n1\n", "3 2 10\n1 2 1\n" ]
[ "12\n", "1\n", "0\n" ]
In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line.
[ { "input": "4 2 5\n1 2 3 1", "output": "12" }, { "input": "1 9 10\n1", "output": "1" }, { "input": "3 2 10\n1 2 1", "output": "0" }, { "input": "1 10 10\n1", "output": "0" }, { "input": "1 1000000000 1000000000\n100000", "output": "0" }, { "input": "2 ...
62
5,632,000
0
90,517
406
Hamming Triples
[ "implementation", "math", "two pointers" ]
null
null
Little Chris is having a nightmare. Even in dreams all he thinks about is math. Chris dreams about *m* binary strings of length *n*, indexed with numbers from 1 to *m*. The most horrifying part is that the bits of each string are ordered in either ascending or descending order. For example, Chris could be dreaming about the following 4 strings of length 5: The Hamming distance *H*(*a*,<=*b*) between two strings *a* and *b* of length *n* is the number of positions at which the corresponding symbols are different. Π‘hris thinks that each three strings with different indices constitute a single triple. Chris's delusion is that he will wake up only if he counts the number of such string triples *a*, *b*, *c* that the sum *H*(*a*,<=*b*)<=+<=*H*(*b*,<=*c*)<=+<=*H*(*c*,<=*a*) is maximal among all the string triples constructed from the dreamed strings. Help Chris wake up from this nightmare!
The first line of input contains two space-separated integers *n* and *m* (1<=≀<=*n*<=≀<=109; 3<=≀<=*m*<=≀<=105), the length and the number of strings. The next *m* lines contain the description of the strings. The *i*-th line contains two space-separated integers *s**i* and *f**i* (0<=≀<=*s**i*<=≀<=1; 1<=≀<=*f**i*<=≀<=*n*), the description of the string with index *i*; that means that the first *f**i* bits of the *i*-th string are equal to *s**i*, and the remaining *n*<=-<=*f**i* bits are equal to 1<=-<=*s**i*. There can be multiple equal strings in Chris's dream.
Output a single integer, the number of such string triples among the given that the sum of the Hamming distances between the strings of the triple is maximal.
[ "5 4\n0 3\n0 5\n1 4\n1 5\n", "10 4\n1 5\n0 5\n0 5\n1 5\n" ]
[ "3\n", "4\n" ]
none
[]
46
0
0
90,536
436
Dungeons and Candies
[ "dsu", "graphs", "greedy", "trees" ]
null
null
During the loading of the game "Dungeons and Candies" you are required to get descriptions of *k* levels from the server. Each description is a map of an *n*<=Γ—<=*m* checkered rectangular field. Some cells of the field contain candies (each cell has at most one candy). An empty cell is denoted as "." on the map, but if a cell has a candy, it is denoted as a letter of the English alphabet. A level may contain identical candies, in this case the letters in the corresponding cells of the map will be the same. When you transmit information via a network, you want to minimize traffic β€” the total size of the transferred data. The levels can be transmitted in any order. There are two ways to transmit the current level *A*: 1. You can transmit the whole level *A*. Then you need to transmit *n*Β·*m* bytes via the network. 1. You can transmit the difference between level *A* and some previously transmitted level *B* (if it exists); this operation requires to transmit *d**A*,<=*B*Β·*w* bytes, where *d**A*,<=*B* is the number of cells of the field that are different for *A* and *B*, and *w* is a constant. Note, that you should compare only the corresponding cells of levels *A* and *B* to calculate *d**A*,<=*B*. You cannot transform the maps of levels, i.e. rotate or shift them relatively to each other. Your task is to find a way to transfer all the *k* levels and minimize the traffic.
The first line contains four integers *n*,<=*m*,<=*k*,<=*w* (1<=≀<=*n*,<=*m*<=≀<=10;Β 1<=≀<=*k*,<=*w*<=≀<=1000). Then follows the description of *k* levels. Each level is described by *n* lines, each line contains *m* characters. Each character is either a letter of the English alphabet or a dot ("."). Please note that the case of the letters matters.
In the first line print the required minimum number of transferred bytes. Then print *k* pairs of integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=...,<=*x**k*,<=*y**k*, describing the way to transfer levels. Pair *x**i*, *y**i* means that level *x**i* needs to be transferred by way *y**i*. If *y**i* equals 0, that means that the level must be transferred using the first way, otherwise *y**i* must be equal to the number of a previously transferred level. It means that you will transfer the difference between levels *y**i* and *x**i* to transfer level *x**i*. Print the pairs in the order of transferring levels. The levels are numbered 1 through *k* in the order they follow in the input. If there are multiple optimal solutions, you can print any of them.
[ "2 3 3 2\nA.A\n...\nA.a\n..C\nX.Y\n...\n", "1 1 4 1\nA\n.\nB\n.\n", "1 3 5 2\nABA\nBBB\nBBA\nBAB\nABB\n" ]
[ "14\n1 0\n2 1\n3 1\n", "3\n1 0\n2 0\n4 2\n3 0\n", "11\n1 0\n3 1\n2 3\n4 2\n5 1\n" ]
none
[ { "input": "1 1 4 1\nA\n.\nB\n.", "output": "3\n1 0\n2 0\n4 2\n3 0" }, { "input": "1 3 5 2\nABA\nBBB\nBBA\nBAB\nABB", "output": "11\n1 0\n3 1\n2 3\n4 2\n5 1" }, { "input": "2 2 5 1\n..\nBA\n.A\nB.\n..\nA.\nAB\n.B\n..\n..", "output": "12\n1 0\n2 1\n3 1\n5 3\n4 5" }, { "input":...
46
614,400
0
90,804
566
Matching Names
[ "dfs and similar", "strings", "trees" ]
null
null
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the "Hobbit" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names. There are *n* students in a summer school. Teachers chose exactly *n* pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym *b* to a student with name *a* as the length of the largest common prefix *a* and *b*. We will represent such value as . Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students. Find the matching between students and pseudonyms with the maximum quality.
The first line contains number *n* (1<=≀<=*n*<=≀<=100<=000) β€” the number of students in the summer school. Next *n* lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating. The last *n* lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating. The total length of all the names and pseudonyms doesn't exceed 800<=000 characters.
In the first line print the maximum possible quality of matching pseudonyms to students. In the next *n* lines describe the optimal matching. Each line must have the form *a* *b* (1<=≀<=*a*,<=*b*<=≀<=*n*), that means that the student who was number *a* in the input, must match to the pseudonym number *b* in the input. The matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.
[ "5\ngennady\ngalya\nboris\nbill\ntoshik\nbilbo\ntorin\ngendalf\nsmaug\ngaladriel\n" ]
[ "11\n4 1\n2 5\n1 3\n5 2\n3 4\n" ]
The first test from the statement the match looks as follows: - bill  →  bilbo (lcp = 3) - galya  →  galadriel (lcp = 3) - gennady  →  gendalf (lcp = 3) - toshik  →  torin (lcp = 2) - boris  →  smaug (lcp = 0)
[ { "input": "5\ngennady\ngalya\nboris\nbill\ntoshik\nbilbo\ntorin\ngendalf\nsmaug\ngaladriel", "output": "11\n4 1\n2 5\n1 3\n5 2\n3 4" }, { "input": "1\na\na", "output": "1\n1 1" }, { "input": "2\na\na\na\na", "output": "2\n1 1\n2 2" }, { "input": "2\na\nb\na\na", "output"...
46
0
0
90,951
257
Greedy Elevator
[ "data structures", "implementation" ]
null
null
The *m*-floor (*m*<=&gt;<=1) office of international corporation CodeForces has the advanced elevator control system established. It works as follows. All office floors are sequentially numbered with integers from 1 to *m*. At time *t*<==<=0, the elevator is on the first floor, the elevator is empty and nobody is waiting for the elevator on other floors. Next, at times *t**i* (*t**i*<=&gt;<=0) people come to the elevator. For simplicity, we assume that one person uses the elevator only once during the reported interval. For every person we know three parameters: the time at which the person comes to the elevator, the floor on which the person is initially, and the floor to which he wants to go. The movement of the elevator between the floors is as follows. At time *t* (*t*<=β‰₯<=0, *t* is an integer) the elevator is always at some floor. First the elevator releases all people who are in the elevator and want to get to the current floor. Then it lets in all the people waiting for the elevator on this floor. If a person comes to the elevator exactly at time *t*, then he has enough time to get into it. We can assume that all of these actions (going in or out from the elevator) are made instantly. After that the elevator decides, which way to move and at time (*t*<=+<=1) the elevator gets to the selected floor. The elevator selects the direction of moving by the following algorithm. - If the elevator is empty and at the current time no one is waiting for the elevator on any floor, then the elevator remains at the current floor. - Otherwise, let's assume that the elevator is on the floor number *x* (1<=≀<=*x*<=≀<=*m*). Then elevator calculates the directions' "priorities" *p**up* and *p**down*: *p**up* is the sum of the number of people waiting for the elevator on the floors with numbers greater than *x*, and the number of people in the elevator, who want to get to the floors with the numbers greater than *x*; *p**down* is the sum of the number of people waiting for the elevator on the floors with numbers less than *x*, and the number of people in the elevator, who want to get to the floors with the numbers less than *x*. If *p**up*<=β‰₯<=*p**down*, then the elevator goes one floor above the current one (that is, from floor *x* to floor *x*<=+<=1), otherwise the elevator goes one floor below the current one (that is, from floor *x* to floor *x*<=-<=1). Your task is to simulate the work of the elevator and for each person to tell the time when the elevator will get to the floor this person needs. Please note that the elevator is large enough to accommodate all the people at once.
The first line contains two space-separated integers: *n*,<=*m* (1<=≀<=*n*<=≀<=105,<=2<=≀<=*m*<=≀<=105) β€” the number of people and floors in the building, correspondingly. Next *n* lines each contain three space-separated integers: *t**i*,<=*s**i*,<=*f**i* (1<=≀<=*t**i*<=≀<=109,<=1<=≀<=*s**i*,<=*f**i*<=≀<=*m*,<=*s**i*<=β‰ <=*f**i*) β€” the time when the *i*-th person begins waiting for the elevator, the floor number, where the *i*-th person was initially located, and the number of the floor, where he wants to go.
Print *n* lines. In the *i*-th line print a single number β€” the moment of time, when the *i*-th person gets to the floor he needs. The people are numbered in the order, in which they are given in the input. Please don't 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.
[ "3 10\n1 2 7\n3 6 5\n3 4 8\n", "2 10\n1 2 5\n7 4 5\n" ]
[ "7\n11\n8\n", "5\n9\n" ]
In the first sample the elevator worked as follows: - *t* = 1. The elevator is on the floor number 1. The elevator is empty. The floor number 2 has one person waiting. *p*<sub class="lower-index">*up*</sub> = 1 + 0 = 1, *p*<sub class="lower-index">*down*</sub> = 0 + 0 = 0, *p*<sub class="lower-index">*up*</sub> β‰₯ *p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 2. - *t* = 2. The elevator is on the floor number 2. One person enters the elevator, he wants to go to the floor number 7. *p*<sub class="lower-index">*up*</sub> = 0 + 1 = 1, *p*<sub class="lower-index">*down*</sub> = 0 + 0 = 0, *p*<sub class="lower-index">*up*</sub> β‰₯ *p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 3. - *t* = 3. The elevator is on the floor number 3. There is one person in the elevator, he wants to go to floor 7. The floors number 4 and 6 have two people waiting for the elevator. *p*<sub class="lower-index">*up*</sub> = 2 + 1 = 3, *p*<sub class="lower-index">*down*</sub> = 0 + 0 = 0, *p*<sub class="lower-index">*up*</sub> β‰₯ *p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 4. - *t* = 4. The elevator is on the floor number 4. There is one person in the elevator who wants to go to the floor number 7. One person goes into the elevator, he wants to get to the floor number 8. The floor number 6 has one man waiting. *p*<sub class="lower-index">*up*</sub> = 1 + 2 = 3, *p*<sub class="lower-index">*down*</sub> = 0 + 0 = 0, *p*<sub class="lower-index">*up*</sub> β‰₯ *p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 5. - *t* = 5. The elevator is on the floor number 5. There are two people in the elevator, they want to get to the floors number 7 and 8, correspondingly. There is one person waiting for the elevator on the floor number 6. *p*<sub class="lower-index">*up*</sub> = 1 + 2 = 3, *p*<sub class="lower-index">*down*</sub> = 0 + 0 = 0, *p*<sub class="lower-index">*up*</sub> β‰₯ *p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 6. - *t* = 6. The elevator is on the floor number 6. There are two people in the elevator, they want to get to the floors number 7 and 8, correspondingly. One man enters the elevator, he wants to get to the floor number 5. *p*<sub class="lower-index">*up*</sub> = 0 + 2 = 2, *p*<sub class="lower-index">*down*</sub> = 0 + 1 = 1, *p*<sub class="lower-index">*up*</sub> β‰₯ *p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 7. - *t* = 7. The elevator is on the floor number 7. One person leaves the elevator, this person wanted to get to the floor number 7. There are two people in the elevator, they want to get to the floors with numbers 8 and 5, correspondingly. *p*<sub class="lower-index">*up*</sub> = 0 + 1 = 1, *p*<sub class="lower-index">*down*</sub> = 0 + 1 = 1, *p*<sub class="lower-index">*up*</sub> β‰₯ *p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 8. - *t* = 8. The elevator is on the floor number 8. One person leaves the elevator, this person wanted to go to the floor number 8. There is one person in the elevator, he wants to go to the floor number 5. *p*<sub class="lower-index">*up*</sub> = 0 + 0 = 0, *p*<sub class="lower-index">*down*</sub> = 0 + 1 = 1, *p*<sub class="lower-index">*up*</sub> &lt; *p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 7. - *t* = 9. The elevator is on the floor number 7. There is one person in the elevator, this person wants to get to the floor number 5. *p*<sub class="lower-index">*up*</sub> = 0 + 0 = 0, *p*<sub class="lower-index">*down*</sub> = 0 + 1 = 1, *p*<sub class="lower-index">*up*</sub> &lt; *p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 6. - *t* = 10. The elevator is on the floor number 6. There is one person in the elevator, he wants to get to the floor number 5. *p*<sub class="lower-index">*up*</sub> = 0 + 0 = 0, *p*<sub class="lower-index">*down*</sub> = 0 + 1 = 1, *p*<sub class="lower-index">*up*</sub> &lt; *p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 5. - *t* = 11. The elevator is on the floor number 5. One person leaves the elevator, this person initially wanted to get to the floor number 5. The elevator is empty and nobody needs it, so the elevator remains at the floor number 5.
[]
92
0
0
90,991
1,007
Ants
[ "2-sat", "data structures", "trees" ]
null
null
There is a tree with $n$ vertices. There are also $m$ ants living on it. Each ant has its own color. The $i$-th ant has two favorite pairs of vertices: ($a_i, b_i$) and ($c_i, d_i$). You need to tell if it is possible to paint the edges of the tree in $m$ colors so that every ant will be able to walk between vertices from one of its favorite pairs using only edges of his color; if it is possible, you need to print which pair every ant should use.
The first line contains a single integer $n$ ($2 \leq n \leq 10^5$)Β β€” the number of vertices. Each of the next $n-1$ lines contains two integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq n$), meaning that there is an edge between vertices $u_i$ and $v_i$. The next line contains a single integer $m$ ($1 \leq m \leq 10^4$)Β β€” the number of ants. Each of the next $m$ lines contains four integers $a_i$, $b_i$, $c_i$, and $d_i$ ($1 \leq a_i, b_i, c_i, d_i \leq n$, $a_i \neq b_i, c_i \neq d_i$), meaning that pairs ($a_i$, $b_i$) and ($c_i$, $d_i$) are favorite for the $i$-th ant.
Print "NO" (without quotes) if the wanted painting is impossible. Otherwise, print "YES" (without quotes). Print $m$ lines. On the $i$-th line, print $1$ if the $i$-th ant will use the first pair and $2$ otherwise. If there are multiple answers, print any.
[ "6\n1 2\n3 1\n4 1\n5 2\n6 2\n3\n2 6 3 4\n1 6 6 5\n1 4 5 2\n", "5\n1 2\n1 3\n1 4\n1 5\n2\n2 3 4 5\n3 4 5 2\n" ]
[ "YES\n2\n1\n2\n", "NO\n" ]
In the sample, the second and the third edge should be painted in the first color, the first and the fifth should be painted in the second color, and the fourth should be painted in the third color.
[]
93
2,150,400
-1
91,077
685
Kay and Eternity
[ "brute force", "implementation", "sortings" ]
null
null
Snow Queen told Kay to form a word "eternity" using pieces of ice. Kay is eager to deal with the task, because he will then become free, and Snow Queen will give him all the world and a pair of skates. Behind the palace of the Snow Queen there is an infinite field consisting of cells. There are *n* pieces of ice spread over the field, each piece occupying exactly one cell and no two pieces occupying the same cell. To estimate the difficulty of the task Kay looks at some squares of size *k*<=Γ—<=*k* cells, with corners located at the corners of the cells and sides parallel to coordinate axis and counts the number of pieces of the ice inside them. This method gives an estimation of the difficulty of some part of the field. However, Kay also wants to estimate the total difficulty, so he came up with the following criteria: for each *x* (1<=≀<=*x*<=≀<=*n*) he wants to count the number of squares of size *k*<=Γ—<=*k*, such that there are exactly *x* pieces of the ice inside. Please, help Kay estimate the difficulty of the task given by the Snow Queen.
The first line of the input contains two integers *n* and *k* (1<=≀<=*n*<=≀<=100<=000, 1<=≀<=*k*<=≀<=300)Β β€” the number of pieces of the ice and the value *k*, respectively. Each of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=109<=≀<=*x**i*,<=*y**i*<=≀<=109)Β β€” coordinates of the cell containing *i*-th piece of the ice. It's guaranteed, that no two pieces of the ice occupy the same cell.
Print *n* integers: the number of squares of size *k*<=Γ—<=*k* containing exactly 1,<=2,<=...,<=*n* pieces of the ice.
[ "5 3\n4 5\n4 6\n5 5\n5 6\n7 7\n" ]
[ "10 8 1 4 0 \n" ]
none
[ { "input": "5 3\n4 5\n4 6\n5 5\n5 6\n7 7", "output": "10 8 1 4 0 " }, { "input": "10 2\n4 7\n-9 -9\n9 2\n4 4\n7 10\n9 8\n-6 -2\n-9 2\n-7 -6\n0 5", "output": "40 0 0 0 0 0 0 0 0 0 " }, { "input": "10 5\n-7 -3\n-2 7\n4 4\n-1 5\n9 4\n-2 -3\n-5 8\n8 -4\n-6 -6\n8 8", "output": "182 31 2 0...
0
0
-1
91,096
0
none
[ "none" ]
null
null
It's May in Flatland, and there are $m$ days in this month. Despite the fact that May Holidays are canceled long time ago, employees of some software company still have a habit of taking short or long vacations in May. Of course, not all managers of the company like this. There are $n$ employees in the company that form a tree-like structure of subordination: each employee has a unique integer id $i$ between $1$ and $n$, and each employee with id $i$ (except the head manager whose id is 1) has exactly one direct manager with id $p_i$. The structure of subordination is not cyclic, i.e. if we start moving from any employee to his direct manager, then we will eventually reach the head manager. We define that an employee $u$ is a subordinate of an employee $v$, if $v$ is a direct manager of $u$, or the direct manager of $u$ is a subordinate of $v$. Let $s_i$ be the number of subordinates the $i$-th employee has (for example, $s_1 = n - 1$, because all employees except himself are subordinates of the head manager). Each employee $i$ has a bearing limit of $t_i$, which is an integer between $0$ and $s_i$. It denotes the maximum number of the subordinates of the $i$-th employee being on vacation at the same moment that he can bear. If at some moment strictly more than $t_i$ subordinates of the $i$-th employee are on vacation, and the $i$-th employee himself is not on a vacation, he becomes displeased. In each of the $m$ days of May exactly one event of the following two types happens: either one employee leaves on a vacation at the beginning of the day, or one employee returns from a vacation in the beginning of the day. You know the sequence of events in the following $m$ days. Your task is to compute for each of the $m$ days the number of displeased employees on that day.
The first line contains two integers $n$ and $m$ ($2 \leq n, m \leq 10^5$) β€” the number of employees in the company and the number of days in May. The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i \leq n$), denoting the direct managers of employees. The third line contains $n$ integers $t_1, t_2, \ldots, t_n$ ($0 \leq t_i \leq s_i$), denoting the bearing limits of empoyees. The fourth line contains $m$ integers $q_1, q_2, \ldots, q_m$ ($1 \leq |q_i| \leq n$, $q_i \ne 0$), denoting the events. If $q_i$ is positive, then the employee with id $q_i$ leaves for a vacation starting from this day, if $q_i$ is negative, then the employee $-q_i$ returns from a vacation starting from this day. In the beginning of May no employee is on vacation. It is guaranteed that if some employee leaves for a vacation, he is not on a vacation at the moment and vice versa.
Print a sequence of $m$ integers $a_1, a_2, \ldots, a_m$, where $a_i$ is the number of displeased employees on the $i$-th day.
[ "7 8\n4 5 1 1 5 5\n0 0 0 1 2 0 0\n2 6 3 7 -2 4 -3 1\n", "5 6\n1 2 3 4\n4 0 0 1 0\n1 5 2 3 -5 -1\n" ]
[ "1 1 1 2 2 2 1 0\n", "0 2 1 0 0 0\n" ]
In the first sample test after employee with id 2 leaves for a vacation at the first day, the head manager with id 1 becomes displeased as he does not want any of his subordinates to go for a vacation. At the fourth day employee with id 5 becomes displeased as his last remaining employee with id 7 leaves for a vacation. At the fifth day employee with id 2 returns from the vacation, but it does not affect the number of displeased employees as the employees 5 and 1 are still displeased. At the sixth day employee with id 3 returns back from the vacation, preventing the employee with id 5 from being displeased and at the last day the head manager with id 1 leaves for a vacation, leaving the company without the displeased people at all.
[]
77
2,867,200
-1
91,150
893
Subtree Minimum Query
[ "data structures", "trees" ]
null
null
You are given a rooted tree consisting of *n* vertices. Each vertex has a number written on it; number *a**i* is written on vertex *i*. Let's denote *d*(*i*,<=*j*) as the distance between vertices *i* and *j* in the tree (that is, the number of edges in the shortest path from *i* to *j*). Also let's denote the *k*-blocked subtree of vertex *x* as the set of vertices *y* such that both these conditions are met: - *x* is an ancestor of *y* (every vertex is an ancestor of itself); - *d*(*x*,<=*y*)<=≀<=*k*. You are given *m* queries to the tree. *i*-th query is represented by two numbers *x**i* and *k**i*, and the answer to this query is the minimum value of *a**j* among such vertices *j* such that *j* belongs to *k**i*-blocked subtree of *x**i*. Write a program that would process these queries quickly! Note that the queries are given in a modified way.
The first line contains two integers *n* and *r* (1<=≀<=*r*<=≀<=*n*<=≀<=100000) β€” the number of vertices in the tree and the index of the root, respectively. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” the numbers written on the vertices. Then *n*<=-<=1 lines follow, each containing two integers *x* and *y* (1<=≀<=*x*,<=*y*<=≀<=*n*) and representing an edge between vertices *x* and *y*. It is guaranteed that these edges form a tree. Next line contains one integer *m* (1<=≀<=*m*<=≀<=106) β€” the number of queries to process. Then *m* lines follow, *i*-th line containing two numbers *p**i* and *q**i*, which can be used to restore *i*-th query (1<=≀<=*p**i*,<=*q**i*<=≀<=*n*). *i*-th query can be restored as follows: Let *last* be the answer for previous query (or 0 if *i*<==<=1). Then *x**i*<==<=((*p**i*<=+<=*last*)<=*mod*<=*n*)<=+<=1, and *k**i*<==<=(*q**i*<=+<=*last*)<=*mod*<=*n*.
Print *m* integers. *i*-th of them has to be equal to the answer to *i*-th query.
[ "5 2\n1 3 2 3 5\n2 3\n5 1\n3 4\n4 1\n2\n1 2\n2 3\n" ]
[ "2\n5\n" ]
none
[ { "input": "5 2\n1 3 2 3 5\n2 3\n5 1\n3 4\n4 1\n2\n1 2\n2 3", "output": "2\n5" }, { "input": "3 3\n1 3 2\n1 2\n1 3\n2\n2 3\n1 1", "output": "2\n1" }, { "input": "1 1\n1\n1\n1 1", "output": "1" } ]
0
0
-1
91,765
280
Rectangle Puzzle
[ "geometry" ]
null
null
You are given two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the *Ox* axis, equals *w*, the length of the side that is parallel to the *Oy* axis, equals *h*. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle Ξ±. Your task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.
The first line contains three integers *w*,<=*h*,<=Ξ± (1<=≀<=*w*,<=*h*<=≀<=106;Β 0<=≀<=Ξ±<=≀<=180). Angle Ξ± is given in degrees.
In a single line print a real number β€” the area of the region which belongs to both given rectangles. The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=6.
[ "1 1 45\n", "6 4 30\n" ]
[ "0.828427125\n", "19.668384925\n" ]
The second sample has been drawn on the picture above.
[ { "input": "1 1 45", "output": "0.828427125" }, { "input": "6 4 30", "output": "19.668384925" }, { "input": "100 100 0", "output": "10000.000000000" }, { "input": "100 100 30", "output": "8452.994616207" }, { "input": "303304 904227 3", "output": "262706079399...
280
0
3
91,830
758
Ability To Convert
[ "constructive algorithms", "dp", "greedy", "math", "strings" ]
null
null
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter *A* he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475<==<=1Β·162<=+<=13Β·161<=+<=11Β·160). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base *n* he will get the number *k*.
The first line contains the integer *n* (2<=≀<=*n*<=≀<=109). The second line contains the integer *k* (0<=≀<=*k*<=&lt;<=1060), it is guaranteed that the number *k* contains no more than 60 symbols. All digits in the second line are strictly less than *n*. Alexander guarantees that the answer exists and does not exceed 1018. The number *k* doesn't contain leading zeros.
Print the number *x* (0<=≀<=*x*<=≀<=1018)Β β€” the answer to the problem.
[ "13\n12\n", "16\n11311\n", "20\n999\n", "17\n2016\n" ]
[ "12", "475", "3789", "594" ]
In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12Β·13<sup class="upper-index">0</sup> or 15 = 1Β·13<sup class="upper-index">1</sup> + 2Β·13<sup class="upper-index">0</sup>.
[ { "input": "13\n12", "output": "12" }, { "input": "16\n11311", "output": "475" }, { "input": "20\n999", "output": "3789" }, { "input": "17\n2016", "output": "594" }, { "input": "1000\n1001", "output": "100001" }, { "input": "1000\n1000", "output": ...
46
0
0
91,835
690
Tree of Life (medium)
[ "constructive algorithms", "hashing", "trees" ]
null
null
Heidi got tired of deciphering the prophecy hidden in the Tree of Life and decided to go back to her headquarters, rest a little and try there. Of course, she cannot uproot the Tree and take it with her, so she made a drawing of the Tree on a piece of paper. On second thought, she made more identical drawings so as to have *n* in total (where *n* is the number of vertices of the Tree of Life) – who knows what might happen? Indeed, on her way back Heidi was ambushed by a group of zombies. While she managed to fend them off, they have damaged her drawings in a peculiar way: from the *i*-th copy, the vertex numbered *i* was removed, along with all adjacent edges. In each picture, the zombies have also erased all the vertex numbers and relabeled the remaining *n*<=-<=1 vertices arbitrarily using numbers 1 to *n* (fortunately, each vertex still has a distinct number). What's more, the drawings have been arbitrarily shuffled/reordered. Now Heidi wants to recover the Tree of Life from her descriptions of all the drawings (as lists of edges).
The first line of the input contains *Z*<=≀<=20 – the number of test cases. *Z* descriptions of single test cases follow. In each test case, the first line of input contains numbers *n* (2<=≀<=*n*<=≀<=100) and *k* (where *k* is the number of drawings; we have *k*<==<=*n*). In the following lines, the descriptions of the *k* drawings are given. The description of the *i*-th drawing is a line containing *m**i* – the number of edges in this drawing, followed by *m**i* lines describing edges, each of which contains two space-separated integers –- the numbers of the two vertices connected by the edge.
If Heidi's drawings cannot possibly come from a single tree, you should output the word NO. Otherwise, output one line containing the word YES and *n*<=-<=1 lines describing any tree that Heidi's drawings could have come from. For every edge you should output the numbers of the vertices that it connects, separated with a single space. If there are many solutions, print any of them.
[ "1\n5 5\n2\n4 1\n2 1\n1\n3 1\n3\n4 1\n4 3\n2 1\n3\n3 1\n3 2\n4 1\n3\n2 1\n3 2\n4 2\n" ]
[ "YES\n2 5\n4 2\n3 2\n5 1\n" ]
none
[]
5,000
7,884,800
0
91,872
207
Military Trainings
[]
null
null
The Smart Beaver from ABBYY started cooperating with the Ministry of Defence. Now they train soldiers to move armoured columns. The training involves testing a new type of tanks that can transmit information. To test the new type of tanks, the training has a special exercise, its essence is as follows. Initially, the column consists of *n* tanks sequentially numbered from 1 to *n* in the order of position in the column from its beginning to its end. During the whole exercise, exactly *n* messages must be transferred from the beginning of the column to its end. Transferring one message is as follows. The tank that goes first in the column transmits the message to some tank in the column. The tank which received the message sends it further down the column. The process is continued until the last tank receives the message. It is possible that not all tanks in the column will receive the message β€” it is important that the last tank in the column should receive the message. After the last tank (tank number *n*) receives the message, it moves to the beginning of the column and sends another message to the end of the column in the same manner. When the message reaches the last tank (tank number *n*<=-<=1), that tank moves to the beginning of the column and sends the next message to the end of the column, and so on. Thus, the exercise is completed when the tanks in the column return to their original order, that is, immediately after tank number 1 moves to the beginning of the column. If the tanks were initially placed in the column in the order 1,<=2,<=...,<=*n*, then after the first message their order changes to *n*,<=1,<=...,<=*n*<=-<=1, after the second message it changes to *n*<=-<=1,<=*n*,<=1,<=...,<=*n*<=-<=2, and so on. The tanks are constructed in a very peculiar way. The tank with number *i* is characterized by one integer *a**i*, which is called the message receiving radius of this tank. Transferring a message between two tanks takes one second, however, not always one tank can transmit a message to another one. Let's consider two tanks in the column such that the first of them is the *i*-th in the column counting from the beginning, and the second one is the *j*-th in the column, and suppose the second tank has number *x*. Then the first tank can transmit a message to the second tank if *i*<=&lt;<=*j* and *i*<=β‰₯<=*j*<=-<=*a**x*. The Ministry of Defense (and soon the Smart Beaver) faced the question of how to organize the training efficiently. The exercise should be finished as quickly as possible. We'll neglect the time that the tanks spend on moving along the column, since improving the tanks' speed is not a priority for this training. You are given the number of tanks, as well as the message receiving radii of all tanks. You must help the Smart Beaver and organize the transferring of messages in a way that makes the total transmission time of all messages as small as possible.
The first line contains integer *n* β€” the number of tanks in the column. Each of the next *n* lines contains one integer *a**i* (1<=≀<=*a**i*<=≀<=250000, 1<=≀<=*i*<=≀<=*n*) β€” the message receiving radii of the tanks in the order from tank 1 to tank *n* (let us remind you that initially the tanks are located in the column in ascending order of their numbers). To get the full points for the first group of tests it is sufficient to solve the problem with 2<=≀<=*n*<=≀<=300. To get the full points for the second group of tests it is sufficient to solve the problem with 2<=≀<=*n*<=≀<=10000. To get the full points for the third group of tests it is sufficient to solve the problem with 2<=≀<=*n*<=≀<=250000.
Print a single integer β€” the minimum possible total time of transmitting the messages. 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.
[ "3\n2\n1\n1\n", "5\n2\n2\n2\n2\n2\n" ]
[ "5\n", "10\n" ]
In the first sample the original order of tanks is 1, 2, 3. The first tank sends a message to the second one, then the second tank sends it to the third one β€” it takes two seconds. The third tank moves to the beginning of the column and the order of tanks now is 3, 1, 2. The third tank sends a message to the first one, then the first one sends it to the second one β€” it takes two more seconds. The second tank moves to the beginning and the order of the tanks is now 2, 3, 1. With this arrangement, the second tank can immediately send a message to the first one, since the message receiving radius of the first tank is large enough β€” it takes one second. Finally, the tanks return to their original order 1, 2, 3. In total, the exercise takes 5 seconds. In the second sample, all five tanks are the same and sending a single message takes two seconds, so in total the exercise takes 10 seconds.
[]
60
0
0
92,069
198
Delivering Carcinogen
[ "binary search", "geometry" ]
null
null
Qwerty the Ranger arrived to the Diatar system with a very important task. He should deliver a special carcinogen for scientific research to planet Persephone. This is urgent, so Qwerty has to get to the planet as soon as possible. A lost day may fail negotiations as nobody is going to pay for an overdue carcinogen. You can consider Qwerty's ship, the planet Persephone and the star Diatar points on a plane. Diatar is located in the origin of coordinate axes β€” at point (0,<=0). Persephone goes round Diatar along a circular orbit with radius *R* in the counter-clockwise direction at constant linear speed *v**p* (thus, for instance, a full circle around the star takes of time). At the initial moment of time Persephone is located at point (*x**p*,<=*y**p*). At the initial moment of time Qwerty's ship is at point (*x*,<=*y*). Qwerty can move in any direction at speed of at most *v* (*v*<=&gt;<=*v**p*). The star Diatar is hot (as all stars), so Qwerty can't get too close to it. The ship's metal sheathing melts at distance *r* (*r*<=&lt;<=*R*) from the star. Find the minimum time Qwerty needs to get the carcinogen to planet Persephone.
The first line contains space-separated integers *x**p*, *y**p* and *v**p* (<=-<=104<=≀<=*x**p*,<=*y**p*<=≀<=104, 1<=≀<=*v**p*<=&lt;<=104) β€” Persephone's initial position and the speed at which it goes round Diatar. The second line contains space-separated integers *x*, *y*, *v* and *r* (<=-<=104<=≀<=*x*,<=*y*<=≀<=104, 1<=&lt;<=*v*<=≀<=104, 1<=≀<=*r*<=≀<=104) β€” The intial position of Qwerty's ship, its maximum speed and the minimum safe distance to star Diatar. It is guaranteed that *r*2<=&lt;<=*x*2<=+<=*y*2, *r*2<=&lt;<=*x**p*2<=+<=*y**p*2 and *v**p*<=&lt;<=*v*.
Print a single real number β€” the minimum possible delivery time. The answer will be considered valid if its absolute or relative error does not exceed 10<=-<=6.
[ "10 0 1\n-10 0 2 8\n", "50 60 10\n50 60 20 40\n" ]
[ "9.584544103", "0.000000000" ]
none
[ { "input": "10 0 1\n-10 0 2 8", "output": "9.584544103" }, { "input": "50 60 10\n50 60 20 40", "output": "0.000000000" }, { "input": "86 -67 50\n-60 -20 91 35", "output": "2.047618248" }, { "input": "-137 -45 64\n-86 -61 91 61", "output": "0.447407904" }, { "input...
124
0
0
92,241
717
Dexterina’s Lab
[ "games", "matrices", "probabilities" ]
null
null
Dexterina and Womandark have been arch-rivals since they’ve known each other. Since both are super-intelligent teenage girls, they’ve always been trying to solve their disputes in a peaceful and nonviolent way. After god knows how many different challenges they’ve given to one another, their score is equal and they’re both desperately trying to best the other in various games of wits. This time, Dexterina challenged Womandark to a game of Nim. Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects from a single heap. The player who can't make a turn loses. By their agreement, the sizes of piles are selected randomly from the range [0,<=*x*]. Each pile's size is taken independently from the same probability distribution that is known before the start of the game. Womandark is coming up with a brand new and evil idea on how to thwart Dexterina’s plans, so she hasn’t got much spare time. She, however, offered you some tips on looking fabulous in exchange for helping her win in Nim. Your task is to tell her what is the probability that the first player to play wins, given the rules as above.
The first line of the input contains two integers *n* (1<=≀<=*n*<=≀<=109) and *x* (1<=≀<=*x*<=≀<=100)Β β€” the number of heaps and the maximum number of objects in a heap, respectively. The second line contains *x*<=+<=1 real numbers, given with up to 6 decimal places each: *P*(0),<=*P*(1),<=... ,<=*P*(*X*). Here, *P*(*i*) is the probability of a heap having exactly *i* objects in start of a game. It's guaranteed that the sum of all *P*(*i*) is equal to 1.
Output a single real number, the probability that the first player wins. The answer will be judged as correct if it differs from the correct answer by at most 10<=-<=6.
[ "2 2\n0.500000 0.250000 0.250000\n" ]
[ "0.62500000\n" ]
none
[ { "input": "2 2\n0.500000 0.250000 0.250000", "output": "0.62500000" }, { "input": "9 9\n0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000 0.100000", "output": "0.93687014" }, { "input": "1000001 5\n0.000000 0.300000 0.000000 0.500000 0.000000 0.200000", ...
46
0
0
92,392
180
Divisibility Rules
[ "math", "number theory" ]
null
null
Vasya studies divisibility rules at school. Here are some of them: - Divisibility by 2. A number is divisible by 2 if and only if its last digit is divisible by 2 or in other words, is even.- Divisibility by 3. A number is divisible by 3 if and only if the sum of its digits is divisible by 3.- Divisibility by 4. A number is divisible by 4 if and only if its last two digits form a number that is divisible by 4.- Divisibility by 5. A number is divisible by 5 if and only if its last digit equals 5 or 0.- Divisibility by 6. A number is divisible by 6 if and only if it is divisible by 2 and 3 simultaneously (that is, if the last digit is even and the sum of all digits is divisible by 3).- Divisibility by 7. Vasya doesn't know such divisibility rule.- Divisibility by 8. A number is divisible by 8 if and only if its last three digits form a number that is divisible by 8.- Divisibility by 9. A number is divisible by 9 if and only if the sum of its digits is divisible by 9.- Divisibility by 10. A number is divisible by 10 if and only if its last digit is a zero.- Divisibility by 11. A number is divisible by 11 if and only if the sum of digits on its odd positions either equals to the sum of digits on the even positions, or they differ in a number that is divisible by 11. Vasya got interested by the fact that some divisibility rules resemble each other. In fact, to check a number's divisibility by 2, 4, 5, 8 and 10 it is enough to check fulfiling some condition for one or several last digits. Vasya calls such rules the 2-type rules. If checking divisibility means finding a sum of digits and checking whether the sum is divisible by the given number, then Vasya calls this rule the 3-type rule (because it works for numbers 3 and 9). If we need to find the difference between the sum of digits on odd and even positions and check whether the difference is divisible by the given divisor, this rule is called the 11-type rule (it works for number 11). In some cases we should divide the divisor into several factors and check whether rules of different types (2-type, 3-type or 11-type) work there. For example, for number 6 we check 2-type and 3-type rules, for number 66 we check all three types. Such mixed divisibility rules are called 6-type rules. And finally, there are some numbers for which no rule works: neither 2-type, nor 3-type, nor 11-type, nor 6-type. The least such number is number 7, so we'll say that in such cases the mysterious 7-type rule works, the one that Vasya hasn't discovered yet. Vasya's dream is finding divisibility rules for all possible numbers. He isn't going to stop on the decimal numbers only. As there are quite many numbers, ha can't do it all by himself. Vasya asked you to write a program that determines the divisibility rule type in the *b*-based notation for the given divisor *d*.
The first input line contains two integers *b* and *d* (2<=≀<=*b*,<=*d*<=≀<=100) β€” the notation system base and the divisor. Both numbers are given in the decimal notation.
On the first output line print the type of the rule in the *b*-based notation system, where the divisor is *d*: "2-type", "3-type", "11-type", "6-type" or "7-type". If there are several such types, print the one that goes earlier in the given sequence. If a number belongs to the 2-type, print on the second line the least number of the last *b*-based digits that we will need to use to check the divisibility.
[ "10 10\n", "2 3\n" ]
[ "2-type\n1\n", "11-type\n" ]
The divisibility rule for number 3 in binary notation looks as follows: "A number is divisible by 3 if and only if the sum of its digits that occupy the even places differs from the sum of digits that occupy the odd places, in a number that is divisible by 3". That's an 11-type rule. For example, 21<sub class="lower-index">10</sub> = 10101<sub class="lower-index">2</sub>. For it the sum of digits on odd positions equals 1 + 1 + 1 = 3, an on even positions β€” 0 + 0 = 0. The rule works and the number is divisible by 3. In some notations a number can fit into the 3-type rule and the 11-type rule. In this case the correct answer is "3-type".
[ { "input": "10 10", "output": "2-type\n1" }, { "input": "2 3", "output": "11-type" }, { "input": "2 2", "output": "2-type\n1" }, { "input": "2 3", "output": "11-type" }, { "input": "2 4", "output": "2-type\n2" }, { "input": "2 5", "output": "7-type...
92
102,400
3
92,475
821
Okabe and El Psy Kongroo
[ "dp", "matrices" ]
null
null
Okabe likes to take walks but knows that spies from the Organization could be anywhere; that's why he wants to know how many different walks he can take in his city safely. Okabe's city can be represented as all points (*x*,<=*y*) such that *x* and *y* are non-negative. Okabe starts at the origin (point (0,<=0)), and needs to reach the point (*k*,<=0). If Okabe is currently at the point (*x*,<=*y*), in one step he can go to (*x*<=+<=1,<=*y*<=+<=1), (*x*<=+<=1,<=*y*), or (*x*<=+<=1,<=*y*<=-<=1). Additionally, there are *n* horizontal line segments, the *i*-th of which goes from *x*<==<=*a**i* to *x*<==<=*b**i* inclusive, and is at *y*<==<=*c**i*. It is guaranteed that *a*1<==<=0, *a**n*<=≀<=*k*<=≀<=*b**n*, and *a**i*<==<=*b**i*<=-<=1 for 2<=≀<=*i*<=≀<=*n*. The *i*-th line segment forces Okabe to walk with *y*-value in the range 0<=≀<=*y*<=≀<=*c**i* when his *x* value satisfies *a**i*<=≀<=*x*<=≀<=*b**i*, or else he might be spied on. This also means he is required to be under two line segments when one segment ends and another begins. Okabe now wants to know how many walks there are from the origin to the point (*k*,<=0) satisfying these conditions, modulo 109<=+<=7.
The first line of input contains the integers *n* and *k* (1<=≀<=*n*<=≀<=100, 1<=≀<=*k*<=≀<=1018)Β β€” the number of segments and the destination *x* coordinate. The next *n* lines contain three space-separated integers *a**i*, *b**i*, and *c**i* (0<=≀<=*a**i*<=&lt;<=*b**i*<=≀<=1018, 0<=≀<=*c**i*<=≀<=15)Β β€” the left and right ends of a segment, and its *y* coordinate. It is guaranteed that *a*1<==<=0, *a**n*<=≀<=*k*<=≀<=*b**n*, and *a**i*<==<=*b**i*<=-<=1 for 2<=≀<=*i*<=≀<=*n*.
Print the number of walks satisfying the conditions, modulo 1000000007 (109<=+<=7).
[ "1 3\n0 3 3\n", "2 6\n0 3 0\n3 10 2\n" ]
[ "4\n", "4\n" ]
The graph above corresponds to sample 1. The possible walks are: - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7fcce410dbd2cf4e427a6b50e0f159b7ce538901.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d3272e751b9cfbd2d66c0477da221a5e709750b7.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bf2314532d33218db716521bdc560bd6a5d19042.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/eda8eb8a4d01a31964b628e1f60b9c975b4ac98c.png" style="max-width: 100.0%;max-height: 100.0%;"/> The graph above corresponds to sample 2. There is only one walk for Okabe to reach (3, 0). After this, the possible walks are: - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c473f0a747d038f29946077ab3ab35a49be51f4c.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/63d2d732544825681ef0f35ecd3a9344df44e7fb.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/84ef9bab9e5e0289798b9252fc0f37fbc26a1158.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4792c74b41188d0f4722fa7378ec34596c6c916e.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "1 3\n0 3 3", "output": "4" }, { "input": "2 6\n0 3 0\n3 10 2", "output": "4" }, { "input": "2 3\n0 2 13\n2 3 11", "output": "4" }, { "input": "2 9\n0 8 0\n8 10 10", "output": "1" }, { "input": "1 1\n0 3 9", "output": "1" }, { "input": "3 8\...
46
0
0
92,599
839
Winter is here
[ "combinatorics", "dp", "math", "number theory" ]
null
null
Winter is here at the North and the White Walkers are close. John Snow has an army consisting of *n* soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a method to know how strong his army is. Let the *i*-th soldier’s strength be *a**i*. For some *k* he calls *i*1,<=*i*2,<=...,<=*i**k* a clan if *i*1<=&lt;<=*i*2<=&lt;<=*i*3<=&lt;<=...<=&lt;<=*i**k* and *gcd*(*a**i*1,<=*a**i*2,<=...,<=*a**i**k*)<=&gt;<=1 . He calls the strength of that clan *k*Β·*gcd*(*a**i*1,<=*a**i*2,<=...,<=*a**i**k*). Then he defines the strength of his army by the sum of strengths of all possible clans. Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109<=+<=7). Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it.
The first line contains integer *n* (1<=≀<=*n*<=≀<=200000)Β β€” the size of the army. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=1000000)Β β€” denoting the strengths of his soldiers.
Print one integerΒ β€” the strength of John Snow's army modulo 1000000007 (109<=+<=7).
[ "3\n3 3 1\n", "4\n2 3 4 6\n" ]
[ "12\n", "39\n" ]
In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1Β·3 + 1Β·3 + 2Β·3 = 12
[ { "input": "3\n3 3 1", "output": "12" }, { "input": "4\n2 3 4 6", "output": "39" } ]
1,106
51,404,800
3
92,714
633
Fibonacci-ish II
[ "data structures", "implementation" ]
null
null
Yash is finally tired of computing the length of the longest Fibonacci-ish sequence. He now plays around with more complex things such as Fibonacci-ish potentials. Fibonacci-ish potential of an array *a**i* is computed as follows: 1. Remove all elements *j* if there exists *i*<=&lt;<=*j* such that *a**i*<==<=*a**j*. 1. Sort the remaining elements in ascending order, i.e. *a*1<=&lt;<=*a*2<=&lt;<=...<=&lt;<=*a**n*. 1. Compute the potential as *P*(*a*)<==<=*a*1Β·*F*1<=+<=*a*2Β·*F*2<=+<=...<=+<=*a**n*Β·*F**n*, where *F**i* is the *i*-th Fibonacci number (see notes for clarification). You are given an array *a**i* of length *n* and *q* ranges from *l**j* to *r**j*. For each range *j* you have to compute the Fibonacci-ish potential of the array *b**i*, composed using all elements of *a**i* from *l**j* to *r**j* inclusive. Find these potentials modulo *m*.
The first line of the input contains integers of *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=30<=000)Β β€” the length of the initial array and the modulo, respectively. The next line contains *n* integers *a**i* (0<=≀<=*a**i*<=≀<=109)Β β€” elements of the array. Then follow the number of ranges *q* (1<=≀<=*q*<=≀<=30<=000). Last *q* lines contain pairs of indices *l**i* and *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*)Β β€” ranges to compute Fibonacci-ish potentials.
Print *q* lines, *i*-th of them must contain the Fibonacci-ish potential of the *i*-th range modulo *m*.
[ "5 10\n2 1 2 1 2\n2\n2 4\n4 5\n" ]
[ "3\n3\n" ]
For the purpose of this problem define Fibonacci numbers as follows: 1. *F*<sub class="lower-index">1</sub> = *F*<sub class="lower-index">2</sub> = 1. 1. *F*<sub class="lower-index">*n*</sub> = *F*<sub class="lower-index">*n* - 1</sub> + *F*<sub class="lower-index">*n* - 2</sub> for each *n* &gt; 2. In the first query, the subarray [1,2,1] can be formed using the minimal set {1,2}. Thus, the potential of this subarray is 1*1+2*1=3.
[]
30
0
0
92,818
394
Lightbulb for Minister
[ "geometry" ]
null
null
The Minister for education is coming! Naturally, nobody wants to perform poorly in front of such a honored guest. However, two hours before the arrival it turned out that one of the classes has a malfunctioning lightbulb β€” for some reason it doesn't get enough energy. The solution was found quickly: all we've got to do is to change the location of the lightbulb so that it got the maximum amount of energy. Everybody knows that the power of the lightbulb equals , where *C* is some constant value and *r**i* is the Euclidean distance from the bulb to the *i*-th generator. Consequently, our task is to minimize . Of course, we know the positions of all generators. The bulb should be on the ceiling of the class. The ceiling of the class is in the form of a strictly convex *m*-gon (the class itself has the form of a right prism with a strictly convex *m*-gon at the bottom). Help to find the optimum location for the bulb. Assume that all generators are in the plane of the class ceiling. Consider that the plane of the class ceiling has some Cartesian coordinate system introduced.
The first line contains integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of generators. Each of the next *n* lines contains a pair of integers *x**i*, *y**i*, representing the coordinates of the *i*-th generator in the plane of the class ceiling. It's guaranteed that no two generators have the same location. The next line contains integer *m* (3<=≀<=*m*<=≀<=105) β€” the number of vertexes in the convex polygon that describes the ceiling of the class. Each of the following *m* lines contains a pair of integers *p**i*,<=*q**i*, representing the coordinates of the *i*-th point of the polygon in the clockwise order. It's guaranteed that the polygon is strictly convex. The absolute value of all the coordinates don't exceed 106.
Print a single real number β€” the minimum value of the sum of squares of distances from the generators to the point of the lightbulb's optimal position. The answer will be considered valid if its absolute or relative error doesn't exceed 10<=-<=4.
[ "4\n3 2\n3 4\n5 4\n5 2\n4\n3 3\n4 4\n5 3\n4 2\n" ]
[ "8.00000000" ]
We'll define a strictly convex polygon as a convex polygon with the following property: no three vertices of the polygon lie on the same line.
[]
108
307,200
0
92,890
285
Positions in Permutations
[ "combinatorics", "dp", "math" ]
null
null
Permutation *p* is an ordered set of integers *p*1,<=<=*p*2,<=<=...,<=<=*p**n*, consisting of *n* distinct positive integers, each of them doesn't exceed *n*. We'll denote the *i*-th element of permutation *p* as *p**i*. We'll call number *n* the size or the length of permutation *p*1,<=<=*p*2,<=<=...,<=<=*p**n*. We'll call position *i* (1<=≀<=*i*<=≀<=*n*) in permutation *p*1,<=*p*2,<=...,<=*p**n* good, if |*p*[*i*]<=-<=*i*|<==<=1. Count the number of permutations of size *n* with exactly *k* good positions. Print the answer modulo 1000000007 (109<=+<=7).
The single line contains two space-separated integers *n* and *k* (1<=≀<=*n*<=≀<=1000,<=0<=≀<=*k*<=≀<=*n*).
Print the number of permutations of length *n* with exactly *k* good positions modulo 1000000007 (109<=+<=7).
[ "1 0\n", "2 1\n", "3 2\n", "4 1\n", "7 4\n" ]
[ "1\n", "0\n", "4\n", "6\n", "328\n" ]
The only permutation of size 1 has 0 good positions. Permutation (1, 2) has 0 good positions, and permutation (2, 1) has 2 positions. Permutations of size 3: 1. (1, 2, 3) β€” 0 positions1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/eacaee9ed78760263daaea6c99f00e6bed9f63f4.png" style="max-width: 100.0%;max-height: 100.0%;"/> β€” 2 positions1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/343aae148f8c6b48690f76a6df180f876ed28a70.png" style="max-width: 100.0%;max-height: 100.0%;"/> β€” 2 positions1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9a68a5e5de70440f14a7d6b51e01c4c2073157ea.png" style="max-width: 100.0%;max-height: 100.0%;"/> β€” 2 positions1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d3fc11bb6a53a29b6b096f9567b8cbdca17addba.png" style="max-width: 100.0%;max-height: 100.0%;"/> β€” 2 positions1. (3, 2, 1) β€” 0 positions
[ { "input": "1 0", "output": "1" }, { "input": "2 1", "output": "0" }, { "input": "3 2", "output": "4" }, { "input": "4 1", "output": "6" }, { "input": "7 4", "output": "328" }, { "input": "7 7", "output": "0" }, { "input": "8 4", "outpu...
60
0
0
92,934
815
Karen and Neighborhood
[ "binary search", "constructive algorithms", "implementation" ]
null
null
It's been long after the events of the previous problems, and Karen has now moved on from student life and is looking to relocate to a new neighborhood. The neighborhood consists of *n* houses in a straight line, labelled 1 to *n* from left to right, all an equal distance apart. Everyone in this neighborhood loves peace and quiet. Because of this, whenever a new person moves into the neighborhood, he or she always chooses the house whose minimum distance to any occupied house is maximized. If there are multiple houses with the maximum possible minimum distance, he or she chooses the leftmost one. Note that the first person to arrive always moves into house 1. Karen is the *k*-th person to enter this neighborhood. If everyone, including herself, follows this rule, which house will she move into?
The first and only line of input contains two integers, *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=1018), describing the number of houses in the neighborhood, and that Karen was the *k*-th person to move in, respectively.
Output a single integer on a line by itself, the label of the house Karen will move into.
[ "6 4\n", "39 3\n" ]
[ "2\n", "20\n" ]
In the first test case, there are 6 houses in the neighborhood, and Karen is the fourth person to move in: 1. The first person moves into house 1. 1. The second person moves into house 6. 1. The third person moves into house 3. 1. The fourth person moves into house 2. In the second test case, there are 39 houses in the neighborhood, and Karen is the third person to move in: 1. The first person moves into house 1. 1. The second person moves into house 39. 1. The third person moves into house 20.
[ { "input": "6 4", "output": "2" }, { "input": "39 3", "output": "20" }, { "input": "1 1", "output": "1" }, { "input": "1000000000000000000 1", "output": "1" }, { "input": "1000000000000000000 2", "output": "1000000000000000000" }, { "input": "69 69", ...
62
0
0
92,983
351
Jeff and Furik
[ "combinatorics", "dp", "probabilities" ]
null
null
Jeff has become friends with Furik. Now these two are going to play one quite amusing game. At the beginning of the game Jeff takes a piece of paper and writes down a permutation consisting of *n* numbers: *p*1, *p*2, ..., *p**n*. Then the guys take turns to make moves, Jeff moves first. During his move, Jeff chooses two adjacent permutation elements and then the boy swaps them. During his move, Furic tosses a coin and if the coin shows "heads" he chooses a random pair of adjacent elements with indexes *i* and *i*<=+<=1, for which an inequality *p**i*<=&gt;<=*p**i*<=+<=1 holds, and swaps them. But if the coin shows "tails", Furik chooses a random pair of adjacent elements with indexes *i* and *i*<=+<=1, for which the inequality *p**i*<=&lt;<=*p**i*<=+<=1 holds, and swaps them. If the coin shows "heads" or "tails" and Furik has multiple ways of adjacent pairs to take, then he uniformly takes one of the pairs. If Furik doesn't have any pair to take, he tosses a coin one more time. The game ends when the permutation is sorted in the increasing order. Jeff wants the game to finish as quickly as possible (that is, he wants both players to make as few moves as possible). Help Jeff find the minimum mathematical expectation of the number of moves in the game if he moves optimally well. You can consider that the coin shows the heads (or tails) with the probability of 50 percent.
The first line contains integer *n* (1<=≀<=*n*<=≀<=3000). The next line contains *n* distinct integers *p*1, *p*2, ..., *p**n* (1<=≀<=*p**i*<=≀<=*n*) β€” the permutation *p*. The numbers are separated by spaces.
In a single line print a single real value β€” the answer to the problem. The answer will be considered correct if the absolute or relative error doesn't exceed 10<=-<=6.
[ "2\n1 2\n", "5\n3 5 2 4 1\n" ]
[ "0.000000\n", "13.000000\n" ]
In the first test the sequence is already sorted, so the answer is 0.
[ { "input": "2\n1 2", "output": "0.000000" }, { "input": "5\n3 5 2 4 1", "output": "13.000000" }, { "input": "16\n6 15 3 8 7 11 9 10 2 13 4 14 1 16 5 12", "output": "108.000000" }, { "input": "9\n1 7 8 5 3 4 6 9 2", "output": "33.000000" }, { "input": "5\n2 3 4 5 1...
124
102,400
3
93,059
959
Mahmoud and Ehab and another array construction task
[ "constructive algorithms", "greedy", "math", "number theory" ]
null
null
Mahmoud has an array *a* consisting of *n* integers. He asked Ehab to find another array *b* of the same length such that: - *b* is lexicographically greater than or equal to *a*. - *b**i*<=β‰₯<=2. - *b* is pairwise coprime: for every 1<=≀<=*i*<=&lt;<=*j*<=≀<=*n*, *b**i* and *b**j* are coprime, i.Β e. *GCD*(*b**i*,<=*b**j*)<==<=1, where *GCD*(*w*,<=*z*) is the greatest common divisor of *w* and *z*. Ehab wants to choose a special array so he wants the lexicographically minimal array between all the variants. Can you find it? An array *x* is lexicographically greater than an array *y* if there exists an index *i* such than *x**i*<=&gt;<=*y**i* and *x**j*<==<=*y**j* for all 1<=≀<=*j*<=&lt;<=*i*. An array *x* is equal to an array *y* if *x**i*<==<=*y**i* for all 1<=≀<=*i*<=≀<=*n*.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105), the number of elements in *a* and *b*. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (2<=≀<=*a**i*<=≀<=105), the elements of *a*.
Output *n* space-separated integers, the *i*-th of them representing *b**i*.
[ "5\n2 3 5 4 13\n", "3\n10 3 7\n" ]
[ "2 3 5 7 11 ", "10 3 7 " ]
Note that in the second sample, the array is already pairwise coprime so we printed it.
[ { "input": "5\n2 3 5 4 13", "output": "2 3 5 7 11 " }, { "input": "3\n10 3 7", "output": "10 3 7 " }, { "input": "5\n7 10 2 5 5", "output": "7 10 3 11 13 " }, { "input": "7\n20 9 7 6 7 9 15", "output": "20 9 7 11 13 17 19 " }, { "input": "10\n5 3 2 2 3 3 3 4 2 5",...
3,000
13,312,000
0
93,245
553
Love Triangles
[ "dfs and similar", "dsu", "graphs" ]
null
null
There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has *n* characters. The characters are labeled from 1 to *n*. Every pair of two characters can either mutually love each other or mutually hate each other (there is no neutral state). You hate love triangles (A-B are in love and B-C are in love, but A-C hate each other), and you also hate it when nobody is in love. So, considering any three characters, you will be happy if exactly one pair is in love (A and B love each other, and C hates both A and B), or if all three pairs are in love (A loves B, B loves C, C loves A). You are given a list of *m* known relationships in the anime. You know for sure that certain pairs love each other, and certain pairs hate each other. You're wondering how many ways you can fill in the remaining relationships so you are happy with every triangle. Two ways are considered different if two characters are in love in one way but hate each other in the other. Print this count modulo 1<=000<=000<=007.
The first line of input will contain two integers *n*,<=*m* (3<=≀<=*n*<=≀<=100<=000, 0<=≀<=*m*<=≀<=100<=000). The next *m* lines will contain the description of the known relationships. The *i*-th line will contain three integers *a**i*,<=*b**i*,<=*c**i*. If *c**i* is 1, then *a**i* and *b**i* are in love, otherwise, they hate each other (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*, ). Each pair of people will be described no more than once.
Print a single integer equal to the number of ways to fill in the remaining pairs so that you are happy with every triangle modulo 1<=000<=000<=007.
[ "3 0\n", "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 0\n", "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 1\n" ]
[ "4\n", "1\n", "0\n" ]
In the first sample, the four ways are to: - Make everyone love each other - Make 1 and 2 love each other, and 3 hate 1 and 2 (symmetrically, we get 3 ways from this). In the second sample, the only possible solution is to make 1 and 3 love each other and 2 and 4 hate each other.
[ { "input": "3 0", "output": "4" }, { "input": "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 0", "output": "1" }, { "input": "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 1", "output": "0" }, { "input": "100000 0", "output": "303861760" }, { "input": "100 3\n1 2 0\n2 3 0\n3 1 0", "output": ...
1,044
23,142,400
-1
93,336
763
Timofey and our friends animals
[ "data structures", "divide and conquer", "dsu" ]
null
null
After his birthday party, Timofey went to his favorite tree alley in a park. He wants to feed there his favorite birdsΒ β€” crows. It's widely known that each tree is occupied by a single crow family. The trees in the alley form a row and are numbered from 1 to *n*. Some families are friends to each other. For some reasons, two families can be friends only if they live not too far from each other, more precisely, there is no more than *k*<=-<=1 trees between any pair of friend families. Formally, the family on the *u*-th tree and the family on the *v*-th tree can be friends only if |*u*<=-<=*v*|<=≀<=*k* holds. One of the friendship features is that if some family learns that Timofey is feeding crows somewhere, it notifies about this all friend families. Thus, after Timofey starts to feed crows under some tree, all the families that are friends to the family living on this tree, as well as their friends and so on, fly to the feeding place. Of course, the family living on the tree also comes to the feeding place. Today Timofey came to the alley and noticed that all the families that live on trees with numbers strictly less than *l* or strictly greater than *r* have flown away. Thus, it is not possible to pass the information about feeding through them. Moreover, there is no need to feed them. Help Timofey to learn what is the minimum number of trees under which he has to feed crows so that all the families that have remained will get the information about feeding. You are given several situations, described by integers *l* and *r*, you need to calculate the answer for all of them.
The first line contains integers *n* and *k* (1<=≀<=*n*<=≀<=105, 1<=≀<=*k*<=≀<=5), where *n* is the number of trees, and *k* is the maximum possible distance between friend families. The next line contains single integer *m* (0<=≀<=*m*<=≀<=*n*Β·*k*)Β β€” the number of pair of friend families. Each of the next *m* lines contains two integers *u* and *v* (1<=≀<=*u*,<=*v*<=≀<=105), that means that the families on trees *u* and *v* are friends. It is guaranteed that *u*<=β‰ <=*v* and |*u*<=-<=*v*|<=≀<=*k*. All the given pairs are distinct. The next line contains single integer *q* (1<=≀<=*q*<=≀<=105)Β β€” the number of situations you need to calculate the answer in. Each of the next *q* lines contains two integers *l* and *r* (1<=≀<=*l*<=≀<=*r*<=≀<=105), that means that in this situation families that have flown away lived on such trees *x*, so that either *x*<=&lt;<=*l* or *x*<=&gt;<=*r*.
Print *q* lines. Line *i* should contain single integerΒ β€” the answer in the *i*-th situation.
[ "5 3\n3\n1 3\n2 3\n4 5\n5\n1 1\n1 2\n2 3\n1 3\n1 5\n" ]
[ "1\n2\n1\n1\n2\n" ]
In the first example the following family pairs are friends: (1, 3), (2, 3) and (4, 5). - In the first situation only the first family has remained, so the answer is 1. - In the second situation the first two families have remained, and they aren't friends, so the answer is 2. - In the third situation the families 2 and 3 are friends, so it is enough to feed any of them, the answer is 1. - In the fourth situation we can feed the first family, then the third family will get the information from the first family, and the second family will get the information from the third. The answer is 1. - In the fifth situation we can feed the first and the fifth families, so the answer is 2.
[]
46
4,608,000
0
93,852
8
Beads
[ "dp", "graphs" ]
E. Beads
5
64
One Martian boy called Zorg wants to present a string of beads to his friend from the Earth β€” Masha. He knows that Masha likes two colours: blue and red, β€” and right in the shop where he has come, there is a variety of adornments with beads of these two colours. All the strings of beads have a small fastener, and if one unfastens it, one might notice that all the strings of beads in the shop are of the same length. Because of the peculiarities of the Martian eyesight, if Zorg sees one blue-and-red string of beads first, and then the other with red beads instead of blue ones, and blue β€” instead of red, he regards these two strings of beads as identical. In other words, Zorg regards as identical not only those strings of beads that can be derived from each other by the string turnover, but as well those that can be derived from each other by a mutual replacement of colours and/or by the string turnover. It is known that all Martians are very orderly, and if a Martian sees some amount of objects, he tries to put them in good order. Zorg thinks that a red bead is smaller than a blue one. Let's put 0 for a red bead, and 1 β€” for a blue one. From two strings the Martian puts earlier the string with a red bead in the *i*-th position, providing that the second string has a blue bead in the *i*-th position, and the first two beads *i*<=-<=1 are identical. At first Zorg unfastens all the strings of beads, and puts them into small heaps so, that in each heap strings are identical, in his opinion. Then he sorts out the heaps and chooses the minimum string in each heap, in his opinion. He gives the unnecassary strings back to the shop assistant and says he doesn't need them any more. Then Zorg sorts out the remaining strings of beads and buys the string with index *k*. All these manupulations will take Zorg a lot of time, that's why he asks you to help and find the string of beads for Masha.
The input file contains two integers *n* and *k* (2<=≀<=*n*<=≀<=50;1<=≀<=*k*<=≀<=1016) β€”the length of a string of beads, and the index of the string, chosen by Zorg.
Output the *k*-th string of beads, putting 0 for a red bead, and 1 β€” for a blue one. If it s impossible to find the required string, output the only number -1.
[ "4 4\n" ]
[ "0101\n" ]
Let's consider the example of strings of length 4 β€” 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110. Zorg will divide them into heaps: {0001, 0111, 1000, 1110}, {0010, 0100, 1011, 1101}, {0011, 1100}, {0101, 1010}, {0110, 1001}. Then he will choose the minimum strings of beads in each heap: 0001, 0010, 0011, 0101, 0110. The forth string β€” 0101.
[ { "input": "4 4", "output": "0101" }, { "input": "2 1", "output": "01" }, { "input": "2 2", "output": "-1" }, { "input": "3 1", "output": "001" }, { "input": "3 2", "output": "010" }, { "input": "3 3", "output": "-1" }, { "input": "4 1", ...
92
0
0
93,916
41
3-cycles
[ "constructive algorithms", "graphs", "greedy" ]
E. 3-cycles
2
256
During a recent research Berland scientists found out that there were *n* cities in Ancient Berland, joined by two-way paths. Any two cities are joined by no more than one path. No path joins a city with itself. According to a well-known tradition, the road network was built so that it would be impossible to choose three cities from each of which one can get to any other one directly. That is, there was no cycle exactly as long as 3. Unfortunately, the road map has not been preserved till nowadays. Now the scientists are interested how much developed a country Ancient Berland was. Help them - find, what maximal number of roads could be in the country. You also have to restore any of the possible road maps.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of cities in Berland.
On the first line must be printed number *m* β€” the maximal number of roads in Berland. Then print *m* lines containing two numbers each β€” the numbers of cities that the given road joins. The cities are numbered with integers from 1 to *n*. If there are several variants of solving the problem, print any of them.
[ "3\n", "4\n" ]
[ "2\n1 2\n2 3\n", "4\n1 2\n2 3\n3 4\n4 1\n" ]
none
[ { "input": "3", "output": "2\n1 2\n1 3" }, { "input": "4", "output": "4\n1 3\n1 4\n2 3\n2 4" }, { "input": "5", "output": "6\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5" }, { "input": "6", "output": "9\n1 4\n1 5\n1 6\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6" }, { "input": "7", "output...
280
2,150,400
3.925995
93,953
786
Rap God
[ "data structures", "dfs and similar", "hashing", "strings", "trees" ]
null
null
Rick is in love with Unity. But Mr. Meeseeks also love Unity, so Rick and Mr. Meeseeks are "love rivals". Unity loves rap, so it decided that they have to compete in a rap game (battle) in order to choose the best. Rick is too nerds, so instead he's gonna make his verse with running his original algorithm on lyrics "Rap God" song. His algorithm is a little bit complicated. He's made a tree with *n* vertices numbered from 1 to *n* and there's a lowercase english letter written on each edge. He denotes *str*(*a*,<=*b*) to be the string made by writing characters on edges on the shortest path from *a* to *b* one by one (a string of length equal to distance of *a* to *b*). Note that *str*(*a*,<=*b*) is reverse of *str*(*b*,<=*a*) and *str*(*a*,<=*a*) is empty. In order to make the best verse he can, he needs to answer some queries, but he's not a computer scientist and is not able to answer those queries, so he asked you to help him. Each query is characterized by two vertices *x* and *y* (*x*<=β‰ <=*y*). Answer to this query is the number of vertices like *z* such that *z*<=β‰ <=*x*,<=*z*<=β‰ <=*y* and *str*(*x*,<=*y*) is lexicographically larger than *str*(*x*,<=*z*). String *x*<=<==<=<=*x*1*x*2...*x*|*x*| is lexicographically larger than string *y*<=<==<=<=*y*1*y*2...*y*|*y*|, if either |*x*|<=<=&gt;<=<=|*y*| and *x*1<=<==<=<=*y*1,<=<=*x*2<=<==<=<=*y*2,<=<=...,<=<=*x*|*y*|<=<==<=<=*y*|*y*|, or exists such number *r* (*r*<=<=&lt;<=<=|*x*|,<=<=*r*<=<=&lt;<=<=|*y*|), that *x*1<=<==<=<=*y*1,<=<=*x*2<=<==<=<=*y*2,<=<=...,<=<=*x**r*<=<==<=<=*y**r* and *x**r*<=<=+<=<=1<=<=&gt;<=<=*y**r*<=<=+<=<=1. Characters are compared like their ASCII codes (or alphabetic order). Help Rick get the girl (or whatever gender Unity has).
The first line of input contain two integers *n* and *q* (2<=≀<=*n*<=≀<=20000, 1<=≀<=*q*<=≀<=20000) β€” number of vertices in tree and number of queries respectively. The next *n*<=-<=1 lines contain the edges. Each line contains two integers *v* and *u* (endpoints of the edge) followed by an English lowercase letter *c* (1<=≀<=*v*,<=*u*<=≀<=*n*,<=*v*<=β‰ <=*u*). The next *q* line contain the queries. Each line contains two integers *x* and *y* (1<=≀<=*x*,<=*y*<=≀<=*n*,<=*x*<=β‰ <=*y*).
Print the answer for each query in one line.
[ "4 3\n4 1 t\n3 2 p\n1 2 s\n3 2\n1 3\n2 1\n", "8 4\n4 6 p\n3 7 o\n7 8 p\n4 5 d\n1 3 o\n4 3 p\n3 2 e\n8 6\n3 7\n8 1\n4 3\n" ]
[ "0\n1\n1\n", "6\n1\n3\n1\n" ]
Here's the tree of first sample testcase: Here's the tree of second sample testcase: In this test: - *str*(8, 1) = poo - *str*(8, 2) = poe - *str*(8, 3) = po - *str*(8, 4) = pop - *str*(8, 5) = popd - *str*(8, 6) = popp - *str*(8, 7) = p So, for the first query, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9ac3bcb50c63fc450b7ff3ad3f82b1aa9b0ffdb8.png" style="max-width: 100.0%;max-height: 100.0%;"/> and for the third query <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/01f13fd2e1e98dac35dfd28b98a1cdb970305a72.png" style="max-width: 100.0%;max-height: 100.0%;"/> is the answer.
[]
15
0
-1
93,995
535
Tavas and Pashmaks
[ "geometry", "math" ]
null
null
Tavas is a cheerleader in the new sports competition named "Pashmaks". This competition consists of two part: swimming and then running. People will immediately start running *R* meters after they finished swimming exactly *S* meters. A winner is a such person that nobody else finishes running before him/her (there may be more than one winner). Before the match starts, Tavas knows that there are *n* competitors registered for the match. Also, he knows that *i*-th person's swimming speed is *s**i* meters per second and his/her running speed is *r**i* meters per second. Unfortunately, he doesn't know the values of *R* and *S*, but he knows that they are real numbers greater than 0. As a cheerleader, Tavas wants to know who to cheer up. So, he wants to know all people that might win. We consider a competitor might win if and only if there are some values of *R* and *S* such that with these values, (s)he will be a winner. Tavas isn't really familiar with programming, so he asked you to help him.
The first line of input contains a single integer *n* (1<=≀<=*n*<=≀<=2<=Γ—<=105). The next *n* lines contain the details of competitors. *i*-th line contains two integers *s**i* and *r**i* (1<=≀<=*s**i*,<=*r**i*<=≀<=104).
In the first and the only line of output, print a sequence of numbers of possible winners in increasing order.
[ "3\n1 3\n2 2\n3 1\n", "3\n1 2\n1 1\n2 1\n" ]
[ "1 2 3 \n", "1 3 \n" ]
none
[]
1,000
29,286,400
0
94,019
887
Row of Models
[ "greedy", "sortings" ]
null
null
During the final part of fashion show all models come to the stage and stay in one row and fashion designer stays to right to model on the right. During the rehearsal, Izabella noticed, that row isn't nice, but she can't figure out how to fix it. Like many other creative people, Izabella has a specific sense of beauty. Evaluating beauty of row of models Izabella looks at heights of models. She thinks that row is nice if for each model distance to nearest model with less height (model or fashion designer) to the right of her doesn't exceed *k* (distance between adjacent people equals 1, the distance between people with exactly one man between them equals 2, etc). She wants to make row nice, but fashion designer has his own sense of beauty, so she can at most one time select two models from the row and swap their positions if the left model from this pair is higher than the right model from this pair. Fashion designer (man to the right of rightmost model) has less height than all models and can't be selected for exchange. You should tell if it's possible to make at most one exchange in such a way that row becomes nice for Izabella.
In first line there are two integers *n* and *k* (1<=≀<=*n*<=≀<=5Β·105, 1<=≀<=*k*<=≀<=*n*)Β β€” number of models and required distance. Second line contains *n* space-separated integers *a**i* (1<=≀<=*a**i*<=≀<=109)Β β€” height of each model. Pay attention that height of fashion designer is not given and can be less than 1.
Print Β«YESΒ» (without quotes) if it's possible to make row nice using at most one exchange, and Β«NOΒ» (without quotes) otherwise.
[ "5 4\n2 3 5 2 5\n", "5 2\n3 6 2 2 1\n", "5 2\n5 3 6 5 2\n" ]
[ "NO", "YES", "YES" ]
none
[]
30
0
0
94,216
223
Spider
[ "geometry", "graphs" ]
null
null
A plane contains a not necessarily convex polygon without self-intersections, consisting of *n* vertexes, numbered from 1 to *n*. There is a spider sitting on the border of the polygon, the spider can move like that: 1. Transfer. The spider moves from the point *p*1 with coordinates (*x*1,<=*y*1), lying on the polygon border, to the point *p*2 with coordinates (*x*2,<=*y*2), also lying on the border. The spider can't go beyond the polygon border as it transfers, that is, the spider's path from point *p*1 to point *p*2 goes along the polygon border. It's up to the spider to choose the direction of walking round the polygon border (clockwise or counterclockwise). 1. Descend. The spider moves from point *p*1 with coordinates (*x*1,<=*y*1) to point *p*2 with coordinates (*x*2,<=*y*2), at that points *p*1 and *p*2 must lie on one vertical straight line (*x*1<==<=*x*2), point *p*1 must be not lower than point *p*2 (*y*1<=β‰₯<=*y*2) and segment *p*1*p*2 mustn't have points, located strictly outside the polygon (specifically, the segment can have common points with the border). Initially the spider is located at the polygon vertex with number *s*. Find the length of the shortest path to the vertex number *t*, consisting of transfers and descends. The distance is determined by the usual Euclidean metric .
The first line contains integer *n* (3<=≀<=*n*<=≀<=105) β€” the number of vertexes of the given polygon. Next *n* lines contain two space-separated integers each β€” the coordinates of the polygon vertexes. The vertexes are listed in the counter-clockwise order. The coordinates of the polygon vertexes do not exceed 104 in their absolute value. The last line contains two space-separated integers *s* and *t* (1<=≀<=*s*,<=*t*<=≀<=*n*) β€” the start and the end vertexes of the sought shortest way. Consider the polygon vertexes numbered in the order they are given in the input, that is, the coordinates of the first vertex are located on the second line of the input and the coordinates of the *n*-th vertex are on the (*n*<=+<=1)-th line. It is guaranteed that the given polygon is simple, that is, it contains no self-intersections or self-tangencies.
In the output print a single real number β€” the length of the shortest way from vertex *s* to vertex *t*. The answer is considered correct, if its absolute or relative error does not exceed 10<=-<=6.
[ "4\n0 0\n1 0\n1 1\n0 1\n1 4\n", "4\n0 0\n1 1\n0 2\n-1 1\n3 3\n", "5\n0 0\n5 0\n1 4\n0 2\n2 1\n3 1\n" ]
[ "1.000000000000000000e+000\n", "0.000000000000000000e+000\n", "5.650281539872884700e+000\n" ]
In the first sample the spider transfers along the side that connects vertexes 1 and 4. In the second sample the spider doesn't have to transfer anywhere, so the distance equals zero. In the third sample the best strategy for the spider is to transfer from vertex 3 to point (2,3), descend to point (2,1), and then transfer to vertex 1.
[]
46
0
0
94,336
641
Little Artem and Random Variable
[ "dp", "implementation", "math", "probabilities" ]
null
null
Little Artyom decided to study probability theory. He found a book with a lot of nice exercises and now wants you to help him with one of them. Consider two dices. When thrown each dice shows some integer from 1 to *n* inclusive. For each dice the probability of each outcome is given (of course, their sum is 1), and different dices may have different probability distributions. We throw both dices simultaneously and then calculate values *max*(*a*,<=*b*) and *min*(*a*,<=*b*), where *a* is equal to the outcome of the first dice, while *b* is equal to the outcome of the second dice. You don't know the probability distributions for particular values on each dice, but you know the probability distributions for *max*(*a*,<=*b*) and *min*(*a*,<=*b*). That is, for each *x* from 1 to *n* you know the probability that *max*(*a*,<=*b*) would be equal to *x* and the probability that *min*(*a*,<=*b*) would be equal to *x*. Find any valid probability distribution for values on the dices. It's guaranteed that the input data is consistent, that is, at least one solution exists.
First line contains the integer *n* (1<=≀<=*n*<=≀<=100<=000)Β β€” the number of different values for both dices. Second line contains an array consisting of *n* real values with up to 8 digits after the decimal point Β β€” probability distribution for *max*(*a*,<=*b*), the *i*-th of these values equals to the probability that *max*(*a*,<=*b*)<==<=*i*. It's guaranteed that the sum of these values for one dice is 1. The third line contains the description of the distribution *min*(*a*,<=*b*) in the same format.
Output two descriptions of the probability distribution for *a* on the first line and for *b* on the second line. The answer will be considered correct if each value of max(*a*,<=*b*) and min(*a*,<=*b*) probability distribution values does not differ by more than 10<=-<=6 from ones given in input. Also, probabilities should be non-negative and their sums should differ from 1 by no more than 10<=-<=6.
[ "2\n0.25 0.75\n0.75 0.25\n", "3\n0.125 0.25 0.625\n0.625 0.25 0.125\n" ]
[ "0.5 0.5 \n0.5 0.5 \n", "0.25 0.25 0.5 \n0.5 0.25 0.25 \n" ]
none
[ { "input": "2\n0.25 0.75\n0.75 0.25", "output": "0.5 0.5 \n0.5 0.5 " }, { "input": "3\n0.125 0.25 0.625\n0.625 0.25 0.125", "output": "0.25 0.25 0.5 \n0.5 0.25 0.25 " }, { "input": "10\n0.01 0.01 0.01 0.01 0.01 0.1 0.2 0.2 0.4 0.05\n1.0 0 0 0 0 0 0 0 0 0", "output": "0.01000000000000...
482
21,606,400
0
94,603
311
Biologist
[ "flows" ]
null
null
SmallR is a biologist. Her latest research finding is how to change the sex of dogs. In other words, she can change female dogs into male dogs and vice versa. She is going to demonstrate this technique. Now SmallR has *n* dogs, the costs of each dog's change may be different. The dogs are numbered from 1 to *n*. The cost of change for dog *i* is *v**i* RMB. By the way, this technique needs a kind of medicine which can be valid for only one day. So the experiment should be taken in one day and each dog can be changed at most once. This experiment has aroused extensive attention from all sectors of society. There are *m* rich folks which are suspicious of this experiment. They all want to bet with SmallR forcibly. If SmallR succeeds, the *i*-th rich folk will pay SmallR *w**i* RMB. But it's strange that they have a special method to determine whether SmallR succeeds. For *i*-th rich folk, in advance, he will appoint certain *k**i* dogs and certain one gender. He will think SmallR succeeds if and only if on some day the *k**i* appointed dogs are all of the appointed gender. Otherwise, he will think SmallR fails. If SmallR can't satisfy some folk that isn't her friend, she need not pay him, but if someone she can't satisfy is her good friend, she must pay *g* RMB to him as apologies for her fail. Then, SmallR hope to acquire money as much as possible by this experiment. Please figure out the maximum money SmallR can acquire. By the way, it is possible that she can't obtain any money, even will lose money. Then, please give out the minimum money she should lose.
The first line contains three integers *n*, *m*, *g* (1<=≀<=*n*<=≀<=104,<=0<=≀<=*m*<=≀<=2000,<=0<=≀<=*g*<=≀<=104). The second line contains *n* integers, each is 0 or 1, the sex of each dog, 0 represent the female and 1 represent the male. The third line contains *n* integers *v*1,<=*v*2,<=...,<=*v**n* (0<=≀<=*v**i*<=≀<=104). Each of the next *m* lines describes a rich folk. On the *i*-th line the first number is the appointed sex of *i*-th folk (0 or 1), the next two integers are *w**i* and *k**i* (0<=≀<=*w**i*<=≀<=104,<=1<=≀<=*k**i*<=≀<=10), next *k**i* distinct integers are the indexes of appointed dogs (each index is between 1 and *n*). The last number of this line represents whether *i*-th folk is SmallR's good friend (0 β€” no or 1 β€” yes).
Print a single integer, the maximum money SmallR can gain. Note that the integer is negative if SmallR will lose money.
[ "5 5 9\n0 1 1 1 0\n1 8 6 2 3\n0 7 3 3 2 1 1\n1 8 1 5 1\n1 0 3 2 1 4 1\n0 8 3 4 2 1 0\n1 7 2 4 1 1\n", "5 5 8\n1 0 1 1 1\n6 5 4 2 8\n0 6 3 2 3 4 0\n0 8 3 3 2 4 0\n0 0 3 3 4 1 1\n0 10 3 4 3 1 1\n0 4 3 3 4 1 1\n" ]
[ "2\n", "16\n" ]
none
[ { "input": "5 5 9\n0 1 1 1 0\n1 8 6 2 3\n0 7 3 3 2 1 1\n1 8 1 5 1\n1 0 3 2 1 4 1\n0 8 3 4 2 1 0\n1 7 2 4 1 1", "output": "2" }, { "input": "5 5 8\n1 0 1 1 1\n6 5 4 2 8\n0 6 3 2 3 4 0\n0 8 3 3 2 4 0\n0 0 3 3 4 1 1\n0 10 3 4 3 1 1\n0 4 3 3 4 1 1", "output": "16" }, { "input": "5 5 8\n0 1 0...
1,500
15,257,600
0
94,701
26
Multithreading
[ "constructive algorithms" ]
E. Multithreading
2
256
You are given the following concurrent program. There are *N* processes and the *i*-th process has the following pseudocode: Here *y* is a shared variable. Everything else is local for the process. All actions on a given row are atomic, i.e. when the process starts executing a row it is never interrupted. Beyond that all interleavings are possible, i.e. every process that has yet work to do can be granted the rights to execute its next row. In the beginning *y*<==<=0. You will be given an integer *W* and *n**i*, for *i*<==<=1,<=... ,<=*N*. Determine if it is possible that after all processes terminate, *y*<==<=*W*, and if it is possible output an arbitrary schedule that will produce this final value.
In the first line of the input you will be given two space separated integers *N* (1<=≀<=*N*<=≀<=100) and *W* (<=-<=109<=≀<=*W*<=≀<=109). In the second line there are *N* space separated integers *n**i* (1<=≀<=*n**i*<=≀<=1000).
On the first line of the output write Yes if it is possible that at the end *y*<==<=*W*, or No otherwise. If the answer is No then there is no second line, but if the answer is Yes, then on the second line output a space separated list of integers representing some schedule that leads to the desired result. For more information see note.
[ "1 10\n11\n", "2 3\n4 4\n", "3 6\n1 2 3\n" ]
[ "No\n", "Yes\n1 1 2 1 2 2 2 2 2 1 2 1 1 1 1 2\n", "Yes\n1 1 2 2 2 2 3 3 3 3 3 3\n" ]
For simplicity, assume that there is no repeat statement in the code of the processes, but the code from the loop is written the correct amount of times. The processes are numbered starting from 1. The list of integers represent which process works on its next instruction at a given step. For example, consider the schedule 1 2 2 1 3. First process 1 executes its first instruction, then process 2 executes its first two instructions, after that process 1 executes its second instruction, and finally process 3 executes its first instruction. The list must consists of exactly 2Β·Ξ£ <sub class="lower-index">*i* = 1...*N*</sub> *n*<sub class="lower-index">*i*</sub> numbers.
[ { "input": "1 10\n11", "output": "No" }, { "input": "3 -1\n1 2 3", "output": "No" }, { "input": "3 6\n1 2 3", "output": "Yes\n1 1 2 2 2 2 3 3 3 3 3 3" }, { "input": "3 5\n3 1 4", "output": "Yes\n2 1 1 1 1 1 1 2 3 3 3 3 3 3 3 3" }, { "input": "2 3\n4 4", "outpu...
0
0
-1
94,785
50
Happy Farm 5
[ "geometry" ]
C. Happy Farm 5
2
256
The Happy Farm 5 creators decided to invent the mechanism of cow grazing. The cows in the game are very slow and they move very slowly, it can even be considered that they stand still. However, carnivores should always be chased off them. For that a young player Vasya decided to make the shepherd run round the cows along one and the same closed path. It is very important that the cows stayed strictly inside the area limited by the path, as otherwise some cows will sooner or later be eaten. To be absolutely sure in the cows' safety, Vasya wants the path completion time to be minimum. The new game is launched for different devices, including mobile phones. That's why the developers decided to quit using the arithmetics with the floating decimal point and use only the arithmetics of integers. The cows and the shepherd in the game are represented as points on the plane with integer coordinates. The playing time is modeled by the turns. During every turn the shepherd can either stay where he stands or step in one of eight directions: horizontally, vertically, or diagonally. As the coordinates should always remain integer, then the length of a horizontal and vertical step is equal to 1, and the length of a diagonal step is equal to . The cows do not move. You have to minimize the number of moves the shepherd needs to run round the whole herd.
The first line contains an integer *N* which represents the number of cows in the herd (1<=≀<=*N*<=≀<=105). Each of the next *N* lines contains two integers *X**i* and *Y**i* which represent the coordinates of one cow of (|*X**i*|,<=|*Y**i*|<=≀<=106). Several cows can stand on one point.
Print the single number β€” the minimum number of moves in the sought path.
[ "4\n1 1\n5 1\n5 3\n1 3\n" ]
[ "16\n" ]
Picture for the example test: The coordinate grid is painted grey, the coordinates axes are painted black, the cows are painted red and the sought route is painted green.
[ { "input": "4\n1 1\n5 1\n5 3\n1 3", "output": "16" }, { "input": "3\n0 0\n5 0\n0 5", "output": "19" }, { "input": "5\n0 0\n7 7\n7 5\n5 7\n1 1", "output": "22" }, { "input": "5\n1 0\n-1 0\n1 0\n-1 0\n0 0", "output": "8" }, { "input": "9\n1 0\n-1 0\n1 0\n-1 0\n0 0\n...
2,000
43,315,200
0
94,852
469
Chat Online
[ "implementation" ]
null
null
Little X and Little Z are good friends. They always chat online. But both of them have schedules. Little Z has fixed schedule. He always online at any moment of time between *a*1 and *b*1, between *a*2 and *b*2, ..., between *a**p* and *b**p* (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between *c*1 and *d*1, between *c*2 and *d*2, ..., between *c**q* and *d**q* (all borders inclusive). But if he gets up at time *t*, these segments will be shifted by *t*. They become [*c**i*<=+<=*t*,<=*d**i*<=+<=*t*] (for all *i*). If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between *l* and *r* (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [*l*,<=*r*] suit for that?
The first line contains four space-separated integers *p*,<=*q*,<=*l*,<=*r* (1<=≀<=<=*p*,<=*q*<=≀<=50;Β 0<=≀<=*l*<=≀<=*r*<=≀<=1000). Each of the next *p* lines contains two space-separated integers *a**i*,<=*b**i* (0<=≀<=*a**i*<=&lt;<=*b**i*<=≀<=1000). Each of the next *q* lines contains two space-separated integers *c**j*,<=*d**j* (0<=≀<=*c**j*<=&lt;<=*d**j*<=≀<=1000). It's guaranteed that *b**i*<=&lt;<=*a**i*<=+<=1 and *d**j*<=&lt;<=*c**j*<=+<=1 for all valid *i* and *j*.
Output a single integer β€” the number of moments of time from the segment [*l*,<=*r*] which suit for online conversation.
[ "1 1 0 4\n2 3\n0 1\n", "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17\n" ]
[ "3\n", "20\n" ]
none
[ { "input": "1 1 0 4\n2 3\n0 1", "output": "3" }, { "input": "2 3 0 20\n15 17\n23 26\n1 4\n7 11\n15 17", "output": "20" }, { "input": "5 2 27 452\n148 154\n421 427\n462 470\n777 786\n969 978\n245 247\n313 322", "output": "54" }, { "input": "3 6 25 785\n273 275\n391 397\n775 78...
77
2,969,600
3
94,879
237
T-decomposition
[ "dfs and similar", "graphs", "greedy", "trees" ]
null
null
You've got a undirected tree *s*, consisting of *n* nodes. Your task is to build an optimal T-decomposition for it. Let's define a T-decomposition as follows. Let's denote the set of all nodes *s* as *v*. Let's consider an undirected tree *t*, whose nodes are some non-empty subsets of *v*, we'll call them *x**i* . The tree *t* is a T-decomposition of *s*, if the following conditions holds: 1. the union of all *x**i* equals *v*; 1. for any edge (*a*,<=*b*) of tree *s* exists the tree node *t*, containing both *a* and *b*; 1. if the nodes of the tree *t* *x**i* and *x**j* contain the node *a* of the tree *s*, then all nodes of the tree *t*, lying on the path from *x**i* to *x**j* also contain node *a*. So this condition is equivalent to the following: all nodes of the tree *t*, that contain node *a* of the tree *s*, form a connected subtree of tree *t*. There are obviously many distinct trees *t*, that are T-decompositions of the tree *s*. For example, a T-decomposition is a tree that consists of a single node, equal to set *v*. Let's define the cardinality of node *x**i* as the number of nodes in tree *s*, containing in the node. Let's choose the node with the maximum cardinality in *t*. Let's assume that its cardinality equals *w*. Then the weight of T-decomposition *t* is value *w*. The optimal T-decomposition is the one with the minimum weight. Your task is to find the optimal T-decomposition of the given tree *s* that has the minimum number of nodes.
The first line contains a single integer *n* (2<=≀<=*n*<=≀<=105), that denotes the number of nodes in tree *s*. Each of the following *n*<=-<=1 lines contains two space-separated integers *a**i*,<=*b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*;Β *a**i*<=β‰ <=*b**i*), denoting that the nodes of tree *s* with indices *a**i* and *b**i* are connected by an edge. Consider the nodes of tree *s* indexed from 1 to *n*. It is guaranteed that *s* is a tree.
In the first line print a single integer *m* that denotes the number of nodes in the required T-decomposition. Then print *m* lines, containing descriptions of the T-decomposition nodes. In the *i*-th (1<=≀<=*i*<=≀<=*m*) of them print the description of node *x**i* of the T-decomposition. The description of each node *x**i* should start from an integer *k**i*, that represents the number of nodes of the initial tree *s*, that are contained in the node *x**i*. Then you should print *k**i* distinct space-separated integers β€” the numbers of nodes from *s*, contained in *x**i*, in arbitrary order. Then print *m*<=-<=1 lines, each consisting two integers *p**i*,<=*q**i* (1<=≀<=*p**i*,<=*q**i*<=≀<=*m*;Β *p**i*<=β‰ <=*q**i*). The pair of integers *p**i*,<=*q**i* means there is an edge between nodes *x**p**i* and *x**q**i* of T-decomposition. The printed T-decomposition should be the optimal T-decomposition for the given tree *s* and have the minimum possible number of nodes among all optimal T-decompositions. If there are multiple optimal T-decompositions with the minimum number of nodes, print any of them.
[ "2\n1 2\n", "3\n1 2\n2 3\n", "4\n2 1\n3 1\n4 1\n" ]
[ "1\n2 1 2\n", "2\n2 1 2\n2 2 3\n1 2\n", "3\n2 2 1\n2 3 1\n2 4 1\n1 2\n2 3\n" ]
none
[ { "input": "2\n1 2", "output": "1\n2 1 2" }, { "input": "3\n1 2\n2 3", "output": "2\n2 1 2\n2 2 3\n1 2" }, { "input": "4\n2 1\n3 1\n4 1", "output": "3\n2 2 1\n2 3 1\n2 4 1\n1 2\n2 3" }, { "input": "6\n2 5\n4 3\n4 2\n4 6\n3 1", "output": "5\n2 2 5\n2 4 3\n2 4 2\n2 4 6\n2 3...
92
0
0
95,045
40
Berland Square
[ "implementation", "math" ]
C. Berland Square
2
256
Last year the world's largest square was built in Berland. It is known that the square can be represented as an infinite plane with an introduced Cartesian system of coordinates. On that square two sets of concentric circles were painted. Let's call the set of concentric circles with radii 1,<=2,<=...,<=*K* and the center in the point (*z*,<=0) a (*K*,<=*z*)-set. Thus, on the square were painted a (*N*,<=*x*)-set and a (*M*,<=*y*)-set. You have to find out how many parts those sets divided the square into.
The first line contains integers *N*,<=*x*,<=*M*,<=*y*. (1<=≀<=*N*,<=*M*<=≀<=100000,<=<=-<=100000<=≀<=*x*,<=*y*<=≀<=100000,<=*x*<=β‰ <=*y*).
Print the sought number of parts.
[ "1 0 1 1\n", "1 0 1 2\n", "3 3 4 7\n" ]
[ "4\n", "3\n", "17\n" ]
Picture for the third sample:
[ { "input": "1 0 1 1", "output": "4" }, { "input": "1 0 1 2", "output": "3" }, { "input": "3 3 4 7", "output": "17" }, { "input": "2 2 2 3", "output": "8" }, { "input": "1 100000 1 99999", "output": "4" }, { "input": "4 7 3 3", "output": "17" }, ...
186
0
0
95,291
722
Cyclic Cipher
[ "chinese remainder theorem", "data structures", "implementation", "number theory", "two pointers" ]
null
null
You are given *n* sequences. Each sequence consists of positive integers, not exceeding *m*. All integers in one sequence are distinct, but the same integer may appear in multiple sequences. The length of the *i*-th sequence is *k**i*. Each second integers in each of the sequences are shifted by one to the left, i.e. integers at positions *i*<=&gt;<=1 go to positions *i*<=-<=1, while the first integers becomes the last. Each second we take the first integer of each sequence and write it down to a new array. Then, for each value *x* from 1 to *m* we compute the longest segment of the array consisting of element *x* only. The above operation is performed for 10100 seconds. For each integer from 1 to *m* find out the longest segment found at this time.
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100<=000)Β β€” the number of sequences and the maximum integer that can appear in the sequences. Then follow *n* lines providing the sequences. Each of them starts with an integer *k**i* (1<=≀<=*k**i*<=≀<=40)Β β€” the number of integers in the sequence, proceeded by *k**i* positive integersΒ β€” elements of the sequence. It's guaranteed that all integers in each sequence are pairwise distinct and do not exceed *m*. The total length of all sequences doesn't exceed 200<=000.
Print *m* integers, the *i*-th of them should be equal to the length of the longest segment of the array with all its values equal to *i* during the first 10100 seconds.
[ "3 4\n3 3 4 1\n4 1 3 4 2\n3 3 1 4\n", "5 5\n2 3 1\n4 5 1 3 2\n4 2 1 3 5\n1 3\n2 5 3\n", "4 6\n3 4 5 3\n2 6 3\n2 3 6\n3 3 6 5\n" ]
[ "2\n1\n3\n2\n", "3\n1\n4\n0\n1\n", "0\n0\n2\n1\n1\n2\n" ]
none
[ { "input": "3 4\n3 3 4 1\n4 1 3 4 2\n3 3 1 4", "output": "2\n1\n3\n2" }, { "input": "5 5\n2 3 1\n4 5 1 3 2\n4 2 1 3 5\n1 3\n2 5 3", "output": "3\n1\n4\n0\n1" }, { "input": "4 6\n3 4 5 3\n2 6 3\n2 3 6\n3 3 6 5", "output": "0\n0\n2\n1\n1\n2" }, { "input": "10 5\n2 2 4\n2 4 5\n2...
2,000
11,264,000
0
95,368
59
Team Arrangement
[ "constructive algorithms", "greedy", "implementation" ]
D. Team Arrangement
3
256
Recently personal training sessions have finished in the Berland State University Olympiad Programmer Training Centre. By the results of these training sessions teams are composed for the oncoming team contest season. Each team consists of three people. All the students of the Centre possess numbers from 1 to 3*n*, and all the teams possess numbers from 1 to *n*. The splitting of students into teams is performed in the following manner: while there are people who are not part of a team, a person with the best total score is chosen among them (the captain of a new team), this person chooses for himself two teammates from those who is left according to his list of priorities. The list of every person's priorities is represented as a permutation from the rest of 3*n*<=-<=1 students who attend the centre, besides himself. You are given the results of personal training sessions which are a permutation of numbers from 1 to 3*n*, where the *i*-th number is the number of student who has won the *i*-th place. No two students share a place. You are also given the arrangement of the already formed teams in the order in which they has been created. Your task is to determine the list of priorities for the student number *k*. If there are several priority lists, choose the lexicographically minimal one.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105) which is the number of resulting teams. The second line contains 3*n* space-separated integers from 1 to 3*n* which are the results of personal training sessions. It is guaranteed that every student appears in the results exactly once. Then follow *n* lines each containing three integers from 1 to 3*n* β€” each line describes the members of a given team. The members of one team can be listed in any order, but the teams themselves are listed in the order in which they were created. It is guaranteed that the arrangement is correct, that is that every student is a member of exactly one team and those teams could really be created from the given results using the method described above. The last line contains number *k* (1<=≀<=*k*<=≀<=3*n*) which is the number of a student for who the list of priorities should be found.
Print 3*n*<=-<=1 numbers β€” the lexicographically smallest list of priorities for the student number *k*. The lexicographical comparison is performed by the standard &lt; operator in modern programming languages. The list *a* is lexicographically less that the list *b* if exists such an *i* (1<=≀<=*i*<=≀<=3*n*), that *a**i*<=&lt;<=*b**i*, and for any *j* (1<=≀<=*j*<=&lt;<=*i*) *a**j*<==<=*b**j*. Note, that the list 1 9 10 is lexicographically less than the list 1 10 9. That is, the comparison of lists is different from the comparison of lines.
[ "3\n5 4 1 2 6 3 7 8 9\n5 6 2\n9 3 4\n1 7 8\n4\n", "3\n5 4 1 2 6 3 7 8 9\n5 6 2\n9 3 4\n1 7 8\n8\n", "2\n4 1 3 2 5 6\n4 6 5\n1 2 3\n4\n" ]
[ "2 3 5 6 9 1 7 8 ", "1 2 3 4 5 6 7 9 ", "5 6 1 2 3 " ]
none
[]
124
0
0
95,672
154
Double Profiles
[ "graphs", "hashing", "sortings" ]
null
null
You have been offered a job in a company developing a large social network. Your first task is connected with searching profiles that most probably belong to the same user. The social network contains *n* registered profiles, numbered from 1 to *n*. Some pairs there are friends (the "friendship" relationship is mutual, that is, if *i* is friends with *j*, then *j* is also friends with *i*). Let's say that profiles *i* and *j* (*i*<=β‰ <=*j*) are doubles, if for any profile *k* (*k*<=β‰ <=*i*, *k*<=β‰ <=*j*) one of the two statements is true: either *k* is friends with *i* and *j*, or *k* isn't friends with either of them. Also, *i* and *j* can be friends or not be friends. Your task is to count the number of different unordered pairs (*i*,<=*j*), such that the profiles *i* and *j* are doubles. Note that the pairs are unordered, that is, pairs (*a*,<=*b*) and (*b*,<=*a*) are considered identical.
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*<=≀<=106, 0<=≀<=*m*<=≀<=106), β€” the number of profiles and the number of pairs of friends, correspondingly. Next *m* lines contains descriptions of pairs of friends in the format "*v* *u*", where *v* and *u* (1<=≀<=*v*,<=*u*<=≀<=*n*,<=*v*<=β‰ <=*u*) are numbers of profiles that are friends with each other. It is guaranteed that each unordered pair of friends occurs no more than once and no profile is friends with itself.
Print the single integer β€” the number of unordered pairs of profiles that are doubles. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the %I64d specificator.
[ "3 3\n1 2\n2 3\n1 3\n", "3 0\n", "4 1\n1 3\n" ]
[ "3\n", "3\n", "2\n" ]
In the first and second sample any two profiles are doubles. In the third sample the doubles are pairs of profiles (1, 3) and (2, 4).
[ { "input": "3 3\n1 2\n2 3\n1 3", "output": "3" }, { "input": "3 0", "output": "3" }, { "input": "4 1\n1 3", "output": "2" }, { "input": "1 0", "output": "0" }, { "input": "2 0", "output": "1" }, { "input": "2 1\n1 2", "output": "1" }, { "in...
124
0
-1
96,038
377
Developing Game
[ "data structures" ]
null
null
Pavel is going to make a game of his dream. However, he knows that he can't make it on his own so he founded a development company and hired *n* workers of staff. Now he wants to pick *n* workers from the staff who will be directly responsible for developing a game. Each worker has a certain skill level *v**i*. Besides, each worker doesn't want to work with the one whose skill is very different. In other words, the *i*-th worker won't work with those whose skill is less than *l**i*, and with those whose skill is more than *r**i*. Pavel understands that the game of his dream isn't too hard to develop, so the worker with any skill will be equally useful. That's why he wants to pick a team of the maximum possible size. Help him pick such team.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=105)Β β€” the number of workers Pavel hired. Each of the following *n* lines contains three space-separated integers *l**i*, *v**i*, *r**i* (1<=≀<=*l**i*<=≀<=*v**i*<=≀<=*r**i*<=≀<=3Β·105)Β β€” the minimum skill value of the workers that the *i*-th worker can work with, the *i*-th worker's skill and the maximum skill value of the workers that the *i*-th worker can work with.
In the first line print a single integer *m*Β β€” the number of workers Pavel must pick for developing the game. In the next line print *m* space-separated integersΒ β€” the numbers of the workers in any order. If there are multiple optimal solutions, print any of them.
[ "4\n2 8 9\n1 4 7\n3 6 8\n5 8 10\n", "6\n3 5 16\n1 6 11\n4 8 12\n7 9 16\n2 10 14\n8 13 15\n" ]
[ "3\n1 3 4\n", "4\n1 2 3 5\n" ]
none
[ { "input": "4\n2 8 9\n1 4 7\n3 6 8\n5 8 10", "output": "3\n1 3 4" }, { "input": "6\n3 5 16\n1 6 11\n4 8 12\n7 9 16\n2 10 14\n8 13 15", "output": "4\n1 2 3 5" }, { "input": "1\n299998 299999 300000", "output": "1\n1" }, { "input": "2\n1 3 6\n2 4 5", "output": "2\n1 2" },...
139
307,200
0
96,068
630
Pyramids
[ "geometry", "math" ]
null
null
IT City administration has no rest because of the fame of the Pyramids in Egypt. There is a project of construction of pyramid complex near the city in the place called Emerald Walley. The distinction of the complex is that its pyramids will be not only quadrangular as in Egypt but also triangular and pentagonal. Of course the amount of the city budget funds for the construction depends on the pyramids' volume. Your task is to calculate the volume of the pilot project consisting of three pyramids β€” one triangular, one quadrangular and one pentagonal. The first pyramid has equilateral triangle as its base, and all 6 edges of the pyramid have equal length. The second pyramid has a square as its base and all 8 edges of the pyramid have equal length. The third pyramid has a regular pentagon as its base and all 10 edges of the pyramid have equal length.
The only line of the input contains three integers *l*3,<=*l*4,<=*l*5 (1<=≀<=*l*3,<=*l*4,<=*l*5<=≀<=1000) β€” the edge lengths of triangular, quadrangular and pentagonal pyramids correspondingly.
Output one number β€” the total volume of the pyramids. Absolute or relative error should not be greater than 10<=-<=9.
[ "2 5 3\n" ]
[ "38.546168065709" ]
none
[ { "input": "2 5 3", "output": "38.546168065709" }, { "input": "3 4 5", "output": "55.954779230131" }, { "input": "1 1 1", "output": "0.655056222989" }, { "input": "1000 1000 1000", "output": "655056222.989098310000" }, { "input": "999 997 998", "output": "6507...
46
0
0
96,258
291
Network Mask
[ "*special", "bitmasks", "brute force", "implementation" ]
null
null
The problem uses a simplified TCP/IP address model, please make sure you've read the statement attentively. Polycarpus has found a job, he is a system administrator. One day he came across *n* IP addresses. Each IP address is a 32 bit number, represented as a group of four 8-bit numbers (without leading zeroes), separated by dots. For example, the record 0.255.1.123 shows a correct IP address and records 0.256.1.123 and 0.255.1.01 do not. In this problem an arbitrary group of four 8-bit numbers is a correct IP address. Having worked as an administrator for some time, Polycarpus learned that if you know the IP address, you can use the subnet mask to get the address of the network that has this IP addess. The subnet mask is an IP address that has the following property: if we write this IP address as a 32 bit string, that it is representable as "11...11000..000". In other words, the subnet mask first has one or more one bits, and then one or more zero bits (overall there are 32 bits). For example, the IP address 2.0.0.0 is not a correct subnet mask as its 32-bit record looks as 00000010000000000000000000000000. To get the network address of the IP address, you need to perform the operation of the bitwise "and" of the IP address and the subnet mask. For example, if the subnet mask is 255.192.0.0, and the IP address is 192.168.1.2, then the network address equals 192.128.0.0. In the bitwise "and" the result has a bit that equals 1 if and only if both operands have corresponding bits equal to one. Now Polycarpus wants to find all networks to which his IP addresses belong. Unfortunately, Polycarpus lost subnet mask. Fortunately, Polycarpus remembers that his IP addresses belonged to exactly *k* distinct networks. Help Polycarpus find the subnet mask, such that his IP addresses will belong to exactly *k* distinct networks. If there are several such subnet masks, find the one whose bit record contains the least number of ones. If such subnet mask do not exist, say so.
The first line contains two integers, *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=105) β€” the number of IP addresses and networks. The next *n* lines contain the IP addresses. It is guaranteed that all IP addresses are distinct.
In a single line print the IP address of the subnet mask in the format that is described in the statement, if the required subnet mask exists. Otherwise, print -1.
[ "5 3\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3\n", "5 2\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3\n", "2 1\n255.0.0.1\n0.0.0.2\n" ]
[ "255.255.254.0", "255.255.0.0", "-1\n" ]
none
[ { "input": "5 3\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3", "output": "255.255.254.0" }, { "input": "5 2\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3", "output": "255.255.0.0" }, { "input": "2 1\n255.0.0.1\n0.0.0.2", "output": "-1" }, { "input": "10 2\n57.11.146.42\n200.13...
31
0
0
96,378
468
Permanent
[ "dp", "graph matchings", "math", "meet-in-the-middle" ]
null
null
Little X has solved the #P-complete problem in polynomial time recently. So he gives this task to you. There is a special *n*<=Γ—<=*n* matrix *A*, you should calculate its permanent modulo 1000000007Β (109<=+<=7). The special property of matrix *A* is almost all its elements equal to 1. Only *k* elements have specified value. You can find the definition of permanent at the link: https://en.wikipedia.org/wiki/Permanent
The first line contains two space-separated integers *n*,<=*k* (1<=≀<=*n*<=≀<=105;Β 1<=≀<=*k*<=≀<=50). The next *k* lines contain the description of the matrix. The *i*-th line contains three space-separated integers *x**i*,<=*y**i*,<=*w**i* (1<=≀<=*x**i*,<=<=*y**i*<=≀<=<=*n*;Β 0<=<=≀<=<=*w**i*<=<=≀<=109). These numbers denote that *A**x**i*,<=*y**i*<==<=*w**i*. All the elements of the matrix except of the given elements are equal to 1. It's guaranteed that all the positions (*x**i*,<=*y**i*) are distinct.
Print the permanent of the matrix modulo 1000000007Β (109<=<=+<=<=7).
[ "3 1\n1 1 2\n", "10 10\n3 3 367056794\n6 2 124561273\n1 3 46718146\n6 9 415916869\n10 5 985968336\n3 1 526792265\n1 4 386357058\n10 4 349304187\n2 7 102032499\n3 6 502679075\n" ]
[ "8\n", "233333333\n" ]
none
[]
15
0
0
96,841
992
Nastya and King-Shamans
[ "binary search", "data structures" ]
null
null
Nastya likes reading and even spends whole days in a library sometimes. Today she found a chronicle of Byteland in the library, and it stated that there lived shamans long time ago. It is known that at every moment there was exactly one shaman in Byteland, and there were *n* shamans in total enumerated with integers from 1 to *n* in the order they lived. Also, each shaman had a magic power which can now be expressed as an integer. The chronicle includes a list of powers of the *n* shamans. Also, some shamans can be king-shamans, if they gathered all the power of their predecessors, i.e. their power is exactly the sum of powers of all previous shamans. Nastya is interested in whether there was at least one king-shaman in Byteland. Unfortunately many of the powers are unreadable in the list, so Nastya is doing the following: - Initially she supposes some power for each shaman. - After that she changes the power of some shaman *q* times (the shamans can differ) and after that wants to check if there is at least one king-shaman in the list. If yes, she wants to know the index of any king-shaman. Unfortunately the list is too large and Nastya wants you to help her.
The first line contains two integers *n* and *q* (1<=≀<=*n*,<=*q*<=≀<=2Β·105). The second line contains *n* integers *a*1,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109), where *a**i* is the magic power of the *i*-th shaman. After that *q* lines follow, the *i*-th of them contains two integers *p**i* and *x**i* (1<=≀<=*p**i*<=≀<=*n*, 0<=≀<=*x**i*<=≀<=109) that mean that the new power of the *p**i*-th shaman is *x**i*.
Print *q* lines, the *i*-th of them should contain <=-<=1, if after the *i*-th change there are no shaman-kings, and otherwise a single integer *j*, where *j* is an index of some king-shaman after the *i*-th change. If there are multiple king-shamans after each change, print the index of any of them.
[ "2 1\n1 3\n1 2\n", "3 4\n2 2 3\n1 1\n1 2\n2 4\n3 6\n", "10 7\n0 3 1 4 6 2 7 8 10 1\n2 5\n1 3\n9 36\n4 10\n4 9\n1 2\n1 0\n" ]
[ "-1\n", "3\n2\n-1\n3\n", "1\n-1\n9\n-1\n4\n-1\n1\n" ]
In the first example powers of shamans after the first change are equal to (2, 3). The answer equals  - 1, because the sum of powers of shamans before the first shaman is equal to 0, and before the second is equal to 2. In the second example after the first change the powers are equal to (1, 2, 3). The answer is equal to 3, because the power of the third shaman is equal to 3, and the sum of powers of the first and the second shaman is also 1 + 2 = 3. After the second change the powers become equal to (2, 2, 3), where the answer equals 2. After the third change the powers become equal to (2, 4, 3), where the answer equals  - 1. After the fourth change the powers become equal to (2, 4, 6), where the answer equals 3.
[ { "input": "2 1\n1 3\n1 2", "output": "-1" }, { "input": "3 4\n2 2 3\n1 1\n1 2\n2 4\n3 6", "output": "3\n2\n-1\n3" }, { "input": "10 7\n0 3 1 4 6 2 7 8 10 1\n2 5\n1 3\n9 36\n4 10\n4 9\n1 2\n1 0", "output": "1\n-1\n9\n-1\n4\n-1\n1" }, { "input": "21 21\n10733833 40401599 15143...
3,000
0
0
96,860
453
Little Pony and Summer Sun Celebration
[ "constructive algorithms", "dfs and similar", "graphs" ]
null
null
Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration. Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information? Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4*n* places.
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=105;Β 0<=≀<=*m*<=≀<=105) β€” the number of places and the number of roads in Ponyville. Each of the following *m* lines contains two integers *u**i*,<=*v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*;Β *u**i*<=β‰ <=*v**i*), these integers describe a road between places *u**i* and *v**i*. The next line contains *n* integers: *x*1,<=*x*2,<=...,<=*x**n* (0<=≀<=*x**i*<=≀<=1) β€” the parity of the number of times that each place must be visited. If *x**i*<==<=0, then the *i*-th place must be visited even number of times, else it must be visited odd number of times.
Output the number of visited places *k* in the first line (0<=≀<=*k*<=≀<=4*n*). Then output *k* integers β€” the numbers of places in the order of path. If *x**i*<==<=0, then the *i*-th place must appear in the path even number of times, else *i*-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct. If there is no required path, output -1. If there multiple possible paths, you can output any of them.
[ "3 2\n1 2\n2 3\n1 1 1\n", "5 7\n1 2\n1 3\n1 4\n1 5\n3 4\n3 5\n4 5\n0 1 0 1 0\n", "2 0\n0 0\n" ]
[ "3\n1 2 3\n", "10\n2 1 3 4 5 4 5 4 3 1 ", "0\n" ]
none
[ { "input": "3 2\n1 2\n2 3\n1 1 1", "output": "3\n1 2 3" }, { "input": "5 7\n1 2\n1 3\n1 4\n1 5\n3 4\n3 5\n4 5\n0 1 0 1 0", "output": "10\n2 1 3 4 5 4 5 4 3 1 " }, { "input": "2 0\n0 0", "output": "0" }, { "input": "10 10\n2 1\n2 3\n4 2\n4 5\n3 6\n5 7\n8 4\n4 9\n5 10\n4 7\n0 0...
93
0
0
97,159
15
Map
[ "data structures", "implementation", "sortings" ]
D. Map
2
128
There is an area map that is a rectangular matrix *n*<=Γ—<=*m*, each cell of the matrix contains the average height of a corresponding area part. Peter works for a company that has to build several cities within this area, each of the cities will occupy a rectangle *a*<=Γ—<=*b* cells on the map. To start construction works in a particular place Peter needs to remove excess ground from the construction site where a new city will be built. To do so he chooses a cell of the minimum height within this site, and removes excess ground from other cells of the site down to this minimum level. Let's consider that to lower the ground level from *h*2 to *h*1 (*h*1<=≀<=*h*2) they need to remove *h*2<=-<=*h*1 ground units. Let's call a site's position optimal, if the amount of the ground removed from this site is minimal compared to other possible positions. Peter constructs cities according to the following algorithm: from all the optimum site's positions he chooses the uppermost one. If this position is not unique, he chooses the leftmost one. Then he builds a city on this site. Peter repeats this process untill he can build at least one more city. For sure, he cannot carry out construction works on the occupied cells. Would you, please, help Peter place cities according to the algorithm?
The first line contains four space-separated integers: map sizes *n*, *m* and city sizes *a*, *b* (1<=≀<=*a*<=≀<=*n*<=≀<=1000, 1<=≀<=*b*<=≀<=*m*<=≀<=1000). Then there follow *n* lines, each contains *m* non-negative space-separated numbers, describing the height matrix. Each number doesn't exceed 109.
In the first line output *k* β€” the amount of constructed cities. In each of the following *k* lines output 3 space-separated numbers β€” the row number and the column number of the upper-left corner of a subsequent construction site, and the amount of the ground to remove from it. Output the sites in the order of their building up.
[ "2 2 1 2\n1 2\n3 5\n", "4 4 2 2\n1 5 3 4\n2 7 6 1\n1 1 2 2\n2 2 1 2\n" ]
[ "2\n1 1 1\n2 1 2\n", "3\n3 1 2\n3 3 3\n1 2 9\n" ]
none
[]
622
7,168,000
0
97,234