第四問を解き直します。
こちらが問題。本当に基本的な2ケース以外全てTLEでした。性能あげます。
public class Main {
public static void main(String[] args) {
HashMap<Integer, P> map = new HashMap<>();
Scanner sc = new Scanner(System.in);
int result = 0;
// 整数の入力
int n = sc.nextInt();
IntStream.rangeClosed(1, n).forEach(i -> map.put(i, new P(i)));
for (int i = 1; i <= n; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
map.get(i).putZahyo(a, b, c);
}
// 入力完了
for (int i = 1; i <= n; i++) {
boolean isInside = false;
P point = map.get(i);
for (int j = 1; j <= n; j++) {
if (map.get(j).isInside(point)) {
isInside = true;
}
}
System.out.println(isInside ? "Yes" : "No");
}
}
}
class P {
private int myNum;
private ArrayList<Integer> zahyo = new ArrayList<>();
P(int a) {
myNum = a;
}
public void putZahyo(int x, int y, int z) {
zahyo.add(x);
zahyo.add(y);
zahyo.add(z);
}
public ArrayList<Integer> getZahyo() {
return zahyo;
}
public boolean isInside(P point) {
return point.getZahyo().get(0) > zahyo.get(0) && point.getZahyo().get(1) > zahyo.get(1)
&& point.getZahyo().get(2) > zahyo.get(2);
}
このような流れで改善していきました。
・Pをnewする→座標を代入をひとまとめに行う
・Pのコンストラクタ廃止、あわせてmyNumも不要なので廃止
・List<>()は重いので、intを3つフィールドで持つように修正
・そもそもクラスPを廃止して、Mapに直接intの配列をいれてみた
これで18/29・・・道のりは長い。。
public class Main {
public static void main(String[] args) {
HashMap<Integer, int[]> map = new HashMap<>();
Scanner sc = new Scanner(System.in);
// 整数の入力
int n = sc.nextInt();
IntStream.rangeClosed(1, n).forEach(i -> map.put(i, new int[] { sc.nextInt(), sc.nextInt(), sc.nextInt() }));
sc.close();
// 入力完了
for (int i = 1; i <= n; i++) {
boolean isInside = false;
int[] point = map.get(i);
for (int j = 1; j <= n; j++) {
int[] point2 = map.get(j);
if (point[0] > point2[0] && point[1] > point2[1] && point[2] > point2[2]) {
isInside = true;
break;
}
}
System.out.println(isInside ? "Yes" : "No");
}
}
}
今後いろいろ改良を試みましたが無理!!
今の自分の実力では無理・・・でした、、orz