为啥Java代码有俩实例MLE呢?检查了半天没看出来哪里超内存限制了!
查看原帖
为啥Java代码有俩实例MLE呢?检查了半天没看出来哪里超内存限制了!
357027
v7fgg楼主2022/9/30 22:00

为啥Java代码有俩实例MLE呢?检查了半天没看出来哪里超内存限制了!

import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class Main{
    public static void main(String args[]) throws IOException{
        int mod=20123;
        Read sc=new Read();
        int n=sc.nextInt();
        int m=sc.nextInt();
        //int arr[][][]=new int[n][m][];
        int count[]=new int[n];
        boolean aa[][]=new boolean[n][m];
        int bb[][]=new int[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                int a=sc.nextInt();
                int b=sc.nextInt();
                aa[i][j]=a==1?true:false;
                bb[i][j]=b;
                if(a==1){
                    count[i]++;
                }
            }
        }
        int x=sc.nextInt();//进入x号房间开始寻宝
        long ans=0;
        for(int i=0;i<n;i++){
            ans+=bb[i][x];
            int k=bb[i][x]%count[i];
            if(k==0){
                k=count[i];
            }
            while(k>0){
                if(aa[i][x]){
                    k--;
                    if(k==0){
                        break;
                    }
                }
                x=(x+1)%m;
            }
        }
        sc.println(ans%mod);
        sc.bw.flush();
        sc.bw.close();
    }
}
//记住看数字范围,需要开long吗,需要用BigInteger吗,需要手动处理字符串吗,复杂度数量级控制在1e7或者以下了吗
//开数组的数据范围最高不能超过1e7,数据范围再大就要用哈希表离散化了
//基本数据类型不能自定义sort排序,二维数组就可以了,顺序排序的时候是小减大,注意返回值应该是int
class Read{
    BufferedReader bf;
    StringTokenizer st;
    BufferedWriter bw;
    public Read(){
        bf=new BufferedReader(new InputStreamReader(System.in));
        st=new StringTokenizer("");
        bw=new BufferedWriter(new OutputStreamWriter(System.out));
    }
    public String nextLine() throws IOException{
        return bf.readLine();
    }
    public String next() throws IOException{
        while(!st.hasMoreTokens()){
            st=new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    public char nextChar() throws IOException{
        //确定下一个token只有一个字符的时候再用
        return next().charAt(0);
    }
    public int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
    public long nextLong() throws IOException{
        return Long.parseLong(next());
    }
    public double nextDouble() throws IOException{
        return Double.parseDouble(next());
    }
    public float nextFloat() throws IOException{
        return Float.parseFloat(next());
    }
    public byte nextByte() throws IOException{
        return Byte.parseByte(next());
    }
    public short nextShort() throws IOException{
        return Short.parseShort(next());
    }
    public BigInteger nextBigInteger() throws IOException{
        return new BigInteger(next());
    }
    public void println(int a) throws IOException{
        bw.write(String.valueOf(a));
        bw.newLine();
        return;
    }
    public void print(int a) throws IOException{
        bw.write(String.valueOf(a));
        return;
    }
    public void println(String a) throws IOException{
        bw.write(a);
        bw.newLine();
        return;
    }
    public void print(String a) throws IOException{
        bw.write(a);
        return;
    }
    public void println(long a) throws IOException{
        bw.write(String.valueOf(a));
        bw.newLine();
        return;
    }
    public void print(long a) throws IOException{
        bw.write(String.valueOf(a));
        return;
    }
    public void println(double a) throws IOException{
        bw.write(String.valueOf(a));
        bw.newLine();
        return;
    }
    public void print(double a) throws IOException{
        bw.write(String.valueOf(a));
        return;
    }
    public void print(BigInteger a) throws IOException{
        bw.write(a.toString());
        return;
    }
    public void print(char a) throws IOException{
        bw.write(String.valueOf(a));
        return;
    }
    public void println(char a) throws IOException{
        bw.write(String.valueOf(a));
        bw.newLine();
        return;
    }
}
2022/9/30 22:00
加载中...