Advertisement
libobil

Untitled

Dec 9th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <limits.h>
  6. using namespace std;
  7. const int maxn = 1e3+10, maxk = 1e6+10, inf = INT_MAX;
  8.  
  9. int t[maxn][maxn];
  10. int bl[maxn][maxn];
  11. pair < int, int > p[] = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
  12. int n, m, k;
  13.  
  14. struct Kid {
  15.  
  16. int x, y;
  17. int courage;
  18.  
  19. Kid(){}
  20. Kid(istream &in) {
  21.  
  22. in >> x >> y >> courage;
  23.  
  24. }
  25.  
  26. inline friend bool operator > (Kid a, Kid b) {
  27.  
  28. if (a.courage != b.courage) return a.courage > b.courage;
  29. return (a.x-1)*m + a.y < (b.x-1)*m + b.y;
  30.  
  31. }
  32.  
  33. } children[maxk];
  34.  
  35. bool outside(int x, int y) {
  36.  
  37. return !(1 <= x && x <= n && 1 <= y && y <= m);
  38.  
  39. }
  40.  
  41. bool dfs(int x, int y, int courage, int time) {
  42.  
  43. if (outside(x, y) || (bl[x][y] >= 1 && bl[x][y] < time)) return 1;
  44.  
  45. bool res = 0;
  46. bl[x][y] = time;
  47.  
  48. for (auto [dx, dy] : p) {
  49.  
  50. x += dx;
  51. y += dy;
  52. if (t[x][y] <= courage && bl[x][y] != time) res |= dfs(x, y, courage, time);
  53. x -= dx;
  54. y -= dy;
  55.  
  56. }
  57.  
  58. return res;
  59.  
  60. }
  61.  
  62. bool check(int d) {
  63.  
  64. for (int i = 1 ; i <= k ; ++i) children[i].courage += d;
  65.  
  66. memset(bl, 0, sizeof(bl));
  67. bool all_reached = 1;
  68. int time = 1;
  69.  
  70. for (int i = 1 ; i <= k && all_reached; ++i) {
  71.  
  72. if (bl[children[i].x][children[i].y]) continue;
  73. if (!dfs(children[i].x, children[i].y, children[i].courage, time)) all_reached = 0;
  74. ++time;
  75.  
  76. }
  77.  
  78. for (int i = 1 ; i <= k ; ++i) children[i].courage -= d;
  79.  
  80. return all_reached;
  81.  
  82. }
  83.  
  84. void solve() {
  85.  
  86. sort(children+1, children+1+k, greater < Kid > ());
  87.  
  88. int l = -1, r = 10, mid;
  89.  
  90. while (l < r - 1) {
  91.  
  92. mid = (l+r)/2;
  93. if (!check(mid)) l = mid;
  94. else r = mid;
  95.  
  96. }
  97.  
  98. cout << r << '\n';
  99.  
  100. }
  101.  
  102. void read() {
  103.  
  104. cin >> n >> m >> k;
  105. for (int i = 1 ; i <= n ; ++i)
  106. for (int j = 1 ; j <= m ; ++j)
  107. cin >> t[i][j];
  108.  
  109. for (int i = 1 ; i <= k ; ++i)
  110. children[i] = Kid(cin);
  111.  
  112.  
  113. }
  114.  
  115. void fast_io() {
  116.  
  117. ios_base :: sync_with_stdio(0);
  118. cin.tie(nullptr);
  119. cout.tie(nullptr);
  120. cerr.tie(nullptr);
  121.  
  122. }
  123.  
  124. int main () {
  125.  
  126. fast_io();
  127. read();
  128. solve();
  129.  
  130. return 0;
  131.  
  132. }
  133.  
  134. /*
  135.  
  136. 5 10 5
  137. 5 5 5 5 6 2 2 2 2 2
  138. 5 1 1 1 6 1 1 1 1 2
  139. 5 1 6 6 6 4 5 3 5 2
  140. 5 1 6 1 6 1 5 1 1 2
  141. 5 5 6 5 6 5 5 2 2 2
  142. 4 6 1
  143. 2 9 2
  144. 4 9 1
  145. 4 4 2
  146. 2 3 4
  147.  
  148. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement