SRM293Siv2,250

この記事は約4分で読めます。

You have a fish aquarium. You are given a int[] fishSizes, denoting the size of each fish in the aquarium. They’ve been getting along pretty well so far, but you would now like to add a new fish (called Bob). You know that fish sometimes eat each other. You estimate that a fish might eat another fish if and only if it is at least two times larger, but no more than ten times larger, than that other fish.

Considering this, you would like to choose Bob’s size such that:

– Bob is not in danger of being eaten by any other fish (i.e., its size is not between 1/10 and 1/2, inclusive, the size of any other fish).

– Bob is not tempted to eat any other fish (i.e., no other fish in the aquarium has a size between 1/10 and 1/2, inclusive, of Bob’s size).

Your task is to return the number of different integer sizes for Bob between minSize and maxSize, inclusive, that won’t cause any eating “conflicts” with other fish.

[java]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Aquarium {

public int peaceful(int minSize, int maxSize, int[] fishSizes) {
List<Integer> bobs = new ArrayList<Integer>();
for (int bob = minSize; bob <= maxSize; bob++) {
int frag = 0;
for (int j = 0; j < fishSizes.length; j++) {
if ((fishSizes[j] * 2) <= bob && bob <= (fishSizes[j] * 10)) {
frag=1;
break;
}
}
if (frag==0) {
bobs.add(bob);
}
}
//System.out.println(bobs);
int count = 0;
for (int bob = bobs.size() – 1; bob >= 0; bob–) {
int flag = 0;
for (int fish = 0; fish < fishSizes.length; fish++) {
if ((bobs.get(bob) * 2) <= fishSizes[fish] && fishSizes[fish] <= (bobs.get(bob) * 10)) {
// bobs.remove(bob);
flag = 1;
break;
}
}
if (flag == 0) {
count++;
}
}
//System.out.println(count);
//System.out.println("▶" + bobs);
return count;
}

}
[/java]

コメント

タイトルとURLをコピーしました