## Wednesday, December 2, 2015

### FlowerGarden - TopCoder

https://community.topcoder.com/stat?c=problem_statement&pm=1918&rd=5006

You are planting a flower garden with bulbs to give you joyous flowers throughout the year. However, you wish to plant the flowers such that they do not block other flowers while they are visible.
You will be given a int[] height, a int[] bloom, and a int[] wilt. Each type of flower is represented by the element at the same index of heightbloom, and wiltheight represents how high each type of flower grows, bloom represents the morning that each type of flower springs from the ground, and wilt represents the evening that each type of flower shrivels up and dies. Each element in bloom and wilt will be a number between 1 and 365 inclusive, and wilt[i] will always be greater than bloom[i]. You must plant all of the flowers of the same type in a single row for appearance, and you also want to have the tallest flowers as far forward as possible. However, if a flower type is taller than another type, and both types can be out of the ground at the same time, the shorter flower must be planted in front of the taller flower to prevent blocking. A flower blooms in the morning, and wilts in the evening, so even if one flower is blooming on the same day another flower is wilting, one can block the other.
You should return a int[] which contains the elements of height in the order you should plant your flowers to acheive the above goals. The front of the garden is represented by the first element in your return value, and is where you view the garden from. The elements of height will all be unique, so there will always be a well-defined ordering.

### Definition

 Class: FlowerGarden Method: getOrdering Parameters: int[], int[], int[] Returns: int[] Method signature: int[] getOrdering(int[] height, int[] bloom, int[] wilt) (be sure your method is public)

### Constraints

-height will have between 2 and 50 elements, inclusive.
-bloom will have the same number of elements as height
-wilt will have the same number of elements as height
-height will have no repeated elements.
-Each element of height will be between 1 and 1000, inclusive.
-Each element of bloom will be between 1 and 365, inclusive.
-Each element of wilt will be between 1 and 365, inclusive.
-For each element i of bloom and wiltwilt[i] will be greater than bloom[i].

### Examples

0)

 `{5,4,3,2,1}` `{1,1,1,1,1}` `{365,365,365,365,365}`
`Returns: { 1,  2,  3,  4,  5 }`
 These flowers all bloom on January 1st and wilt on December 31st. Since they all may block each other, you must order them from shortest to tallest.
1)

 `{5,4,3,2,1}` `{1,5,10,15,20}` `{4,9,14,19,24}`
`Returns: { 5,  4,  3,  2,  1 }`
 The same set of flowers now bloom all at separate times. Since they will never block each other, you can order them from tallest to shortest to get the tallest ones as far forward as possible.
2)

 `{5,4,3,2,1}` `{1,5,10,15,20}` `{5,10,15,20,25}`
`Returns: { 1,  2,  3,  4,  5 }`
 Although each flower only blocks at most one other, they all must be ordered from shortest to tallest to prevent any blocking from occurring.
3)

 `{5,4,3,2,1}` `{1,5,10,15,20}` `{5,10,14,20,25}`
`Returns: { 3,  4,  5,  1,  2 }`
 The difference here is that the third type of flower wilts one day earlier than the blooming of the fourth flower. Therefore, we can put the flowers of height 3 first, then the flowers of height 4, then height 5, and finally the flowers of height 1 and 2. Note that we could have also ordered them with height 1 first, but this does not result in the maximum possible height being first in the garden.
4)

 `{1,2,3,4,5,6}` `{1,3,1,3,1,3}` `{2,4,2,4,2,4}`
`Returns: { 2,  4,  6,  1,  3,  5 }`
5)

 `{3,2,5,4}` `{1,2,11,10}` `{4,3,12,13}`
`Returns: { 4,  5,  2,  3 }`
http://www.cnblogs.com/lautsie/p/3256269.html

2.基本思路就是先找到在第一个位置的花。这样就双层遍历，找到所有和其他不冲突的花中最高的一个，然后放到结果的首位。然后去掉此花，继续使用此方法对余下花朵。
3.要注意的是首先不能直接排序，比如a和b，b和c有冲突，但a和c不一定有冲突。
4.判断两段是否重叠的最简单式子是!(a.start > b.end || b.start > a.end)
`    ``public` `int``[] getOrdering(``int``[] height, ``int``[] bloom, ``int``[] wilt) {`
`        ``int` `len = height.length;`
`        ``if` `(len == ``0``) ``return` `height; ``// assert length != 0`
`        ``int` `order[] = ``new` `int``[len];`
`        ``boolean` `used[] = ``new` `boolean``[len];`

`        ``for` `(``int` `i = ``0``; i < len; i++) {`
`            ``int` `mxH = ``0``;`
`            ``int` `pos = -``1``;`
`        `
`            ``for` `(``int` `j = ``0``; j < len; j++) {`
`                ``if` `(used[j]) ``continue``;`
`                ``boolean` `found = ``true``;`
`                ``for` `(``int` `k = ``0``; k < len; k++) {`
`                    ``if` `(used[k]) ``continue``;`
`                    ``boolean` `blocking = !(bloom[j] > wilt[k] || bloom [k] > wilt[j]);`
`                    ``if` `(height[j] > height[k] && blocking) {`
`                        ``found = ``false``;`
`                        ``break``;`
`                    ``}`
`                ``}`
`                ``if` `(found) {`
`                    ``if` `(height[j] > mxH) {`
`                        ``mxH = height[j];`
`                        ``pos = j;`
`                    ``}`
`                ``}`
`            ``}`
`            ``order[i] = height[pos];`
`            ``used[pos] = ``true``;`
`        ``}`
`        ``return` `order;`
`    ``}`

http://stackoverflow.com/questions/11248764/how-is-the-flowergarden-pr0blem-on-topcoder-a-dp-one
http://www.topcoder.com/tc?module=Static&d1=match_editorials&d2=tccc04_online_rd_1
This problem is pretty simple to solve -- as long as you are solving the correct problem! A subtle ambiguity in the problem statement made the problem seem more difficult than it really was. For those of you who missed the broadcast, the problem statement will be updated soon to make the statement clear for the practice room. In short, you are looking to make flowers which are closest to the front of the garden as tall as possible, not trying to put the tallest flower as close as possible.
Sorting the flowers in this garden isn't as simple as writing a sort comparison routine, because you can't always tell what the ordering will be with just two elements. In reality, the ordering is easy to compute if you think about the flowers one at a time. The first step is to determine which flowers cannot go in front of other flowers. This is done by comparing the bloom date of each flower with the bloom and wilt date of every other flower. If two flowers conflict, then the bloom date of one will lay between the bloom and wilt date of the other. Within a conflict, the larger of the two cannot be placed in front of the other, or blockage will occur.
When figuring out which type of flower to place next, you can only place flowers that can go in front of all the flowers left to place. Of those, you should place the type which is tallest. For the next type, the flower type just placed no longer can be blocked, so it could allow more flower types to be placed. Because there can be no circular dependencies, this algorithm can be used to place the entire garden.
https://github.com/irpap/TopCoder/blob/master/FlowerGarden/src/FlowerGarden.java
private Flower[] flowers;

public int[] getOrdering(int[] height, int[] bloom, int[] wilt) {
flowers = new Flower[height.length];
for (int i = 0; i < height.length; i++) {
flowers[i] = new Flower(height[i], bloom[i], wilt[i]);
}

Arrays.sort(flowers, new Comparator<Flower>() {
public int compare(Flower f1, Flower f2) {
if (livesOverlap(f1, f2)) {
f1.born = f2.born = Math.min(f1.born, f2.born);
f1.die = f2.die = Math.max(f1.die, f2.die);
return f1.height - f2.height;

} else return (f2.height - f1.height);
}
});

int[] order = new int[flowers.length];

for (int i = 0; i < flowers.length; i++) {
order[i] = flowers[i].height;
}
return order;
}

private boolean livesOverlap(Flower f1, Flower f2) {
return Math.min(f1.die, f2.die) >= Math.max(f1.born, f2.born);
}

private static class Flower {
int height;
int born;
int die;

private Flower(int height, int born, int die) {
this.height = height;
this.born = born;
this.die = die;
}
}