Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cassert>
- #include <cmath>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <ctime>
- #include <iostream>
- #include <map>
- #include <queue>
- #include <set>
- #include <sstream>
- #include <string>
- #include <utility>
- #include <vector>
- using namespace std;
- const int MAX_N = 205, NA = -1, MAX_C = 2000, INF = 0x3F3F3F3F;
- const int DIRS = 4;
- const int DR [DIRS] = {+1, +1, -1, -1};
- const int DC [DIRS] = {+1, -1, +1, -1};
- class TheMountain
- {
- private:
- int a [MAX_N] [MAX_N];
- int p [DIRS] [MAX_N] [MAX_N];
- int s [DIRS] [MAX_N] [MAX_N];
- int t [DIRS] [MAX_N] [MAX_N];
- public:
- int minSum (int n, int m,
- vector <int> rowIndex, vector <int> columnIndex,
- vector <int> element)
- {
- int k = rowIndex.size ();
- memset (a, 0, sizeof (a));
- for (int row = 1; row <= n; row++)
- for (int col = 1; col <= m; col++)
- a[row][col] = NA;
- for (int i = 0; i < k; i++)
- {
- int row = rowIndex[i];
- int col = columnIndex[i];
- int value = element[i];
- row++;
- col++;
- a[row][col] = value;
- }
- int SR [DIRS];
- for (int d = 0; d < DIRS; d++)
- SR[d] = (DR[d] == +1) ? 1 : n;
- int ER [DIRS];
- for (int d = 0; d < DIRS; d++)
- ER[d] = (n + 1 - SR[d]) + DR[d];
- int SC [DIRS];
- for (int d = 0; d < DIRS; d++)
- SC[d] = (DC[d] == +1) ? 1 : m;
- int EC [DIRS];
- for (int d = 0; d < DIRS; d++)
- EC[d] = (m + 1 - SC[d]) + DC[d];
- for (int d = 0; d < DIRS; d++)
- {
- memmove (p[d], a, sizeof (p[d]));
- for (int row = SR[d]; row != ER[d]; row += DR[d])
- for (int col = SC[d]; col != EC[d]; col += DC[d])
- {
- int value = 0;
- value = max (value, p[d][row - DR[d]][col]);
- value = max (value, p[d][row][col - DC[d]]);
- value++;
- if (p[d][row][col] != NA)
- {
- if (value <= p[d][row][col])
- value = p[d][row][col];
- else
- value = MAX_C;
- }
- p[d][row][col] = value;
- }
- memset (t[d], 0, sizeof (t[d]));
- for (int row = SR[d]; row != ER[d]; row += DR[d])
- for (int col = SC[d]; col != EC[d]; col += DC[d])
- t[d][row][col] = t[d][row - DR[d]][col] + p[d][row][col];
- memset (s[d], 0, sizeof (s[d]));
- for (int row = SR[d]; row != ER[d]; row += DR[d])
- for (int col = SC[d]; col != EC[d]; col += DC[d])
- s[d][row][col] = s[d][row][col - DC[d]] + t[d][row][col];
- }
- int res = INF;
- for (int row = 1; row <= n; row++)
- for (int col = 1; col <= m; col++)
- {
- bool ok = true;
- for (int d = 0; d < DIRS; d++)
- if (p[d][row][col] >= MAX_C)
- ok = false;
- if (!ok)
- continue;
- int cur = 0;
- for (int d = 0; d < DIRS; d++)
- cur = max (cur, p[d][row][col]);
- for (int d = 0; d < DIRS; d++)
- cur += s[d][row - DR[d]][col - DC[d]];
- for (int row2 = 1; row2 <= n; row2++)
- {
- int temp = 0;
- for (int d = 0; d < DIRS; d++)
- {
- if ((row2 < row) && (DR[d] == +1))
- temp = max (temp, p[d][row2][col]);
- if ((row2 > row) && (DR[d] == -1))
- temp = max (temp, p[d][row2][col]);
- }
- cur += temp;
- }
- for (int col2 = 1; col2 <= m; col2++)
- {
- int temp = 0;
- for (int d = 0; d < DIRS; d++)
- {
- if ((col2 < col) && (DC[d] == +1))
- temp = max (temp, p[d][row][col2]);
- if ((col2 > col) && (DC[d] == -1))
- temp = max (temp, p[d][row][col2]);
- }
- cur += temp;
- }
- res = min (res, cur);
- }
- if (res >= INF)
- res = -1;
- return res;
- }
- };
- <%:testing-code%>
- //Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement