mr_dot_convict

1353F-Decreasing-Heights-codeforces-mr.convict

May 19th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. // Hack it and have it ;) //
  2. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  3.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  4.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  5.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  6.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  7. When I wrote this, only God and I understood what I was doing
  8.  ** * * * * * * * Now, only God knows!* * * * * * */
  9. #include      <bits/stdc++.h>
  10. using namespace std;
  11. #define IOS       ios_base::sync_with_stdio(false); cin.tie (nullptr)
  12. #define PREC      cout.precision (10); cout << fixed
  13. #ifdef CONVICTION
  14. #include "/home/convict/Dropbox/myfiles/sport_coding/cplib/snippets/debug.cpp"
  15. #else
  16. #define debug(x...)
  17. #endif
  18. //Don’t practice until you get it right. Practice until you can’t get it wrong
  19.  
  20. void preproc() {
  21. }
  22.  
  23. void solve() {
  24.   int n, m;
  25.   vector <vector <long long>> ar;
  26.  
  27.   cin >> n >> m;
  28.   ar.assign(n, vector <long long> (m));
  29.   for (int i = 0; i < n; ++i) {
  30.     for (int j = 0; j < m; ++j) {
  31.       cin >> ar[i][j];
  32.     }
  33.   }
  34.  
  35.   long long res = LLONG_MAX;
  36.   for (int x = 0; x < n; ++x) {
  37.     for (int y = 0; y < m; ++y) {
  38.       long long start = ar[x][y] - x - y;
  39.       vector <vector <long long>> dp;
  40.       dp.assign(n, vector <long long> (m, LLONG_MAX));
  41.       long long up, left;
  42.  
  43.       for (int i = 0; i < n; ++i) {
  44.         for (int j = 0; j < m; ++j) {
  45.           up = (i ? dp[i - 1][j] : LLONG_MAX);
  46.           left = (j ? dp[i][j - 1] : LLONG_MAX);
  47.           long long val = start + i + j;
  48.           if (ar[i][j] < val) {
  49.             continue;
  50.           }
  51.  
  52.           if (!(up == LLONG_MAX && left == LLONG_MAX) || !(i | j)) {
  53.             dp[i][j] = ar[i][j] - val + (i | j ? min(up, left) : 0ll);
  54.           }
  55.         }
  56.       }
  57.       res = min(res, dp[n - 1][m - 1]);
  58.     }
  59.   }
  60.  
  61.   cout << res << '\n';
  62. }
  63.  
  64. signed main() {
  65.   IOS; PREC;
  66.   preproc();
  67.  
  68.   int tc = 1;
  69.   cin >> tc;
  70.   for (int Tt = 1; Tt <= tc; ++Tt) {
  71.     // cout << "Case #" << Tt << ": ";
  72.     solve();
  73.   }
  74.   return EXIT_SUCCESS;
  75. }
Add Comment
Please, Sign In to add comment