結果

問題 No.861 ケーキカット
ユーザー 37zigen37zigen
提出日時 2020-04-07 17:20:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 637 ms / 1,000 ms
コード長 4,985 bytes
コンパイル時間 3,280 ms
コンパイル使用メモリ 74,964 KB
実行使用メモリ 57,640 KB
最終ジャッジ日時 2023-09-22 02:24:04
合計ジャッジ時間 13,942 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 256 ms
54,256 KB
testcase_01 AC 263 ms
54,768 KB
testcase_02 AC 47 ms
49,232 KB
testcase_03 AC 491 ms
57,640 KB
testcase_04 AC 237 ms
54,228 KB
testcase_05 AC 48 ms
49,280 KB
testcase_06 AC 318 ms
54,624 KB
testcase_07 AC 103 ms
53,696 KB
testcase_08 AC 49 ms
49,812 KB
testcase_09 AC 178 ms
54,476 KB
testcase_10 AC 105 ms
53,424 KB
testcase_11 AC 389 ms
56,892 KB
testcase_12 AC 632 ms
57,240 KB
testcase_13 AC 637 ms
57,496 KB
testcase_14 AC 501 ms
57,616 KB
testcase_15 AC 508 ms
57,156 KB
testcase_16 AC 548 ms
57,336 KB
testcase_17 AC 501 ms
57,484 KB
testcase_18 AC 550 ms
57,596 KB
testcase_19 AC 527 ms
57,548 KB
testcase_20 AC 530 ms
57,376 KB
testcase_21 AC 544 ms
57,568 KB
testcase_22 AC 489 ms
57,588 KB
testcase_23 AC 502 ms
57,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;


class DJSet{
	int n;
	int num;
	int[] upper;
	
	public DJSet(int n_) {
		n=n_;
		num=n_;
		upper=new int[n];
		Arrays.fill(upper, -1);
	}
	
	boolean equiv(int x,int y) {
		return root(x)==root(y);
	}
	
	int root(int x) {
		return upper[x]<0?x:(upper[x]=root(upper[x]));
	}
	
	void setUnion(int x,int y) {
		x=root(x);y=root(y);
		if(x==y)return;
		if(upper[x]<upper[y]) {
			x^=y;y^=x;x^=y;
		}
		if(upper[x]==upper[y])--upper[y];
		upper[x]=y;
		--num;
	}
}

public class Main{

	public static void main(String[] args) {
		new Main().run();
	}
	
	int f(int s) {
		int cnt=0;
		for(int i=0;i+1<5;++i)if((s>>i)%2!=(s>>(i+1))%2)++cnt;
		return cnt;
	}
	
	void run() {
		FastScanner sc=new FastScanner();
		int[][] C=new int[5][5];
		long[][] sum=new long[5][1<<5];
		long z=0;
		for(int i=0;i<5;++i) {
			for(int j=0;j<5;++j) {
				C[i][j]=sc.nextInt();
				z=Math.max(z, Math.abs(C[i][j]));
			}
		}
		for(int i=0;i<5;++i) {
			for(int s=0;s<1<<5;++s) {
				for(int j=0;j<5;++j) {
					if((s>>j)%2==1) sum[i][s]+=C[i][j];
					else sum[i][s]-=C[i][j];
				}
			}
		}
		int[] s=new int[5];
		long ans=Long.MAX_VALUE/3;
		for(int s0=0;s0<1<<5;s0+=2) {
			if(f(s0)>=3)continue;
			if(Math.max(Math.abs(sum[0][s0])-20*z,0)>=ans) continue;
			for(int s1=0;s1<1<<5;++s1) {
				if((s0&s1)==0&&s0>0&&s1>0)continue;
				if(Math.max(Math.abs(sum[0][s0]+sum[1][s1])-15*z,0)>=ans) continue;
				for(int s2=0;s2<1<<5;++s2) {
					if((s1&s2)==0&&s1>0&&s2>0)continue;
					if(Math.max(Math.abs(sum[0][s0]+sum[1][s1]+sum[2][s2])-10*z,0)>=ans) continue;
					for(int s3=0;s3<1<<5;++s3) {
						if((s2&s3)==0&&s2>0&&s3>0)continue;
						if(f(s0%2|s1%2<<1|s2%2<<2|s3%2<<3)>=3) continue;
						if(f((s0>>4)%2|(s1>>4)%2<<1|(s2>>4)%2<<2|(s3>>4)%2<<3)>=3) continue;
						if(Math.max(Math.abs(sum[0][s0]+sum[1][s1]+sum[2][s2]+sum[3][s3])-5*z,0)>=ans) continue;
						in:for(int s4=0;s4<1<<5;++s4) {
							if((s3&s4)==0&&s3>0&&s4>0)continue;
							if(f(s4)>=3) continue;
							if(f(s0%2|s1%2<<1|s2%2<<2|s3%2<<3|s4%2<<4)>=3) continue;
							if(f((s0>>4)%2|(s1>>4)%2<<1|(s2>>4)%2<<2|(s3>>4)%2<<3)>=3) continue;
							long cur=Math.abs(sum[0][s0]+sum[1][s1]+sum[2][s2]+sum[3][s3]+sum[4][s4]);
							if(cur>=ans) continue;
							DJSet ds=new DJSet(5*5);
							s[0]=s0;
							s[1]=s1;
							s[2]=s2;
							s[3]=s3;
							s[4]=s4;
							for(int i=0;i<5;++i) {
								for(int j=0;j<5;++j) {
									if(i+1<5&&(s[i]>>j)%2==(s[i+1]>>j)%2) ds.setUnion(i+5*j, (i+1)+5*j);
									if(j+1<5&&(s[i]>>j)%2==(s[i]>>(j+1))%2) ds.setUnion(i+5*j, i+5*(j+1));
								}
							}
							if(ds.num!=2) continue in;
							ans=Math.min(ans, Math.abs(cur));
						}
					}
				}
			}
		}
		System.out.println(ans);
	}
	
	
	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