Advertisement
FyanRu

Untitled

Sep 10th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. //numbers
  5. using ll=long long;
  6. #define int ll
  7. //std data structure
  8. #define all(x) begin(x), end(x)
  9. #define sz(x) (int) (x).size()
  10. //vectors
  11. #define V vector
  12. #define vi V<int>
  13. #define v2i V<vi>
  14. #define v3i V<v2i>
  15. #define vpi V<pi>
  16. #define vsi V<si>
  17. #define vb V<bool>
  18. #define pb push_back
  19. //constants
  20. const int INF=922337203685477580;
  21. int h,w;
  22. v2i arr;
  23. v2i adj;
  24. int smin=INF;
  25. bool verif(int k) {
  26. int h=sz(arr);
  27. int w=sz(arr[0]);
  28. vi bds(h+1,w);
  29. for (int i=0; i<h; i++) {
  30. bds[i+1]=bds[i];
  31. for (int j=0; j<bds[i]; j++) {
  32. if (arr[i][j]>smin+k) {
  33. bds[i+1]=j-1; break;
  34. }
  35. }
  36. }
  37. int mn=INF; int mx=-INF;
  38. for (int i=0; i<h; i++) {
  39. for (int j=w-1; j>bds[i+1]; j--) {
  40. mn=min(mn,arr[i][j]);
  41. mx=max(mx,arr[i][j]);
  42. }
  43. }
  44. if (mx==-INF || mx-mn<=k) return true;
  45. return false;
  46. }
  47. void flip() {
  48. int h=sz(arr);
  49. int w=sz(arr[0]);
  50. adj.resize(h);
  51. for (int i=0; i<h; i++) {
  52. adj[i].resize(w);
  53. }
  54. for (int i=0; i<h; i++) {
  55. for (int j=0; j<w; j++) {
  56. adj[i][j]=arr[i][w-1-j];
  57. }
  58. }
  59. swap(adj,arr);
  60. }
  61. void rot() {
  62. int h=sz(arr);
  63. int w=sz(arr[0]);
  64. adj.resize(w);
  65. for (int i=0; i<w; i++) {
  66. adj[i].resize(h);
  67. }
  68. for (int i=0; i<h; i++) {
  69. for (int j=0; j<w; j++) {
  70. adj[j][i]=arr[h-i-1][j];
  71. }
  72. }
  73. swap(adj,arr);
  74. }
  75. int bsearch() {
  76. int lo=0; int hi=5e9;
  77. while (lo<=hi) {
  78. int mid=(lo+hi)/2;
  79. if (verif(mid)) {
  80. hi=mid-1;
  81. } else {
  82. lo=mid+1;
  83. }
  84. }
  85. return lo;
  86. }
  87. signed main() {
  88. ios::sync_with_stdio(false); cin.tie(nullptr);
  89. cin>>h>>w;
  90. arr=v2i(h,vi(w));
  91. for (int i=0; i<h; i++) {
  92. for (int j=0; j<w; j++) {
  93. cin>>arr[i][j]; smin=min(smin,arr[i][j]);
  94. }
  95. }
  96. int emin=INF;
  97. for (int i=0; i<4; i++) {
  98. emin=min(emin,bsearch());
  99. flip();
  100. emin=min(emin,bsearch());
  101. flip(); rot();
  102. }
  103. cout<<emin;
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement