Among the many duties of a Software Engineer at the railway company is that of managing the schedule of each beggar working at the Northern Railway Station. It is a well known fact that the beggars are resilient human beings - they do not take breaks from begging and are always onboard a train during the interval of time [0, d]; and also quite quarrelsome - it is best if they do not meet with other beggars while switching trains during the interval of time (0, d) or, even worse, inside a train.
Knowing exactly the time intervals that each of the $$$n$$$ trains stay inside the terminal, you have to find out the maximum number of beggars which can work together at the Northern Railway Station. Keep in mind that a beggar is able to switch trains instantly and will always remain inside a chosen train for the entire duration said train stays in the terminal.
The first line of the input will contain the number of test cases $$$T$$$ ($$$1 \leq T \leq 10$$$).
The first line of each test case will contain $$$d$$$ ($$$0 \leq d \leq 200$$$) the length of the daily begging session and $$$n$$$ ($$$1 \leq n \leq 20.000$$$) the number of trains.
The following $$$n$$$ lines of each test case will contain a pair of integers $$$x_i, y_i$$$, the time interval that the $$$i$$$-th train stays in the terminal ($$$0 \leq x_i \lt y_i \leq d$$$).
The output should contain $$$T$$$ lines, each containing the answer for a test.
1 9 7 0 2 0 2 0 3 2 5 2 9 3 9 5 9
2
Trains are
$$$1: [0-2]$$$
$$$2: [0-2]$$$
$$$3: [0-3]$$$
$$$4: [2-5]$$$
$$$5: [2-9]$$$
$$$6: [3-9]$$$
$$$7: [5-9]$$$
The following is an example scheduling of the beggars:
Beggar 1 goes on trains 1, 4, 7 [0-2-5-9]
Beggar 2 goes on trains 2, 5 [0-2-9]
Beggar 3 goes on trains 3, 6 [0-3-9]
Beggars 1 & 2 would meet on the platform at time 2, so this choice does not satisfy the constraint.
There is no valid choice of more than 2 beggars. Beggars 1 & 3 is an example valid solution.
Inspired by the ingenious encryption techniques of messages used during wars, the AGM team decided to form its own method and named it "Beautiful Lexicographical Alphabetical Trees" or BLAT for short.
We give you a tree with $$$N$$$ ($$$1 \leq N \leq 100.000$$$) nodes, each having a letter assigned to them. The path from node $$$A$$$ to node $$$B$$$ is represented by the succession of letters of the nodes you pass through along the path, including the ones of $$$A$$$ and $$$B$$$. Be careful, the path from $$$A$$$ to $$$B$$$ may be different from the path from $$$B$$$ to $$$A$$$, so they are considered separate paths. Of course, the AGM team didn't want you to find the decryption too boring, so you receive a number $$$K$$$ ($$$1 \leq K \leq min(N^2, 100.000)$$$) as well. Our message is represented by the $$$K$$$th lexicographical smallest path, considering all of them. Decrypt it and you will be rewarded.
The first line of the input will contain three positive integers $$$N,\ K$$$ that represent the number of nodes and the number received from the AGM team.
The next line will contain $$$N$$$ lowercase letters $$$l_1,l_2,l_3,...,l_n$$$, where $$$l_i$$$ represents the letter corresponding to the $$$i$$$th node.
The last $$$N-1$$$ lines will contain three positive integers $$$a,\ b$$$ that represent that there exists a bidirectional road between nodes $$$a$$$ and $$$b$$$.
The output should contain a string of lowercase letters representing the decrypted message.
6 6 a b a a a a 1 2 2 3 2 5 2 6 3 4
aa
6 8 a b a a a a 1 2 2 3 2 5 2 6 3 4
aab
Georgel wants to make a girlfriend, so he decided to impress the girl he is currently talking to through text messages. Now, it is currently 2020, and everybody knows that you are cool only if you text solely with emojis in a non-decreasing order. Georgel is quite the antisocial guy, so he naturally does not know of this convention – in fact, he is beyond salvation. He does use emojis, but in a blasphemous manner.
You, contestant, are a lovely, caring person and decided to help him out of your own free will by removing some of the emojis of some text messages that he has yet to send.
You are given $$$T$$$ ($$$1 \leq T \leq 100.000)$$$ text messages of $$$N$$$ emojis $$$N$$$ ($$$1 \leq N \leq 100.000$$$) which can be understood as integers ranging from $$$1$$$ to $$$10^9$$$. To save Georgel, we ask of you that you remove some of them in order to obtain the largest non-decreasing substring possible. However, since the longer the text, the better the message, you must minimise the number of emojis removed.
Bear in mind that you only ought to remove the emojis which interfere with said substring. For instance, if the longest non-decreasing substring that can be obtained has elements ranging from index $$$i$$$ to index $$$j$$$, the emojis whose positions belong to $$$[1, i-1]$$$ and $$$[j+1, N]$$$ need not be removed.
It is guaranteed that the the sum of $$$N$$$'s over test cases will not exceed $$$10^6$$$.
The first line of input will contain a single positive integer, $$$T$$$, representing the number of test cases. Each test case will consist of two lines.
The first line will contain a single positive integer, $$$N$$$, representing the number of emojis in the text message.
The second line will contain $$$N$$$ positive integers $$$a_i \ (1 \leq i \leq N)$$$, each of them representing an emoji.
The output should contain, on a single line per test case, two integers $$$X$$$, $$$Y$$$, representing the length of the longest increasing substring of emojis that can be obtained and the number of emojis that has been removed.
1 10 4 3 2 1 5 6 3 3 1 3
4 3
The longest increasing substring of emojis that can be obtained is $$$1$$$ $$$3$$$ $$$3$$$ $$$3$$$. In order to obtain this substring, emojis $$$5$$$, $$$6$$$ and $$$1$$$ must be removed.
In a cool country we can find $$$M$$$ cities. The gods above wish to accomplish $$$N$$$ development projects. Each of the $$$N$$$ projects has two distinct investment schemes. For each of the $$$2 \cdot N$$$ investment schemes we know the respective cost for each of the $$$M$$$ cities. Please note that one of the two investment schemes of each project is seen as harmful in the eyes of public opinion. The exact budget for each of the $$$M$$$ cities was already decided by the Government. Your task is to determine the minimum number of harmful investment schemes which can be selected, such that the total spending for each city to be exactly the budget allocated for it. If it is impossible to spend the exact amount of money allocated for each city you should print out "$$$impossible$$$". Keep in mind that for each of the $$$N$$$ projects you have to pick exactly one of the investment schemes.
The first line of the input will contain the number of test cases $$$T$$$ ($$$1 \leq T \leq 30)$$$. The first line of each test case will contain $$$N$$$ ($$$1 \leq N \leq 30$$$, the number of projects) and $$$M$$$ ($$$1 \leq M \leq 30$$$, the number of cities). The second line of each test case will contain $$$M$$$ integers: $$$b_1, b_2, ..., b_M$$$, the budget allocated to each of the $$$M$$$ cities ($$$0 \leq b_i \leq 2000$$$). The following $$$N$$$ lines of each test case will be structured as follows. For each project $$$i$$$ there will be a line containing $$$M$$$ pairs of integers $$$x_j$$$ ($$$0 \leq x_j \leq 100$$$, the cost of the non-harmful scheme) and $$$y_j$$$ ($$$0 \leq y_j \leq 100$$$, the cost of the harmful scheme) - the two possible costs of the $$$i$$$-th project for the $$$j$$$-th city.
The output should contains $$$T$$$ lines, each containing the answer to a test.
2 3 2 10 5 3 4 2 0 3 1 1 4 1 4 4 2 3 4 2 0 3 2 0 1 1 0 1 1 0 1 2 0 0 0 1 1 1 0 0 2 0 0 1 1 0 1
1 impossible
Jimmy is a hungry boy who really enjoys some finger lickin' good crispy strips from KFC. He bought so many that Colonel Sanders offered him a special deal.
He was presented with a pile of $$$K$$$ ($$$1 \leq K \leq 1.000.000$$$) plastic straws (because KFC had no use for them anymore after the "save the turtles" movement) and $$$N$$$ ($$$2 \leq N \leq 1.000.000$$$) buckets. In each bucket, there is an initial $$$a_i$$$ number of straws ($$$1 \leq a_i \leq 1000$$$). Jimmy was told he can take some (or all) of the straws from the pile and put them in the buckets. After that, he has to choose 2 buckets and he will receive a number of crispy strips equal to the lowest common multiple of the number of straws that are now in the 2 buckets.
Jimmy really is a hungry boy and he wants your help to get as many crispy strips as possible. To prove that you found the finger lickin' good strategy, you have to tell us the number of crispy strips that he will receive in the end.
The first line of the input will contain three positive integers $$$N,\ K$$$ that represent the number of buckets and the number of straws in the pile.
The next line will contain $$$N$$$ integers $$$a_1,a_2,a_3,...,a_n$$$, where $$$a_i$$$ represents the number of straws in the $$$i$$$th bucket.
The output should contain a single integer representing the maximum number of crispy strips Jimmy can receive.
2 2 3 5
21
You have been captured by a group of magic wizards and they will not let you go unless you solve their puzzle.
You are given an array $$$A$$$ of $$$N \leq 10^5$$$ integers, $$$0 \leq A_i \leq 10^9$$$ that you need to sort in non-decreasing order. Because it is part of the magic realm, the only way you can manipulate it is using a magic wand that you were given.
The wand allows you to pick a contiguous subsequence of integers $$$A[i...j]$$$ and sort it, consuming $$$(j - i + 1)^3$$$ energy in the process.
Because you don't know how much energy the wand initially contains, you have to find the minimum amount you need to sort the entire array.
The first line of the input will contain the number $$$T$$$, representing the number of test cases.
The first line of each test will contain one positive integer: $$$N$$$
It is guaranteed that among all test cases $$$\sum N \leq 2* 10^6$$$.
The second line of each test case will contain $$$N$$$ integers, the initial configuration of the array.
For each testcase, you should output one line containing one integer: the minimum amount of energy required to sort the array using the wand.
1 4 2 1 4 3
16
The first move is $$$[1..2]$$$ with cost $$$(2-1+1)^3 = 8$$$. The new array is 1 2 4 3.
The second move is $$$[3..4]$$$ with cost $$$(4-3+1)^3 = 8$$$. The new array is 1 2 3 4, which is sorted.
It can be shown that 16 is the minimum cost for this array.
The PokerStars team has came up with a new form of poker. They weren't very original so it is almost like Texas Hold'em but each player receives $$$5$$$ cards and there are only $$$2$$$ cards on the table (do not worry, the rules will be explained in more detail below).
They want to inaugurate it with a broadcasted competition but they need something from you. In order to make it more interesting for the viewers they want to show them the probability of each player of winning after all of them received their cards. Can you help them?
Game Rules :
Poker Hands (from the highest to the lowest) :
If 2 players have the same type of hand :
Note : A kicker is the set of cards in a standard five-card poker hand which are not the part of the hand which makes its rank. The kicker is used to break ties between poker hands of otherwise equivalent rank.
Because the PokerStars team does not like split pots, if after all the criteria above are applied and the $$$2$$$ players are still at a tie, the player with the smallest index wins (formally, player $$$i$$$ beats player $$$j$$$ if $$$i \lt j$$$).
The first line of the input will contain the number $$$T$$$ ($$$1 \leq T \leq 30$$$), representing the number of test cases.
The first line of each test will contain one positive integer $$$N$$$, the number of players.
The next $$$5*N$$$ lines will contain the cards of each player, player $$$i$$$ ($$$1 \leq i \leq N$$$) will have the cards from line $$$(i-1)*5+1$$$ to line $$$(i-1)*5+4$$$.
Each card is represented by $$$2$$$ strings, $$$rank$$$ and $$$suit$$$:
It is guaranteed that all cards in a test case are distinct.
The output file should consist of one line for each test case.
Each line should contain $$$N$$$ numbers (where $$$N$$$ is the number of players for each test case), the probabilities of winning the game for each player.
The probability of winning the game for player $$$i$$$ can be expressed by $$$P_i/Q$$$ where $$$P_i$$$ represents the number of cases where player $$$i$$$ wins and $$$Q$$$ represents the number of total cases. You should output the probability of winning the game for player $$$i$$$ as $$$P_i*Q^{-1}$$$ modulo $$$100055128505716009$$$.
1 4 2 clubs 4 diamonds 7 hearts J spades Q clubs 2 diamonds 4 hearts 7 spades J clubs Q diamonds 2 hearts 4 spades 7 clubs J diamonds Q hearts 2 spades 4 clubs 7 diamonds J hearts Q spades
1 0 0 0
The example consists of only one test case.
In the example, player 1 wins all the possible outcomes.
This year the Christmas tree will look like a DAG (Directed Acyclic Graph) with $$$n$$$ nodes and $$$m$$$ edges. Each node will contain a festive candy which has an expiration date. Miaunel wants to eat exactly one candy per day until there is no candy left. To eat a candy he has to first consume all the candies in the subtree of the node the candy is in, or else the Christmas tree would become unbalanced. Knowing how many days each candy has left until expiration, find out if there is a way for Miaunel to eat all the candies and not get sick (by not consuming an expired candy). Output "YES" if there exists an order of eating the candies given the above constraints, or "NO" otherwise.
The first line of the input will contain the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^5$$$).
The first line of each test case will contain the number of nodes $$$n$$$ ($$$1 \leq n \leq 10^5$$$) and the number of edges $$$m$$$ ($$$1 \leq m \leq 10^5$$$)
The second line of each test case will contain $$$e_1, e_2..., e_n$$$ ($$$0 \leq e_i \leq 2^{30}$$$), the number of days left until expiration for each one of the $$$n$$$ candies. The following $$$m$$$ lines will each contain a pair of integers ($$$a, b$$$) signifying that there is an edge from node $$$a$$$ to node $$$b$$$ ($$$1 \leq a, b \leq n$$$).
It is guaranteed that the sum of all $$$n$$$ is less than $$$10^5$$$ and the sum of all $$$m$$$ is less than $$$10^5$$$.
The output should contain $$$T$$$ lines, each containing an answer to a test.
4 7 6 7 5 6 2 1 4 3 1 2 1 3 2 4 2 5 3 6 3 7 3 2 3 1 1 1 2 1 3 4 4 4 2 3 1 1 2 1 3 2 4 3 4 4 4 4 2 2 1 1 2 1 3 2 4 3 4
YES NO YES NO
You have recently been hired as a software engineer by a new Auto Racing Company. Since you are new in this field, the company gave you one of their easiest tasks.
Basically, they got some races scheduled for the next week, with huge prizes, but also a lot of bets, and, therefore, they would like to find out some relevant information about the final scoreboard. Given that these races are in a straight line and each of them lasts for exactly $$$10^{18}$$$ minutes (the researchers proved that this is the exact moment of time when a person loses interest in what is going on), and you know for each car what is its speed and its starting position, you need to find out how the final scoreboards is going to look like, and what is the last moment of time when a change in the scoreboard happens.
The first line of the input will contain the number $$$R \leq 20$$$, representing the number of races.
The first line of each race will contain one positive integer: $$$N \leq 10^5$$$
The next $$$N$$$ lines of each race will contain two integers, $$$0 \leq P \leq 10^5$$$, the position of the current car, and $$$0 \leq S \leq 10^5$$$, the speed of the current car.
The company guarantees that there will not be two cars with the same characteristics (i.e. there is no pair of cars such that they have both, the same staring position and the same speed).
For each race, you should output two lines, first one containing $$$N$$$ integers, the indexes of the cars as they appear in the final scoreboard, second one containing one floating number, $$$T$$$, the time of the last change in the scoreboard, with a precision of $$$10^{-5}$$$.
1 10 1 20 0 20 5 6 7 6 3 9 2 10 3 14 18 18 30 30 0 0
9 1 2 8 7 6 5 4 3 10 9.000000
Jimmy is not really the sharpest tool in the shed. For instance, he detests notebooks and enjoys writing on toilet paper rolls (for reasons we do not know and do not need to know). One day, while relaxing in his chair after eating an industrial amount of crispy strips, he came up with an idea for a game. So he pulled out his trusty toilet paper ("The Sacred Texts") and began writing what resembled a very long and not so wide matrix. But Jimmy needs a friend to play this game with and it would make him very happy if you would be the one.
Formally, you are given a matrix with $$$N$$$ rows ($$$1 \leq N \leq 10$$$) and $$$M$$$ columns ($$$1 \leq M \leq 100.000$$$) with integers ($$$-1.000.000.000 \leq v_{i,j} \leq 1.000.000.000$$$) and $$$Q$$$ queries ($$$1 \leq Q \leq 1.000$$$) of $$$2$$$ types :
The first line of the input contains two positive integers $$$N$$$ and $$$M$$$ that represent the number of rows and the number of columns of the matrix.
The next $$$N$$$ lines each contain $$$M$$$ integers describing rows of the matrix.
The next line contains one positive integer $$$Q$$$ that represents the number of queries.
The last $$$Q$$$ lines each contain a query of either type $$$1$$$ or $$$2$$$.
Print answers to queries of type $$$2$$$ in the order they appear in the input.
2 3 3 5 2 -1 -3 -1 3 2 1 1 2 3 1 2 2 3 2 1 1 2 2
10 10
2020 has just begun, but the criminals are not resting! The city's biggest bank has just been robbed and the police are asking for your help to identify the thieves!
You know that the city is composed of intersections and directed roads in between them. You also know that there is a security camera situated in each intersection, but there are none on the roads.
Formally, the city can be represented as a directed graph with a security camera placed in each node.
You know the location of the bank and you also know the location where the thieves were last seen.
The police don't have time to go through the footage of all the security cameras so they ask you: regardless of the route they took, what are the intersections the robbers certainly passed through and in what order were they visited?
The first line of the input will contain the number $$$T$$$, representing the number of test cases.
The first line of each test will contain two positive integers: $$$N$$$, the number of intersections, and $$$M$$$, the number of roads.
It is guaranteed that among all test cases $$$\sum N \leq 2*10^5$$$ and $$$\sum M \leq 2*10^6$$$.
The second line will contain two integers $$$A$$$, $$$B$$$, representing the bank location and the intersections where the thieves were last seen respectively. You may assume there is at least one route from intersection $$$A$$$ to intersection $$$B$$$.
The next $$$M$$$ lines will each contain a pair of integers $$$X$$$ and $$$Y$$$ ($$$X \neq Y$$$) describing a directed road from intersection $$$X$$$ to intersection $$$Y$$$. You may assume that the roads are pairwise distinct.
The output file should consist of two lines for each test case.
On the first line you should output one number $$$S$$$ representing the number of cameras that certainly filmed the thieves.
The second line should contain $$$S$$$ numbers, the indices of intersections where said cameras are, in the same order they appeared in the thieves' path.
1 5 5 1 5 1 2 1 3 2 4 3 4 4 5
3 1 4 5
The example consists of only one test case.
The bank is situated in intersection $$$1$$$ and the criminals were last seen in intersection $$$5$$$.
The routes the thieves could have taken are $$$1-2-4-5$$$ and $$$1-3-4-5$$$.
There are $$$3$$$ cameras that certainly saw them and they are situated in intersections $$$1$$$, $$$4$$$ and $$$5$$$.
Bored of studying web design at university, Peter decided it's time to come back to his only friend: an unpredictable labeled rooted tree who always gives him mixed signals. Initially, the tree consists only of its root, a node with label 0. The signals either are information about changes of the shape of the tree, or questions about the current state of the tree (the tree has an existential crisis and does not know all the information about himself).
Thus, the tree gives Peter one of the following signals:
The first line of the input will contain the number $$$S \leq 250.000$$$, representing the number of signals that Peter receives. Each of the following lines contains a signal, that Peter needs to process.
The labels of the nodes are integers between $$$0$$$ and $$$10^6$$$.
For signals of type $$$0$$$, $$$X$$$ has never been in a signal before and $$$0 \leq V \leq 2 * 10^9$$$.
For signals of type $$$1$$$, it is guaranteed that $$$X$$$ still exists in the tree.
For signals of type $$$2$$$, it is guaranteed that $$$Y$$$ is not in the subtree of node $$$X$$$.
For signals of type $$$3$$$, $$$0 \leq V \leq 2 * 10^9$$$.
The output file should contain the answer for each signal of type $$$4$$$ and $$$5$$$.
23 0 8 0 1 0 19 0 9 0 15 8 7 5 8 19 2 19 8 5 19 8 4 8 0 1 0 5 0 2 1 4 0 3 1 6 0 4 3 11 0 5 3 10 0 6 3 0 4 1 5 8 15 5 19 6 1 8 3 3 2 5 6 19 4 15 2 3 15 5 6 19 4 15
8 9 14 3 7 11 8 0 10 5