Advertisement
Gassa

my TCO13 R2C 550 solution

May 11th, 2013
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <ctime>
  8. #include <iostream>
  9. #include <map>
  10. #include <queue>
  11. #include <set>
  12. #include <sstream>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16.  
  17. using namespace std;
  18.  
  19. const int MAX_N = 205, NA = -1, MAX_C = 2000, INF = 0x3F3F3F3F;
  20. const int DIRS = 4;
  21. const int DR [DIRS] = {+1, +1, -1, -1};
  22. const int DC [DIRS] = {+1, -1, +1, -1};
  23.  
  24. class TheMountain
  25. {
  26. private:
  27.  int a [MAX_N] [MAX_N];
  28.  int p [DIRS] [MAX_N] [MAX_N];
  29.  int s [DIRS] [MAX_N] [MAX_N];
  30.  int t [DIRS] [MAX_N] [MAX_N];
  31.  
  32. public:
  33.  int minSum (int n, int m,
  34.              vector <int> rowIndex, vector <int> columnIndex,
  35.              vector <int> element)
  36.  {
  37.   int k = rowIndex.size ();
  38.   memset (a, 0, sizeof (a));
  39.   for (int row = 1; row <= n; row++)
  40.    for (int col = 1; col <= m; col++)
  41.     a[row][col] = NA;
  42.   for (int i = 0; i < k; i++)
  43.   {
  44.    int row = rowIndex[i];
  45.    int col = columnIndex[i];
  46.    int value = element[i];
  47.    row++;
  48.    col++;
  49.    a[row][col] = value;
  50.   }
  51.  
  52.   int SR [DIRS];
  53.   for (int d = 0; d < DIRS; d++)
  54.    SR[d] = (DR[d] == +1) ? 1 : n;
  55.   int ER [DIRS];
  56.   for (int d = 0; d < DIRS; d++)
  57.    ER[d] = (n + 1 - SR[d]) + DR[d];
  58.  
  59.   int SC [DIRS];
  60.   for (int d = 0; d < DIRS; d++)
  61.    SC[d] = (DC[d] == +1) ? 1 : m;
  62.   int EC [DIRS];
  63.   for (int d = 0; d < DIRS; d++)
  64.    EC[d] = (m + 1 - SC[d]) + DC[d];
  65.  
  66.   for (int d = 0; d < DIRS; d++)
  67.   {
  68.    memmove (p[d], a, sizeof (p[d]));
  69.    for (int row = SR[d]; row != ER[d]; row += DR[d])
  70.     for (int col = SC[d]; col != EC[d]; col += DC[d])
  71.     {
  72.      int value = 0;
  73.      value = max (value, p[d][row - DR[d]][col]);
  74.      value = max (value, p[d][row][col - DC[d]]);
  75.      value++;
  76.      if (p[d][row][col] != NA)
  77.      {
  78.       if (value <= p[d][row][col])
  79.        value = p[d][row][col];
  80.       else
  81.        value = MAX_C;
  82.      }
  83.      p[d][row][col] = value;
  84.     }
  85.  
  86.    memset (t[d], 0, sizeof (t[d]));
  87.    for (int row = SR[d]; row != ER[d]; row += DR[d])
  88.     for (int col = SC[d]; col != EC[d]; col += DC[d])
  89.      t[d][row][col] = t[d][row - DR[d]][col] + p[d][row][col];
  90.  
  91.    memset (s[d], 0, sizeof (s[d]));
  92.    for (int row = SR[d]; row != ER[d]; row += DR[d])
  93.     for (int col = SC[d]; col != EC[d]; col += DC[d])
  94.      s[d][row][col] = s[d][row][col - DC[d]] + t[d][row][col];
  95.   }
  96.  
  97.   int res = INF;
  98.   for (int row = 1; row <= n; row++)
  99.    for (int col = 1; col <= m; col++)
  100.    {
  101.     bool ok = true;
  102.     for (int d = 0; d < DIRS; d++)
  103.      if (p[d][row][col] >= MAX_C)
  104.       ok = false;
  105.     if (!ok)
  106.      continue;
  107.  
  108.     int cur = 0;
  109.     for (int d = 0; d < DIRS; d++)
  110.      cur = max (cur, p[d][row][col]);
  111.     for (int d = 0; d < DIRS; d++)
  112.      cur += s[d][row - DR[d]][col - DC[d]];
  113.  
  114.     for (int row2 = 1; row2 <= n; row2++)
  115.     {
  116.      int temp = 0;
  117.      for (int d = 0; d < DIRS; d++)
  118.      {
  119.       if ((row2 < row) && (DR[d] == +1))
  120.        temp = max (temp, p[d][row2][col]);
  121.       if ((row2 > row) && (DR[d] == -1))
  122.        temp = max (temp, p[d][row2][col]);
  123.      }
  124.      cur += temp;
  125.     }
  126.  
  127.     for (int col2 = 1; col2 <= m; col2++)
  128.     {
  129.      int temp = 0;
  130.      for (int d = 0; d < DIRS; d++)
  131.      {
  132.       if ((col2 < col) && (DC[d] == +1))
  133.        temp = max (temp, p[d][row][col2]);
  134.       if ((col2 > col) && (DC[d] == -1))
  135.        temp = max (temp, p[d][row][col2]);
  136.      }
  137.      cur += temp;
  138.     }
  139.  
  140.     res = min (res, cur);
  141.    }
  142.  
  143.   if (res >= INF)
  144.    res = -1;
  145.   return res;
  146.  }
  147. };
  148.  
  149. <%:testing-code%>
  150.  
  151. //Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement