package jp.yucchi.dp4lambda;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class DP4Lambda {
public static void main(String[] args) {
List<Person> person = new ArrayList<>();
person.add(new Person("柴田", "恭平", LocalDate.of(1951, 8, 18), Person.Sex.MALE, 70, 8));
person.add(new Person("壇", "蜜", LocalDate.of(1980, 12, 3), Person.Sex.FEMALE, 60, 6));
person.add(new Person("北川", "景子", LocalDate.of(1986, 8, 22), Person.Sex.FEMALE, 55, 7));
person.add(new Person("綾瀬", "はるか", LocalDate.of(1985, 3, 24), Person.Sex.FEMALE, 50, 4));
person.add(new Person("佐々木", "希", LocalDate.of(1988, 2, 8), Person.Sex.FEMALE, 48, 9));
person.add(new Person("剛力", "彩芽", LocalDate.of(1992, 8, 27), Person.Sex.FEMALE, 45, 6));
person.add(new Person("小栗", "旬", LocalDate.of(1982, 12, 26), Person.Sex.MALE, 65, 3));
person.add(new Person("堀北", "真希", LocalDate.of(1988, 10, 6), Person.Sex.FEMALE, 45, 10));
person.add(new Person("武井", "咲", LocalDate.of(1993, 12, 25), Person.Sex.FEMALE, 50, 5));
person.add(new Person("市原", "隼人", LocalDate.of(1987, 2, 6), Person.Sex.MALE, 67, 2));
person.add(new Person("深田", "恭子", LocalDate.of(1982, 11, 2), Person.Sex.FEMALE, 50, 8));
int maxW = 180;
int[] pw = person.parallelStream()
.filter(e -> e.getGender() == Person.Sex.FEMALE)
.mapToInt(p -> p.getWeight()).toArray();
int[] pv = person.parallelStream()
.filter(e -> e.getGender() == Person.Sex.FEMALE)
.mapToInt(p -> p.getEvaluation()).toArray();
int[][] dp = new int[pw.length + 1][maxW + 1];
IntStream.range(0, pw.length).forEach(i -> {
IntStream.rangeClosed(0, maxW).forEach(j -> {
if (j < pw[i]) {
dp[i + 1][j] = dp[i][j];
} else {
dp[i + 1][j] = Math.max(dp[i][j], dp[i][j - pw[i]] + pv[i]);
}
});
});
System.out.println("合計評価 : " + dp[pw.length][maxW]);
boolean[] isChoice = new boolean[pw.length];
int mW = maxW;
for (int i = pw.length - 1; i >= 0; i--) {
if (pw[i] <= mW && dp[i][mW] < dp[i][mW - pw[i]] + pv[i]) {
isChoice[i] = true;
mW -= pw[i];
}
}
IntStream.range(0, isChoice.length).forEach(e -> {
if (isChoice[e]) {
// Optional 使用
Optional<Person> p = person.parallelStream().filter(k -> k.getGender() == Person.Sex.FEMALE).substream(e, e + 1).findFirst();
if (p.isPresent()) {
p.get().printPerson();
} else {
System.out.println("エラー発生! (×_×)");
}
// Person 使用
// Person pp = person.parallelStream().filter(k -> k.getGender() == Person.Sex.FEMALE).collect(Collectors.<Person>toList()).get(e);
// pp.printPerson();
}
});
}
}