Posts

ACM ICPC Live Archive 3902

ACM ICPC Live Archive 3902 Problem Statement Wrong: Take the root, S, and use Dijkstra's to try to find the shortest path between S and every other leaf. Once an optimal new server is found, repeat the process, but with both S and the new server. Keep adding new servers until the distance is less than or equal to K. Notice that for one thing, this is immensely inefficient in terms of time. It is repeating repeating Dijkstra's. So no. Furthermore, this will not create the most optimal node set. What if the range of one of these new servers overlap with the range of one of the old servers? Lastly, this is complicated beyond need. The following method is simpler: Analysis The "network" is an acylic graph. However, by marking a node, S, on the graph, as a "propagation center", i.e. root, it effectively becomes an directed acylic graph, which can be reorganized into a tree. Notice that the network is an acylic graph. Normally, acyclic graphs don...

USACO Training: Controlling Companies

 USACO Training: Controlling Companies (concom) Some companies are partial owners of other companies because they have acquired part of their total shares of stock. For example, Ford at one point owned 12% of Mazda. It is said that a company A controls company B if at least one of the following conditions is satisfied: Company A = Company B Company A owns more than 50% of Company B Company A controls K (K >= 1) companies denoted C 1 , ..., C K with each company C i owning x i % of company B and x 1 + .... + x K > 50%. Given a list of triples (i,j,p) which denote company i owning p% of company j, calculate all the pairs (h,s) in which company h controls company s. There are at most 100 companies. Write a program to read the list of triples (i,j,p) where i, j and p are positive integers all in the range (1..100) and find all the pairs (h,s) so that company h controls company s. My 1st Method I enumerated the amount of companies to find pairs in whi...

BZOJ 3343 and Chunks

BZOJ 3343 and Chunks Problem Statement There are N≤1000000 counters. You are asked to do Q≤3000 actions on these counters, either: Increment all counters between L and R by W, given that 1≤L≤R≤N and 1≤W≤1000. This command will be in the format "M <L> <R> <W>", where L, R, and W are integers. Count the amount of counters between L and R and are greater than W, given that 1≤L≤R≤N and 1≤W≤1000. This command will be in the format "A <L> <R> <W>", where L, R, and W are integers. Core Idea Any array of integer size N can be broke up into √N+1 chunks of √N elements each for the first N blocks and then the remainder amount for the last block. Although this may seem as  the bloody obvious at first, splitting up an extremely large array into chunks and then doing work on the chunks can make a program vastly more efficient. There are various example: Searching - if a block's lowest value is above the targe...

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

LA 2678: Subsequence

LA 2678: Sub-sequence Problem Statement Analysis     We are asked to find the length of the shortest sub-sequence such that the sum of all elements in it is at least S. In other words, given the array A_0 ~ A_N-1, find the smallest possible X such that there exists a i which satisfies ∑A_j ≥ S, where j = i ~ i + X - 1.     For example, when A = [1, 2, 3, 4, 5], N = 5, and S = 11, the smallest possible X is 3 and the i is 2. This is sample test case 2. Algorithm: O(N^3)     The most naive method is to permute through all possible sub-sequence beginnings and ends. There are N beginnings and N/2 ends on average, making just permuting through possible sub-sequences O(N^2). Multiplying by the cost of finding the sum of that sub-sequence, O(N), gives us a total O(N^3) algorithm. Clearly that will not work! Algorithm: O(N^2)     However, we can use a prefix sum to improve on the cost of finding the sum of that sub-sequence....

OpenJudge 1768: Kadane's Algorithm on 2D Arrays

OpenJudge 1768: Kadane's Algorithm on 2-D Arrays Simple Version Given a 2-D matrix of integers, find the sub-matrix of this whose sum is maximized, and find that sum. The size of the matrix is no larger than 100 by 100. Analysis O(N^4) - Works, but naive     That's true, an naive O(N^4) version would work. We simply need to calculate (two dimensional!) prefix sums first. A quick review to what 2-D prefix sums are:     The 2-D prefix sum of two coordinates, X and Y, is the sum of all elements that have X-coordinate less than or equal to X, and Y coordinate less than or equal to Y.     It can be calculated through tabulation by removing overlapping areas.     Let P be the prefix sum array and V be the array of values, so the formula is as follows:     P[x, y] = V[x, y] - P[x-1, y] - P[x, y-1] + P[x-1, y-1].     We need to add the P[x-1, y-1] at the end since subtracting P[x-1, y] and P[x, y-1]...