0% found this document useful (0 votes)
29 views

Acm Amman Collegiate Programming Contest en

Uploaded by

yamauchis184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Acm Amman Collegiate Programming Contest en

Uploaded by

yamauchis184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ACM Amman Collegiate Programming Contest

Problem Set

​of July, 2015


4th​
A. Who Is The Winner?

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.

It is guaranteed that there is only one winner in the contest.

Output

For each test case, print a single line with the name of the team who won.

Sample Input Sample Output


2 endagorion
3 bayashout
tourist 13 567
petr 13 600
endagorion 14 419
2
tourist 7 512
bayashout ​
7 477
B. Rock-Paper-Scissors

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:

­ Choosing Rock wins over a player choosing scissors.


­ Choosing Scissors wins over a player choosing Paper.
­ Choosing Paper wins over a player choosing Rock.

In all other cases, the player doesn’t get any points.

Bahosain and his friend Bayashout played ​ N​r​


ounds of this game. Unlike Bayashout, Bahosain was too lazy to
X Y Z​
decide what to play next for each round, so before starting to play, he chose three integers ​ ​
such that
X+Y+Z = N​ ​
and ​X, Y, Z ≥ 0​ X​
, and then played Rock for the first ​ Y​
rounds, Paper for the next ​rounds, and
Z​
Scissors for the last ​rounds.

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.

Sample Input Sample Output


4 3
3 1
RPS 1
1 5
R
5
PPRSR
5
RPSPR
C. Street Lamps

Bahosain is walking in a street of ​ N​blocks. 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.

Sample Input Sample Output


3 2
6 0
...... 1
3
*.*
8
.*.....*
D. Alternating Strings

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.

A string with one character is not an alternating string.

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: ​ ​
and​K (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.

Sample Input Sample Output


4 1
63 3
111000 0
52 2
11010
33
110
33
101
E. Epic Professor

Dr. Bahosain works as a professor of Computer Science at HU (Hadramout University).

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.

Sample Input Sample Output


2 3
5 4
0 21 83 45 64
7
99 50 46 47 48 49 98
F. Travelling Salesman

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.

Each of the following ​M​ X Y C (1 ≤ X, Y ≤ N)(X ≠ Y)(1 ≤ C ≤ 100,000)​


lines contains three integers: ​ , where
C​is the length in kilometers between city ​X​ Y​
and city ​ . Roads can be used in both ways.

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.

Sample Input Sample Output


2 4
67 2
123
233
315
344
454
463
655

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.

Whenever Bahosain has to pay ​ S​​ maximum ​


pennies for the taxi driver, he tries to choose the ​ number of coin
pieces to pay. The driver will accept receiving more than ​S​pennies only if he can’t remove one or more of the
S​
given coins and still has ​or more pennies.

For example, if Bahosain uses the coins of the following values: ​


2​
,​
7​
and ​
5​to pay ​
11​
pennies, the taxi driver
will not accept this because the coin of value 2 can be removed. On the other hand, when Bahosain uses coins
of ​
7​and ​5​
to pay ​
11 ​pennies, the driver will accept it.

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.

Sample Input Sample Output


2 3
59 6
41354
7 37
7 5 8 8 5 10 4

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

An edge in an undirected graph is a ​


bridge​
if after removing it the graph will be disconnected.

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 ​.

It is guaranteed that each pair of nodes is connected by at most one edge.

Test cases are separated by a blank line.

Output

For each test case, print a single line with the minimum possible number of bridges after adding one edge.

Sample Input Sample Output


2 1
77 0
12
23
31
34
45
46
67

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’​
.

For example, applying the operation on the string ​


905​
will change it to ​
016​
.

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.

Each test case contains a string of no more than ​


250 ​
digits ​
(0 - 9)​
.

Each string contains at least two different digits.

Output

K​
For each test case print a single line with the maximum possible ​.

Sample Input Sample Output


3 1
04 2
651 3
0552

Note

In the last test case, when ​K​=​


3​, Bahosain can perform the operation on the substring ​
552 ​
five times to get
the string ​
0007​ , then perform the operation again on the substring ​
000​
seven times to get ​
7777​ .
J. Candy

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”.

Sample Input Sample Output


3 YES
36 NO
5 15 10 NO
222334
44
5567
5466
56
86755
121312
K. Runtime Error

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​.

The chosen numbers must have different indices in the array.

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

For each test case, if there is no solution, print ​


-1​on a single line. Otherwise print a single line with two
space-separated integers ​ X Y (X ≤ Y)​ , where ​X​ Y​
and ​ are two numbers from the given array and ​ X​ ×​
Y​=​
K​
.

X​
If there is more than one possible solution, print the one with the minimum ​.

Sample Input Sample Output


4 26
6 12 -1
362429 3 12
21 1 12
12
4 36
12 18 3 36
4 12
1 2 6 12
L. Alternating Strings II

This problem is the same as problem D but with different constraints!

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.

A string with one character is not an alternating string.

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 ​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.

Sample Input Sample Output


4 1
63 3
111000 0
52 2
11010
33
110
33
101

You might also like