Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de...

20
Escola de Inverno da Maratona SBC de Programa¸ ao 2019 E s c o l a d e I n v e r n o U F R G S 2 0 1 9 Caderno de Problemas 1 Realização Apoio

Transcript of Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de...

Page 1: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

Escola de Invernoda Maratona SBC de Programacao

2019

Escola de Inverno UFRGS 2019

Caderno de Problemas 1

Realização

Apoio

Page 2: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

2

Page 3: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

A – Maximum Element In A Stack10s per test, 256MB

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Problem A. Maximum Element In A Stack

Time limit: 10 secondsAs an ACM-ICPC newbie, Aishah is learning data structures in computer science. She has already known that astack, as a data structure, can serve as a collection of elements with two operations:

• push, which inserts an element to the collection, and

• pop, which deletes the most recently inserted element that has not yet deleted.

Now, Aishah hopes a more intelligent stack which can display the maximum element in the stack dynamically.Please write a program to help her accomplish this goal and go through a test with several operations.

Aishah assumes that the stack is empty at first. Your program will output the maximum element in the stack aftereach operation. If at some point the stack is empty, the output should be zero.

InputThe input contains several test cases, and the first line is a positive integer T indicating the number of test caseswhich is up to 50.

To avoid unconcerned time consuming in reading data, each test case is described by seven integersn (1 ≤ n ≤ 5 × 106), p, q, m (1 ≤ p, q,m ≤ 109), SA, SB and SC (104 ≤ SA, SB, SC ≤ 106). The integern is the number of operations, and your program should generate all operations using the following code in C++.

1 int n, p, q, m;2 unsigned int SA, SB, SC;3 unsigned int rng61(){4 SA ^= SA << 16;5 SA ^= SA >> 5;6 SA ^= SA << 1;7 unsigned int t = SA;8 SA = SB;9 SB = SC;10 SC ^= t ^ SA;11 return SC;12 }13 void gen(){14 scanf("%d%d%d%d%u%u%u", &n, &p, &q, &m, &SA, &SB, &SC);15 for(int i = 1; i <= n; i++){16 if(rng61() % (p + q) < p)17 PUSH(rng61() % m + 1);18 else19 POP();20 }21 }

The procedure PUSH(v) used in the code inserts a new element with value v into the stack and the procedure POP()pops the topmost element in the stack or does nothing if the stack is empty.

OutputFor each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and y isequal to

n⊕i=1

(i · ai) where ai is the answer after the i-th operation and ⊕ means bitwise xor.

Page 1 of 17

3

Page 4: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Samplestandard input standard output

24 1 1 4 23333 66666 2333334 2 1 4 23333 66666 233333

Case #1: 19Case #2: 1

HintThe first test case in the sample input has 4 operations:

• POP();

• POP();

• PUSH(1);

• PUSH(4).

The second test case also has 4 operations:

• PUSH(2);

• POP();

• PUSH(1);

• POP().

Page 2 of 17

4

Page 5: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

B – Rolling The Polygon10s per test, 256MB

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Problem B. Rolling The Polygon

Time limit: 10 secondsBahiyyah has a convex polygon with n vertices P0, P1, · · · , Pn−1 in the counterclockwise order. Two vertices withconsecutive indexes are adjacent, and besides, P0 and Pn−1 are adjacent. She also assigns a point Q inside thepolygon which may appear on the border.

Now, Bahiyyah decides to roll the polygon along a straight line and calculate the length of the trajectory (or track)of point Q.

To help clarify, we suppose Pn = P0, Pn+1 = P1 and assume the edge between P0 and P1 is lying on the line at first.At that point when the edge between Pi−1 and Pi lies on the line, Bahiyyah rolls the polygon forward rotating thepolygon along the vertex Pi until the next edge (which is between Pi and Pi+1) meets the line. She will stop therolling when the edge between Pn and Pn+1 (which is same as the edge between P0 and P1) meets the line again.

InputThe input contains several test cases, and the first line is a positive integer T indicating the number of test caseswhich is up to 50.

For each test case, the first line contains an integer n (3 ≤ n ≤ 50) indicating the number of vertices of the givenconvex polygon. Following n lines describe vertices of the polygon in the counterclockwise order. The i-th lineof them contains two integers xi−1 and yi−1, which are the coordinates of point Pi−1. The last line contains twointegers xQ and yQ, which are the coordinates of point Q.

We guarantee that all coordinates are in the range of −103 to 103, and point Q is located inside the polygon orlies on its border.

OutputFor each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and yis the length of the trajectory of the point Q rounded to 3 places. We guarantee that 4-th place after the decimalpoint in the precise answer would not be 4 or 5.

Page 3 of 17

5

Page 6: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Samplestandard input standard output

440 02 02 20 21 130 02 11 21 150 01 02 21 3-1 20 060 03 04 12 21 2-1 11 0

Case #1: 8.886Case #2: 7.318Case #3: 12.102Case #4: 14.537

HintThe following figure is the the trajectory of the point Q in the first sample test case.

Page 4 of 17

6

Page 7: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

C – Caesar Cipher10s per test, 256MB

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Problem C. Caesar Cipher

Time limit: 10 secondsIn cryptography, a Caesar cipher, also known as the shift cipher, is one of the most straightforward and most widelyknown encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replacedby a letter some fixed number of positions up (or down) the alphabet.

For example, with the right shift of 19, A would be replaced by T, B would be replaced by U, and so on. A fullexhaustive list is as follows:

The plaintext : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z;

The ciphertext : T U V W X Y Z A B C D E F G H I J K L M N O P Q R S.

Now you have a plaintext and its ciphertext encrypted by a Caesar Cipher. You also have another ciphertextencrypted by the same method and are asked to decrypt it.

InputThe input contains several test cases, and the first line is a positive integer T indicating the number of test caseswhich is up to 50.

For each test case, the first line contains two integers n and m (1 ≤ n,m ≤ 50) indicating the length of the firsttwo texts (a plaintext and its ciphertext) and the length of the third text which will be given. Each of the secondline and the third line contains a string only with capital letters of length n, indicating a given plaintext and itsciphertext respectively. The fourth line gives another ciphertext only with capital letters of length m.

We guarantee that the pair of given plaintext (in the second line) and ciphertext (in the third line) is unambiguouswith a certain Caesar Cipher.

OutputFor each test case, output a line containing Case #x: T, where x is the test case number starting from 1, and T isthe plaintext of the ciphertext given in the fourth line.

Samplestandard input standard output

17 7ACMICPCCEOKEREPKPIZKC

Case #1: NINGXIA

Page 5 of 17

7

Page 8: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

D – Moving On10s per test, 256MB

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Problem F. Moving On

Time limit: 10 secondsFirdaws and Fatinah are living in a country with n cities, numbered from 1 to n. Each city has a risk of kidnappingor robbery.

Firdaws’s home locates in the city u, and Fatinah’s home locates in the city v. Now you are asked to find theshortest path from the city u to the city v that does not pass through any other city with the risk of kidnappingor robbery higher than w, a threshold given by Firdaws.

InputThe input contains several test cases, and the first line is a positive integer T indicating the number of test caseswhich is up to 50.

For each test case, the first line contains two integers n (1 ≤ n ≤ 200) which is the number of cities, andq (1 ≤ q ≤ 2 × 104) which is the number of queries that will be given. The second line contains n integersr1, r2, · · · , rn indicating the risk of kidnapping or robbery in the city 1 to n respectively. Each of the following n

lines contains n integers, the j-th one in the i-th line of which, denoted by di,j , is the distance from the city i tothe city j.

Each of the following q lines gives an independent query with three integers u, v and w, which are described asabove.

We guarantee that 1 ≤ ri ≤ 105, 1 ≤ di,j ≤ 105 (i = j), di,i = 0 and di,j = dj,i. Besides, each query satisfies1 ≤ u, v ≤ n and 1 ≤ w ≤ 105.

OutputFor each test case, output a line containing Case #x: at first, where x is the test case number starting from 1.Each of the following q lines contains an integer indicating the length of the shortest path of the correspondingquery.

Samplestandard input standard output

13 61 2 30 1 31 0 13 1 01 1 11 2 11 3 11 1 21 2 21 3 2

Case #1:013012

Page 9 of 17

8

Page 9: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

E – Factories10s per test, 256MB

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Problem G. Factories

Time limit: 10 secondsByteland has n cities numbered from 1 to n, and n − 1 bi-directional roads connecting them. For each pair ofcities, the residents can arrive one from another one through these roads (which also means the road network inByteland is a tree).

Ghaliyah, the queen of the land, has decided to construct k new factories. To avoid contamination, she requiresthat a factory can locate at a city with only one road (which also means this city is a leaf in the road network).Besides, a city can only have one factory.

You, as the royal designer, are appointed to arrange the construction and meanwhile, minimize the sum of distancesbetween every two factories.

InputThe input contains several test cases, and the first line is a positive integer T indicating the number of test caseswhich is up to 103.

For each test case, the first line contains two integers n (2 ≤ n ≤ 105) and k (1 ≤ k ≤ 100) indicating the numberof cities in Byteland and the number of new factories which are asked to construct.

Each of the following n− 1 lines contains three integers u, v (1 ≤ u, v ≤ n) and w (1 ≤ w ≤ 105) which describes aroad between the city u and the city v of length w.

We guarantee that the number of leaves in the road network is no smaller than k, and the sum of n in all test casesis up to 106.

OutputFor each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and y isthe minimum sum of distances between every two factories.

Samplestandard input standard output

24 21 2 21 3 31 4 44 31 2 21 3 31 4 4

Case #1: 5Case #2: 18

Page 10 of 17

9

Page 10: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

F – Fight Against Monsters10s per test, 256MB

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Problem H. Fight Against Monsters

Time limit: 10 secondsIt is my great honour to introduce myself to you here. My name is Aloysius Benjy Cobweb Dartagnan Egbert FelixGaspar Humbert Ignatius Jayden Kasper Leroy Maximilian. As a storyteller, today I decide to tell you and othersa story about the hero Huriyyah, and the monsters.

Once upon a time, citizens in the city were suffering from n powerful monsters. They ate small children who wentout alone and even killed innocent persons. Before the hero appeared, the apprehension had overwhelmed thepeople for several decades. For the good of these unfortunate citizens, Huriyyah set off to the forest which was themain lair of monsters and fought with n fierce and cruel monsters. The health point of the i-th monster was HPi,and its attack value was ATKi.

They fought in a cave through a turn-based battle. During each second, the hero Huriyyah was attacked bymonsters at first, and the damage was the sum of attack values of all alive monsters. Then he selected a monsterand attacked it. The monster would suffer the damage of k (its health point would decrease by k) which wasthe times of attacks it had been came under. That is to say, for each monster, the damage of the first time thatHuriyyah attacked it was 1, and the damage of Huriyyah’s second attack to this monster was 2, the third time tothis monster was 3, and so on. If at some time, the health point of a monster was less than or equal to zero, itdied. The hero won if all monsters were killed.

Now, my smart audience, can you calculate the minimum amount of total damages our hero should suffer beforehe won the battle?

InputThe input contains several test cases, and the first line is a positive integer T indicating the number of test caseswhich is up to 103.

For each test case, the first line contains an integers n (1 ≤ n ≤ 105) which is the number of monsters. The i-th lineof the following n lines contains two integers HPi and ATKi (1 ≤ HPi, ATKi ≤ 105) which describe a monster.

We guarantee that the sum of n in all test cases is up to 106.

OutputFor each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and y isthe minimum amount of total damages the hero should suffer.

Samplestandard input standard output

231 12 23 333 12 21 3

Case #1: 19Case #2: 14

Page 11 of 17

10

Page 11: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

G – Bubblesort10s per test, 256MB

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Problem I. Bubble Sort

Time limit: 10 secondsBubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pairof adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until noswaps are needed, which indicates that the list is sorted.

Izdihar is an ACM-ICPC master and provides a pseudocode implementation about the bubble sort for you. Thealgorithm for a list of sortable items A can be expressed as (1-based array):

1: function BubbleSort(A)2: n← the length of A3: k ← n− 1 ▷ note the initial value of k might be replaced by a given number4: for step = 1 to k do ▷ passing through the list k times5: for i = 2 to n do6: if A[i− 1] > A[i] then7: swap A[i− 1] and A[i]

She says a permutation of 1 to n is almost sorted if the length of its longest increasing subsequence is at least n−1.

You are asked to count the number of permutations of 1 to n which, after k times passing through the list in thebubble sort, would become an almost sorted permutation.

InputThe input contains several test cases, and the first line is a positive T indicating the number of test cases which isup to 5000.

For each test case, a line contains three integers n, k (1 ≤ n, k ≤ 50) which are described as above, andq (108 ≤ q ≤ 109) which is a prime number for the output.

OutputFor each test case, output a line containing Case #x: y, where x is the test case number starting from 1. And yis the remainder of the number of permutations which meet the requirement, divided by q.

Samplestandard input standard output

55 1 9982443535 2 9982443535 3 9982443535 4 9982443535 5 998244353

Case #1: 74Case #2: 114Case #3: 120Case #4: 120Case #5: 120

Page 12 of 17

11

Page 12: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

H – Nested Triangles10s per test, 256MB

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Problem J. Nested Triangles

Time limit: 10 secondsJamilah is obsessed with nested triangles which share a common edge. Now she selects two points P and Q inthe plane and calls them pivots. She also provides several other points A1, A2, · · · , An−1 and An, none of which islying on the line passing through the points P and Q.

As the one with the same interest, you are asked to find the largest size of a group of nested triangles, and a feasiblesolution of the largest group with the smallest lexicographical order.

A group of nested triangles, with pivots P and Q, is a list of selected points provided by Jamilah, denoted byAv1 , Av2 , · · · , Avk

, such that for any i ≥ 2 the point Avi is located inside the triangle PQAvi−1 , excluding theborder.

The solution v1, v2, · · · , vk is the one with the smallest lexicographical order if, for any other feasible solutionu1, u2, · · · , uk, v1 < u1 or there exists an integer i (1 ≤ i < k) such that v1 = u1, v2 = u2, · · · , vi = ui butvi+1 < ui+1.

InputThe input contains several test cases, and the first line is a positive integer T indicating the number of test caseswhich is up to 1000.

For each test case, the first line contains four integers xP , yP , xQ, yQ, which are the coordinates of points P and Q

respectively. The second line contains an integer n (1 ≤ n ≤ 105), which is the number of other points provided byJamilah. Each of the following n lines contains two integers, which in the i-th line are the coordinates of point Ai.

We guarantee that all points in a test case are distinct, all coordinates lie in the range of −109 to 109, and the sumof n in all test cases is up to 106.

OutputFor each test case, output a line containing Case #x: y at first, where x is the test case number starting from 1,and y is the size of the largest group of nested triangles. Each of the following y lines contains an integer, whichin the i-th line is the integer vi.

Page 13 of 17

12

Page 13: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Samplestandard input standard output

30 0 10 065 15 25 36 46 56 60 0 10 1091 62 34 76 88 29 37 62 42 70 10 10 090 00 22 00 44 00 66 00 88 0

Case #1: 6654321Case #2: 3132Case #3: 11

Page 14 of 17

13

Page 14: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

I – Continuous Intervals10s per test, 256MB

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Problem L. Continuous Intervals

Time limit: 10 secondsLamis is a smart girl. She is interested in problems about sequences and their intervals.

Here she shows you a sequence of length n with positive integers, denoted by a1, a2, a3, · · · , an. She is amazed atthose intervals, which is a consecutive subsequence of a1, a2, · · · , an, with several continuous numbers, and namesthem continuous intervals.

More precisely, consider a interval al, al+1, · · · , ar−1, ar for instance where 1 ≤ l ≤ r ≤ n. If, after sorting theinterval, the difference of any two neighbouring items is less than or equal to 1, the interval will be considered ascontinuous.

As her best friends, you came from far and wide and travelled thousands of miles to Ningxia, to help her count thenumber of continuous intervals of the sequence.

InputThe input contains several test cases, and the first line is a positive integer T indicating the number of test caseswhich is up to 1000.

For each test case, the first line contains an integer n (1 ≤ n ≤ 105) which is the length of the given se-quence. The second line contains n integers describing all items of the sequence, where the i-th one is denoted byai (1 ≤ ai ≤ 109).

We guarantee that the sum of n in all test cases is up to 106.

OutputFor each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and y isthe number of continuous intervals in this test case.

Samplestandard input standard output

241 2 1 241 3 2 4

Case #1: 10Case #2: 8

Page 16 of 17

14

Page 15: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

J – Acyclic Orientation10s per test, 256MB

The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest

Problem M. Acyclic Orientation

Time limit: 10 secondsThe chromatic polynomial is a graph polynomial. It counts the number of graph colourings as a function of thenumber of colours.

Precisely speaking, for an undirected graph G, χG(k) counts the number of its vertex k-colourings. Here a vertexk-colouring of G is an assignment of one of k possible colours to each vertex of G such that no two adjacent verticesreceive the same colour. There is a unique polynomial χG(x) which evaluated at any integer k ≥ 0 coincides withχG(k). If G has n vertices, the chromatic polynomial χG(x) is of degree exactly n with integer coefficients.

Malak can use the chromatic polynomial to solve several interesting problems, including the question about acyclicorientation. In graph theory, an acyclic orientation of an undirected graph is an assignment of a direction to eachedge (an orientation) that does not form any directed cycle and therefore makes it into a directed acyclic graph(DAG). Every graph G has exactly |χG(−1)| different acyclic orientations, so in this sense, an acyclic orientationcan be interpreted as a colouring with −1 colours.

Now Malak shows you a complete bipartite graph with n and m vertices in its two sets respectively, denoted byKn,m. You are asked to count the number of different acyclic orientations of Kn,m.

InputThe input contains several test cases, and the first line is a positive integer T indicating the number of test caseswhich is up to 600.

Each test case is described by three integers n,m (1 ≤ n,m ≤ 60000) which are the size of the given completebipartite graph and q (108 ≤ q ≤ 109) which is a prime number for the output.

We guarantee that no more than 60 test cases satisfy n > 60 or m > 60, and no more than 6 test cases satisfyn > 600 or m > 600.

OutputFor each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and y isthe remainder of the number of different acyclic orientations divided by q.

Samplestandard input standard output

41 1 9982443531 2 9982443532 1 9982443532 2 998244353

Case #1: 2Case #2: 4Case #3: 4Case #4: 14

Page 17 of 17

15

Page 16: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

K – Game of Stones II5s, 1536MB

Status (/status/TAP2015J/) Ranking (/ranks/TAP2015J/)

Problems (/problems) /  classical (/problems/classical) /  Game of stones II

(/)

PROBLEMS (/problems) STATUS (/status) RANKS (/ranks)

DISCUSS (http://spoj.com/forum) CONTESTS (/contests) sign in (/login)

TAP2015J - Game of stones II

[Due to SPOJ restrictions, this problem has been modified with respect to theoriginal version used in the Argentinian Programming Tournament of 2015 in orderto have multiple test cases per input file. The original version of this problem (inSpanish) can be found at http://torneoprogramacion.com.ar/wp-content/uploads/2015/09/pruebaTAP2015.pdf (http://torneoprogramacion.com.ar/wp-content/uploads/2015/09/pruebaTAP2015.pdf) ]

Jaimito didn't waste any time since he was o�ered his stones in 2013. He no longerspends his time playing with his mother Jimena to games in which his victory isguaranteed, for he explored many stone game variants, as every child should. He evenbecame very good at some of these games, and took part in various championshipsobtaining excellent results.

For a few months now, he has been particularly interested in a game of stones calledTArros con Piedras (TAP). In this game there are N jars with stones inside, and two playerstake turns to play. In his turn, each player should take one stone from one jar, two stonesfrom another jar, and three stones from a third one, and so on until taking N stones fromsome jar, so that the player takes stones from every jar in a single turn. The gamecontinues in this way until one of the players cannot take stones from the jars in a validway in his turn. Said player loses the match, the other player being the winner.

For example, consider a match with N = 3 jars with one of them initially having P  = 3stones, another one having P  = 4 stones and the third one having P  = 10 stones. In thismatch the player who starts playing has a winning strategy, as he can take one stone fromthe jar originally having ten stones, two stones from the jar which had three stones, and�nally three stones from the jar that initially had four stones. He would then leave the jarswith P  = 1, P  = 1 and P  = 9 stones, so that the second player cannot take stones fromthe jars in a valid way.

Jaimito is taking part in a TAP championship, and he has reached the �nals, where he willface Jacinta. Both of them are expert players, so they make no mistakes when they play.Jaimito has told his mother everything about the match, i.e. the number N of jars and howmany stones each of them will start with. Jimena knows Jacinta will start playing, and shewould like to know if her son will win the championship, but she can't �gure it outbecause she is too nervous to think properly. Can you help her?

InputThere are multiple test cases in the input �le. For each test case, the �rst line contains aninteger N representing the number of jars in the match (1 ≤ N ≤ 10 ). The second linecontains N integers P  representing the number of stones in each jar before starting thematch (1 ≤ P ≤ 10 for i = 1, 2, ..., N).

OutputFor each test case, print a line containing a charactner indicating if Jaimito will win the�nals or not. The printed character should be an 'S' if Jaimito is to win, otherwise it shouldbe an 'N'.

Example

Input:33 4 10210 3

Output:NS

no tags

1

2 3

1 2 3

5

i

i9

HOTJAR

See howyourvisitorsarereallyusingyourwebsite.

TRY IT FOR FREE(//srv.buysellads.com/ads/click/x/GTND42QNCVADVKJUCKY4YKQMCY7DV2JLC67DVZ3JCWSD623MFTAD453KC6BIKKJYCYBDVK3EHJNCLSIZ?segment=placement:demo;)

HIDE A D • A D V I A BU YSELL AD S ( HT TP S: // W W W. B U YSELL A DS. COM/ ? U TM_SOU RCE= SPOJ-COM-F LEX-BA R& U TM_MED IU M= A D _VI A _L INK & U TM _CA MPAIG N= IN_U NIT& U TM_TER M= F LEXBA R )

1 of 2

16

Page 17: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

L – Happiness for all3s, 1536MB

The separatist state of Nlogonia has N cities numbered 1 through N. Some pairs of citiesare connected by highways, so that if there is a highway between city A and city B it ispossible to travel from A to B or from B to A. We say that a citizen from city i can visitanother from city j if there exists a sequence of di�erent cities c , c , ..., c  withm ≥ 2 such that c  = i, c  = j and there is a highway between cities c  and c  for k = 1, 2,..., m-1.

The people of Nlogonia are very sociable, so they have friends in all the cities they canvisit. However, they are also a bit lazy, so that they are not happy unless they can visiteach of their friends by taking exactly one highway directly from their city to their friend's.

To avoid Nlogonia's scission, the queen has decided to perform a number ofinfrastructure works in order to make all of its citizens happy. One possibility consists inbuilding new highways between the cities of Nlogonia, incurring in a cost of R per newhighway built. Because building many highways can be very expensive, another possibilityis to build stadiums in some of Nlogonia's cities. The building of a stadium costs E, andimmediately makes all the citizens happy in the city where it is built. Then again, youshould know that Nlogonia's inhabitants are furthermore somewhat jealous, so that theyshall never be happy if there is no stadium in their own city, but there is one in some oftheir friend's cities. Can you help the queen calculate the minimum cost of theconstruction work necessary to make everyone in Nlogonia happy?

InputThere are multiple test cases in the input �le. For each test case, the �rst line containsfour integers N, M, R and E. The number N represents the number of cities in Nlogonia (2≤ N ≤ 1000), M represents the original number of highways (1 ≤ M ≤ 10 ), whereas R and Erepresent the cost of building a highway and a stadium, respectively (1 ≤ R, E ≤ 1000).Each of the following M lines describes a di�erent highway using two integers A and B,representing the cities connected by said highway (1 ≤ A, B ≤ N with A ≠ B).

OutputFor each test case, print one line containing an integer representing the minimum cost ofthe construction work necessary to make everyone in Nlogonia happy.

Example

Input:9 6 11 121 23 24 55 66 79 7

Output:71

1 2 m

1 m k k+1

5

1 of 1

17

Page 18: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

M – Kimetto Kipsang and Kipchoge14s, 1536MB

Kenya is the birthplace of some of the best long-distance runners of all times. Indeed,eight of the most recent ten best times in the traditional 41.195 km of the marathon havebeen set by runners from that country. Dennis Kimetto, Wilson Kipsang and EliudKipchoge are three such runners, and they want to beat their discipline's world recordonce more tomorrow September 27, when they compete in the 42 edition of the BerlinMarathon.

Kimetto, Kipsang and Kipchoge are good friends, and they like to train together runningby the Tana river in order to appreciate the beautiful trees that grow there. There are Ntrees by the river, which we will number from 1 to N. The i-th tree is of the species S  andstands at a distance of i meters from the mouth of the river. Our three runners areespecially motivated by the sight of many trees of di�erent species. For this reason, eachtraining day they choose a tree with number K from 1 to N, and then run from the K-thtree following the river, i.e. in the direction of trees K-1, K-2 and so on, stopping only whenthey see a tree of some species they have already seen that day, or when they reach themouth of the river, whichever comes �rst.

For example, if there are N = 4 trees of species S  = 1, S  = 2, S  = 1 and S  = 3, when theychoose K = 4 the training consists in running 3 meters, from tree number 4 up to treenumber 1 (where they stop because this tree is of the same species as tree number 3).However, if they chose K = 2 they would run two meters up to the mouth of the river,where they would stop even without having seen two trees of the same species as theywent.

Long-distance running requires decades of training, and in this time it is common forsome trees to fall during storms. When this happens, the fallen tree is immediatelyreplaced by another one, not necessarily of the same species. Kimetto, Kipsang andKipchoge keep a diary where they take note of all the information relevant to theirtraining. In particular, they know the species of all trees, and which number they chose tostart running each day of training. Can you help them calculate how much they ran eachtraining day?

InputThere are multiple test cases in the input �le. For each test case, the �rst line contains twointegers N and R, representing respectively the number of trees by the river and thenumber of entries in the training diary (1 ≤ N, R ≤ 5 × 10 ). The second line contains Nintegers S  indicating the species of the i-th tree when the training began (1 ≤ S ≤ 10 for i= 1, 2, ..., N). Each of the following R lines contains the description of an entry in thetraining diary, in chronological order. This description starts with a character which can bea 'C' if the entry corresponds to a fallen tree or an 'E' if it corresponds to a training day.The entries for fallen trees contain two integers K and S after the 'C', indicating that theK-th tree fell and was replaced by another tree of species S (1 ≤ K ≤ N and 1 ≤ S ≤ 10 ). Theentries for training days contain an integer K after the 'E', indicating that the runnersbegan a training day by running from the K-th tree (1 ≤ K ≤ N). There will always be atleast one entry for a training day in each test case.

OutputFor each test case, print one line for each entry corresponding to a training day, indicatingthe number of meters Kimetto, Kipsang and Kipchoge ran during that day.

nd

i

1 2 3 4

4

i i6

6

1 of 2

18

Page 19: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio

Example

Input:4 21 2 1 3E 4E 210 101 2 3 4 5 6 7 8 9 10E 1E 2E 3E 4E 5E 6E 7E 8E 9E 105 71 2 3 4 5E 3E 5C 3 1E 4C 2 5E 3E 5

Output:321234567891035323

2 of 219

Page 20: Caderno de Problemas 1 - INFEscola de Inverno da Maratona SBC de Programa˘c~ao 2019 Escola de Inverno UFRGS 2019 Caderno de Problemas 1 Realização Apoio