結果

問題 No.861 ケーキカット
ユーザー 37zigen37zigen
提出日時 2020-04-07 17:38:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 820 ms / 1,000 ms
コード長 5,122 bytes
コンパイル時間 3,398 ms
コンパイル使用メモリ 88,592 KB
実行使用メモリ 60,304 KB
最終ジャッジ日時 2023-09-22 02:26:05
合計ジャッジ時間 16,716 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 317 ms
56,540 KB
testcase_01 AC 309 ms
56,960 KB
testcase_02 AC 75 ms
51,948 KB
testcase_03 AC 699 ms
60,000 KB
testcase_04 AC 318 ms
57,120 KB
testcase_05 AC 77 ms
49,672 KB
testcase_06 AC 378 ms
56,792 KB
testcase_07 AC 141 ms
56,800 KB
testcase_08 AC 72 ms
52,156 KB
testcase_09 AC 219 ms
56,596 KB
testcase_10 AC 133 ms
55,892 KB
testcase_11 AC 420 ms
59,104 KB
testcase_12 AC 736 ms
59,684 KB
testcase_13 AC 748 ms
59,880 KB
testcase_14 AC 500 ms
60,144 KB
testcase_15 AC 766 ms
59,464 KB
testcase_16 AC 635 ms
59,332 KB
testcase_17 AC 766 ms
60,212 KB
testcase_18 AC 820 ms
60,016 KB
testcase_19 AC 625 ms
59,680 KB
testcase_20 AC 762 ms
59,172 KB
testcase_21 AC 736 ms
60,304 KB
testcase_22 AC 528 ms
59,940 KB
testcase_23 AC 717 ms
59,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


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=new long[25];
		for(int i=0;i<5;++i) {
			for(int j=0;j<5;++j) {
				C[i][j]=sc.nextInt();
				z[i+j*5]=-Math.abs(C[i][j]);
			}
		}
		z=Arrays.stream(z).sorted().map(v->-v).toArray();
		Arrays.parallelPrefix(z, (a,b)->(a+b));
		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])-z[19],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])-z[14],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])-z[9],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])-z[4],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