代码没什么问题,就是java太慢了,在这很多都超时,这有啥办法捏~(摊手)

P3397 地毯

youyang123123 @ 2023-01-26 17:07:15

import java.util.*;

/**
 * @ClassName : P3397地毯  //类名
 * @Description :   //描述
 * @Author : 悠扬 //作者
 * @Date: 2023/1/26  15:52
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);;
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int [][]diff = new int[n+2][n+2];
        for(int i = 0;i<m;i++){
            int x1 = scanner.nextInt();
            int y1 = scanner.nextInt();
            int x2 = scanner.nextInt();
            int y2 = scanner.nextInt();
            //左上角
            diff[x1][y1]++;
            //右下角
            diff[x2+1][y2+1]++;
            //右上角
            diff[x1][y2+1]--;
            //左下角
            diff[x2+1][y1]--;
        }
        //复原数组
        for(int i = 1;i<=n;i++){
            for (int j = 1; j <= n; j++) {
                diff[i][j] += diff[i-1][j]+diff[i][j-1]-diff[i-1][j-1];
            }
        }
        for(int i = 1;i<=n;i++){
            for(int j = 1;j<=n;j++){
                System.out.print(diff[i][j]+" ");
            }
            System.out.println();
        }
    }

by Killer_joke @ 2023-01-26 17:19:11

@youyang123123 您可以尝试一下更好的输出方式(


by BaiChuan_Nayuta @ 2023-03-11 16:30:00

加一加一!!我也这样,不知道该怎么优化了。。。。同样代码用c++就过了


by youyang123123 @ 2023-03-12 12:04:18

@BaiChuan_Nayuta 用输入/出流咯 BufferReader PrintWriter


by youyang123123 @ 2023-03-12 12:08:27

java代码

import java.util.*;
import java.io.*;
/**
 * @ClassName : P3397地毯  //类名
 * @Description :   //描述
 * @[Author](/user/583141) : 悠扬 //作者
 * @Date: 2023/1/26  15:52
 */
public class Main {
    public static void main(String[] args)  throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
        String []s = bf.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);
        int [][]diff = new int[n+2][n+2];
        for(int i = 0;i<m;i++){
            String[]s1 = bf.readLine().split(" ");
            int x1 = Integer.parseInt(s1[0]);
            int y1 = Integer.parseInt(s1[1]);
            int x2 = Integer.parseInt(s1[2]);
            int y2 = Integer.parseInt(s1[3]);
            //左上角
            diff[x1][y1]++;
            //右下角
            diff[x2+1][y2+1]++;
            //右上角
            diff[x1][y2+1]--;
            //左下角
            diff[x2+1][y1]--;
        }
        //复原数组
        for(int i = 1;i<=n;i++){
            for (int j = 1; j <= n; j++) {
                diff[i][j] += diff[i-1][j]+diff[i][j-1]-diff[i-1][j-1];
            }
        }
        for(int i = 1;i<=n;i++){
            for(int j = 1;j<=n;j++){
                pw.print(diff[i][j]+" ");
            }
            pw.println();
        }
        pw.flush();
    }
}

by youyang123123 @ 2023-03-12 12:09:23

@Killer_joke 谢谢,当时没有学会


by BaiChuan_Nayuta @ 2023-03-17 09:07:41

@youyang123123 感谢!!!


|