Questionsforcasestudy
Questionsforcasestudy
Write C++ program program using virtual functions to generate the following output:
“To print random characters over a defined width, where the two successive characters may or
may not have certain amount of gap defined randomly. A certain amount of delay between printing
successive lines has to be implemented in order to have a falling effect.”
2. Write a program that sorts a list of integers in ascending order. However if a -r flag is present on
the command line your program should sort the list in descending order.
3. Functions
4. Method overriding(#optional)
3. Write a program in c++ that Counts of distinct consecutive sub-string of length two.
the task is to print all distinct sub-strings of length two in the given string. All substrings should
be printed in lexicographical order.
str = "abcab"
Output:
ab-2
bc-1
ca-1
Input:
str = "xyz"
Output:
xy-1
yz-1
1. Paramaterised constructors
4. Implement binary searching in c++ using following concepts of object oriented programming
2. Pointers to functions
3. Recursion (optional)
4. Virtual functions
Malvika's cats, Chingam and Jimma are also bored of solving problems. Fortunately,
Chingam has found n bars of dark chocolate. They all have the same width, but differ in their
lengths. In fact, no two chocolate bars have the same length. All the n bars are kept in a row.
Let's say that their lengths are given by L[1], L[2], … , L[n] from left to right. That is, length of bar i
is L[i].
Chingam decided to play a little game with Jimma. In one step, Jimma has to choose some
bar which has not been eaten yet. Chingam hates inversions, so he will instantaneously eat that
bar, along with every other Bar with which it forms an Inversion Pair. We say that bars i and j
form an Inversion Pair, if the bar to the left is longer than the bar to the right.
That is, if Jimma selects bar i, whose length is L[i], then Chingam will eat this, as well as
every bar j such that j > i and L[j] < L[i], as well as every bar k such that k < i and L[k] > L[i]. After
this, the step ends, they proceed to the next step, where Jimma selects another bar and the
whole process repeats. The game ends when all the n chocolate bars are eaten.
Jimma doesn't like this game because she does not get any chocolate. So, she wants to
finish it as soon as possible and go back to sleep. Please help her find the minimum number of
steps needed for the game to end.
Input
The first line of input contains a single integer T denoting number of test cases
The second line contains n space separated integers, which correspond to L[1],
L[2], … , L[n].
Output
For each test case, output a single integer in a line corresponding to the answer
of the problem.
Constraints
1≤T≤5
1 ≤ n ≤ 105
1 ≤ L[i] ≤ n
Example
Input:
3142
Output:
Explanation
In the first step, Jimma can choose bar 4, which is of length 2. Bar 3 and bar 1 form
Inversion Pairs with bar 4. So Chingam eats all these 3 bars. In the second step, Jimma chooses
bar 2, and she's done. You can check that Jimma cannot get rid of all the four bars in one step.
Spider-Chef is well revered in Chefland for his generous behavior of helping hungry
people by collecting food for them by flying places to places. All was going good, until one
day, a dark liquid from a far away galaxy came and turned the Spider-Chef into an evil
person and his famous red suit turned into a dark suit. Now he just likes to scare children by
flying around and shouting at them.
Spider-Chef has a set of n points: p1, p2, .., pn which will be determining his motion.
He starts initially at coordinates (0, 0). He shoots his web at point p1 and revolves around
this point in anti-clockwise direction till he traverses one quarter of the circle, i.e. till he
covers 90' angle in anti-clockwise direction. After that, from that point, he will shoot a web
at p2 and will start revolving around p2 in anti-clockwise fashion again for an angle of 90'.
He does this for the entire set of n points. He stops after revolving 90' around pn.
When Bat-Chef learned about Spider-Chef's condition, he was extremely sad and
wanted to catch Spider-Chef so that he can remove the dark liquid from Spider-Chef's body
and turn him into a good person again. Unlike Spider-Chef, Bat-Chef can move in any way
he desires. Bat-Chef does not want to delay things any more and wants to catch
Spider-Chef as soon as possible. Help him in finding the minimum time required to catch
the Spider-Chef.
Note that the speed of both Spider-Chef and Bat-Chef is same and is equal to 1 unit
per second.
We are not quite sure about the initial position of Bat-Chef. So, we will ask Q queries.
Each of the queries will contain a possible initial position of Bat-Chef. For each query, you
have to tell minimum time required for Bat-Chef to catch Spider-Chef.
Input
The first line contains a single integer n denoting the number of points
Each of the next n lines contains two space separated integers denoting the x and y
coordinates of i-th point pi.
Each of the next Q lines contains two space separated integers denoting the x and y
coordinates of initial starting position of Bat-Chef.
Output
For each test case, for each query, output a single line containing the minimum time
required for Bat-Chef to catch Spider-Chef. Your answer will be considered correct if it
has an absolute or relative error less than or equal to 10-6.
Constraints
1 ≤ n ≤ 1,000,000
1 ≤ Q ≤ 100,000
-107 ≤ x and y coordinates of all the points in the input (i.e. for all pi and initial query
positions of Bat-Chef) ≤ 107
Spider-Chef can shoot his web no farther than 100 unit distance from his current place.
Therefore, it is guaranteed that the distance between the point where he finishes the 90'
revolution with pi, and pi+1, is atmost 100 units.
Example
Input:
-1 0
02
00
-2 1
20
10 10
Output:
0.00000000
1.2837616026
2.3803973373
12.7279220614
Explanation:
Example case 1. Luckily initial position of Bat-Chef and Spider-Chef is precisely the same, so it
won't take any time to Bat-Chef to catch Spider-Chef.
There are N dragons in Wonderland (numbered 1 through N). Each dragon lives in one of N
caves, which are located in a single row and numbered 1 through N in the order from left to right.
Let's denote the strength of the i-th dragon by Si.
Also, a beautiful princess lives in Wonderland. Many princes would like to marry her. But
the princess feels self-sufficient, so she doesn't want to get married. She loves living in dragon
caves and talking to dragons.
One day, the princess heard that a new prince is travelling to Wonderland to propose to her.
She doesn't want to even see the prince — instead, she's going to choose some cave to live in
and assign exactly one dragon to each cave to protect her from the prince.
Let's denote the cave the princess chose by F and the strength of the dragon inhabiting the
i-th cave after the princess assigns the dragons to caves by Xi. The prince will start his journey
near some cave V and move straight towards cave F, fighting the dragons in all caves between V
and F (both inclusive). The prince initially has strength P; by defeating a dragon with strength X,
the prince's strength decreases by X. If the prince's strength becomes non-positive at any time,
he dies.
The princess does not know the cave the prince will choose to start from. Therefore, she
wants to assign the dragons to caves in such a way that the number of caves the prince can start
from and die while going to the princess (possibly after defeating all dragons) is maximum
possible. Help the princess calculate this number for each possible choice of her cave F.
Input
The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows.
The first line of each test case contains two space-separated integers N and P denoting the
number of dragons and the strength of the prince.
The second line contains N space-separated integers S1,S2,…,SN denoting the strengths of
the dragons.
Output
For each test case, print a single line containing N space-separated integers. For each valid i,
the i-th of these integers should denote the number of starting caves for the prince such
that the prince dies if the princess stays in the i-th cave.
Constraints
1≤T≤10
1≤N≤105
1≤P≤105
Subtasks
N,P≤2,000
Si≤2,000 for each valid i
N,P≤20,000
Example Input
34
212
45
1111
Example Output
212
0000
Explanation
Example case 1:
If F=1, the optimal sequence X is [2,2,1]. Then, if the prince starts at the second or third
cave, he will die while going to the princess, so the answer is 2.
If F=2, the optimal sequence X is again [2,2,1]; the prince will die if he starts near the first
cave.
If F=3, the optimal sequence X is [1,2,2]; the prince will die if he starts near the first or
second cave.
On the contrary, if X=[2,1,2] for F=2, the prince would be able to reach the princess starting
from any cave, so this is not an optimal assignment of dragons.
The company structure of ShareChat can be viewed as a tree with N nodes (numbered 1 through
N); node 1 is the root of this tree. You should choose a (possibly empty) subset of nodes and place
one coin in each node from this set. We call the tree proper if the following condition holds:
Consider two arbitrary disjont subtrees. Let's denote the total number of coins in one
subtree by X and the total number of coins in the other subtree by Y.
If X>0 and Y>0, the greatest common divisior (GCD) of X and Y must be 1.
Please calculate the number of different assignments of coins to nodes (different sets of nodes
containing coins) such that the resulting tree is proper. Since the answer could be very big, compute it
modulo 109+7.
Note: A subtree is the tree formed by some node v and all (direct or indirect) descendants of v. Two
subtrees are disjont if they do not have any common node.
Input
The first line of the input contains a single integer N denoting the number of nodes in the
tree.
The second line contains N−1 space-separated integers p2,p3,…,pN, where pi denotes the
parent of node i.
Output
Print a single line containing one integer — the number of assignments of coins to nodes
which form a proper tree, modulo 109+7.
Constraints
1≤N≤70
Example Input
1123
Example Output
30
Explanation
There are only two ways to assign coins to nodes such that the resulting tree is not proper. Both
contain coins in nodes 2, 3, 4, 5 and one of them contains a coin in node 1 too.
As you might have probably guessed, TreeLand is a country located far to the east from ByteLand,
the country he lives in. As you might have also probably guessed, it is called TreeLand, because
historically the road system of the country forms a tree. So, there are N cities in the country and N-1
bidirectional roads, connecting them. The cities are numbered from 1 to N. Of course, it is possible to
get from any city of the country to any other city via these roads.
Not long ago, a new King came to the throne in TreeLand. The first thing he had done was
re-organizing the road system of the country. It was considered that cities with more than 8 adjacent
roads might cause disruption in the road system, because of the high traffic load. So now, every city in
TreeLand is directly connected with no more than 8 cities.
Usually, when Sergey travels to the country, he visits all its cities. So now he visited all the N cities of
TreeLand. Any time he first visited a city, he wrote its number in his notebook. Eventually, there was a
sequence of N integers in the notebook.
But what Sergey didn't know was that it was rainy season in TreeLand, and he was not prepared for it.
Sadly, his notebook got wet, and the records became obscure.
Sergey wants to tell his friends about the journey, and so he wants to restore the order in which he
visited the cities. He has partially restored his records: for some cities he is certain about when he
visited them. He is also certain that he did not wander around wasting his time: he started from some
city, visited all the cities and got back to the starting city, and did all this by travelling along the
minimum possible number of roads (If he travelled along some road multiple times, it's counted
multiple times).
Sergey found a map of the road system of TreeLand. After seeing it, he realized that generally there is
more than one way to accomplish his trip. So now he is interested in the number of ways to complete
the trip so that it doesn't contradict his records.
Please help him to calculate this number. Since it can be large, output it modulo 109+7.
Input
The first line of the input contains an integer N denoting the number of cities in TreeLand.
The second line contains N space-separated integer numbers. Let's denote the ith of them
by Ai. If Ai is 0, then Sergey is not certain about the number of the city that was the ith in
his records. Otherwise, Ai denotes the number of the ith city in Sergey's records.
Each of the following N-1 lines contains two space-separated integers, Xj Yj, denoting a
road in TreeLand.
Output
Output the total number of possible trips Sergey could have done, which do not contradict
the restored records. Since this number can be large, output it modulo 109+7.
Constraints
1 ≤ N ≤ 500
0 ≤ Ai ≤ N
1 ≤ Xj, Yj ≤ N
Example
Input:
10000
12
13
34
35
Output:
Explanation
Here, all the trips have a length of 8 roads. No other trip has length less than or equal to 8 roads.
It's summer — the time for holidays! Chef finally finished all university exams. Now he can rest
and play with sequences.
Today, Chef took a sequence A with N elements. He wants to partition this sequence into an arbitrary
number of non-empty contiguous subsequences (i.e. each of the subsequences has to consist of one
or more consecutive elements of the original sequence). Each element of the original sequence must
belong to exactly one subsequence.
This would be easy for Chef, so he is only interested in safe partitions of A. A safe partition is a
partition into subsequences S1,S2,…,SK such that for each valid i, min(Si)≤|Si|≤max(Si) — that is, for
each subsequence in this partition, its length is greater or equal to its smallest element and smaller or
equal to its largest element.
Finding one safe partition would still be easy for Chef, so he wants to find the number of safe
partitions of A. Since this number could be very big, please compute it modulo 1000000007 (109+7).
Input
Output
Print a single line containing one integer — the number of safe partitions of A modulo
109+7.
Constraints
1≤N≤5*105
Subtasks
1≤N≤105
Subtask #3 (15 points): there are exactly two different values of elements of A
Example Input
1623434
Example Output
Explanation
[1],[6,2,3,4,3,4]
[1,6,2],[3,4,3,4]
[1,6,2,3],[4,3,4]
[1],[6,2],[3,4,3,4]
[1],[6,2,3],[4,3,4]
[1,6],[2,3],[4,3,4]
The summer is coming and Chef wants to fill his swimming pool with water so that he could
enjoy swimming in it during these hot days.
The swimming pool is N metres in length (long course) and a fixed width 1 metre. Its long course is
split into N equal parts, with possibly different depths. For each valid i, the i-th part has depth Di
metres.
1xv — pour v cubic metres of water into the x-th part of the pool
2x — find how much metres deep is the water surface from the ground in the x-th part of the pool. If
there's no water on this part then the water surface depth is same as the depth of this part.
Water level obeys the following rules. As long as the water level in some part of the pool is higher
than the water level in at least one of the adjacent parts, the water from it will move to adjacent parts
with lower water levels until water levels become equal or there is no water left in this part. If the
adjacent part from both sides (left and right) have lower water level then the half of the quantity of
water in the current part will move to left and the other half will move to the right. The water on the
leftmost part can't move to the left, and the water on the rightmost part can't move to the right.
When the water level reaches the surface of the pool (i.e. its depth become 0), the pool overflows
and the water level does not increase anymore.
Input
The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows.
The first line of each test case contains two space-separated integers N and Q.
Q lines follow. Each of these lines describes a query in the format 1xv or 2x.
Output
For each query of the second type, print a single line containing one real number — the
depth of the water surface of the x-th part of the pool if it contains water, or Dx if it does not.
Your answer will be considered correct if its relative or absolute error does not exceed 10−6.
Constraints
1≤T≤100
1≤N,Q≤105
1≤v≤1014
Subtasks
Subtask #1 (30 points): there is at most one query of the first type
Subtask #2 (30 points): all queries of the first type appear before any queries of the second type
Example Input
56
35216
142
22
123
22
25
23
68
551122
141
111
21
22
23
24
25
26
78
8351538
147
21
22
23
24
25
26
27
34
121
1 1 10
21
22
23
Example Output
4.0000000000
2.0000000000
5.0000000000
2.0000000000
4.2500000000
4.2500000000
1.0000000000
1.0000000000
1.7500000000
1.7500000000
6.5000000000
3.0000000000
3.0000000000
1.0000000000
3.0000000000
3.0000000000
6.5000000000
0.0000000000
0.0000000000
0.0000000000
Explanation
Example case 1:
The pool is divided into 5 parts, the depths of the parts are 3 5 2 1 6, in first query we pour 2 cubic
units of water into 4-th part, since both 3-th part and 5-th part are deeper than 4-th part then the
water can't stay in 4-th part, 1 unit will go to left and 1 unit to right. the 1 unit that went to the right
stayed in 5-th part and made its depth equal to 5, the 1 unit that went to the right couldn't stay in
3-th part because 2nd part is deeper than it so it moved to 2nd part and made its depth equal to 4.
in the third query we pour 3 water units into 2nd part, in the beginning 2nd part is deeper than than
1st part and 3rd part so water will stay in 2nd part until its depth become 3 it will have same depth as
1st part so the remaining water will be evenly distributed between 1st and 2nd parts.
Example case 2:
in the first query we pour 1 cubic unit of water above 4th part, since it has same depth as 3rd part,
and both 2nd and 5th parts are deeper than 4th part then 0.5 units of water will go to 5th part (and
will be distributed equally between 5th and 6th parts) and 0.5 units of water will go to 2nd part (and
will be ditributed equally between 1st and 2nd parts). so after first query depths of all parts will
become (4.75 4.75 1.00 1.00 1.75 1.75).
Type 1 - represented as 1 i v meaning that you have to change Ai = v (change is carried on for
further queries too)
Consider another new array B, which has the elements of A, but in sorted, increasing order.
Partition the array B into minimum number of interesting subsegments. You can prove that there is
an unique way to do so. In each of those interesting subsegments, find the minimum frequency of an
existing element, and add this minimum frequency over all the interesting subsegments in the
partition. Output this sum as the answer to the query of Type 2.
Note: Since the input is large, it is recommended to use Fast I/O techniques.
Input
The first line contains a single integer, L, which is the length of the array
The second line contains L space separated integers: A1, A2, ..., AL, which are the original
values in the array.
The third line contains a single integer, Q, which is the number of queries.
Output
Constraints
1 ≤ L ≤ 105
0 ≤ Ai ≤ 109
1 ≤ Q ≤ 5*105
1≤i≤L
1 ≤ v ≤ 109
Example
Input:
2727123
152
172
2
Output:
Explanation
For the second query, B = {2, 2, 2, 2, 3, 7, 7}. The way to break it into minimum number of
interesting subsegments is to partition it into {2,2,2,2,3} and {7,7}. The minimum frequency
in the first subsegment is 1 (because 3 appears once), and in the second subsegment, there
is only one frequency, which is 2. Therefore, the answer for the second query is 1 + 2 = 3.
For the second query, B = {2, 2, 2, 2, 2, 7, 7}. The way to break it into minimum number of
interesting subsegments is to partition it into {2,2,2,2,2} and {7,7}. The minimum frequency
in the first subsegment is 5 (it is the only frequency), and in the second subsegment, there
is only one frequency, which is 2. Therefore, the answer for the second query is 5 + 2 = 7.
All submissions for this problem are available.Niket is an incredible magician. He has prepared an
extraordinary magic trick which he calls as the biggest achievement of his life. He will be performing
this magic trick for the first time in the K-Pop World Festival. He is nervous and wants to make sure
that everything goes well. His magic trick consists of N acts labeled from 1 to N. Every act except act 1
is directly dependent upon the successful execution of exactly one of the previous acts. The act i is
said to be dependent upon act j iff -
act i is dependent upon some act x which is dependent upon the act j. (j < x < i)
An act can never happen if one or more of its dependencies failed. Note that not happening is
not the same as failing. Also, every act has an associated value ai which gets added to the audience's
anger if that act fails. If the anger becomes very large, people will start throwing sharp objects at
Niket.
Since he is going to perform in the K-Pop World Festival, he is interested in knowing the
minimum possible anger value of audience in the hypothetical situation where he was able to
successfully execute exactly K acts during his performance. This means that the remaining acts either
failed or could not happen (because of the dependency constraints).
Input
The following N-1 lines contain the information about the acts 2 to N. ith of these lines
contains two space separated integers pi (pi < i+1) and ai- This means that the (i+1)th
act is directly dependent upon the pith act and adds ai to the anger value.
Output
For each testcase, print a line containing the minimum anger value of audience if Niket
could perform exactly K of the acts successfully.
Constraints
1≤T≤10
1≤N≤2000
1≤K≤N
1≤ai≤109
Sample Input
53
15
24
26
38
Sample Output
Explanation
If Niket fails on act 3, a3 = 4 gets added to the audience's anger and the act 5 can
never happen. This means only 3 acts 1,2 and 4 are successful.
All submissions for this problem are available.You are given two arrays of length N. Array A = A1,
A2, …, AN. Array L = L1, L2, …, LN. For every k such that 1≤k≤N, you have to find the length of the
longest possible subsequence in the first k elements of array A such that if Aj immediately follows Ai
in the subsequence, then |Aj−Ai|≤Lj
Please note that the input is encrypted. Read Input section for information.
Input:
The i-th of the next N lines contains two values, A′i and L′i, the encrypted values of Ai
and Li. Suppose that the (i-1)-th output was Ansi-1, then Ai = Ansi-1 XOR A′i, and Li =
Ansi-1 XOR L′i, where XOR denotes the bitwise xor operator. Assume Ans0 = 0.
Output:
Output N integers, each in a new line, the i-th of which should be the length of the longest
possible subsequence uptil Ai.
Constraints
1≤N≤105
1≤Ai≤109
1≤Li≤109
0≤A′i≤2∗109
0≤L′i≤2∗109
Sample Input:
11
30
61
52
40
Sample Output:
EXPLANATION:
11
21
43
61
73
1) {1}
2) {1,2}
3) {1,2.4}
4) {1,2,4}
5) {1,2,4,7}
In racing, you refuel your car in the pitstops, and it’s very important to carefully plan pit stops.
1 unit of fuel is enough for exactly 1 lap. At the beginning of each lap, the number of units of fuel
in your car must be a positive integer that doesn't exceed the tank capacity N, and it will decrease by
1 during a lap. After each lap, you can spend P seconds to visit a pit stop and refuel your car (add
some fuel). It's allowed to start a lap with only 1 unit of fuel and thus finish it with 0 units — but you
have to visit a pit stop right after that, unless it was the last lap of the race. You can start the race with
any allowed (positive integer that doesn't exceed N) number of units of fuel.
The more fuel your car contains, the slower it is. If you start a lap with x units of fuel, you will
finish this lap in tx seconds. You can assume that tx ≤ tx+1.
There are Q independent races, each described by two integers K and P, denoting the number of
laps and the time needed to visit a pit stop in that race (let's say that in some races the pit stop crew
is lazy and works slower). For each race, find the minimum possible number of seconds in which you
can finish that race.
Input
The first line of the input contains two integers N and Q, denoting the tank capacity
and the number of races respectively.
The second line contains N integers t1, t2, ..., tN, where tx denotes the time needed to
finish a lap if you start it with x units of fuel.
Each of the next Q lines contains two integers K and P, denoting the number of laps
and the number of seconds needed to visit a pit stop in one race.
Output
For each of the Q races, in a separate line, print a single integer — the minimum possible
number of seconds needed to finish the race. Please note the unusual constraint about the
answer.
Constraints
1 ≤ N, Q ≤ 300,000
1 ≤ K, P, ti ≤ 1013
ti ≤ ti+1
It's guaranteed that the answer fits in the signed 64-bit type.
Example
Input:
3 14
5 7 20
5 60
53
11
12
13
21
22
23
31
32
33
7 60
999999999 60
1000 123456789123
Output:
104
35
11
12
12
17
19
20
176
30666666576
41111110788607
Explanation
Your car fits up to N = 3 units of fuel. Let's take a look at the first two races.
Race #1. This race consists of K = 5 laps and you need P = 60 seconds to visit a pit stop. One
optimal scenario is:
Spend t3 = 20 seconds on the first lap. Now you have 2 units of fuel.
Spend t2 = 7 seconds on the second lap. Now you have 1 unit of fuel.
Spend t1 = 5 seconds on the third lap. Now you have 0 units of fuel.
Spend 60 seconds to visit a pit stop. Add 2 units of fuel there.
This total time is 20 + 7 + 5 + 60 + 7 + 5 = 104 seconds and it turns out to be the optimal answer.
Race #2. This race also consists of K = 5 laps, but this time you need only P = 3 seconds to visit a
pit stop, so it might be better to visit a pit stop more often.