제출 #534564

#제출 시각아이디문제언어결과실행 시간메모리
534564ioiThe Kingdom of JOIOI (JOI17_joioi)C++14
100 / 100
1497 ms227552 KiB
#include<bits/stdc++.h> using namespace std; #define debug(x) cout << '[' << #x << " is: " << x << "] " << endl; #define inF freopen("6in.txt" , "r" , stdin); #define outF freopen("6.txt" , "w" , stdout); #define imod(a , n) (a % n + n ) % n #define sor(v) sort(v.begin() , v.end()); #define print(v) for(auto f : v ) cout << f << " " ; #define rsor(v) sort(v.rbegin() , v.rend()); #define rev(v) reverse(v.begin() , v.end()); #define scan(v)for(auto &it : v)cin >> it ; #define ll long long #define int ll #define logar(x , y) log(x) / log(y) #define __sum(n) n * (n + 1) / 2 #define __lcm(a , b) a / __gcd(a , b) * b #define pii pair<int , int > #define fastio ios_base::sync_with_stdio(false);cin.tie(0); const int N = 2e3 + 5 ; int n , m ; int arr[N][N] , arr2[N][N]; int themax[N][N] , themin[N][N] , rmax[N][N] , rmin[N][N]; void process(){ for(int c = 0 ; c <m ; c ++){ for(int r = 0 ; r < n ; r ++){ themax[r][c] = themin[r][c] = arr[r][c]; if(r)themax[r][c] = max(themax[r][c] , themax[r - 1][c]) , themin[r][c] = min(themin[r][c] , themin[r - 1][c]); } } for(int c = 0 ; c < m ; c ++){ rmin[n][c] = 1e9 , rmax[n][c] = 0 ; for(int r = n - 1 ; r >= 0 ; r --){ rmin[r][c] = rmax[r][c] = arr[r][c] ; rmin[r][c] = min(rmin[r][c] , rmin[r + 1][c]) , rmax[r][c] = max(rmax[r][c] , rmax[r + 1][c]); } } } int mnmm = 1e9 ; bool check(int number){ int mx = 0 , mn = 1e9 ; int en = n ; for(int c = 0 ; c < m; c ++){ int l = 0 , r = en - 1 , md , ans = n; while(l <= r ){ md = (l + r) / 2 ; if(themax[md][c] > number) ans = md , r = md - 1 ; else l = md + 1 ; } en = min(en , ans); mx = max(mx , rmax[en][c] ), mn = min(mn , rmin[en][c]); } return mx - mn <= number - mnmm; } int32_t main() { //inF; //inF;outF; fastio; cin >> n >>m; for(int i = 0 ;i < n ; i ++) for(int j = 0 ; j < m ; j ++){ cin >> arr[i][j]; mnmm = min(mnmm , arr[i][j]); // debug(mnmm) arr2[i][j] = arr[i][j] ; } // debug(mnmm) int mnans = 1e9 ; for(int c = 0 ; c < 4 ; c ++){ process(); // cout << " NEW \n"; int l = 0 , r = 1e9 , md ; while(l <= r ){ md = (l + r) / 2 ; if(check(md + mnmm))mnans = min(mnans , md) , r = md - 1 ; else l = md + 1; } // break; if(c != 1){ for(int col = 0 ; col < m ; col ++){ for(int r = 0 ; r < n ; r ++){ arr[r][col] = arr2[r][m - 1 - col]; } } } else { for(int r = 0 ; r < n ; r ++){ for(int col = 0 ; col < m ; col ++) arr[r][col] = arr2[n - r - 1][col]; } // cout << endl ; } // cout << " NEW " << c << " \n"; for (int i = 0; i < n; ++ i){ for (int j = 0; j < m; ++ j){ // cout << arr[i][j] << " " ; arr2[i][j] = arr[i][j]; } // cout << endl ; } // debug(mnans) } // cout << 11 ; cout << mnans ; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...