結果

問題 No.3558 Dominoes, Black and White
コンテスト
ユーザー 37zigen
提出日時 2026-05-29 20:14:51
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 263 ms / 2,000 ms
コード長 7,417 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,845 ms
コンパイル使用メモリ 96,384 KB
実行使用メモリ 62,752 KB
最終ジャッジ日時 2026-05-29 20:15:13
合計ジャッジ時間 17,699 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge2_0
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
部分点 10 % AC * 30
満点 90 % AC * 89
合計 100 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.DoubleUnaryOperator;
import java.util.function.IntBinaryOperator;
import java.util.function.IntFunction;
import java.util.function.IntToDoubleFunction;
import java.util.function.IntToLongFunction;
import java.util.function.IntUnaryOperator;
import java.util.function.LongBinaryOperator;
import java.util.function.LongToDoubleFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;
import java.util.random.RandomGenerator;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Main {
    static MyPrintWriter pw = MyPrintWriter.getInstance();

    static FastScanner sc = FastScanner.getInstance();

    public static void main(String[] args) throws IOException {
        Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.exit(1));
        new Main().run();
        pw.flush();
    }

    void run() {
        int N = sc.nextInt();
        char[][] S = sc.nextChars(N, 2 * N);
        int[] A = new int[N * N];
        int[] B = new int[N * N];
        int p = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < (2 * N); j++) {
                if (S[i][j] == '.') {
                    A[p] = j;
                    B[p] = i;
                    p++;
                }
            }
        }
        Arrays.sort(A);
        Arrays.sort(B);
        long ans = ArrayUtils.sum(A) - (MathUtils.pow1Sum(0, N) * N);
        for (int i = 0; i < B.length; i++) {
            ans += Math.abs(B[i] - (i / N));
        }
        pw.println(ans);
    }
}

class ArrayUtils {
    public static long sum(int[] a) {
        long ret = 0;
        for (int val : a) {
            ret += val;
        }
        return ret;
    }
}

class FastScanner {
    private static FastScanner instance = null;

    private final InputStream in = System.in;

    private final byte[] buffer = new byte[1 << 16];

    private int ptr = 0;

    private int buflen = 0;

    private FastScanner() {
    }

    public static FastScanner getInstance() {
        if (instance == null) {
            instance = new FastScanner();
        }
        return instance;
    }

    private boolean hasNextByte() {
        if (this.ptr < this.buflen) {
            return true;
        }
        this.ptr = 0;
        try {
            this.buflen = this.in.read(this.buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this.buflen > 0;
    }

    private int readByte() {
        if (hasNextByte()) {
            return this.buffer[this.ptr++];
        } else {
            return -1;
        }
    }

    private boolean isPrintableChar(int c) {
        return (33 <= c) && (c <= 126);
    }

    public boolean hasNext() {
        while (hasNextByte() && (!isPrintableChar(this.buffer[this.ptr]))) {
            this.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();
        }
        while ((b >= '0') && (b <= '9')) {
            // n = n * 10 + (b - '0');
            n = ((n << 1) + (n << 3)) + (b - '0');
            b = readByte();
        } 
        return minus ? -n : n;
    }

    public int nextInt() {
        return ((int) (nextLong()));
    }

    public char[][] nextChars(int H, int W) {
        if (W == 0) {
            return new char[H][0];
        }
        char[][] a = new char[H][];
        for (int i = 0; i < H; i++) {
            a[i] = next().toCharArray();
        }
        return a;
    }
}

class MathUtils {
    /**
     * start+(start+1)+..+(start+length-1)
     *
     * @param a
     * @param b
     * @return  */
    public static long pow1Sum(long start, long length) {
        if (length <= 0) {
            return 0;
        }
        return (length * ((length - 1) + (2 * start))) / 2;
    }
}

class MergeFiles {}

class MyPrintWriter extends PrintWriter {
    private static MyPrintWriter instance = null;

    private MyPrintWriter() {
        super(System.out);
    }

    public static MyPrintWriter getInstance() {
        if (instance == null) {
            instance = new MyPrintWriter();
        }
        return instance;
    }
}


// --- Original Code ---
// import java.io.IOException;
// import java.util.Arrays;
// 
// import library.tools.FastScanner;
// import library.tools.MergeFiles;
// import library.tools.MyPrintWriter;
// import library.util.ArrayUtils;
// import library.util.MathUtils;
// import library.util.polynomial.PolynomialFpDynamic;
// import library.util.polynomial.PolynomialFpDynamic2D;
// import library.util.polynomial.PolynomialFpDynamic3D;
// import library.util.polynomial.PolynomialLong2D;
// import library.util.polynomial.PolynomialLong3D;
// 
// public class Main {
// 	static MyPrintWriter pw = MyPrintWriter.getInstance();
// 	static FastScanner sc = FastScanner.getInstance();
// 
// 	public static void main(String[] args) throws IOException {
// 		new Main().run();
// 		pw.flush();
// 		MergeFiles.export();
// 	}
// 
// 	void run() {
// 		int N=sc.nextInt();
// 		char[][]S=sc.nextChars(N, 2*N);
// 		int[]A=new int[N*N];
// 		int[]B=new int[N*N];
// 		int p=0;
// 		for (int i = 0; i < N; i++) {
// 			for (int j = 0; j < 2*N; j++) {
// 				if(S[i][j]=='.') {
// 					A[p]=j;
// 					B[p]=i;
// 					p++;
// 				}
// 			}
// 		}
// 		Arrays.sort(A);
// 		Arrays.sort(B);
// 		long ans=ArrayUtils.sum(A)-MathUtils.pow1Sum(0, N)*N;
// 		for (int i = 0; i < B.length; i++) {
// 			ans+=Math.abs(B[i]-i/N);
// 		}
// 		pw.println(ans);
// 	}
// 	
// 	
// 
// 	void tr(Object... objects) {
// 		System.out.println(Arrays.deepToString(objects));
// 	}
// }
0