Submission #1149321

#TimeUsernameProblemLanguageResultExecution timeMemory
1149321KluydQMaxcomp (info1cup18_maxcomp)C++20
0 / 100
133 ms452 KiB
#include <bits/stdc++.h>
 
#define respagold ios_base::sync_with_stdio(0), cin.tie(0);
#define ll long long
#define int long long
#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pb push_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define pii pair <int, int>
#define mkp make_pair
 
using namespace std;
 
const int N = 1e6 + 123;
const int inf = 2e18;
const int MOD = 1e9 + 7;
const int MOD1 = 998244353;
const int P = 6547;
 
int a[N], b[N], c[N], n, m, x, y, z, w;
int mz[N];
 
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
 
int rand( int l, int r )
{
    uniform_int_distribution <int> uid( l, r );
    return uid( rng );
}
bool valid( int x, int y )
{
	if( x < 1 || x > n || y < 1 || y > m ) return 0;
	return 1;
}
int calc( int x, int y )
{
	return (x - 1) * m + y;
}
bool check( int x, int y )
{
	int xy = x % m, yy = y % m; xy = ( xy == 0 ? m : xy ), yy = ( yy == 0 ? m : yy );
	int xx = ( x - xy ) / m + 1, yx = ( y - yy ) / m + 1;
	
	if( xx == yx ) return ( abs( xy - yy ) <= 1 );
	if( xy == yy ) return ( abs( xx - yx ) <= 1 );
	return 0;
}
void solve()
{
	cin >> n >> m;
	int ans = 0;
	
	FOR( i, 1, n, 1 )
	{
		FOR( j, 1, m, 1 )
		{
			cin >> mz[calc(i, j)];
		}
	}
	FOR( mask, 0, (1 << n * m) - 1, 1 )
	{
		int mn = inf, mx = -inf, cnt = 0;
		int lst = -1, ok = 1;
		
		FOR( i, 0, n * m - 1, 1 )
		{
			if( (mask >> i) & 1 )
			{
				if( lst != -1 && !check( lst, i + 1 ) ) ok = 0;
				lst = i + 1;
				
				mn = min( mn, mz[i + 1] );
				mx = max( mx, mz[i + 1] );
				cnt ++;
			}
		}
		if(ok) ans = max( ans, mx - mn - cnt );
	}
	cout << ans;
}
signed main() 
{
//	freopen("connect.in", "r", stdin);
//	freopen("connect.out", "w", stdout);
	    
    respagold
    
    int test = 1;
    
    if( !test ) cin >> test;
	
	while( test -- )
	{
		solve();
	}
}
// solved by KluydQ  
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...