Acm Amman Collegiate Programming Contest en
Acm Amman Collegiate Programming Contest en
Problem Set
Bahosain is a judge in one of the programming contests. The winner of this contest will be the team that
solves the maximum number of problems. If more than one team solves the maximum number of problems,
then the one with the minimum penalty will be the winner.
Given the number of problems solved by each team and the penalty, can you help Bahosain by determining
the winner of the contest?
Input
T (1 ≤ T ≤ 128)
The first line of input contains an integer that represents the number of test cases.
N (1 ≤ N ≤ 100)
The first line of each test case contains one integer that represents the number of teams in
the contest.
N
Each of the next name
lines contains a string S
followed by two integers and P name
, where represents the
name of the team and contains at most 20 S
lowercase letters, (0 ≤ S ≤ 16)
represents the number of
problems solved by this team, and P(0 ≤ P ≤ 1500) represents the penalty of the team.
Output
For each test case, print a single line with the name of the team who won.
Rock-Paper-Scissors is a two-player game, where each player chooses one of Rock, Paper, or Scissors. Here
are the three cases in which a player gets one point:
N
Bayashout got more points in the r
ounds and won. Given the moves played by Bayashout in each round,
X,
Bahosain wants to know the number of ways in which he could have chosen Y
Z
and such that he wins in
N
the rounds.
N
The winner of the N
rounds is the player that gets more total points in the
rounds.
Input
T (1 ≤ T ≤ 64)
The first line of input contains T
, where is the number of test cases.
N (1 ≤ N ≤ 1000)
The first line of each test case contains an integer that represents the number of rounds.
N
The next line contains a string of uppercase letters, the first letter represents the choice of Bayashout for
the first round, the second letter represents his choice for the second round, and so on.
R (
Each letter in the string is one of the following: P
Rock), S
(Paper), or (Scissors).
Output
For each test case, print a single line with the number of ways in which Bahosain could have won.
Bahosain is walking in a street of Nblocks. Each block is either empty or has one lamp. If there is a lamp in a
block, it will light it’s block and the direct adjacent blocks. For example, if there is a lamp at block 3, it will light
the blocks 2, 3, and 4.
Given the state of the street, determine the minimum number of lamps to be installed such that each block is
lit.
Input
T (1 ≤ T ≤ 1025)
The first line of input contains an integer that represents the number of test cases.
N (1 ≤ N ≤ 100)
The first line of each test case contains one integer that represents the number of blocks in
the street.
N
The next line contains ’
characters, each is either a dot .
’ ’
or an asterisk *
’
.
A dot represents an empty block, while an asterisk represents a block with a lamp installed in it.
Output
For each test case, print a single line with the minimum number of lamps that have to be installed so that all
blocks are lit.
Bahosain has a strange habit. He writes his daily notes in binary! His little brother also has a strange habit. He
hates seeing alternating patterns of 0s and 1s or strings longer than K. Therefore, whenever Bahosain writes a
binary note on a paper, his little brother cuts it at night with scissors in order to make each part of it not
K
longer than and not alternating.
After several years of suffering from cutting binary notes with scissors, Bahosain decided to cut his notes in
such a way that his brother won’t touch them.
N
Given a binary string of length , find the minimum number of cuts Bahosain has to make such that each of
K
the resulting strings are NOT alternating and contains no more than bits.
A binary string is considered alternating only if each bit after the first one is different from the one before it.
For example: strings 110, 0110 and 010100 are not alternating, while 101 and 01 are alternating strings.
Input
T (1 ≤ T ≤ 64)
The first line of input contains that represents the number of test cases.
N
The first line of each test case contains two integers:
andK (1 ≤ K ≤ N ≤ 1000) N
, where is the length of
the string and K is the maximum length of a resulting string.
N
The next line contains a string of bits (0 or 1).
Output
For each test case, print a single line with the minimum number of cuts.
After grading his programming exam, he noticed that most of the students have failed. Since this is the last
semester for him teaching in Yemen, Dr. Bahosain decided to give bonus marks to all students in a fair way.
He decided to give the same bonus marks to all students without making the mark of any student exceed 100
.
Help Dr. Bahosain by finding the maximum possible number of students that will pass the course after adding
the bonus marks.
A student will pass the course if his mark after adding the bonus marks is more than or equal to
50
.
Input
T (1 ≤ T ≤ 1024)
The first line of input contains an integer that represents the number of test cases.
N (1 ≤ N ≤ 100)
The first line of each test case contains one integer that represents the number of students
in Dr. Bahosain’s class.
N
The next line contains space-separated integers between
0
and
100
, each representing the initial mark of a
student.
Output
For each test case, print a single line with the maximum number of students that will pass the course.
After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends most of his time travelling
between different cities. He decided to buy a new car to help him in his job, but he has to decide about the
capacity of the fuel tank. The new car consumes one liter of fuel for each kilometer.
Each city has at least one gas station where Bahosain can refill the tank, but there are no stations on the roads
between cities.
Given the description of cities and the roads between them, find the minimum capacity for the fuel tank
needed so that Bahosain can travel between any pair of cities in at least one way.
Input
T (1 ≤ T ≤ 64)
The first line of input contains that represents the number of test cases.
N (3 ≤ N ≤ 100,000)
The first line of each test case contains two integers: M (N-1 ≤ M ≤ 100,000)
and ,
where N M
is the number of cities, and is the number of roads.
It is guaranteed that each pair of cities is connected by at most one road, and one can travel between any pair
of cities using the given roads.
Output
For each test case, print a single line with the minimum needed capacity for the fuel tank.
33
121
232
313
G. Heavy Coins
Bahosain has a lot of coins in his pocket. These coins are really heavy, so he always tries to get rid of some of
the coins by using them when paying for the taxi.
S
Note that the driver won’t give Bahosain any change back if he receives more than pennies, and Bahosain
doesn’t care!
Input
T (1 ≤ T ≤ 1001)
The first line of input contains , the number of test cases.
N (1 ≤ N ≤ 10)
The first line of each test case contains two integers: and
S (1 ≤ S ≤ 1000), where N is the
number of coins in Bahosain’s pocket and S is the amount (in pennies) Bahosain has to pay for the taxi driver.
N
The next line contains space-separated integers between
1
and
100
that represent the values (in pennies)
of the coins in Bahosain’s pocket.
Output
For each test case, print a single line with the maximum number of coins Bahosain can use to pay for the
driver.
Note
In the first test case, Bahosain can pay in any of the following ways:
(1, 3, 5)
,
(3, 4, 4)
or
(1, 4, 4)
.
H. Bridges
Given an undirected connected graph, you are allowed to add one edge between any pair of nodes so that the
total number of bridges in the graph is minimized.
Input
T (1 ≤ T ≤ 64)
The first line of input contains that represents the number of test cases.
N (3 ≤ N ≤ 100,000)
The first line of each test case contains two integers: M (N-1 ≤ M ≤ 100,000)
and ,
where N is the number of nodes, and M is the number of edges.
M
Each of the following X Y (1 ≤ X, Y ≤ N)(X ≠ Y)
lines contains two space-separated integers: , which
means that there is an edge between node X Y
and node .
Output
For each test case, print a single line with the minimum possible number of bridges after adding one edge.
33
12
23
31
I. Bahosain and Digits
Bahosain has a string of digits. He is going to perform the following operation on the string as many times as
needed to make all digits in the string the same.
K
In one move, he can choose a substring of length and change each digit in this substring to it’s next digit,
the digit that goes after ‘
9’
is ‘
0’
.
K
Your task is to determine the maximum such that it is possible to make all digits in the string equal using
the described operation.
Input
N (1 ≤ T ≤ 128)
The first line of input contains an integer that represents the number of test cases.
Output
K
For each test case print a single line with the maximum possible .
Note
N
Bahosain went back for teaching but now in a primary school. His classes, which contain a total of students,
M
went to a school trip. Bahosain has packets of candies; each packet contains one or more candies.
Students think that a distribution of packets is fair if all students from the same age get the same number of
candies, and older students get more candies than the younger students.
Given the age of each student, and the number of candies in each packet, determine if it is possible to
exactly one packet
distribute the packets so that every student gets and the distribution is fair according to
the students.
Input
T (1 ≤ T ≤ 128)
The first line of input contains , the number of test cases.
N (1 ≤ N ≤ 100)
The first line of each test case contains two integers: M (N ≤ M ≤ 200)
and , which represent
the number of students and the number of candy packets respectively.
N
The next line contains space-separated integers between
5
and
15
, each representing the age of one
student.
M
The next line contains space-separated integers between
1
and
50
, each representing the amount of
candies in one of the packets.
Output
For each test case print a single line with “YES” if it is possible to distribute the packets according to the
problem statement, otherwise print “NO”.
Bahosain was trying to solve this simple problem, but he got a Runtime Error on one of the test cases, can you
help him by solving it?
N
Given an array of K
non-negative integers and an integer X
, your task is to find two integers Y
and from the
X
given array such that ×Y
=K.
Input
T (1 ≤ T ≤ 128)
The first line of input contains , the number of test cases.
N (2 ≤ N ≤ 100,000)
The first line of each test case contains two integers: K (1 ≤ K ≤ 100,000)
and .
N
The next line contains space-separated integers, each between
0
and
100,000
.
Output
X
If there is more than one possible solution, print the one with the minimum .
Bahosain has a strange habit. He writes his daily notes in binary! His little brother also has a strange habit. He
hates seeing alternating patterns of 0s and 1s or strings longer than K. Therefore, whenever Bahosain writes a
binary note on a paper, his little brother cuts it at night with scissors in order to make each part of it not
K
longer than and not alternating.
After several years of suffering from cutting binary notes with scissors, Bahosain decided to cut his notes in
such a way that his brother won’t touch them.
N
Given a binary string of length , find the minimum number of cuts Bahosain has to make such that each of
K
the resulting strings are NOT alternating and contains no more than bits.
A binary string is considered alternating only if each bit after the first one is different from the one before it.
For example: strings 110, 0110 and 010100 are not alternating, while 101 and 01 are alternating strings.
Input
T (1 ≤ T ≤ 128)
The first line of input contains that represents the number of test cases.
N
The first line of each test case contains two integers:
and K (1 ≤ K ≤ N ≤ 100,000) N
, where is the length
of the string and Kis the maximum length of a resulting string.
N
The next line contains a string of bits (0 or 1).
Output
For each test case, print a single line with the minimum number of cuts.