Submission #1352064

#TimeUsernameProblemLanguageResultExecution timeMemory
1352064luvwinterThe Kingdom of JOIOI (JOI17_joioi)C++17
100 / 100
502 ms63220 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int , int>
#define fi first
#define se second
#define FOR(i , l , r) for(int i = (l) ; i <= (r) ; i++)
#define FOD(i , r , l) for(int i = (r) ; i >= (l) ; i--)
const int N = 2e3 + 5;
const int INF = 1e9 + 5;

int a[N][N];
int b[N][N];
int h , w;
vector<int> mrotate = {1 , 2 , 3 , 4};
int mx_val;

bool check(int mid) {
     int mx = w;
     int mnblue = INF;
     int mxblue = 0;
     FOR(i , 1 , h) {
         FOR(j , 1 , mx) {
             if(mx_val - a[i][j] > mid) {
                mx = j - 1;
                break;
             }
         }
         FOR(j , mx + 1 , w) {
             mnblue = min(a[i][j] , mnblue);
             mxblue = max(a[i][j] , mxblue);
         }
     }
     if(mxblue - mnblue > mid) return false;
     return true;

}


void solve () {
     cin >> h >> w;
     FOR(i , 1 , h) {
         FOR(j , 1 , w) {
             cin >> a[i][j];
             mx_val = max(mx_val , a[i][j]);
         }
     }
     int ans = INF;
     for(auto kk : mrotate) {
            int l = 0;
            int r = INF;
            while(l <= r) {
                int mid = (l + r) / 2;
                if(check(mid)) {
                    ans = min(ans , mid);
                    r = mid - 1;
                }
                else{
                    l = mid + 1;
                }
            }
            int curr_h = h;
            int curr_w = w;
            swap(h , w);
            FOR(i , 1 , curr_h) {
               FOR(j , 1 , curr_w) {
                   b[j][curr_h - i + 1] = a[i][j];
               }
            }
            FOR(i , 1 , h) {
                FOR(j , 1 , w) {
                   a[i][j] = b[i][j];
                }
            }
     }


     cout << ans;



}


main () {
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);
    solve();

    return 0;

}

Compilation message (stderr)

joioi.cpp:85:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   85 | main () {
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...