Advertisement
bkunjesh

prob-C kickstart-A

Mar 21st, 2021
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. //@ikung
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. #define fast                      \
  6.     ios_base::sync_with_stdio(0); \
  7.     cin.tie(0);
  8. #define f(i, k) for (int i = 0; i < k; i++)
  9. #define F first
  10. #define dbg(x) cout << #x << " " << x << endl;
  11. #define S second
  12. #define endl "\n"
  13. #define rep(i, n) for (int i = 1; i <= n; i++)
  14. #define rew(i, a, b) for (int i = a; i <= b; i++)
  15. #define mod 1000000007
  16. const int inf = 1e18;
  17. const int N = 302;
  18. const int M = 302;
  19.  
  20. int n, m;
  21.  
  22. void solve()
  23. {
  24.     int i, j, k;
  25.  
  26.     cin >> n >> m;
  27.     vector<vector<int>> a(n, vector<int>(m));
  28.     map<pair<int, int>, int> visited;
  29.     int mxval = -1;
  30.     f(i, n)
  31.     {
  32.         f(j, m)
  33.         {
  34.             cin >> a[i][j];
  35.             if (a[i][j] > mxval)
  36.             {
  37.                 mxval = a[i][j];
  38.             }
  39.         }
  40.     }
  41.     queue<pair<int, int>> q;
  42.     f(i, n)
  43.     {
  44.         f(j, m)
  45.         {
  46.             if (a[i][j] == mxval)
  47.             {
  48.                 q.push({i, j});
  49.                 visited[{i, j}] = 1;
  50.             }
  51.         }
  52.     }
  53.     auto isvalid = [&](int x, int y) {
  54.         if (x >= 0 && x < n && y >= 0 && y < m && visited[{x, y}] == 0)
  55.             return 1;
  56.  
  57.         return 0;
  58.     };
  59.     vector<int> dx = {1ll, -1ll, 0ll, 0ll};
  60.     vector<int> dy = {0ll, 0ll, 1ll, -1ll};
  61.     int ans = 0;
  62.     while (!q.empty())
  63.     {
  64.         int sz = q.size();
  65.         while (sz--)
  66.         {
  67.             auto p = q.front();
  68.             int parentstack = a[p.F][p.S];
  69.             // cout << parentstack << endl;
  70.             q.pop();
  71.  
  72.             for (i = 0; i < 4; i++)
  73.             {
  74.                 int curx = p.F + dx[i], cury = p.S + dy[i];
  75.                 if(visited[{curx,cury}])
  76.                 {
  77.                     while (abs(parentstack - a[curx][cury])>1)
  78.                         ;
  79.                 }
  80.                 if (isvalid(curx, cury))
  81.                 {
  82.                     visited[{curx, cury}] = 1;
  83.                     q.push({curx, cury});
  84.                     ans = ans + max(((parentstack - 1) - a[curx][cury]), 0ll);
  85.                     a[curx][cury] = max(a[curx][cury], parentstack - 1);
  86.                 }
  87.  
  88.             }
  89.         }
  90.     }
  91.     // cout << endl;
  92.     // f(i, n)
  93.     // {
  94.     //     f(j, m)
  95.     //     {
  96.     //         cout << a[i][j] << " ";
  97.     //     }
  98.     //     cout << endl;
  99.     // }
  100.     cout << ans << endl;
  101.  
  102.     return;
  103. }
  104.  
  105. signed main()
  106. {
  107.     fast int t = 1, i, j, k;
  108.     cin >> t;
  109.     for (i = 1; i <= t; i++)
  110.     {
  111.         cout << "case #" << i << ": ";
  112.         solve();
  113.     }
  114.     return 0;
  115. }
  116. //#ifndef ONLINE_JUDGE
  117. //freopen("input.txt", "r", stdin);
  118. //freopen("output.txt", "w", stdout);
  119. //#endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement