暴力+稍微剪枝 SRM 660 Div1 Easy: Coversta

来源:互联网 发布:淘宝针织衫热词 编辑:程序博客网 时间:2024/05/29 16:41

Coversta


Problem Statement

There is a rectangular field divided into a grid of n rows by m columns of unit square cells. Each cell (i, j) has some strategic value which is an integer between 0 and 9, inclusive. You are given these values as a String[] a where each a[i][j] is a digit between '0' and '9', inclusive.

It is possible to build stations in some cells. A station built in a cell covers some set of cells. You are given the offsets of those cells asint[]s x and y. More precisely, for each valid k, a station in the cell (i, j) covers the cell (i + x[k], j + y[k]) if a cell with those coordinates exists. Note that it may be the case that a station in (i, j) does not cover the cell (i, j).

Your task is to place two stations into two distinct cells. The total strategic value of the two stations is the sum of strategic values of all cells that are covered by at least one of the stations. Return the largest possible total strategic value of the two stations.

Definition

  • ClassCoversta
  • Methodplace
  • Parametersvector<string> , vector<int> , vector<int>
  • Returnsint
  • Method signatureint place(vector<string> a, vector<int> x, vector<int> y)
(be sure your method is public)

Limits

  • Time limit (s)2.000
  • Memory limit (MB)256

Notes

  • The two stations must be built in two distinct cells of the given rectangular array. It is not allowed to build the stations at coordinates that are outside the given array.

Constraints

  • n will be between 2 and 100, inclusive.
  • m will be between 2 and 100, inclusive.
  • a will contain exactly n elements.
  • Each element of a will contain exactly m characters.
  • Each character in a will be a digit ('0'-'9').
  • x will contain between 1 and 10 elements, inclusive.
  • x and y will contain the same number of elements.
  • Each element in x will be between -(n-1) and (n-1), inclusive.
  • Each element in y will be between -(m-1) and (m-1), inclusive.
  • The pairs (x[k], y[k]) will be distinct.

Test cases

    • a{"11",
       "11"}
    • x{ 0, 0 }
    • y{ 0, 1 }
    Returns4
    A station at (i, j) covers the cells (i, j) and (i, j+1). The optimal solution is to place the two stations at (0, 0) and (1, 0).
    • a{"11",
       "11"}
    • x{ 0, 1 }
    • y{ 0, 1 }
    Returns3
    Here a station at (i, j) also covers (i+1, j+1). One optimal solution is to place the two stations at (0, 0) and (0, 1). The first station also covers the cell (1, 1).
    • a{"15",
       "61"}
    • x{ 0 }
    • y{ 0 }
    Returns11
    In this test case each station only covers its own cell. The optimal solution is to build the two stations in the two most important locations.
    • a{"151",
       "655",
       "661"}
    • x{ 0, 0, -1 }
    • y{ 0, 1, 0 }
    Returns33
    • a{"303",
       "333",
       "000"}
    • x{ -1, -1 }
    • y{ -1, 1 }
    Returns12
    Note that in this test case the offset (0, 0) is not among the offsets (x[k], y[k]).
    • a{"0000000",
       "1010101"}
    • x{ -1, -1 }
    • y{ -1, 1 }
    Returns0
    The stations must be built on some cells of the given field. They cannot be built outside the field.

The straight forward solution is to try all pairs of cells for the two stations. There are O(whwh) such pairs (w is the width of the grid andh the height). For each pair we need to find the cells that are covered by at least one offset of one of the stations. For this matter we can add all the offsets from each station and then subtract the intersection . It is possible to find the intersection in O(n) (Where n is the number of offsets). We have an O(whwhn) algorithm. The maximum values for w and h can be considerably large: 100 each. n is at most 10. For the worst case we have around 10010010010010 . This will be too much.

If we could just get rid of one of the factors in O(whwhn), the new algorithm would be able to run under the time limit. There is a very nice trick to consider. Imagine we decide the position for the first station. This station will cover O(n) cells. For each of these cells (x,y), there are O(n) other cells in which the second station could be placed and have an intersection with station (x,y). For example, imagine we have a cell (x,y) covered by station 1 and one of the offsets (xo,yo) in station 2 placed at (x2,y2) overlapped with (x,y). This would mean: (x,y)=(x2+xo,y2+yo). We can find (x2,y2) with this logic: (x2,y2)=(xx0,yyo). Therefore, for each cell (x,y) covered by station 1, and for each offset (xo,yo), then the cells (xxo,yyo) are the only locations where stations that overlap with (x,y) exist. For n offsets in station 1 this means there will be at most n2 locations where the result for the sum of station 2 can change.

So this will be our approach: Since only a reduced number of station positions can overlap with station 1, we will keep in a table the normalsum (ignoring any possible overlaps) for a station with position (x,y), for all positions (x,y). This is an O(whn) calculation. Then for eachO(wh) position where we place station 1, there will be O(n2) positions for station 2 in which the precalculated sum will change, we can just update the values for only those O(n2) positions. This can be implemented in O(whnn) if we notice that for each of the n cells (x1,y1)covered by station 1 and each offset (ox,oy), the sum of station 2 targeted by this offset will decrement by the strategic value of cell (x1,y1). Finally, we need an O(wh) loop to pick the best position for station 2 considering the updated sums. This approach is: O(whn+whnn+whwh)O(wh(wh+n)) which is appropriate.

int place(vector<string> a, vector<int> x, vector<int> y){    int w = a.size(), h = a[0].size();    int n = x.size();    // first precalculate the sum of strategic values acquired by a station    // when placed in a given cell:    vector<vector<int>> sum1(w, vector<int>(h));    for (int i = 0; i < w; i++) {        for (int j = 0; j < h; j++) {            for (int k = 0; k < n; k++) {                int nx = i + x[k], ny = j + y[k];                if ( (0 <= nx && nx < w) && (0 <= ny && ny < h) ) {                    sum1[i][j] += (a[nx][ny] - '0');                }            }        }    }    int res = 0;    // for each position (i,j) in which we place station 1:    for (int i = 0; i < w; i++) {        for (int j = 0; j < h; j++) {            // initially the results for station 2 are the same as calculated            vector<vector<int>> sum2 = sum1;            // update the values for the O(n^2) stations that changed:            for (int k1 = 0; k1 < n; k1++) {                int nx = i + x[k1], ny = j + y[k1];                if ( (0 <= nx && nx < w) && (0 <= ny && ny < h) ) {                    for (int k2 = 0; k2 < n; k2++) {                        int nnx = nx - x[k2], nny = ny - y[k2];                        if ( (0 <= nnx && nnx < w) && (0 <= nny && nny < h) ) {                            sum2[nnx][nny] -= (a[nx][ny] - '0');                        }                    }                }            }            // pick the best:            for (int a = 0; a < w; a++) {                for (int b = 0; b < h; b++) {                    res = std::max(res, sum2[a][b] + sum1[i][j]);                }            }         }    }    return res;           }


0 0