国际大学生程序设计竞赛2019年赛题

国际大学生程序设计竞赛2019年赛题
国际大学生程序设计竞赛2019年赛题

Problem A

Azulejos

Time limit:10

seconds

Azulejo in the cathedral of Porto.Source:Wikimedia Commons

Ceramic artists Maria and Jo?o are opening a small azulejo store in Porto.Azule-jos are the beautiful ceramic tiles for which Portugal is famous.Maria and Jo?o want to create an attractive window display,but,due to limited space in their shop,they must arrange their tile samples in two rows on a single shelf.Each of Jo?o’s tiles has exactly one of Maria’s tiles in front of it and each of Maria’s tiles has exactly one of Jo?o’s tiles behind it.These hand-crafted tiles are of many different sizes,and it is important that each tile in the back row is taller than the tile in front of it so that both are visible to passers-by.For the convenience of shoppers,tiles in each row are arranged in non-decreasing order of price from left to right.Tiles of the same price may be arranged in any order subject to the visibility condition stated above.

Your task is to ?nd an ordering of the tiles in each row that satis?es these constraints,or determine that no such ordering exists.

Input

The ?rst line of input contains an integer n (1≤n ≤5·105),the number of tiles in each row.The next four lines contain n integers each;the ?rst pair of lines represents the back row of tiles and the second pair of lines represents the front row.Tiles in each row are numbered from 1to n according to their ordering in the input.The ?rst line in each pair contains n integers p 1,...,p n (1≤p i ≤109for each i ),where p i is the price of tile number i in that row.The second line in each pair contains n integers h 1,...,h n (1≤h i ≤109for each i ),where h i is the height of tile number i in that row.

Output

If there is a valid ordering,output it as two lines of n integers,each consisting of a permutation of the tile numbers from 1to n .The ?rst line represents the ordering of the tiles in the back row and the second represents the ordering of the tiles in the front row.If more than one pair of permutations satis?es the constraints,any such pair will be accepted.If no ordering exists,output impossible .Sample Input 1Sample Output 143212234321212213

32414213

Sample Input 2Sample Output 2212232821

impossible

This page is intentionally left blank.

Problem B

Beautiful Bridges

Time limit:10

seconds

Example of a Roman arch bridge.Source:Wikimedia Commons

What connects us all?Well,it is often bridges.Since an-cient times,people have been building bridges for roads,for trains,for pedestrians,and as aqueducts to transport water.It is humanity’s way of not taking inconvenient geography for an answer.

The company Arch Bridges Construction (ABC)specializes in—you guessed it—the construction of arch bridges.This classical style of bridge is supported by pillars that extend from the ground below the bridge.Arches between pillars dis-tribute the bridge’s weight onto the adjacent pillars.

The bridges built by ABC often have pillars spaced at irregular intervals.For aesthetic reasons,ABC’s bridges always have semicircular arches,as illustrated in Figure B.1.However,while a bridge arch can touch the ground,it cannot extend below the ground.This makes some pillar placements impossible.

r

d

(a)Consecutive pillars at distance d are connected by a semicircular arch with radius r =d/2

.

(b)An invalid pillar placement (arches cannot extend below

ground).(c)A bridge corresponding to Sample Input 1.

Figure B.1:Bridge examples.

Given a ground pro?le and a desired bridge height h ,there are usually many ways of building an arch bridge.We model the ground pro?le as a piecewise-linear function described by n key points (x 1,y 1),(x 2,y 2),...,(x n ,y n ),where the x -coordinate of a point is the position along the bridge,and the y -coordinate is the elevation of the ground above sea level at this position along the bridge.The ?rst and last pillars must be built at the ?rst and last key points,and any intermediate pillars can be built only at these key points.The cost of a bridge is the cost of its pillars (which is proportional to their heights)plus the cost of its arches (which is proportional to the amount of material used).So a bridge with k pillars of heights h 1,...,h k that are separated by horizontal distances d 1,...,d k ?1has a total cost of

α·

k i =1

h i +β·

k ?1 i =1

d 2i

for some given constants αand β.ABC wants to construct each bridge at the lowest possible cost.

Input

The?rst line of input contains four integers n,h,α,andβ,where n(2≤n≤104)is the number of points describing the ground pro?le,h(1≤h≤105)is the desired height of the bridge above sea level, andα,β(1≤α,β≤104)are the cost factors as described earlier.Then follow n lines,the i th of which contains two integers x i,y i(0≤x1

Output

Output the minimum cost of building a bridge from horizontal position x1to x n at height h above sea level.If it is impossible to build any such bridge,output impossible.

Sample Input1Sample Output1

6460

560182

00

2020

3010

5030

7020

Sample Input2Sample Output2

impossible

41011

00

19

99

100

Problem C

Checks Post Facto

Time limit:1second

Your university’s board game club just hosted a Checkers tournament,and you were assigned to take notes on the games.Unfortunately,while walking home,you dropped all of your papers into a puddle! Disaster!Much of what you wrote is now unreadable;all you have left are some lists of moves played in the middle of various games.Is there some way you can reconstruct what happened in those games? You had better?x things fast,or they will demote you to Tic-Tac-Toe recordkeeper!

Checkers(or English Draughts)is a well-known board game with simple rules.It is played on the dark squares of an8×8checkerboard.There are two players,Black and White,who alternate turns moving their pieces(all of Black’s pieces are black and all of White’s pieces are white).Each piece occupies a single dark square,and can be either a normal man or a promoted king.A turn consists of choosing one piece and moving it in one of two ways:

1.Shifting it diagonally to an unoccupied adjacent dark square,as shown in Figure C.1(a).This is

called a simple move.If the piece is a man,it can move only in the two diagonal directions towards the opposing side of the board(towards the bottom for Black,the top for White).If the piece is a king,it can move in all four diagonal directions.

2.Jumping over an adjacent enemy piece to an unoccupied square immediately on the other side,

then removing(capturing)that piece.Men can jump only in the two directions described above, while kings can jump in all four.The player can then repeat this step,continuing to jump with the same piece as long as there are properly-positioned enemy pieces to capture.Such a sequence of one or more jumps is called a jump move.Figure C.1(b)shows a jump move comprising three jumps.

(a)Simple move

(b)Jump

move(c)Board numbering

Figure C.1:(a),(b)The?rst two moves in Sample Input1.The larger pieces are kings and

the smaller ones are men.Black sits at the top and White at the bottom.(c)Numbering

used in the input.

In Checkers,captures are forced moves.If a jump move is available at the start of a player’s turn,they must jump,and cannot stop jumping with that piece until it has no more possible jumps.They are free to choose which piece to jump with,and where,if there are multiple possibilities.In Figure C.1(b),Black could not have made any other move.

If a man reaches the farthest row from its player(that is,a black man reaches the bottom row or a white man reaches the top row),it is removed from the board and replaced by a king of the same color(it is said to be promoted),and the turn ends.A piece cannot be promoted and then jump backwards as a new king in the same turn.

Given a list of moves,?nd a setup of pieces such that the moves can be legally played in sequence starting from that setup.This setup may not have black men on the bottom row or white men on the top row,since they would have been promoted to kings already.You need only ensure that the rules above are obeyed;you do not need to ensure that this setup is reachable in a real game of Checkers.

Input

The?rst line of input contains a character c and an integer n,where c∈{B,W}indicates which player makes the?rst move(Black or White respectively)and n(1≤n≤100)is the number of moves in the list.Then follow n lines,each of which describes a move in the standard Checkers notation de?ned below.

The dark squares are identi?ed by numbers1–32,as shown in Figure C.1(c).A simple move from square a to square b is written as a-b.A jump move starting at a and jumping to b1,b2,...,b k is written as a x b1x b2x···x b k.

There is always a valid solution for the given set of moves.

Output

Output two boards side-by-side(separated by a space),giving the positions of all pieces on the board before(on the left)and after(on the right)the given https://www.360docs.net/doc/a55798638.html,e‘-’for light squares,‘.’for empty dark squares,lowercase‘b’and‘w’for black and white men,and uppercase‘B’and‘W’for black and white kings.If there is more than one valid solution,any one is acceptable.

Sample Input1Sample Output1

W3

21-17

13x22x31x24 19x28-b-.-.-.-b-.-.-. .-.-.-.-.-.-.-.--.-.-.-.-.-.-.-. B-.-w-.-.-.-w-.--.-.-W-.-.-.-.-. w-.-.-.-.-.-.-.--.-w-w-.-.-.-.-W .-.-.-.-.-.-.-.-

Sample Input2Sample Output2

B5

2-7

9x2

32-27

2x11x18 5-9-.-b-.-W-.-.-.-W b-b-.-.-.-.-.-.--w-.-.-.-b-.-.-. B-w-b-.-B-w-.-.--.-.-.-.-.-W-.-. .-.-.-.-.-.-.-.--.-.-.-.-.-.-B-. .-.-.-B-.-.-.-.-

Problem D

Circular DNA

Time limit:3seconds

You have an internship with a bioinformatics research group studying DNA.A single strand of DNA consists of many genes,which fall into different categories called gene types.Gene types are delimited by speci?c nucleotide sequences known as gene markers.Each gene type i has a unique start marker s i and a unique end marker e i.After many dirty jobs(growing bacteria,cell extraction,protein engineering, and so on),your research group can convert DNA into a form consisting of only the gene markers, removing all the genetic material lying between the markers.

Your research group came up with the interesting hypothesis that gene interpretation depends on whether the markers of some gene types form properly nested structures.To decide whether markers of gene type i form a proper nesting in a given sequence of markers w,one needs to consider the subsequence of w containing only the markers of gene type i(s i and e i),leaving none of them out.The following(and only the following)are considered to be properly nested structures:

?s i e i

?s i N e i,where N is a properly nested structure

?AB,where A and B are properly nested structures

Given your computing background,you were assigned to investigate this property,but there is one further complication.Your group is studying a speci?c type of DNA called circular DNA,which is DNA that forms a closed loop.To study nesting in circular DNA,it is necessary to cut the loop at some location,which results in a unique sequence of markers(the direction of reading is?xed by molecular properties).Whether a gene type i forms a proper nesting now also depends on where the circular DNA is cut.Your task is to?nd the cutting location that maximizes the number of gene types that form a properly nested structure.Figure D.1shows an example corresponding to Sample Input1.The indicated

nested.

cut results in the markers for gene type1being properly

Input

The?rst line of input contains an integer n(1≤n≤106),the length of the DNA.The next line contains the DNA sequence,that is,n markers.Each marker is a character c followed by an integer i, where c∈{s,e}speci?es whether it is a start or an end marker and i(1≤i≤106)is the gene type of the marker.The given DNA sequence has been obtained from the circular DNA by cutting at an arbitrary location.

Output

Output one line with two integers p and m,where p is the cutting position that maximizes the number of different gene types that form a proper nesting,and m is this maximum number of gene types.The DNA is cut just before the p th input marker(for instance,the cut shown in Figure D.1has p=3).If more than one cutting position yields the same maximum value of m,output the smallest p that does so. Sample Input1Sample Output1

9

31

e1e1s1e2s1s2e42e1s1

Sample Input2Sample Output2

8

82

s1s1e3e1s3e1e3s3

Problem E

Dead-End Detector

Time limit:5

seconds

A dead-end sign.

Source:Wikimedia Commons

The council of your home town has decided to improve road sign placement,especially for dead ends.They have given you a road map,and you must determine where to put up signs to mark the dead ends.They want you to use as few signs as possible.

The road map is a collection of locations connected by two-way streets.The following rule describes how to obtain a complete placement of dead-end signs.Consider a street S connecting a location x with another location.The x -entrance of S gets a dead-end sign if,after entering S from x ,it is not possible to come back to x without making a U-turn.A U-turn is a 180-degree turn immediately reversing the direction.

To save costs,you have decided not to install redundant dead-end signs,as speci?ed by the following rule.Consider a street S with a dead-end sign at its x -entrance and another street T with a dead-end sign at its y -entrance.If,after entering S from x ,it is possible to go to y and enter T without making a U-turn,the dead-end sign at the y -entrance of T is redundant.See Figure E.1for

examples.

(a)Sample Input 1(b)Sample Input 2

Figure E.1:Illustration of sample inputs,indicating where non-redundant dead-end signs are placed.

Input

The ?rst line of input contains two integers n and m ,where n (1≤n ≤5·105)is the number of locations and m (0≤m ≤5·105)is the number of streets.Each of the following m lines contains two integers v and w (1≤v

Output

On the ?rst line,output k ,the number of dead-end signs installed.On each of the next k lines,output two integers v and w marking that a dead-end sign should be installed at the v -entrance of a street connecting locations v and w .The lines describing dead-end signs must be sorted in ascending order of v -locations,breaking ties in ascending order of w -locations.

Sample Input1Sample Output1

65 12 13 23 45 562 45 65

Sample Input2Sample Output2

88 12 13 23 34 15 16 67 683 15 16 34

Problem F

Directing Rainfall

Time limit:15seconds

Porto and the nearby Douro Valley are famous for producing port wine.Wine lovers from all over the world come here to enjoy this sweet wine where it is made.The International Consortium of Port Connoisseurs(ICPC)is organizing tours to the vineyards that are upstream on the Douro River.To make visits more pleasurable for tourists,the ICPC has recently installed sun tarps above the vineyards.The tarps protect tourists from sunburn when strolling among the vines and sipping on a vintage port. Unfortunately,there is a small problem with the tarps.Grapes need sunlight and water to grow.While the tarps let through enough sunlight,they are entirely waterproof.This means that rainwater might not reach the vineyards below.If nothing is done,this year’s wine harvest is in peril!

The ICPC wants to solve their problem by puncturing the tarps so that they let rainwater through to the vineyards below.Since there is little time to waste before the rainy season starts,the ICPC wants to make the minimum number of punctures that achieve this goal.

We will consider a two-dimensional version of this problem.The vineyard to be watered is an interval on the x-axis,and the tarps are modeled as line segments above the x-axis.The tarps are slanted,that is, not parallel to the x-or y-axes(see Figure F.1for an example).Rain falls straight down from in?nitely high.When any rain falls on a tarp,it?ows toward the tarp’s lower end and falls off from there,unless there is a puncture between the place where the rain falls and the tarp’s lower end—in which case the rain will fall through the puncture instead.After the rain falls off a tarp,it continues to fall vertically. This repeats until the rain hits the ground(the x

-axis).

(a)Tarps are shown as black slanted

line segments and the vineyard as a

green line segment at the

bottom.

starts above the vineyard will reach

the vineyard.

Figure F.1:Illustration of Sample Input1.

For legal reasons you have to ensure that at least some of the rain that reaches the vineyard originated from directly above the vineyard.This is to prevent any vineyard from stealing all their rain from neighboring vineyards(see the second sample input for an example).

Input

The?rst line of input contains three integers ,r and n,where( ,r)(0≤

Output the smallest number of punctures that need to be made to get some rain falling from above the vineyard to the vineyard.

Sample Input1Sample Output1

10205

2

32501260

3060870

2570080

15302840

5201425

Sample Input2Sample Output2

242

1

3203

5215

Problem G

First of Her Name

Time limit:10seconds

In the Royal Family,names are very important!As the Royal Historian you have been charged with analyzing the patterns in the names of the Royal Ladies in the realm.

There have been n Royal Ladies,for convenience numbered from1to n.The name of each Lady is an uppercase letter concatenated with the name of her mother.The exception is the Lady numbered1,the founder of the Royal Family,whose name is just a single uppercase letter.

For example,ENERYS could be the mother of AENERYS(as the name AENERYS consists of the single uppercase letter‘A’concatenated with ENERYS,which is her mother’s name).Similarly,AENERYS could be the mother of DAENERYS and Y AENERYS.

You are given the description of all the Royal Ladies.Your task is to determine,for certain interesting strings s,the number of Royal Ladies for whom s is a pre?x of their name.

For example,consider Sample Input1below,with a Royal Line that goes straight from the founder S to AENERYS(through YS,RYS,ERYS,NERYS and ENERYS),with each Lady having exactly one daughter.Then AENERYS has two daughters—DAENERYS and Y AENERYS,with the latter having one daughter,RY AENERYS.

In such a family,RY is a pre?x of the names of two ladies:RYS and RY AENERYS.E is a pre?x of the names of ERYS and ENERYS.N is a pre?x only of NERYS’s name,while S is a pre?x only of the name of the founder,S.AY is not a pre?x of any Royal Lady’s name.

Input

The?rst line of input contains two integers n and k,where n(1≤n≤106)is the total number of Royal Ladies and k(1≤k≤106)is the number of query strings.

Then follow n lines describing the Royal Ladies.The i th of these lines describes the Royal Lady num-bered i,and contains an uppercase letter c i(‘A’–‘Z’)and an integer p i,where c i is the?rst letter of the name of Lady i,and p i(p1=0and1≤p i1)is the number of her mother(or0,in the case of the First Lady).All the names are unique.

The remaining k lines each contain one nonempty query string,consisting only of uppercase letters.The sum of the lengths of the query strings is at most106.

Output

Output k lines,with the i th line containing the number of Royal Ladies who have the i th query string as a pre?x of their name.

Sample Input1Sample Output1

105 S0 Y1 R2 E3 N4 E5 A6 D7 Y7 R9 RY

E

N

S

AY 2 2 1 1 0

Problem H

Hobson’s T rains

Time limit:5seconds

Mr.Hobson has retired from running a stable and has invested in a more modern form of transport,trains.He has built a rail network with n stations.However,he has retained his commitment to free the passenger from the burden of too many choices:from each station,a passenger can catch a train to exactly one other station.Such a journey is referred to as a leg .Note that this is a one-way journey,and it might not be possible to get back again.

Hobson also offers exactly one choice of ticket,which allows a passenger to travel up to k legs in one trip.At the exit from each station is an automated ticket reader (only one,so that passengers do not need to decide which to use).The reader checks that the distance from the initial station to the ?nal station does not exceed k legs.

Each ticket reader must be programmed with a list of valid starting stations,but the more memory this list needs,the more expensive the machine will be.Help Hobson by determining,for each station A ,the number of stations (including A )from which a customer can reach A in at most k

legs.

2,3,4,5,6

3,4,5

1,2,3,6

1,2

1

6

Figure H.1:Illustration of Sample Input 1.Each circle represents a station.The numbers outside the circles are the station numbers loaded into the ticket readers when k =2.

Input

The ?rst line of input contains two integers n and k ,where n (2≤n ≤5·105)is the number of stations and k (1≤k ≤n ?1)is the maximum number of legs that may be traveled on a ticket.Then follow n lines,the i th of which contains an integer d i (1≤d i ≤n and d i =i ),the station which may be reached from station i in one leg.

Output

Output n lines,with the i th line containing the number of stations from which station i can be reached in at most k legs.

Sample Input1Sample Output1

62 2

3

4

5

4

31 2 4 5 3 1

Sample Input2Sample Output2

53 2

3

1

5

43 3 3 2 2

Problem I

Karel the Robot

Time limit:10seconds

Did you know that the word“robot”is almost100years old?It was?rst introduced in1920,in the science-?ction theatrical play R.U.R.,written by KarelˇCapek.As a tribute to this Czech writer,an educational programming language was named Karel many years later at Stanford University.Your task is to implement an interpreter of a simpli?ed version of this programming language.

The Karel programming language controls a robot named Karel,who lives in a grid of unit squares. Some of the squares are free,while others contain a barrier.Karel always occupies one of the free squares and faces one of the four cardinal directions.The two basic commands are“move forward”and “turn left.”The language also provides simple conditional and looping statements.The main educational potential of the language lies in the possibility of de?ning new procedures for more complex tasks. Our simpli?ed version of the language can be described by the following grammar:

:=""|

:="m"|"l"||

"i""("")("")"|

"u""("")"

:="b"|"n"|"s"|"e"|"w"

:=

:="="

There are?ve types of commands:

m(“move forward”)advances Karel’s position by one grid square in its current heading,unless there is a barrier,in which case the command has no effect.

l(“turn left”)makes Karel turn left90degrees.

X where X is any uppercase letter,invokes the procedure named X.

i(“if”)followed by a single-letter condition,and two programs in parentheses.If the condition is satis?ed,the?rst program is executed.Otherwise,the second program is executed.

u(“until”)followed by a single-letter condition,and a program in parentheses.If the condition is satis?ed,nothing is done.Otherwise,the program is executed and then the command is repeated.

A condition can be either‘b’,which is satis?ed if and only if there is a barrier in the next square in Karel’s current heading,or one of the four directional letters‘n’,‘s’,‘e’,or‘w’,which is satis?ed if and only if Karel’s current heading is north,south,east,or west,respectively.

For instance,a simple program ub(m)can be understood to mean:“keep moving forward until there is a barrier,”while un(l)means“turn to the north.”A procedure de?nition R=lll de?nes a new procedure‘R’which effectively means“turn right.”

Input

The?rst line of input contains four integers r,c,d,and e,where r and c(1≤r,c≤40)are the dimensions of the grid in which Karel lives,d(0≤d≤26)is the number of procedure de?nitions,and e(1≤e≤10)is the number of programs to be executed.

Then follow r lines describing the grid(running north to south),each containing c characters(running west to east),each character being either‘.’(denoting a free square)or‘#’(denoting a barrier).All squares outside this given area are considered barriers,which means Karel may never leave the area. Each of the next d lines contains a procedure de?nition,associating a procedure name(one uppercase letter)with a program forming the procedure body.No procedure name is de?ned more than once. Procedure bodies may contain invocations of procedures that have not yet been de?ned.

The last2e lines describe the programs to be executed.Each such description consists of a pair of lines. The?rst line of each pair contains two integers i and j and a character h,where i(1≤i≤r)is the row and j(1≤j≤c)is the column of Karel’s initial position,and h∈{n,s,e,w}represents Karel’s initial heading.It is guaranteed that the initial position is a free square.The second line of each pair contains a program to be executed from that initial position.

All procedure bodies and all programs to be executed are at least1and at most100characters long,syn-tactically correct,and only contain invocations of procedures that are de?ned.The lines with procedure de?nitions and programs to be executed contain no whitespace characters.

Output

For each program execution,output the?nal position of Karel after the complete program is executed from the respective initial position.Follow the format used to describe initial positions,that is,two numbers and a directional character.If a particular execution never terminates,output inf instead. Sample Input1Sample Output1

4857

.......#

..#....#

.###...#

.....###

R=lll

G=ub(B)

B=ub(m)lib(l)(m)

H=ib()(mmHllmll)

I=III

11w

G

11e

G

22n

G

26w

BR

41s

ib(lib()(mmm))(mmmm) 11e

H

22s

I 11w inf 11w 24s 44e 14e inf

Problem J

Miniature Golf

Time limit:6seconds

A group of friends has just played a round of miniature golf.Miniature golf courses consist of a number of holes.Each player takes a turn to play each hole by hitting a ball repeatedly until it drops into the hole.

A player’s score on that hole is the number of times they hit the ball.To prevent incompetent players slowing down the game too much,there is also an upper limit (a positive integer)on the score:if a player has hit the ball times without the ball dropping into the hole,the score for that hole is recorded as and that player’s turn is over.The total score of each player is simply the sum of their scores on all the holes.Naturally,a lower score is considered better.

There is only one problem:none of the players can remember the value of the integer .They decide that they will not apply any upper limit while playing,allowing each player to keep playing until the ball drops into the hole.After the game they intend to look up the value of and adjust the scores,replacing any score on a hole that is larger than with .

The game has just?nished,but the players have not yet looked up .They wonder what their best possible ranks are.For this problem,the rank of a player is the number of players who achieved an equal or lower total score after the scores are adjusted with .For example,if the adjusted scores of the players are3,5,5,4,and3,then their ranks are2,5,5,3and2respectively.

Given the scores of the players on each hole,determine the smallest possible rank for each player. Input

The?rst line of input contains two integers p and h,where p(2≤p≤500)is the number of players and h(1≤h≤50)is the number of holes.The next p lines each contain h positive integers.The j th number on the i th of these lines is the score for player i on hole j,and does not exceed109.

Output

Output a line with the minimum possible rank for each player,in the same order as players are listed in the input.

Sample Input1Sample Output1

33 222 421 4411 2 2

Sample Input2Sample Output2

64 3122 4322 6632 7343 3424 23351 2 5 5 4 3

This page is intentionally left blank.

相关主题