fork download
  1. #include <algorithm>
  2. #include <array>
  3. #include <bitset>
  4. #include <cassert>
  5. #include <chrono>
  6. #include <climits>
  7. #include <cmath>
  8. #include <complex>
  9. #include <cstring>
  10. #include <unordered_set>
  11. #include <functional>
  12. #include <iomanip>
  13. #include <iostream>
  14. #include <map>
  15. #include <stack>
  16. #include <numeric>
  17. #include <queue>
  18. #include <random>
  19. #include <set>
  20. #include <vector>
  21.  
  22. using namespace std;
  23.  
  24. #pragma GCC diagnostic ignored "-Wunused-variable"
  25. #pragma GCC diagnostic ignored "-Wunused-parameter"
  26.  
  27. using ll = long long;
  28. using ull = unsigned long long;
  29. using ld = long double;
  30.  
  31. #define sz(x) ((int)(x).size())
  32. #define all(x) (x).begin(), (x).end()
  33. #define rall(x) (x).rbegin(), (x).rend()
  34. #define hashmap unordered_map
  35. #define hashset unordered_set
  36. #define pb push_back
  37. #define ff first
  38. #define ss second
  39. #define en "\n"
  40.  
  41. const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  42. const ll linf = 0x3f3f3f3f3f3f3f3f;
  43. const int inf = 0x3f3f3f3f;
  44.  
  45. void __print(int x) { cerr << x; }
  46. void __print(ll x) { cerr << x; }
  47. void __print(ull x) { cerr << x; }
  48. void __print(double x) { cerr << x; }
  49. void __print(ld x) { cerr << x; }
  50. void __print(char x) { cerr << '\'' << x << '\''; }
  51. void __print(const string &x) { cerr << '"' << x << '"'; }
  52. void __print(bool x) { cerr << (x ? "true" : "false"); }
  53.  
  54. #ifdef LOCAL
  55. #define debug(...) \
  56. { \
  57.   cerr << "[" << #__VA_ARGS__ << "] = ["; \
  58.   _print(__VA_ARGS__); \
  59. }
  60. #else
  61. #define debug(...)
  62. #endif
  63.  
  64. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  65.  
  66. ll rnd(ll l, ll r)
  67. {
  68. return uniform_int_distribution<ll>(l, r)(rng);
  69. }
  70.  
  71. template<typename T, typename V>
  72. void __print(const pair<T, V> &x);
  73.  
  74. template<typename T>
  75. void __print(const T &x);
  76.  
  77. template<typename T, size_t N>
  78. void __print(const array<T, N> &x);
  79.  
  80. template<typename... T>
  81. void __print(const tuple<T...> &x);
  82.  
  83. template<class T>
  84. bool maximize(T &a, const T &b)
  85. {
  86. if(a < b)
  87. {
  88. a = b;
  89.  
  90. return true;
  91. }
  92.  
  93. return false;
  94. }
  95.  
  96. template<class T>
  97. bool minimize(T &a, const T &b)
  98. {
  99. if(a > b)
  100. {
  101. a = b;
  102.  
  103. return true;
  104. }
  105.  
  106. return false;
  107. }
  108.  
  109. template<typename T, typename V>
  110. void __print(const pair<T, V> &x)
  111. {
  112. cerr << '{';
  113. __print(x.ff);
  114. cerr << ", ";
  115. __print(x.ss);
  116. cerr << '}';
  117. }
  118.  
  119. template<typename T>
  120. void __print(const T &x)
  121. {
  122. int f = 0;
  123. cerr << '{';
  124. for(auto &i : x) cerr << (f++ ? ", " : ""), __print(i);
  125. cerr << "}";
  126. }
  127.  
  128. template<typename T, size_t N>
  129. void __print(const array<T, N> &x)
  130. {
  131. cerr << '{';
  132. int f = 0;
  133. for(auto &i : x) cerr << (f++ ? ", " : ""), __print(i);
  134. cerr << '}';
  135. }
  136.  
  137. template<typename... T>
  138. void __print(const tuple<T...> &x)
  139. {
  140. cerr << '(';
  141. apply([&](const auto&... args)
  142. {
  143. int f = 0;
  144. ((cerr << (f++ ? ", " : ""), __print(args)), ...);
  145. }, x);
  146. cerr << ')';
  147. }
  148.  
  149. void _print()
  150. {
  151. cerr << "]" << en;
  152. }
  153.  
  154. template<typename T, typename... V>
  155. void _print(T t, V... v)
  156. {
  157. __print(t);
  158. if(sizeof...(v)) cerr << ", ";
  159. _print(v...);
  160. }
  161.  
  162. namespace brute_force
  163. {
  164. void progress()
  165. {
  166.  
  167. }
  168. }
  169.  
  170. int m, n, h, w;
  171. vector<vector<int>> a;
  172.  
  173. namespace accepted_solution
  174. {
  175. void solve()
  176. {
  177. vector<vector<int>> cnt(m - h + 2, vector<int>(n - w + 2, 0));
  178. vector<vector<int>> prefix(m + 1, vector<int>(n + 1, 0));
  179. // for(int i = 1 ; i <= m ; i++)
  180. // {
  181. // for(int j = 1 ; j <= n ; j++)
  182. // {
  183. // for(int v = 0 ; v <= 100 ; v++)
  184. // {
  185. // prefix[v][i][j] = prefix[v][i - 1][j] + prefix[v][i][j - 1] - prefix[v][i - 1][j - 1] + (a[i][j] == v);
  186. // }
  187. // }
  188. // }
  189.  
  190. int need = (h * w + 1) / 2;
  191. int ans = inf;
  192. // for(int i = h ; i <= m ; i++)
  193. // {
  194. // for(int j = w ; j <= n ; j++)
  195. // {
  196. // int x1 = i - h + 1, y1 = j - w + 1;
  197. // int sum = 0;
  198.  
  199. // for(int v = 0 ; v <= 100 ; v++)
  200. // {
  201. // sum += prefix[v][i][j] - prefix[v][x1 - 1][j] - prefix[v][i][y1 - 1] + prefix[v][x1 - 1][y1 - 1];
  202.  
  203. // if(sum >= need)
  204. // {
  205. // minimize(ans, v);
  206. // break;
  207. // }
  208. // }
  209. // }
  210. // }
  211. for(int v = 0 ; v <= 100 ; v++)
  212. {
  213. for(int i = 1 ; i <= m ; i++)
  214. {
  215. for(int j =1 ; j <= n ; j++)
  216. {
  217. prefix[i][j] = prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1] + (a[i][j] == v);
  218. }
  219. }
  220.  
  221. for(int i = h ; i <= m ; i++)
  222. {
  223. for(int j = w ; j <= n ; j++)
  224. {
  225. int x1 = i - h + 1, y1 = j - w + 1;
  226. cnt[x1][y1] += prefix[i][j] - prefix[x1 - 1][j] - prefix[i][y1 - 1] + prefix[x1 - 1][y1 - 1];
  227.  
  228. if(cnt[x1][y1] >= need) minimize(ans, v);
  229. }
  230. }
  231. }
  232.  
  233. cout << ans;
  234. }
  235. }
  236.  
  237. int main()
  238. {
  239. ios_base::sync_with_stdio(0);
  240. cin.tie(0);
  241. cout.tie(0);
  242.  
  243. #define task "VUONHOA"
  244. if(fopen(task ".inp", "r"))
  245. {
  246. freopen(task ".inp", "r", stdin);
  247. freopen(task ".out", "w", stdout);
  248. }
  249.  
  250. cin >> m >> n >> h >> w;
  251. a.assign(m + 1, vector<int>(n+ 1, 0));
  252. for(int i = 1 ; i <= m ; i++)
  253. {
  254. for(int j = 1 ; j <= n ; j++)
  255. {
  256. cin >> a[i][j];
  257. }
  258. }
  259.  
  260. accepted_solution::solve();
  261.  
  262. return 0;
  263. }
  264.  
  265. /*
  266. Compile in terminal:
  267. g++ -std=c++17 -O2 -Wall -Wextra -Wshadow -Wconversion main.cpp -o main.exe
  268. main.exe
  269.  
  270. Debugging:
  271. g++ -std=c++17 -O0 -g -DLOCAL main.cpp -o main
  272. */
Success #stdin #stdout 0s 544KB
stdin
5 6 3 3
89 53 45 1 1 76
76 77 66 73 76 53
1 71 91 17 55 61
91 19 9 29 21 89
11 21 81 81 61 81
stdout
21