There are T1 and T2 Teams playing a game.Both Team have N players.The power of T1 team player are represented in an array and Similarly T2 team player.
The rule of Game are follows:
1.There are only N fights.
2.No player of any team can play twice
3.For each fight score generated from (P1 + P2)%N P1 represent power of T1 .Similarly P2
T2 is aware of the order of T1 is sending their player to play the game T2 wants to obtains minimum score in each fight.
Your task is to determine the order in Which T2 is sending player such that they obtain minimum score for each fight
You are required to print score for each round and the other players of T2.
N=3
T1=[1,2,3]
T2=[2,0,1]
Output:[0,0,0]
N=4
T1=[0,1,2,1]
T2=[3,2,1,1]
output:[1,0,0,2]
Explanation Case 1 N=3:
[(1+2)%3,(2+1)%3,(3+0)%3]
What is optimal solution for this problem?
One approach is checking one by one power with T1.
This isn't a complete answer, but here's how I would think about it. Having more interesting examples could help develop the idea.
Sort the arrays, one descending, one ascending:
N=4
T1=[0,1,2,1]
T2=[3,2,1,1]
0 1 1 2
3 2 1 1
Now use a pointer to "shift" the alignment while maintaining a variable for the current, ascending target remainder.
Target remainder 0
0 1 1 2
3 2 1 1
^
useless, shift pointer right
0 1 1 2
3 2 1 1
^
useless, shift pointer right
0 1 1 2
3 2 1 1
Related
I need some help making a program that finds the best solution for everyone (more on that later).
6 7
0 0 0 0 0 0 0
1 0 0 1 1 0 0
2 2 2 1 2 2 2
2 1 1 1 2 1 2
0 1 2 2 1 0 0
1 2 1 2 0 1 1
The example given above is a problem that the algorithm is supposed to solve,
the first number of the first row indicates the number of people (6)
the second number of the first row indicates the number of appointments (7)
0 = the person doesnt have a problem with the date
1 = the person could choose these date if none else is available
2 = the person cant choose this appointment
Row = Person
Colum = Available Appointment
What the program needs to do now is to find the best possible solution for everyone by choosing which colum would be the best for the person's desire by arranging peoples appointments based on their choices
ex.
In the 3rd row the person can only attend the appointment on the 4th column since he cant attend to the other ones (2) which also makes column 4 complete and out of use for the other people.
The reason I need help with this is because I have no idea on how to approach this because this might be a simple example but since its an algorithm its supposed to work with dozens of peoples and appointments.
The exercise is somewhat ambiguous, probably on purpose. My wild guess would be to sort the meetings by:
the highest number of possible participants, i.e., the lowest number of 2s in a matrix column.
the lowest “badness”, i.e., the lowest number of 1s in a matrix column.
Why not #2s: Because we don’t care about those who cannot participate at this sorting stage.
Why not #0s: Because we want to minimize the number of people inconvenienced by the meeting time, not (necessarily) maximize the number of people pleased with the meeting time.
#!/usr/bin/env python
import sys
n_people, n_appointments = (int(i)
for i in sys.stdin.readline().split())
people_appointments = tuple(tuple(int(i)
for i in line.split())
for line in sys.stdin)
assert len(people_appointments) == n_people
for appointments in people_appointments:
assert len(appointments) == n_appointments
appointment_metric = {}
for appointment in range(n_appointments):
n_missing = sum(people_appointments[i][appointment] == 2
for i in range(n_people))
badness = sum(people_appointments[i][appointment] == 1
for i in range(n_people))
appointment_metric.setdefault(
(n_missing, badness), []).append(str(appointment + 1))
for metric in sorted(appointment_metric):
print(f'Appointment Nr. {" / ".join(appointment_metric[metric])} '
f'(absence {metric[0]}, badness {metric[1]})')
Possible output (best appointment (by the metric described above) to worst appointment):
Appointment Nr. 6 (absence 1, badness 2)
Appointment Nr. 7 (absence 2, badness 1)
Appointment Nr. 1 / 2 / 3 / 5 (absence 2, badness 2)
Appointment Nr. 4 (absence 2, badness 3)
There are (of course) many other ways to evaluate meetings. Picking and defining a metric is quite likely an implicit part of the exercise.
You are given two array, first array contain integer which represent heights of persons and second array contain how many persons in front of him are standing who are greater than him in term of height and forming a queue.
heights are unique means no two person can have same height.
Example-
A: 3 2 1
B: 0 1 1
It means in front of person of height 3 there is no person standing, person of height 2 there is one person in front of him who has greater height then he, similar to person of height 1. Your task to arrange them Ouput should be. 3 1 2
My approach
1.Sort persons according to their frequency(no. of taller person).
2.Now fix the position of each person in appropriate position. for example:
3 1 2 4
0 2 1 0
after sorting
3 4 2 1
0 0 1 2
now we see that first and second person are at right place so we move third person to it's right position that is 2nd(base index 1) as he has only one taller before him.
3 2 4 1
0 1 0 2
now for 4th person we just place it at 3rd position
3 2 1 4
0 1 2 0
final answer.
I think it has O(n^2) complexity.Can we do better, and what about correctness of this algorithm?
The left-most value of sequence B is always 0-- it's the person standing in front of the queue and there's noone before him.
Whenever the sequence B has a sequence of consecutive 0-s on the left, that portion of the queue is sorted in itself.
So, when you have, say k consecutive 0-s to the left and the value t at the (k+1)-st position, (t necessarily <= k),
the rightmost t of those k people are taller than the one standing at that (k+1)st posn.
Eg.:
suppose
A: '5' '3' '6' '9' '1' '4'
B: 0 0 0 2 0 1
the height of the leftmost 3 are '5' < '3' < '6',
and since the value of '9' is 2, the one of height '9' is shorter than those in
the rightmost 2 and taller than the other among these 3. So, just move '9' to 2 positions left and update its value to 0.
You can solve this in O(n) time using a linked list implemented on an array.
I need to assign random papers to students of a class, but I have the constraints that:
Each student should have two papers assigned.
Each paper should be assigned to (approximately) the same number of students.
Is there an elegant way to generate a matrix that has this property? i.e. it is shuffled but the row and column sums are constant? As an illustration:
Student A 1 0 0 1 1 0 | 3
Student B 1 0 1 0 0 1 | 3
Student C 0 1 1 0 1 0 | 3
Student D 0 1 0 1 0 1 | 3
----------------
2 2 2 2 2 2
I thought of first building an "initial matrix" with the right row/column sum, then randomly permuting first the rows, then the colums, but how do I generate this initial matrix? The problem here is that I'd be choosing between (e.g.) the following alternatives, and the fact that there are two students with the same pair of papers assigned (in the left setup) won't change through row/column shuffling:
INITIAL (MA): OR (MB):
A 1 1 1 0 0 0 || 1 1 1 0 0 0
B 1 1 1 0 0 0 || 0 1 1 1 0 0
C 0 0 0 1 1 1 || 0 0 0 1 1 1
D 0 0 0 1 1 1 || 1 0 0 0 1 1
I know I could come up with something quick/dirty and just tweak where necessary but it seemed like a fun exercise.
If you want to make permutations, what about:
Chose randomly a student, say student 1
For this student, chose a random paper he has, say paper A
Chose randomly another student
For this student, chose a random paper he has, say paper B (different from A)
Give paper B to student 1 and paper A to student 2.
That way, you preserve both the number of different papers and the number of papers per student. Indeed, both students give one paper and receive one back. Moreover, no paper is created nor deleted.
In term of table, it means finding two pairs of indices(i1,i2) and (j1,j2) such that A(i1,j1) = 1, A(i2,j2)=1, A(i1,j2)=0 and A(i2,j1)=0 and changing the 0s for 1s and the 1s for 0s => The sums of the rows and columns do not change.
Remark 1: If you do not want to proceed by permutations, you can simply put in a vector all the paper (put 2 times paper A, 2 times paper B,...). Then, random shuffle the vector and attribute the k first to the first student, the k next ones to student 2, ... However, you can end with a student having several times the same paper. In this case, make some permutations starting with the surnumerary papers.
You can generate the initial matrix as follows (pseudo-Python syntax):
column_sum = [0] * n_students
for i in range(n_students):
if column_sum[i] < max_allowed:
for j in range(i + 1, n_students):
if column_sum[j] < max_allowed:
generate_row_with_ones_at(i, j)
column_sum[i] += 1
column_sum[j] += 1
if n_rows == n_wanted:
return
This is a straightforward iteration over all n choose 2 distinct rows, but with the constraint on column sums enforced as early as possible.
Permutation Game (30 Points)
Alice and Bob play the following game:
1) They choose a permutation of the first N numbers to begin with.
2) They play alternately and Alice plays first.
3) In a turn, they can remove any one remaining number from the permutation.
4) The game ends when the remaining numbers form an increasing sequence. The person who played the last turn (after which the sequence becomes increasing) wins the game.
Assuming both play optimally, who wins the game?
Input:
The first line contains the number of test cases T. T test cases follow. Each case contains an integer N on the first line, followed by a permutation of the integers 1..N on the second line.
Output:
Output T lines, one for each test case, containing "Alice" if Alice wins the game and "Bob" otherwise.
Constraints:
1 <= T <= 100
2 <= N <= 15
The permutation will not be an increasing sequence initially.
Sample Input:
2
3
1 3 2
5
5 3 2 1 4
Sample Output:
Alice
Bob
Explanation: For the first example, Alice can remove the 3 or the 2 to make the sequence increasing and wins the game.
Can someone please help me out on the second input case: 5 3 2 1 4
The increasing sequences possible are:
1) 3 4 - Removing 5 , 2 , 1 in any sequence
2) 2 4 - Removing 5 , 3 , 1 in any sequence
3) 1 4 - Removing 5 , 3 , 2 in any sequence
So the output should be Alice?
Please do not share any code. Thanks
If Alice removes any of 5,3,2,1 then Bob removes 4. So, the increasing sequence can be of only one element, elements can be removed in any order. Hence, Bob wins.
If Alice removes 4, then also the increasing sequence has to be of one element. Bob wins.
So, Bob wins.
A possible case might be 4 or 5 is considered as increasing seq
As the input parameters are n>=2
But Alice would play optimally and remove 5 to win
NOTE: This isn't a programming problem and really doesn't belong on this site...
It sure looks like Alice should be the winner of the second test case.
Flow:
// Start state
5 3 2 1 4
// Alice remove 5
3 2 1 4
// Bob remove 3, 2, or 1
(2 1 4) or (3 1 4) or (3 2 4)
// Alice remove first number remaining
(1 4) or (2 4)
// Alice won!
Alice invents a key (s1, s2, s3, ... , sk). Bob makes a guess (g1, g2, g3, ... , gk).He is awarded one point for each si = gi.
Each s1 is an integer with the range of 0<=si<=11.
Given a q guesses with their scores bi
(g1, g2, g3, ... , gk) b1
(g1, g2, g3, ... , gk) b2
.
.
.
(g1, g2, g3, ... , gk) bq
Can you state if there is a key possible. Given 0<=si<=11, 1<=k<=11, 1<=q<=8.
For Example
2 2 1 1 2
1 1 2 2 1
For the guess 2 2 1 1 the score is 2
For the guess 1 1 2 2 the score is 1
Because there is a key possible let's say 2 1 1 3 which gives the desired scores.Hence the answer is yes
Another Example
1 2 3 4 4
4 3 2 1 1
For the guess 1 2 3 4 the score is 4
For the guess 4 3 2 1 the score is 1
This has no key which gives the desired scores hence answer is NO
I tried the brute force approach generating n^k such keys where n is the range of si.But it gave Time Limit exceeding error.
Its an interview puzzle. I have seen variants of this question but was not able to solve them.Can you tell me what should I read for such type of questions.
I don't know the best solution to this problem, but if you did a recursive search of the possible solution space, pruning branches which could not possibly lead to a solution, it would be much faster than trying all (n^k) keys.
Take your example:
1 2 3 4 4 -> 4
4 3 2 1 1 -> 1
The 3 possible values for g1 which could be significant are: 1, 4, and "neither 1 nor 4". Choose one of them, and then recursively look at the possible values for g2. Choose one, and recursively look at the possible values for g3, etc.
As you search, keep track of a cumulative score for each of the guesses from b1 to bq. Whenever you choose a value for a digit, increment the cumulative scores for all the guesses which have the same number in that position. Keep these cumulative scores on a stack (so you can back up).
When you reach a point where no solution is possible, back up and continue searching a different path. If you back all the way up to g1 and no more paths are left to search, then the answer is NO. If you find a solution, then the answer is YES.
When to stop searching a path and back up:
If the cumulative score of one of the guesses exceeds the given score
If the cumulative score of one of the guesses is less than the given score minus the number of levels left in the search tree (before you hit the bottom)
This approach could still be very slow, especially if "k" was large. But again, it will be far faster than generating (n^k) keys.