Posts

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

Code Collection: VIJOS 1255

Code Collection: VIJOS 1255 METHOD: O(n^3), Prefix Sum + O(n) Kadane's Algorithm (10/10) #include <algorithm> #include <iostream> #include <cstring> using namespace std; int aray[305][305], psum[305][305], comp[305]; const int SMALL = -22950001; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int R, C; cin >> R >> C; for(int i=1; i<=R; i++) for(int j=1; j<=C; j++) { cin >> aray[i][j]; if(aray[i][j] == 0) aray[i][j] = SMALL; psum[i][j] = aray[i][j] + psum[i-1][j]; } int best = aray[1][1]; for(int s=1; s<=R; s++) for(int e=s; e<=R; e++) { memset(comp, 0, sizeof comp); for(int i=1; i<=C; i++) { comp[i] = psum[e][i] - psum[s-1][i]; if(comp[i] < 0) comp[i] = SMALL; } int cmax = comp[1], cbest = comp[1]; for(int i=2; i<=C; i++) { cmax = max(comp[i], cmax+comp[i]); cbest = max(cbest...

BZOJ 1207: Whack-a-Mole

Problem Statement 1207: [HNOI2004]打鼹鼠 Time Limit:  10 Sec   Memory Limit:  162 MB Submit:  3975   Solved:  1893 [ Submit ][ Status ][ Discuss ] Description 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的。根据这个特点阿Q编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气。你可以控制一个机器人来打鼹鼠,如果i时刻鼹鼠在某个网格中出现,而机器人也处于同一网格的话,那么这个鼹鼠就会被机器人打死。而机器人每一时刻只能够移动一格或停留在原地不动。机器人的移动是指从当前所处的网格移向相邻的网格,即从坐标为(i,j)的网格移向(i-1, j),(i+1, j),(i,j-1),(i,j+1)四个网格,机器人不能走出整个n*n的网格。游戏开始时,你可以自由选定机器人的初始位置。现在你知道在一段时间内,鼹鼠出现的时间和地点,希望你编写一个程序使机器人在这一段时间内打死尽可能多的鼹鼠。 Input 第一行为n(n<=1000), m(m<=10000),其中m表示在这一段时间内出现的鼹鼠的个数,接下来的m行每行有三个数据time,x,y表示有一只鼹鼠在游戏开始后time个时刻,在第x行第y个网格里出现了一只鼹鼠。Time按递增的顺序给出。注意同一时刻可能出现多只鼹鼠,但同一时刻同一地点只可能出现一只鼹鼠。 Output 仅包含一个正整数,表示被打死鼹鼠的最大数目 Sample Input 2 2 1 1 1 2 2 2 Sample Output 1 My Translation Moles appear on a 2-D grid on certain points at certain times. A robot knows when the moles will emerge and where they will emerge, and starts at the most opti...

USACO 2018 February: Snow Boots (Part 2)

USACO 2018 February: Snow Boots (Part 2) Using  a Monotone Deque! 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 t...