Submission #1985669


Source Code Expand

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

using int64 = long long;

const int MAX_W = 301;
const int MAX_H = 301;
const int64 INF = (1LL << 50);

char C[MAX_W][MAX_H];   // C[i][j] := 
int cost[MAX_W][MAX_H][MAX_H];
int64 dp[MAX_H][MAX_H];

int H, W;

int calc_cost(int id, int LH, int RH) {
    if (cost[id][LH][RH] >= 0) return cost[id][LH][RH];
    if (LH == 0 or RH == 0) return cost[id][LH][RH] = 0;
    // どっちも1より大きい
    int idxl = H - LH + 1;
    int idxr = H - RH + 1;
    return calc_cost(id, LH - 1, RH - 1) + (C[id][idxl] == C[id + 1][idxr]);
}

int main() {
    cin >> H >> W;

    for (int r = 0; r < H; r++) {
        string s;
        cin >> s;
        for (int c = 0; c < W; c++) C[c][H - r] = s[c];
    }

    memset(cost, -1, sizeof(cost));

    int64 ans = 0;
    for (int c = 0; c < W - 1; c++) {
        // dp
        fill((int64*)dp, (int64*)(dp + MAX_H), INF);

        dp[0][0] = 0;
        for (int LH = 0; LH <= H; LH++) {
            for (int RH = 0; RH <= H; RH++) {
                if (LH == 0 and RH == 0) continue;
                if (LH > 0) dp[LH][RH] = min(dp[LH][RH], dp[LH - 1][RH] + calc_cost(c, LH, RH));
                if (RH > 0) dp[LH][RH] = min(dp[LH][RH], dp[LH][RH - 1] + calc_cost(c, LH, RH));
            }
        }

        ans += dp[H][H];
        // cerr << "c: " << dp[H][H] << endl;
    }
    cout << ans << endl;

    /*
    cerr << "deg: " << calc_cost(1, 2, 2) << endl;
    cerr << "deg: " << calc_cost(1, 1, 1) << endl;
    */
    
    return 0;
}

Submission Info

Submission Time
Task D - Friction
User myusa
Language C++14 (GCC 5.4.1)
Score 300
Code Size 1624 Byte
Status TLE
Exec Time 2104 ms
Memory 107520 KB

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 300 / 300 0 / 500
Status
AC × 5
AC × 20
AC × 28
TLE × 13
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 28 ms 107520 KB
0_001.txt AC 28 ms 107520 KB
0_002.txt AC 28 ms 107520 KB
0_003.txt AC 28 ms 107520 KB
0_004.txt AC 28 ms 107520 KB
1_005.txt AC 28 ms 107520 KB
1_006.txt AC 191 ms 107520 KB
1_007.txt AC 191 ms 107520 KB
1_008.txt AC 192 ms 107520 KB
1_009.txt AC 191 ms 107520 KB
1_010.txt AC 191 ms 107520 KB
1_011.txt AC 168 ms 107520 KB
1_012.txt AC 192 ms 107520 KB
1_013.txt AC 63 ms 107520 KB
1_014.txt AC 191 ms 107520 KB
1_015.txt AC 82 ms 107520 KB
1_016.txt AC 191 ms 107520 KB
1_017.txt AC 28 ms 107520 KB
1_018.txt AC 192 ms 107520 KB
1_019.txt AC 66 ms 107520 KB
1_020.txt AC 192 ms 107520 KB
1_021.txt AC 179 ms 107520 KB
1_022.txt AC 191 ms 107520 KB
2_023.txt AC 39 ms 107520 KB
2_024.txt TLE 2104 ms 107520 KB
2_025.txt TLE 2104 ms 107520 KB
2_026.txt TLE 2104 ms 107520 KB
2_027.txt TLE 2104 ms 107520 KB
2_028.txt TLE 2104 ms 107520 KB
2_029.txt AC 211 ms 107520 KB
2_030.txt TLE 2104 ms 107520 KB
2_031.txt AC 173 ms 107520 KB
2_032.txt TLE 2104 ms 107520 KB
2_033.txt TLE 2104 ms 107520 KB
2_034.txt TLE 2104 ms 107520 KB
2_035.txt TLE 2104 ms 107520 KB
2_036.txt TLE 2104 ms 107520 KB
2_037.txt AC 330 ms 107520 KB
2_038.txt TLE 2104 ms 107520 KB
2_039.txt AC 160 ms 107520 KB
2_040.txt TLE 2104 ms 107520 KB