Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #include <list>
  2. #include <map>
  3. #include <set>
  4. #include <deque>
  5. #include <stack>
  6. #include <queue>
  7. #include <algorithm>
  8. #include <sstream>
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <cstdio>
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <memory.h>
  15. #include <ctime>
  16. #include <bitset>
  17.  
  18. using namespace std;
  19.  
  20. #define ABS(a) ((a>0)?a:-(a))
  21. #define MIN(a,b) ((a<b)?(a):(b))
  22. #define MAX(a,b) ((a<b)?(b):(a))
  23. #define FOR(i,a,n) for (int i=(a);i<(n);++i)
  24. #define FI(i,n) for (int i=0; i<(n); ++i)
  25. #define pnt pair <int, int>
  26. #define mp make_pair
  27. #define PI 3.1415926535897
  28. #define MEMS(a,b) memset(a,b,sizeof(a))
  29. #define LL long long
  30. #define U unsigned
  31.  
  32. #define V vector
  33.  
  34. int a[2050][2050];
  35. int b[2050][2050];
  36. int c[2050][2050];
  37. int n, m;
  38.  
  39. bool check()
  40. {
  41.     FOR(i, 1, n)
  42.     {
  43.         FOR(j, 1, m)
  44.         {
  45.             if (a[i][j] >= a[i][j + 1])
  46.                 return false;
  47.             if (a[i][j] >= a[i + 1][j])
  48.                 return false;
  49.         }
  50.     }
  51.     return true;
  52. }
  53.  
  54. void solution()
  55. {
  56.     cin >> n >> m;
  57.     MEMS(a, 0);
  58.     char odd = 0;
  59.     char even = 0;
  60.     FOR(i, 1, n + 1)
  61.     {
  62.         FOR(j, 1, m + 1)
  63.         {
  64.             scanf("%d", &a[i][j]);
  65.         }
  66.     }
  67.  
  68.     if (n == 1)
  69.     {
  70.         int mx = 0;
  71.         FOR(j, 1, m + 1)
  72.         {
  73.             if (a[n][j])
  74.                 mx = a[n][j];
  75.             else
  76.             {
  77.                 mx++;
  78.                 a[n][j] = mx;
  79.             }
  80.         }
  81.         mx = 0;
  82.         FOR(j, 1, m)
  83.         {
  84.             if (a[n][j + 1] <= a[n][j])
  85.             {
  86.                 cout << "-1" << endl;
  87.                 return;
  88.             }
  89.             mx += a[n][j];
  90.         }
  91.         cout << mx + a[n][m] << endl;
  92.         return;
  93.     }
  94.  
  95.     if (m == 1)
  96.     {
  97.         int mx = 0;
  98.         FOR(i, 1, n + 1)
  99.         {
  100.             if (a[i][m])
  101.                 mx = a[i][m];
  102.             else
  103.             {
  104.                 mx++;
  105.                 a[i][m] = mx;
  106.             }
  107.         }
  108.         mx = 0;
  109.         FOR(i, 1, n)
  110.         {
  111.             if (a[i + 1][m] <= a[i][m])
  112.             {
  113.                 cout << "-1" << endl;
  114.                 return;
  115.             }
  116.             mx += a[i][m];
  117.         }
  118.         cout << mx + a[n][m] << endl;
  119.         return;
  120.     }
  121.  
  122.     LL ans = -1;
  123.  
  124.     // CASE 1
  125.     FOR(PPP, 0, 2)
  126.     {
  127.         FOR(i, 1, n + 1)
  128.         {
  129.             FOR(j, 1, m + 1)
  130.             {
  131.                 b[j][i] = a[i][j];
  132.             }
  133.         }
  134.  
  135.         memcpy(c, a, sizeof(a));
  136.  
  137.         LL sum = 0;
  138.         char good = 1;
  139.         int q = a[1][1];
  140.         if (q == 0)
  141.             a[1][1] = 1;
  142.  
  143.         FOR(i, 1, n + 1)
  144.         {
  145.             FOR(j, 1, m + 1)
  146.             {
  147.                 if ((j & 1) == 0)
  148.                 {
  149.                     if (a[i][j])
  150.                     {
  151.                         if (a[i][j] & 1)
  152.                             good = 0;
  153.                     }
  154.                     else
  155.                     {
  156.                         a[i][j] = 1 + MAX(a[i - 1][j], a[i][j - 1]);
  157.                         if (a[i][j] & 1)
  158.                             a[i][j]++;
  159.                     }
  160.                 }
  161.                 else
  162.                 {
  163.                     if (a[i][j])
  164.                     {
  165.                         if ((a[i][j] & 1) == 0)
  166.                             good = 0;
  167.                     }
  168.                     else
  169.                     {
  170.                         a[i][j] = 1 + MAX(a[i - 1][j], a[i][j - 1]);
  171.                         if ((a[i][j] & 1) == 0)
  172.                             a[i][j]++;
  173.                     }
  174.                 }
  175.                 sum += a[i][j];
  176.             }
  177.         }
  178.  
  179.         if (good && check())
  180.         {
  181.             if (ans == -1)
  182.                 ans = sum;
  183.             else
  184.                 ans = MIN(ans, sum);
  185.         }
  186.         a[1][1] = q;
  187.         good = 1;
  188.         sum = 0;
  189.  
  190.         memcpy(a, c, sizeof(a));
  191.  
  192.         q = a[1][1];
  193.         if (a[1][1] == 0)
  194.             a[1][1] = 2;
  195.  
  196.  
  197.         FOR(i, 1, n + 1)
  198.         {
  199.             FOR(j, 1, m + 1)
  200.             {
  201.                 if (j & 1)
  202.                 {
  203.                     if (a[i][j])
  204.                     {
  205.                         if (a[i][j] & 1)
  206.                             good = 0;
  207.                     }
  208.                     else
  209.                     {
  210.                         a[i][j] = 1 + MAX(a[i - 1][j], a[i][j - 1]);
  211.                         if (a[i][j] & 1)
  212.                             a[i][j]++;
  213.                     }
  214.                 }
  215.                 else
  216.                 {
  217.                     if (a[i][j])
  218.                     {
  219.                         if ((a[i][j] & 1) == 0)
  220.                             good = 0;
  221.                     }
  222.                     else
  223.                     {
  224.                         a[i][j] = 1 + MAX(a[i - 1][j], a[i][j - 1]);
  225.                         if ((a[i][j] & 1) == 0)
  226.                             a[i][j]++;
  227.                     }
  228.                 }
  229.                 sum += a[i][j];
  230.             }
  231.         }
  232.  
  233.         if (good && check())
  234.         {
  235.             if (ans == -1)
  236.                 ans = sum;
  237.             else
  238.                 ans = MIN(ans, sum);
  239.         }
  240.         a[1][1] = q;
  241.        
  242.         swap(n, m);
  243.         memcpy(a, b, sizeof(a));
  244.  
  245.     }
  246.  
  247.     //
  248.     cout << ans << endl;
  249.     return;
  250. }
  251.  
  252. int main()
  253. {
  254. #ifdef Fcdkbear
  255.     freopen("in.txt", "r", stdin);
  256.     //freopen("out.txt", "w", stdout);
  257.     double beg = clock();
  258. #endif
  259.  
  260.     solution();
  261.  
  262. #ifdef Fcdkbear
  263.     double end = clock();
  264.     fprintf(stderr, "*** Total time = %.3lf ***\n", (end - beg) / CLOCKS_PER_SEC);
  265. #endif
  266.     return 0;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement