Submission #1286735

#TimeUsernameProblemLanguageResultExecution timeMemory
1286735trinm01The Kingdom of JOIOI (JOI17_joioi)C++20
100 / 100
1877 ms94764 KiB
// #pragma GCC optimize("O3")
// #pragma GCC optimization("Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;

#define int long long 
#define ll long long
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define FOD(i, r, l) for (int i = (r); i >= (l); i--)
#define fi first
#define se second
#define pii pair<int, int>

const ll mod = 1e9 + 7;
const int MAXN = 2e3 + 5;
const ll oo = 1e18 + 7;  
const int base = 10;

int m, n, a[MAXN][MAXN], mi=oo;

int hx[]={-1, 1, 0, 0}, hy[]={0, 0, -1, 1};
int f[MAXN][MAXN];
int ps[MAXN][MAXN];

int b[MAXN][MAXN];
bool chk(int k){
	FOR(i, 1, m){
		FOR(j, 1, n){
			f[i][j]=0;
			// cout << b[i][j] << ' ';
		}
		// cout << '\n';
	}
	int last=oo;
	FOR(i, 1, m){
		int pos=last;
		FOR(j, 1, n){
			if(b[i][j]-mi>k){
				pos=j;
				break;
			}
		}
		pos=min(pos, last);
		last=pos;
		FOR(j, pos, n){
			f[i][j]=1;
		}
	}
	
	int mi0=oo, mx0=-oo, mi1=oo, mx1=-oo;
	FOR(i, 1, m){
		FOR(j, 1, n){
			if(f[i][j]==0){
				mi0=min(mi0, b[i][j]);
				mx0=max(mx0, b[i][j]);
			}
			else{
				mi1=min(mi1, b[i][j]);
				mx1=max(mx1, b[i][j]);				
			}
		}
	}
	return (mx0-mi0<=k && mx1-mi1<=k);
}

bool check(int k){
	FOR(i, 1, m){
		FOR(j, 1, n){
			b[i][j]=a[i][j];
		}
	}
	if(chk(k)) return 1;
	FOR(i, 1, m){
		FOR(j, 1, n){
			b[i][j]=a[i][n-j+1];
		}
	}
	if(chk(k)) return 1;
	FOR(i, 1, m){
		FOR(j, 1, n){
			b[i][j]=a[m-i+1][j];
		}
	}
	if(chk(k)) return 1;
	FOR(i, 1, m){
		FOR(j, 1, n){
			b[i][j]=a[m-i+1][n-j+1];
		}
	}
	if(chk(k)) return 1;
	return 0;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    
    // freopen("test.txt", "r", stdin);
    // freopen("o2.out", "w", stdout);

    if(fopen(".inp", "r")){
        freopen(".inp", "r", stdin);
        freopen(".out", "w", stdout);
    }

    cin >> m >> n;
    FOR(i, 1, m){
    	FOR(j, 1, n){
    		cin >> a[i][j];
    		mi=min(mi, a[i][j]);
    	}
    }
    
    // cout << check(18) << '\n';
    // cout << check(17);
    
    int l=0, r=1e9, ans=0;
    while(l<=r){
    	int mid=(l+r)/2;
    	if(check(mid)){
    		ans=mid;
    		r=mid-1;
    	}
    	else{
    		l=mid+1;
    	}
    }
    cout << ans;
    
    return 0;
}

Compilation message (stderr)

joioi.cpp: In function 'int main()':
joioi.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
joioi.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         freopen(".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...