結果
| 問題 |
No.861 ケーキカット
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-07 17:38:33 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 550 ms / 1,000 ms |
| コード長 | 5,122 bytes |
| コンパイル時間 | 2,470 ms |
| コンパイル使用メモリ | 83,180 KB |
| 実行使用メモリ | 45,536 KB |
| 最終ジャッジ日時 | 2024-07-07 19:14:00 |
| 合計ジャッジ時間 | 11,236 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 |
ソースコード
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());}
}