stanford proco 2014 may 18, 2014 adv a deciphering...

16
Stanford ProCo 2014 May 18, 2014 Adv A Deciphering Silverware (page 1 of 1) Overview: Given a string encrypted with a box cipher, decrypt the string. Description: Emergency! Before embarking on the journey of a lifetime, Frodo hid all the silverware residing at Bag End, so that his enterprising relatives wouldn’t make off with the goods. You see, some of the other Baggins’ had designs on that silverware, and while Frodo wasn’t super attached, he’d never really liked his relatives much for making fun of his hairy feet (pot calling kettle black much?). Frodo, however, does not have the best memory. Fortunately, he came up with a clever solution. Before he left, he wrote down a message to himself with the location where he hid all the silverware. Of course, he made sure to encrypt his message with a flawless cipher strategy, making it impossible for anyone else to steal his silverware! But now that he’s back, he wants to host a dinner party with Gandalf, and he really needs that message to be decrypted so he can find his silverware again. The encrypted message Frodo wrote can be conveniently expressed as a string s that has n×m characters. It is possible to decrypt this silverware string if we arrange all of the characters in a box with n rows and m columns. The characters should be ordered top to bottom, left to right (so the first n characters of s should be placed in the first column). By then reading the characters left to right, and then top to bottom (so the top row corresponds to the first m characters), it’s possible to decrypt the encrypted string and recover the valuable silverware! (See below for an example.) Can you help Frodo ensure that his dinner party is a raging success? Filename: advA.{java, cpp, c, cc, py} Input: The input will consist of two lines of input. The first line will contain n and m, separated by a space The second line will contain the encrypted string s with mn characters Output: A single string, the decrypted form of string s Assumptions: 1≤ n×m ≤ 100,000 m and n are positive integers. The string s will consist of only lowercase alphabetic characters Sample Input #1: 43 hlifeorflgae Sample Output #1: hellogiraffe Sample Output #1 Comments: Constructing 4x3 box as described above with the input string hlifeorflgae gives us: hel log ira ffe Reading the characters in the box left to right, top to bottom, gives us the decrypted string hellogiraffe.

Upload: others

Post on 02-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014

Adv A Deciphering Silverware (page 1 of 1)

Overview: Given a string encrypted with a box cipher, decrypt the string.

Description: Emergency! Before embarking on the journey of a lifetime, Frodo hid all the silverware

residing at Bag End, so that his enterprising relatives wouldn’t make off with the goods. You

see, some of the other Baggins’ had designs on that silverware, and while Frodo wasn’t

super attached, he’d never really liked his relatives much for making fun of his hairy feet

(pot calling kettle black much?).

Frodo, however, does not have the best memory. Fortunately, he came up with a clever

solution. Before he left, he wrote down a message to himself with the location where he hid

all the silverware. Of course, he made sure to encrypt his message with a flawless cipher

strategy, making it impossible for anyone else to steal his silverware! But now that he’s

back, he wants to host a dinner party with Gandalf, and he really needs that message to be

decrypted so he can find his silverware again.

The encrypted message Frodo wrote can be conveniently expressed as a string s that has

n×m characters. It is possible to decrypt this silverware string if we arrange all of the

characters in a box with n rows and m columns. The characters should be ordered top to

bottom, left to right (so the first n characters of s should be placed in the first column). By

then reading the characters left to right, and then top to bottom (so the top row corresponds

to the first m characters), it’s possible to decrypt the encrypted string and recover the

valuable silverware! (See below for an example.) Can you help Frodo ensure that his dinner

party is a raging success?

Filename: advA.java, cpp, c, cc, py

Input: The input will consist of two lines of input.

The first line will contain n and m, separated by a space The second line will contain the encrypted string s with mn characters

Output: A single string, the decrypted form of string s

Assumptions: 1 ≤ n×m ≤ 100,000 m and n are positive integers. The string s will consist of only lowercase alphabetic characters

Sample

Input #1:

4 3 hlifeorflgae

Sample

Output #1:

hellogiraffe

Sample

Output #1

Comments:

Constructing 4x3 box as described above with the input string hlifeorflgae gives us: hel log ira ffe Reading the characters in the box left to right, top to bottom, gives us the decrypted string

hellogiraffe.

Page 2: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014 Adv B Shopping For Dwarves (page 1 of 1)

Overview: Given a limited budget and some items, find the maximum utility that can be purchased. Description: The hospitable Bard is shopping in the market at Laketown for some supplies for his guests,

the dwarves. These dwarves are rather demanding when it comes to food, so Bard must choose wisely when it comes to his shopping list. Thankfully, they have provided him with the foods that they enjoy the most, along with their price. Now, Bard just needs to find the right combination of foods within his budget that will make his guests happiest. Can you help him with his important task? Bard shares with you a list of n items and a budget of b dollars. Each item has a price p and dwarf utility u. He may purchase fractional amounts of each item, but not more than one of any particular item. A fraction f of an item has a price p×f and utility u×f. Help Bard come up with a shopping list of (possibly fractions of) items that have the maximum utility for the dwarves without exceeding the budget of b dollars.

Filename: advB.java, cpp, c, cc, py Input: The input will contain exactly n+2 lines.

The first line contains b, the budget in dollars. The second line contains n, the total number of items for sale. The third line to the (n+2)th lines each contain space separated numbers p u representing the price and utility of one of the n items.

Output: One line containing the maximum total utility found. Print the utility rounded exactly to six

decimal places. Assumptions: 1 ≤ n ≤ 100,000

0 ≤ b ≤ 1,000,000,000 0 ≤ p, u ≤ 1,000 All floating point numbers in the input will be given to exactly two decimal places.

Sample Input #1:

2.50 3 2.00 1.00 0.50 2.00 6.00 2.50

Sample Output #1:

3.000000

Sample Input #2:

4.00 4 1.00 2.00 5.00 2.50 2.00 1.20 3.00 1.00

Sample Output #2:

3.700000

Page 3: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014

Adv C Runic Evaluation (page 1 of 1)

Overview: Evaluate an arithmetic expression of rational numbers.

Description: Elrond, Lord of Rivendell, has been asked to decode some ancient mystical runes for his

adventuring friends again! To be honest, he’s not too keen on helping them, considering

how much fun they make of his vegetarian habits… but hey, that’s what friends do, right?

These runes are like none other that Elrond has ever seen. The sequence of runes is an

arithmetic expression consisting of adding and subtracting rational numbers. Despite being

practically immortal, Elrond has never encountered such runes before. All he knows is that

to decode the runes, the arithmetic expression must be evaluated, but he has no idea how

to do so. Can you help him?

Filename: advC.java, cpp, c, cc, py

Input: The rational numbers are comprised of two positive integers (the numerator and the

denominator, in that order), separated by a forward slash. Adjacent rational numbers will be

separated by an operator (‘+’ or ‘-­’). There will be exactly one operator between each pair of

adjacent rational numbers, and no other operators (in particular, no leading ‘+’ or ‘-­’). In a

monumental waste of quality parchment, all of the integers, slashes, and operators will be

space-­separated.

Output: One line containing the result of decoding the runic sequence. The result should be in the

form of a / b where a is a (possibly negative) integer and b is a positive integer. The integer

a and b should not share any common factors greater than 1. If the arithmetic expression

evaluates to 0, output “0 / 1”. Print a, the slash, and b space-­separated.

Assumptions: The input expression/rune sequence will contain ≤ 100,000 characters.

Every numerator and denominator will be a positive integer that is at most 10.

Sample

Input #1:

10 / 3 + 5 / 2 -­ 4 / 2 -­ 1 / 2 + 4 / 6

Sample

Output #1:

4 / 1

Sample

Input #2:

1 / 1 -­ 2 / 1

Sample

Output #2:

-­1 / 1

Page 4: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014 Adv D Happy Birthday Happy Birthday (page 1 of 1)

Overview: Find the minimum number of people in the room such that the probability of at least two people in the room having the same birthday is at least a certain percentage.

Description: Hobbits are well known for their love of celebrations, as was indicated by Bilbo’s farewell

extravaganza, complete with live dragon fireworks. What’s more, the party celebrated not only Bilbo’s 111th birthday, but Frodo’s also 33rd birthday as well, for why celebrate one birthday when you can celebrate two! In fact, hobbits love double birthdays so much that they want to form an entire system of communities around celebrating double birthdays. Each community will be set a threshold probability p, which is the probability that at least two people in the community share the same birthday. The higher the threshold, the more festive the community. However, in the interest of avoiding overcrowding, they would like to know the minimum number of people required in a community to achieve the desired threshold p. Can you help? You are given a probability p. Taking the assumption that there are 366 possible birthdays and each birthday is equally likely, find the smallest number of people in the room such that the probability that at least two people in the room share the same birthday is at least p.

Filename: advD.java, cpp, c, cc, py Input: The input will be exactly one line containing a number p. p will consist of '0.' followed by

exactly 4 decimal digits. Output: The minimum number of people in the room such that the problem conditions are satisfied. Assumptions: 0.0001 ≤ p ≤ 0.9995

p is selected such that given that the answer is N, the probability that at least two of the N people share a birthday differs from p by at least 10-­4.

Sample Input #1:

0.5000

Sample Output #1:

23

Sample Input #2:

0.9995

Sample Output #2:

73

Page 5: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014 Adv E Smaug’s Maze (page 1 of 2)

Overview: Find the shortest route to escape a maze. Description: Bilbo has found the Arkenstone in the massive treasure room! However, his elation is

short-­lived when he discovers that Smaug, in his infinite boredom, has created a maze out of the treasure, and Bilbo must traverse the maze in order to get out. He only has so much time before Smaug tracks him down, and the dragon is getting ever closer… THUD… THUD…

THUD… Can you help him survive Smaug’s Maze? You are given a map of the maze with n rows and m columns in the form of a grid of characters.

‘S’ represents Bilbo’s starting point. ‘.’ represents a traversable space. ‘#’ represents an impassable wall of treasure.

Bilbo must start at the specified location. Each step, he can move from his current location to an adjacent traversable space (horizontally, vertically or diagonally). He wants to escape the maze by reaching the edge of the maze, and then stepping out of the maze. Write a program that prints out for Bilbo the length of the shortest escape route from the maze. If there is no way to escape the maze, your program should print “NO ESCAPE!” instead and wish Bilbo the best of luck!

Filename: advE.java, cpp, c, cc, py Input: The input consists of exactly n+1 lines.

The first line of the input will contain two space separated integers n m. The integers n and m specify the number of rows and columns of the maze, respectively. The next n lines each containm characters specifying the map of the maze. Each character specifies a tile in the maze, as described above.

Output: If an escape route exists, output the length in steps of the shortest escape route. If no

escape route exists, output the string “NO ESCAPE!”. Assumptions: 1 ≤ n,m ≤ 1000

The character ‘S’ will appear exactly once in the maze. Sample Input #1:

3 3

###

#S#

.#.

Sample Output #1:

2

Sample Input #2:

3 3

###

#S#

###

Page 6: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014 Adv E Smaug’s Maze (page 2 of 2)

Sample Output #2:

NO ESCAPE!

Sample Input #3:

7 12

.###########

#.#########.

#.S........#

#.#######.##

#...........

#.#########.

#.#########.

Sample Output #3:

3

Page 7: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014 Adv F Food Coma (page 1 of 2)

Overview: Determine the location and direction of dwarves after a certain time Description: As mentioned before, dwarves love to eat as much as hobbits love to party. Thorin’s

company has eaten plenty at Bilbo’s house and a food coma is coming over them all. They are beginning to stumble about aimlessly, bouncing off of each other when they collide, wandering about the house and out into the streets. Gandalf saunters in at this moment and sees all the dwarves meandering about rather foolishly and must gather them all together to deliver his news. He needs your help to find all of the dwarves. You are given n dwarves, and for simplicity’s sake, let us assume they only move about on the x-­axis. The dwarves are numbered 1 to n from left (lowest x-­value) to right (highest x-­value). You are given the locations of all of the dwarves on the x-­axis, and the directions in which they are moving (negative or positive x-­direction). All dwarves move at the same speed of 1 unit per second. When dwarves collide, they bounce off of each other, reversing their directions while still travelling at the same speed. You are to determine the locations of all the dwarves and the direction in which they are moving after t seconds.

Filename: advF.java, cpp, c, cc, py Input: The input will consist of exactly n+1 lines.

The first line contains two space separated integers n t, denoting the number of dwarves and the number of seconds respectively. The next n lines will each contain an integer and a character separated by a space, denoting the position of a dwarf on the x-­axis and the direction he or she is moving. Line i (2 ≤ i ≤ n+1) will contain the information for dwarf numbered i-­1. The character will be either ‘+’ or ‘-­’ . A ‘+’ means the dwarf is moving in the positive x-­direction while a ‘-­’ means the dwarf is moving in the negative x-­direction.

Output: The output consists of n lines. The ith line should contain the location of dwarf i after t

seconds, and the direction it is moving in. If it is moving in negative x-­direction, output ‘+’. Otherwise, output ‘-­’. If dwarf i is involved in a collision during the tth second, output the direction they face before the collision has taken place. Refer to the sample testcases for more details.

Assumptions: 1 ≤ n ≤ 100,000

1 ≤ t ≤ 1,000,000,000 All dwarves start with an x-­coordinate at least 0 and at most 1,000,000,000. You can assume that no two dwarves have the same initial x-­coordinate. Also, the x-­coordinates will be given in ascending order (i.e. if i < j, then dwarf i will have a smaller x-­coordinate than dwarf j).

Page 8: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014 Adv F Food Coma (page 2 of 2)

Sample Input #1:

5 3 0 + 6 -­ 7 + 10 -­ 11 -­

Sample Output #1:

3 + 3 -­ 7 -­ 8 -­ 10 +

Sample Output #1 Comments

Note that dwarf 1 is moving in positive x-­direction before the collision at the tth second and dwarf 2 is moving in negative x-­direction before the collision at the tth second.

Page 9: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014 Adv G Shooting Spiders (page 1 of 1)

Overview: Find the minimum number of range decrements required to zero a given array. Description: Travelling through Mirkwood, Tauriel and Legolas decide to have a shooting match in which

they see who can shoot down more spiders with fewer bow releases. The spiders are advancing rather peculiarly in columns of varying depth. Being talented archers, both can release several arrows at once and shoot down a contiguous series of spiders. However, they are not so talented that they can skip over a spider in a series to shoot its neighbor. Having made a good sport of these devilish spiders, Tauriel and Legolas now want to take the challenge one step further: to take down all the spiders using the least number of bow releases possible. However, they need help determining exactly what that number is. You will be given an array of n positive integers, representing the number of spiders in each column. In a single release of a bow, you are allowed to shoot at a range of columns and eliminate one spider in each. However, you are only allowed to do this if there is a spider in each of those columns. In other words, in each step, you can pick any two indices in the array such that every integer in between (inclusive) is positive, and decrement all of those integers. Find the minimum number of bow releases required to shoot down all the spiders, i.e. decrement the entire array to zero.

Filename: advG.java, cpp, c, cc, py Input: The input consists of exactly 2 lines

The first line contains the number of columns n The second line will contain n space separated integers: the number of spiders in each column.

Output: Output the minimum number of steps to shoot down all the spiders. Assumptions: 1 ≤ n ≤ 100,000

Each column has a positive number of spiders no greater than 1,000,000. Sample Input #1:

10 1 2 3 2 4 5 1 2 4 3

Sample Output #1:

9

Sample Input #2:

10 10 20 30 40 50 40 30 20 10 1

Sample Output #2:

50

Page 10: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014 Adv H Cliquey Friends (page 1 of 2)

Overview: Find a clique in a graph of a given size Description: Aragorn, son of Arathorn, is convinced that some of his friends are becoming too cliquey

and exclusionary. To prove it to them via inspiring speech (on horseback), he is determined to find a k-­clique among the interaction network of his friends. Aragorn, son of Arathorn has n friends, labeled 1 to n. Two friends are called connected if they have conversed about anything (Mordor, Frodo, the shower situation) in the last day. A k-­clique in this network is defined as k friends that are all connected to each other (i.e. every pair of them is connected). Armed with futuristic magical spells, Aragorn can pick any group of his friends and learn whether or not there are k friends among them that form a k-­clique. But he doesn't learn the k-­clique itself. He wishes to enlist your help in finding a k-­clique among his friends. You are absolutely certain that there is indeed a k-­clique in Aragorn’s friend interaction network. You will be given n and k. You can use the magical spell to learn whether some subset of the n friends includes a k-­clique, but you can do so at most 150 times. Will you be able to find a list of k friends that are a k-­clique and help bolster Aragorn’s speech?

Filename: advH.java, cpp, c, cc, py Input/Output: This is an interactive problem. This means that your program will receive input based on the

output your program produces. When your program starts, you will be provided two space-­separated integers n and k as specified above. Your program may then do the following up to 150 times:

Output between 1 and n unique space separated integers vi (1 ≤ vi ≤ n) representing the set of friends you want to give to our "magic spell." Your program will be provided with a line of input: either “YES” or “NO”, indicating whether or not the given set of friends contains a k-­clique.

Your program must then finish by producing the following output. Output the k-­clique. Output the string “CLIQUE: “ followed by k space separated

integers representing the k-­clique. You MUST output a newline character and flush the output stream after each output:

In C, use printf("\n");; fflush(stdout);; In C++, use cout << "\n" << flush;; or cout << endl;; In Java, use System.out.println();; System.out.flush();; In Python, use print and sys.stdout.flush()

Assumptions: 1 ≤ k ≤ n ≤ 100 Sample Sequence #1:

COMPUTER 5 3

YOU 1 2 3

COMPUTER NO

YOU 1 2 4

Page 11: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014 Adv H Cliquey Friends (page 2 of 2)

COMPUTER NO

YOU 1 2 5

COMPUTER NO

YOU 1 3 4

COMPUTER NO

YOU 1 3 5

COMPUTER YES

YOU CLIQUE: 1 3 5 Sample Sequence #2:

COMPUTER 5 3

YOU 1 2 3 4 5

COMPUTER YES

YOU 1 3 4 5

COMPUTER YES

YOU 1 3 5

COMPUTER YES

YOU CLIQUE: 1 3 5

Page 12: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014 Adv I Crossing the Dead Marshes (page 1 of 1)

Overview: Find the maximum number of islands in a array Description: Frodo, Sam, and Gollum have reached the Dead Marshes, the last thing standing between

them and the Black Gates. However, as they cannot touch the marsh itself as they cross, they can only walk on islands of elevated dry land. The difficulty of crossing the marshes depends on the number of islands. To this end, they provide you with the topography of a (one-­dimensional) stretch of the marshland, represented by an array A of n integer heights. However, the height of the marsh water varies. If the marsh water is at height x, then an island as defined as an interval [i, j] where x < A[k] for all i ≤ k ≤ j and the two heights immediately on either side of the island (i.e. A[i-­1] and A[j+1], if they are part of the array) are no greater than x. Thus, the island remains elevated above the deadly marsh water. You wish to find the maximum possible number of islands, to help them determine whether they can safely cross the deadly stretch of marshland

Filename: advI.java, cpp, c, cc, py Input: The input will contain exactly 2 lines.

The first line contains a single integer n The second line contains n space-­separated integers represent the array A

Output: Output a single integer, the maximum number of islands. Assumptions: 1 ≤ n ≤ 100,000

1 ≤ A[i] ≤ 100,000 for every i Sample Input #1:

4 4 4 4 4

Sample Output #1:

1

Sample Input #2:

12 1 1 1 2 1 1 1 2 3 2 3 2

Sample Output #2:

2

Page 13: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014

Adv J A Game of Heights (page 1 of 1)

Overview: Find a balanced partition of a list of numbers to minimize their difference

Description: The Fellowship is having a rare moment of respite and decides that the best way to spend

their time is to play a game of bushel-­ball, the Middle Earth version of basketball. They want

to figure out the fairest teams based on heights and would like your help to do so. Fairness

is judged by minimizing the difference between each team’s total heights.

You are given a list of 2n integers Si, which represent the each Fellowship member’s height

(in unknown units). You wish to split these heights into two lists A and B of n heights each

such that the difference between the sum of A and the sum of B is as small as possible.

Find the minimum possible difference.

Filename: advJ.java, cpp, c, cc, py

Input: The input will contain exactly 2 lines.

The first line contains a single integer n. The second line contains 2n space-­separated integers S1, …, S2n

Output: Output a single integer, the minimum possible difference.

Assumptions: 1 ≤ n ≤ 50 -­50 ≤ Si ≤ 50 for every i

Sample

Input #1:

2 1 2 3 4

Sample

Output #1:

0

Sample

Input #2:

2 -­11 1 4 7

Sample

Output #2:

9

Page 14: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014

Adv K Factional Warfare (page 1 of 2)

Overview: Determine if a majority element exists in a given subarray

Description: After Sauron’s crushing defeat, his minions have been running around like headless

chickens, unsure of what to do next. But you’re not like all the other minions. Armed with

your programming knowledge, you would like to digitally reconstruct the all-­seeing Eye of

Sauron! Performing quality mass-­surveillance on the inhabitants of Middle Earth is no easy

task, but fortunately you have an idea of how to proceed.

Middle Earth consists of n cities, which are conveniently arranged along a one-­dimensional

path, numbered from 1 to n. There are many different factions vying for control of different

parts of Middle Earth. Each city is controlled by one faction. Sauron has a list of important

regions of Middle Earth, each of which is an interval [i, j] of the cities. If any one faction is

able to dominate a region, they might present a threat to Sauron – otherwise, the different

factions will be too busy fighting each other to pose a threat. A faction dominates a region if

it controls a majority of the cities within the region, i.e. it controls more cities within this slice

than all other factions combined. In other words, a faction dominates a region [i, j] if it

controls more than (j-­i+1)/2 of the cities.

Any proper digital manifestation of Sauron must be able to quickly determine if there are

threats in any important region of Middle Earth. Fortunately, you know which faction is in

control of each of the n cities, and a list of q important regions [i, j]. For each of these

regions, the new Eye of Sauron should determine if some faction dominates the region, and

if so, which faction. Are you ready to become the minion who brought Sauron back, greater

and more terrible than before?

Filename: advK.java, cpp, c, cc, py

Input: The first line will contain two space-­separated integers: n, the number of cities, and q, the

number of important regions.

The second line will contain n space-­separated integers f1, … , fn: the faction that controls

each city.

The following q lines will each contain two space-­separated integers i j representing the

bounds of the region.

Output: For each given region, output a line containing "NO" if no faction controls a majority of the

cities in the region, or "YES x" if faction x dominates the region. There are q lines of output.

Assumptions: 1 ≤ n,q ≤ 100,000 For each city i, 1 ≤ fi ≤ 100,000 For each given region, 1 ≤ i ≤ j ≤ n

Sample

Input #1:

11 5

1 1 2 1 2 1 2 2 1 2 2

1 7

1 8

1 9

1 10

1 11

Page 15: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014

Adv K Factional Warfare (page 2 of 2)

Sample

Output #1:

YES 1

NO

YES 1

NO

YES 2

Sample

Input #2:

9 5

5 2 3 2 5 2 3 3 3

2 6

2 7

2 8

2 9

3 9

Sample

Output #2:

YES 2

NO

NO

NO

YES 3

Page 16: Stanford ProCo 2014 May 18, 2014 Adv A Deciphering ...web.stanford.edu/group/sumo/pdfs/proco/2014/ProCo2014Advanced.pdfFilename: advB.{java, cpp, c, cc, py} Input: The input will contain

Stanford ProCo 2014 May 18, 2014

Adv L Eagle Rescue (page 1 of 1)

Overview: Determine the smallest possible sum of Manhattan distances to k of the given locations

Description: It is time for the infamous golden eagle trump card! Gandalf and his party of men have once

again found themselves in a rather undesirable situation that they cannot get out of, so

Gandalf calls for help from his eagle friends. The party needs k of the eagles to help them

out. Because this happens with fair regularity, the party can really be anywhere in Middle

Earth, but the eagles must still get to them in a timely manner to help. Thankfully, there are n

eagles are spread throughout Middle Earth, so we just need the nearest k eagles to fly to

Gandalf's aid. Eagles fly only along cardinal directions, so the distance they need to fly is

the Manhattan distance. The Manhattan distance between two points (a,b) and (c,d) is

defined as |a -­ c| + |b -­ d|. In an attempt to make life easier for his eagle friends, Gandalf is

wondering where he should get in trouble next time so that eagles must do as little total

travelling as possible to rescue his company,.

There are n eagles with integer coordinates on the 2D plane. For any point on the 2D plane,

the amount the eagles must travel is the sum of the Manhattan distances to the k nearest

eagles. Given the n locations and the integer k, determine the minimum possible eagle flight

distance of any point (not necessarily the location of one of the eagles or with integer

coordinates).

Filename: advL.java, cpp, c, cc, py

Input: The input consists of exactly n+1 lines. The first line contains two integers, n and k, denoting respectively the number of total eagles

and the number of nearest eagles you need to consider.

The next n lines contains two integers each, denoting the x and y-­coordinates of a location.

Output: Output on a single line the minimum possible eagle flight distance. Print the reach rounded

to exactly five decimal places.

Assumptions: 1 ≤ k ≤ n ≤ 1,000 The x and y coordinates are integers between 1 and 1,000,000,000.

All points are distinct.

Sample

Input #1:

3 2 2 1 1 3 5 3

Sample

Output #1:

3.00000

Sample

Input #2:

3 3 0 1 1 2 2 0

Sample

Output #2:

4.00000