Beingamanforever

ABC 384, Problem E

Dec 14th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-12-14 22:59:34
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. void solve()
  26. {
  27.     int h, w, X;
  28.     cin >> h >> w >> X;
  29.     int x, y;
  30.     cin >> x >> y;
  31.     int grid[h + 1][w + 1];
  32.     for (int i = 1; i <= h; i++)
  33.     {
  34.         for (int j = 1; j <= w; j++)
  35.         {
  36.             cin >> grid[i][j];
  37.         }
  38.     }
  39.     vvi visited(h + 1, vi(w + 1, 0));
  40.     int sum = grid[x][y];
  41.     int dx[] = {0, 0, 1, -1};
  42.     int dy[] = {1, -1, 0, 0};
  43.     priority_queue<pair<int, pair<int, int>>> pq; // maximum priority queue, implementation using -ve sign
  44.     pq.push({0, {x, y}});
  45.     visited[x][y] = 1;
  46.     while (!pq.empty())
  47.     {
  48.         auto [size, now] = pq.top();
  49.         auto [nowx, nowy] = now;
  50.         pq.pop();
  51.         int cur_size = -size;
  52.         if (cur_size >= (sum + X - 1) / X)
  53.         {
  54.             break;
  55.         }
  56.         sum += cur_size; // absorb the maximum value
  57.         for (int i = 0; i < 4; i++)
  58.         {
  59.             int newx = nowx + dx[i];
  60.             int newy = nowy + dy[i];
  61.             if (newx >= 1 && newx <= h && newy >= 1 && newy <= w && !visited[newx][newy])
  62.             {
  63.                 if (!visited[newx][newy])
  64.                 {
  65.                     visited[newx][newy] = 1;
  66.                     pq.push({-grid[newx][newy], {newx, newy}});
  67.                 }
  68.             }
  69.         }
  70.     }
  71.     cout << sum << endl;
  72. }
  73.  
  74. signed main()
  75. {
  76.     NeedForSpeed;
  77.     int t = 1;
  78.     // cin >> t;
  79.     while (t--)
  80.     {
  81.         solve();
  82.     }
  83.     return 0;
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment