APIO 2009 - Digging for Oil

Author: Albert Ye

Explanation

Official Explanation: APIO 2009 solutions document

We want the three largest disjoint squares with the largest total sums. Note that every pair of rectangles must be on opposite sides of some line. This leaves us with two basic configurations; either there are two parallel lines dividing the squares or there are two perpendicular lines. The configurations are illustrated below:

apio09oil fig1

Note that solutions need to account for all possible rotations of these configurations.

Configuration 1

In this configuration, we can find the answer for each non-middle subrectangle using the method in Configuration 2. However, we need to use another recurrence to find the middle rectangle. WLOG let the subrectangles be arranged in top-down order, as the left-right order can be found similarly. Let be the answer for the rows in interval . Obviously has no answers if . We can also manually find manually as a base case. Next, note that consists of all squares counted in and . Then, there are just the squares counted in row and row , which is just and respectively. Thus, the recurrence is Note that intervals must be traversed from smallest to largest.

Configuration 2

It suffices to use prefix sums. From each corner, find the sum of the values of all points that can be reached solely by travelling towards that corner from a point , and store the value with . We can use this to find the total amount of oil for the squares on each corner of . These values help us find the maximum amount of oil in a square on each corner of . We then perform some casework for each reflection to get the answer.

Code

C++

implements the prefix sum function for each corner, and is as described.

1#include <bits/stdc++.h>
2
3using namespace std;
4
5#define MAXN 1505
6
7int m,n,k,dp[MAXN][MAXN][2],pp[MAXN][MAXN][4],pq[MAXN][MAXN],ar[MAXN][MAXN];
8int main()
9{
10 ios_base::sync_with_stdio(0);

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

Give Us Feedback on APIO 2009 - Digging for Oil!