洛谷题号:3933
乱搞、二分答案+贪心,根本想不出来,打暴力30分……
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include <iostream> #include <algorithm> #include <cstring> #include <string> #include <queue> using namespace std; int n, m; int arr[2005][2005]; int arr_rotate[2005][2005]; int pos[2005]; int minnum = 2e9, maxnum = 0; bool check(int value){ int limit = 0; for (int i = 1; i <= n; i++){ for (int j = 1; j <= m; j++){ if (arr[i][j] >minnum+value)limit = max(limit, j + 1); } for (int j = 1; j <= m; j++){ if (maxnum - value > arr[i][j])if (j < limit)return false; } } return true; } /*check问题搞不懂可以仔细阅读T2题解 check的等效写法: bool check(int value){ int limit = 0; for (int i = 1; i <= n; i++){ for (int j = 1; j <= m; j++){ if (arr[i][j] < maxnum - value)limit = max(limit, j + 1); } for (int j = 1; j <= m; j++){ if (minnum + value < arr[i][j])if (j < limit)return false; } } return true; } */ void rotate90degrees(){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= m; j++){ arr_rotate[j][n-i+1] = arr[i][j]; } } memcpy(arr, arr_rotate, sizeof(arr)); swap(n, m); } int solve(){ int low = 0, high = maxnum - minnum,mid; while (low < high){ mid = (low + high) / 2; if (check(mid)){ high= mid; } else{ low = mid + 1; } } return low; } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; i++){ for (int j = 1; j <= m; j++){ cin >> arr[i][j]; maxnum = max(maxnum,arr[i][j]); minnum = min(minnum,arr[i][j]); } } int re = 2e9; for (int i = 1; i <= 4; i++){ re = min(re, solve()); rotate90degrees(); } cout << re; return 0; } |