Posts

Showing posts with the label USACO

UVA 787 and Big Number Multiplication

UVA 787 and Big Number Multiplication Problem Background: Big Numbers in this problem Big numbers are effectively storing sequence of digit chunks and directly manipulating them as if they were a large number. The most intuitive way to do this is to make an array where every element is a digit, but this is not effectively using the memory. Hence, it is logical to store numbers "millions at a time", i.e. 324876498726832746  [2]  | [1] | [0] like that. Necessities to Implement Notice that although this problem may LOOK like DP, it's really solvable by O(N^2) enumeration. Also notice that in this enumeration we will only be multiplying a large number with a normal number. Pseudocode for algorithm: CREATE LargeNumber NAME best SET -INFINITY FOR EACH i BETWEEN 0 and LENGTH OF list DO:     CREATE LargeNumber NAME product SET 1     FOR EACH j BETWEEN i AND LENGTH OF list DO         SET product TO product TIMES list[j] ...

USACO 2018 Open: Milking Order and Topological Sort

USACO 2018 US Open Contest, Gold Problem 2. Milking Order We need to find the biggest number X such that the first X observations are satisfied. Notice that this is perfect to do a binary search on: the invariant is that if the first X observations are satisfied, all observations X-1 will be satisfied; if the first X are insatisfied, all observations X+1 will not be satisfied. So how do we check if all observations to X are satisfied? Let us assemble a graph, in which each observation O_1, O_2, O_3, ... O_i, ... O_l(o) is represented as an edge between O_i and O_i+1 for all i. These observations are satisfiable if and only if this graph has a topological sorting. Lexicographic topological sorting on a graph can be done in O(E + V log_2 V) time (log_2 V because of priority_queue to extract lowest element). So, our total time complexity will be O((E + NlogN) logM). Here, E=200K (the sum of the M_i), N=100K, and M=50K, so it's fast enough. LTS can be easily implemented with ...

USACO 2018 Open: Talent Show

Image
USACO 2018 Open Talent Show In Short Given N objects with value Vi and weight Wi, a collection of elements is said to have total value-to-weight ratio as (the sum of their value) divided by (the sum of their weight). Find the best possible value-to-weight ratio of these X objects, given the restraint that the collection must have total weight at least W. Return that ratio multiplied by 1000 and floored. Analysis It would be very difficult to calculate this optimal ratio - we'd have to either try all possible collections - O(2^n), where n is at most 250, about 10 to the power of 75 calculations, OR use a greedy heuristic - get the objects with the best ratio until it satisfies the weight limit. Neither of those are too to our liking, so how about searching for it? The search space is linear and not infinite - it asks for the ratio to the closest thousandths. The search space is sorted as well - if ratio X is achievable, then ratio Y < X is also achievable, if...

USACO 2018 Febuary: Snow Boots (Part 1)

USACO 2018 Febuary: Snow Boots (Part 1) Using an Union-Find Set! Problem It's winter on the farm, and that means snow! There are  N N  tiles on the path from the farmhouse to the barn, conveniently numbered  1 … N 1 … N , and tile  i i  is covered in  f i f i  feet of snow. In his farmhouse cellar, Farmer John has  B B  pairs of boots, numbered  1 … B 1 … B . Some pairs are more heavy-duty than others, and some pairs are more agile than others. In particular, pair  i i  lets FJ step in snow at most  s i s i  feet deep, and lets FJ move at most  d i d i forward in each step. Farmer John starts off on tile  1 1  and must reach tile  N N  to wake up the cows. Tile  1 1  is sheltered by the farmhouse roof, and tile  N N is sheltered by the barn roof, so neither of these tiles has any snow. Help Farmer John determine which pairs of snow boots will allow him to make the tre...