Java 求助MLE

P1464 Function

Tomatos03 @ 2023-11-29 23:14:48

Subtask #1 第2个点 一直MLE,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

import static java.lang.Math.*;

public class Main {
    static HashMap<String, Integer> map = new HashMap<>();
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder ans = new StringBuilder();
        while (true) {
            String[] nums = in.readLine().split(" ");
            long a = Long.parseLong(nums[0]);
            long b = Long.parseLong(nums[1]);
            long c = Long.parseLong(nums[2]);
            if (a == -1 && b == -1 && c == -1) {
                break;
            }
            int val = w((int)min(a, 21), (int)min(b, 21), (int)min(c, 21));
            ans.append(String.format("w(%d, %d, %d) = %d\n", a, b, c, val));
        }
        System.out.print(ans);
        in.close();
    }
    static private int w(int a, int b, int c) {
        if (a <= 0 || b <= 0 || c <= 0) {
            return 1;
        }

        String cur = String.format("%d %d %d", a, b, c);
        if (map.containsKey(cur)) {
            return map.get(cur);
        }

        if (a > 20 || b > 20 || c > 20) {
            int d = w(20, 20, 20);
            map.put(cur, d);
            return d;
        }
        if (a < b && b < c) {
            int d = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c);
            map.put(cur, d);
            return d;
        }

        int d = w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1);
        map.put(cur, d);
        return d;
    }
}

by qq202225310113 @ 2024-02-19 03:01:38

用bufferedreader读取,scaner效率太低会爆掉


by Tomatos03 @ 2024-02-20 20:00:06

@qq202225310113 用的就是BufferedReader...


|