結果

問題 No.165 四角で囲え!
ユーザー 37zigen37zigen
提出日時 2020-04-03 17:22:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,548 ms / 5,000 ms
コード長 4,203 bytes
コンパイル時間 5,897 ms
コンパイル使用メモリ 76,220 KB
実行使用メモリ 59,272 KB
最終ジャッジ日時 2023-09-15 23:53:53
合計ジャッジ時間 52,872 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,130 ms
56,824 KB
testcase_01 AC 1,915 ms
56,460 KB
testcase_02 AC 1,914 ms
56,708 KB
testcase_03 AC 1,916 ms
56,508 KB
testcase_04 AC 1,917 ms
56,760 KB
testcase_05 AC 2,185 ms
56,952 KB
testcase_06 AC 2,200 ms
56,576 KB
testcase_07 AC 1,958 ms
56,396 KB
testcase_08 AC 1,908 ms
56,968 KB
testcase_09 AC 1,911 ms
56,404 KB
testcase_10 AC 1,928 ms
56,596 KB
testcase_11 AC 2,134 ms
58,956 KB
testcase_12 AC 2,000 ms
56,736 KB
testcase_13 AC 2,115 ms
56,940 KB
testcase_14 AC 2,056 ms
57,032 KB
testcase_15 AC 2,025 ms
56,944 KB
testcase_16 AC 2,209 ms
57,128 KB
testcase_17 AC 1,982 ms
57,564 KB
testcase_18 AC 2,548 ms
57,140 KB
testcase_19 AC 2,256 ms
59,272 KB
testcase_20 AC 1,943 ms
55,328 KB
testcase_21 AC 2,107 ms
57,684 KB
testcase_22 AC 2,006 ms
56,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	long access(int[][] a,int i,int j) {
		if(i<0||j<0)return 0;
		return a[i][j];
	}
	
	int[] get(int[][][] sum,int left,int right,int top,int bottom){
		int[] ret=new int[2];
		for(int t=0;t<2;++t) {
			ret[t]=(int)(access(sum[t],bottom-1,right-1)-access(sum[t],bottom-1,left-1)-access(sum[t],top-1,right-1)+access(sum[t],top-1,left-1));
		}
		return ret;
	}
	
	void solve(int N,int B,int[] X,int[] Y,int[] P) {
		int M=400;
		int[][][] sum=new int[2][M][M];
		for(int i=0;i<N;++i) {
			sum[0][Y[i]][X[i]]+=1;
			sum[1][Y[i]][X[i]]+=P[i];
		}
		for(int t=0;t<2;++t) {
			for(int i=0;i<M;++i) {
				for(int j=0;j<M;++j) {
					sum[t][i][j]+=(int)(access(sum[t],i-1,j)+access(sum[t],i,j-1)-access(sum[t],i-1,j-1));
				}
			}
		}
		
		int ans=0;
		for(int left=0;left<M;++left) {
			for(int top=0;top<M;++top) {
				int right=M;
				for(int bottom=top+1;bottom<=M;++bottom) {
					while(get(sum,left,right,top,bottom)[1]>B)--right;
					int[] a=get(sum,left,right,top,bottom);
					ans=Math.max(ans, get(sum,left,right,top,bottom)[0]);
				}
			}
		}
		System.out.println(ans);
	}
	
	void run() {
		FastScanner sc=new FastScanner();
		int N=sc.nextInt();
		int B=sc.nextInt();
		int[] X=new int[N];
		int[] Y=new int[N];
		int[] P=new int[N];
		for(int i=0;i<N;++i) {
			X[i]=sc.nextInt();
			Y[i]=sc.nextInt();
			P[i]=sc.nextInt();
		}
		int[] X_distinct=Arrays.stream(X).distinct().sorted().toArray();
		int[] Y_distinct=Arrays.stream(Y).distinct().sorted().toArray();
		for(int i=0;i<N;++i) {
			for(int j=0;j<X_distinct.length;++j) {
				if(X[i]==X_distinct[j]) {
					X[i]=j;
					break;
				}
			}
			for(int j=0;j<Y_distinct.length;++j) {
				if(Y[i]==Y_distinct[j]) {
					Y[i]=j;
					break;
				}
			}
		}
		solve(N,B,X,Y,P);
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}


class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}
0