Submission #1986308


Source Code Expand

import java.io.*;
import java.util.*;
import java.math.*;
// import java.awt.Point;
 
public class Main {
    InputStream is;
    PrintWriter out;
    String INPUT = "";
 
    long MOD = 1_000_000_007;
    int inf = Integer.MAX_VALUE/2;


    void solve(){
        int h = ni();
        int w = ni();
        char[][] b = nm(h,w);
        int ans = 0;
        for(int k = 0; k < w-1; k++){
            int[][] cost = new int[h+1][h+1];
            for(int i = 1; i <= h; i++){
                for(int j = 1; j <= h; j++){
                    cost[i][j] = cost[i-1][j-1] + (b[i-1][k]==b[j-1][k+1] ? 1 : 0);
                }
            }
            int[][] dp = new int[h+1][h+1];
            for(int i = 0; i <= h; i++){
                for(int j = 0; j <= h; j++){
                    if(i==0 && j==0) continue;
                    else if(i==0) dp[i][j] = dp[i][j-1]+cost[h-i][h-(j-1)];
                    else if(j==0) dp[i][j] = dp[i-1][j]+cost[h-(i-1)][h-j];
                    else dp[i][j] = Math.min(dp[i-1][j]+cost[h-(i-1)][h-j], dp[i][j-1]+cost[h-i][h-(j-1)]);
                }
            }
            ans += dp[h][h];
        }
        out.println(ans);
    }    

    void run() throws Exception
    {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        solve();
        out.flush();
        if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
    }
    
    public static void main(String[] args) throws Exception { new Main().run(); }
    
    private byte[] inbuf = new byte[1024];
    private int lenbuf = 0, ptrbuf = 0;
    
    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }
    
    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
    
    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }
    
    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b) && b != ' ')){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    
    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }
    
    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }
    
    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }
    
    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
    
    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
    
    private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
 
}

Submission Info

Submission Time
Task D - Friction
User yuya178
Language Java8 (OpenJDK 1.8.0)
Score 800
Code Size 4381 Byte
Status AC
Exec Time 447 ms
Memory 93116 KB

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 300 / 300 500 / 500
Status
AC × 5
AC × 20
AC × 41
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt
Subtask 0_000.txt, 0_001.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 2_023.txt, 2_024.txt, 2_025.txt, 2_026.txt, 2_027.txt, 2_028.txt, 2_029.txt, 2_030.txt, 2_031.txt, 2_032.txt, 2_033.txt, 2_034.txt, 2_035.txt, 2_036.txt, 2_037.txt, 2_038.txt, 2_039.txt, 2_040.txt
Case Name Status Exec Time Memory
0_000.txt AC 69 ms 23252 KB
0_001.txt AC 69 ms 19156 KB
0_002.txt AC 68 ms 18260 KB
0_003.txt AC 70 ms 19156 KB
0_004.txt AC 67 ms 18644 KB
1_005.txt AC 68 ms 19028 KB
1_006.txt AC 93 ms 20564 KB
1_007.txt AC 94 ms 21844 KB
1_008.txt AC 93 ms 25684 KB
1_009.txt AC 93 ms 24020 KB
1_010.txt AC 91 ms 20564 KB
1_011.txt AC 94 ms 19924 KB
1_012.txt AC 94 ms 23892 KB
1_013.txt AC 93 ms 19540 KB
1_014.txt AC 94 ms 22228 KB
1_015.txt AC 81 ms 20180 KB
1_016.txt AC 94 ms 21972 KB
1_017.txt AC 70 ms 18644 KB
1_018.txt AC 94 ms 22100 KB
1_019.txt AC 82 ms 21588 KB
1_020.txt AC 93 ms 21076 KB
1_021.txt AC 93 ms 18516 KB
1_022.txt AC 94 ms 22612 KB
2_023.txt AC 68 ms 18260 KB
2_024.txt AC 438 ms 84908 KB
2_025.txt AC 436 ms 85012 KB
2_026.txt AC 428 ms 86568 KB
2_027.txt AC 365 ms 87056 KB
2_028.txt AC 444 ms 84140 KB
2_029.txt AC 126 ms 31492 KB
2_030.txt AC 418 ms 93116 KB
2_031.txt AC 122 ms 25860 KB
2_032.txt AC 441 ms 86988 KB
2_033.txt AC 166 ms 43012 KB
2_034.txt AC 440 ms 84524 KB
2_035.txt AC 302 ms 58668 KB
2_036.txt AC 447 ms 83732 KB
2_037.txt AC 117 ms 26584 KB
2_038.txt AC 441 ms 90448 KB
2_039.txt AC 107 ms 24668 KB
2_040.txt AC 439 ms 83536 KB