Problem Statement
A common problem in the logging industry is determining how many trees can be harvested without damaging the long-term sustainability of a forest. To help solve this problem, you need to write a logging simulation to determine the maximum amount of wood that can be harvested every year, for the next 1000 years. To simplify things, trees will be square for the purposes of this problem. A tree grows by 1/8th of an inch in diameter (the diameter of a square is the length of each side) every year, and by twenty-four inches in height every year. To complicate matters, you know the size of the forest, and what the maximum tree density is such that all trees can survive. If the forest becomes overgrown, so that its density becomes greater than this maximum, the oldest trees die until the forest density is once again equal to or less than the maximum. Additionally, these trees can only be of a certain age before they just die due to old age. You will be given the number of trees of each age in year 0 of the forest (when your simulation starts) in a int[], trees. The ith element of trees corresponds to the number of trees of age i. When trees are harvested, they must be harvested as a whole tree (i.e. it is not possible to harvest half a tree), and in order for a tree to be harvested, its age must be equal to or greater than the age of every other living tree in the forest. Thus, if the oldest tree is 10 years old, all of the 10 year old trees must be harvested before any 9 year old trees are harvested, etc.
Trees must be a certain age before they are able to bear seedlings which grow into new trees. Also, since not every seedling becomes a new tree, a new tree will only grow for every treesPerNewTree trees whose age is equal to greater than reproduceAge.
The parameter density should be treated as a percentage. That is, if density = 50, then the forest can be covered up to 50% before trees start dying due to being overcrowded.
For instance, say reproduceAge = 4, treesPerNewTree = 10, and trees = {10, 15, 20, 25, 30}. If no trees are harvested and no trees die, every tree grows by one year and the forest looks like this (in a int[]): {0, 10, 15, 20, 25, 30}. Note that all the trees formerly of age 0 (seedlings) became age 1, age 1 became age 2, etc. At the end of the year, the trees reproduce, and seedlings are planted. There are 10 trees of age 1, 15 trees of age 2, 20 trees of age 3, 25 trees of age 4, and 30 trees of age 5. Thus, there are 55 trees whose age is equal to or greater than reproduceAge. Thus, 55 trees / treesPerNewTree = 5 new seedlings which will becomes trees, giving us a forest that looks like this: {5, 10, 15, 20, 25, 30}. When calculating the number of new seedlings which will become trees, you should use integer divison.
To summarize, in a given year, events happen in the following order:
- The trees are harvested
- The remaining trees grow for the year
- All trees greater than or equal to dieAge die
- If the forest is overgrown, trees die until it is not overgrown, starting with the oldest
- Seedlings are planted
You must determine the maximum number of cubic inches of wood (rounded down to the nearest integer less than or equal to the actual value) which can be harvested every year (you may have to harvest more in certain years because you must harvest whole trees), for the next 1000 years, without depleting the forest of trees. Because we are not wasteful people, you should never harvest any more trees than necessary in order to meet the annual harvest.
Definition
- Class:
- Forestry
- Method:
- maxProfit
- Parameters:
- int[], int, int, int, int, int
- Returns:
- int
- Method signature:
- int maxProfit(int[] trees, int dieAge, int forestArea, int density, int reproduceAge, int treesPerNewTree)
- (be sure your method is public)
Notes
- forestArea is the area of the forest in square inches.
Constraints
- trees will contain between 1 and 50 elements, inclusive.
- Every element in trees will be between 0 and 100, inclusive.
- The number of elements in trees will be less than or equal to dieAge
- dieAge will be between 1 and 100, inclusive.
- forestArea will be between 1 and 30000000, inclusive.
- density will be between 1 and 100, inclusive.
- reproduceAge will be between 1 and 100, inclusive.
- treesPerNewTree will be between 1 and 10000, inclusive.
- The initial area covered by trees will be less than or equal to forestArea.
Examples
{10,10,10,10,10,10,10,10,10,10}20
1000
40
8
7
Returns: 2460
The first year, 9 trees of age 9 are harvested for 2460.375. The next year, 1 tree of age 10 is harvested for 375 and 8 trees of age 9 are harvested for 2187, for a total of 2562. You can continue harvesting 2460 for 1000 years. However, harvesting 2461 means that all 10 trees of age 9 are harvested in the first year, and this does not leave enough trees to sustain an annual harvest of 2461 for 1000 years.{59,57,65,54,82,0,18,84,41,41,89,51,87, 61,17,83,41,50,58,12,89,58,75,57,24,21, 45,83,90,12,20,20,96,30,42,85,80,61,94, 24,28,62}90
235649
86
16
14
Returns: 1421495
{100,100,100,100,100,100,100,100,100,100, 100,100,100,100,100,100,100,100,100,100, 100,100,100,100,100,100,100,100,100,100, 100,100,100,100,100,100,100,100,100,100, 100,100,100,100,100,100,100,100,100,100}51
30000000
100
48
1
Returns: 4440984
{90,72,92,94,69,19,80,52,93,69,2,32,96,65, 10,90,3,41,8,34,47,30,16,39,67,19,37,68}28
15361502
63
22
6
Returns: 238046
{0, 100, 100, 100, 100, 45, 0}7
30000000
100
6
1
Returns: 1
{32,32,80,95,70,50,19,58,48,67,44,17, 55,38,24,43,92,12,36,85,88}30
123456
78
55
15
Returns: 0