結果
| 問題 |
No.165 四角で囲え!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-03 17:22:05 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 2,238 ms / 5,000 ms |
| コード長 | 4,203 bytes |
| コンパイル時間 | 2,198 ms |
| コンパイル使用メモリ | 79,372 KB |
| 実行使用メモリ | 52,552 KB |
| 最終ジャッジ日時 | 2024-07-03 01:04:18 |
| 合計ジャッジ時間 | 45,895 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 19 |
ソースコード
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());}
}