1393D - Rarity and New Dress - CodeForces Solution


dfs and similar dp implementation shortest paths *2100

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>

void getWatman(std::vector<std::string>& whatman,
    int& whatman_row_count, int& whatman_column_count,
    std::istream& input_stream = std::cin)
{
    input_stream >> whatman_row_count >> whatman_column_count;
    whatman.resize(whatman_row_count);
    for (int row_index = 0; row_index < whatman_row_count; ++row_index) {
        input_stream >> whatman[row_index];
    }
}

int64_t getTheNumberOfWays(const std::vector<std::string>& whatman,
    int whatman_row_count, int whatman_column_count)
{
    int64_t theNumberOfWays = 0;
    const int k_index_offset = 1;

    std::vector<std::vector<int>> count_up(whatman_row_count, std::vector<int>(whatman_column_count, 0));
    for (int row_index = 0; row_index < whatman_row_count; ++row_index) {
        for (int column_index = 0; column_index < whatman_column_count; ++column_index) {
            if (row_index != 0 && whatman[row_index][column_index] == whatman[row_index - k_index_offset][column_index]) {
                count_up[row_index][column_index] = count_up[row_index - k_index_offset][column_index] + k_index_offset;
            }
        }
    }

    std::vector<std::vector<int>> cnt_down(whatman_row_count, std::vector<int>(whatman_column_count, 0));
    for (int row_index = whatman_row_count - k_index_offset; row_index >= 0; --row_index) {
        for (int column_index = 0; column_index < whatman_column_count; ++column_index) {
            if (row_index != whatman_row_count - k_index_offset
                && whatman[row_index][column_index] == whatman[row_index + k_index_offset][column_index])
            {
                cnt_down[row_index][column_index] = cnt_down[row_index + k_index_offset][column_index] + k_index_offset;
            }
        }
    }

    for (int row_index = 0; row_index < whatman_row_count; ++row_index) {
        std::vector<int> left_part(whatman_column_count, 0);
        for (int column_index = 0; column_index < whatman_column_count; ++column_index) {
            int go = column_index;
            while (go < whatman_column_count && whatman[row_index][column_index] == whatman[row_index][go]) {
                go++;
            }
            go--;
            for (int pos = column_index; pos <= go; ++pos) {
                if (pos == column_index) {
                    left_part[pos] = pos;
                }
                else {
                    left_part[pos] = std::max(left_part[pos - k_index_offset], pos - std::min(count_up[row_index][pos], cnt_down[row_index][pos]));
                }
            }
            column_index = go;
        }
        std::vector<int> rigth_part(whatman_column_count, 0);
        for (int column_index = whatman_column_count - k_index_offset; column_index >= 0; --column_index) {
            int go = column_index;
            while (go >= 0 && whatman[row_index][column_index] == whatman[row_index][go]) {
                go--;
            }
            go++;
            for (int pos = column_index; pos >= go; --pos) {
                if (pos == column_index) {
                    rigth_part[pos] = pos;
                }
                else {
                    rigth_part[pos] = std::min(rigth_part[pos + k_index_offset], pos + std::min(count_up[row_index][pos], cnt_down[row_index][pos]));
                }
            }
            column_index = go;
        }
        for (int column_index = 0; column_index < whatman_column_count; ++column_index) {
            theNumberOfWays += std::min(column_index - left_part[column_index] + k_index_offset, rigth_part[column_index] - column_index + k_index_offset);
        }
    }
    return theNumberOfWays;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::vector<std::string> whatman;
    int whatman_row_count = 0;
    int whatman_column_count = 0;
    getWatman(whatman, whatman_row_count, whatman_column_count);

    std::cout << getTheNumberOfWays(whatman, whatman_row_count, whatman_column_count) << std::endl;

    return 0;
}


Comments

Submit
0 Comments
More Questions

Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately