暴力+稍微剪枝 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)
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 }
Returns4A 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",
- a{"11",
"11"} - x{ 0, 1 }
- y{ 0, 1 }
Returns3Here 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{"11",
- a{"15",
"61"} - x{ 0 }
- y{ 0 }
Returns11In 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{"15",
- a{"151",
"655",
"661"} - x{ 0, 0, -1 }
- y{ 0, 1, 0 }
Returns33- a{"151",
- a{"303",
"333",
"000"} - x{ -1, -1 }
- y{ -1, 1 }
Returns12Note that in this test case the offset (0, 0) is not among the offsets (x[k], y[k]).- a{"303",
- a{"0000000",
"1010101"} - x{ -1, -1 }
- y{ -1, 1 }
Returns0The stations must be built on some cells of the given field. They cannot be built outside the field.- a{"0000000",
题解
The straight forward solution is to try all pairs of cells for the two stations. There are
If we could just get rid of one of the factors in
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
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; }
- 暴力+稍微剪枝 SRM 660 Div1 Easy: Coversta
- Topcoder SRM660 DIV1 250 Coversta(暴力枚举)
- Topcoder SRM 661 Div1 Easy: MissingLCM
- SRM 465(DIV1 DIV2)
- SRM 465(DIV1 DIV2)
- SRM 144 DIV1 second
- SRM 507 DIV1 B
- SRM 513 DIV1 C
- SRM 500 DIV1 B
- 1-SRM 144 DIV1
- TC SRM 559 DIV1
- SRM 195 Div1 总结
- SRM 497 DIV1 550
- SRM 450 div1(practice)
- SRM 449 div1 (practice)
- SRM 448 div1(practice)
- SRM 452 div1(practice)
- SRM 477 DIV1 1000
- WAMP设置通过IP地址访问
- EMMA 覆盖率工具
- 做一个PC聊天工具(一)
- 3D建模基础教程
- MFC修改视图CView的背景颜色
- 暴力+稍微剪枝 SRM 660 Div1 Easy: Coversta
- UIDatePicker 日期/时间选取器(滚轮)—IOS开发
- crontab 定时任务没有执行
- Codeforces Round #311 (Div. 2) C. Arthur and Table
- sip协议内容解析
- Android BatteryStatsHelper深入理解(and5.1)
- Yii 表模型类具备的方法的一个例子(必须+可选)
- MonkeyRunner简介
- 总帐凭证和总帐余额