42782023-03-21 15:48:41mraron2015. októbercpp17Accepted 815ms32812 KiB
//Noszály Áron 8o Debreceni Fazekas Mihály Gimnázium

#include<bits/stdc++.h>
#include<cstdlib>

using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef long double ld;

#define all(s) (s).begin(),(s).end()
#define pb push_back
#define IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define INF std::numeric_limits<int>::max()
#define tmax(a,b,c) max((a),max((b),(c)))
#define tmin(a,b,c) min((a),min((b),(c)))
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
#define mp make_pair

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif


int d2[8][2]={{1,0},{0,1},{0,-1},{-1,0},{1,1},{-1,1},{1,-1},{-1,-1}};

//---PROGRAM KEZDETE---

int d1[4][2]={{1,0},{0,1},{0,-1},{-1,0}}; //szomszédok meghatározásához
int t[805][805]; //magasságok
int h[805][805]; //szép-e ?
int n,m; //méretek
int startx, starty; //egy szép hely
	
bool valid(int x, int y) //egy jó koordináta, ami nem lóg ki sehol
{
	return (x>=0 && y>=0 && x<n && y<m);
}

	
int a[805][805]; //bejárt helyek
int cnt = 0, cntmax = 0; //bejárt szép helyek, az összes szép hely
void dfs(int xx, int yy, int q)  //mélységi bejárás, honnan kezdjük, legfeljebb q magasság különbség
{
	stack<pair<int, int>> s;s.push(mp(xx,yy));
	while(!s.empty())
	{
		pair<int, int> top=s.top(); s.pop();
		int x=top.first, y=top.second; //mostani helyzet
		if(h[x][y]==1) cnt++; //ez egy szép hely
		for(int j=0;j<4;++j) //szomszédai
		{
			int dx = d1[j][0], dy = d1[j][1]; 
			int xn = x+dx, yn=y+dy;  //következő hely
			if(valid(xn,yn) && a[xn][yn]==0 && abs(t[x][y]-t[xn][yn])<=q) //koordinátok jók és nem jártunk itt és megfelelő a magasság különbség is ideális
			{
				a[xn][yn]=1; //ide többé nem jövünk
				s.push(mp(xn,yn)); //tovább
			}
		}
	}
}

map<int, int> ppp;
int calc(int mid) //f(c)
{
	if ( ppp.find(mid)!=ppp.end() ) return ppp[mid];
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			a[i][j]=0; //nem jártunk még sehol
		}
	}
	a[startx][starty]=1; //dfs()-ben csak azokat 1-ezzük ki melyekbe lépünk
	cnt=0; 
	dfs(startx, starty, mid); //mélységi bejárás
	
	ppp[mid]=(cntmax==cnt?1:0);
	
	return (cntmax==cnt?1:0); //ha minden szép helyet bejártunk-e?
}
int main()
{
	//---BEOLVASÁS---
	scanf("%d%d", &n, &m);
	
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			scanf("%d", &t[i][j]);
		}
	}
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			scanf("%d", &h[i][j]);
			if(h[i][j]) //ez egy szép hely
			{
				cntmax++; //széphelyeink számának növelése
				startx = i; starty = j; 
			}
		}
	}
	//---BEOLVASÁS---
	vector<int> k={0}; // az összes élszomszéd különbségének abszolút értékei (hogy ne kelljen mindig 10^9-t nézni), {0}=>hogy lekezeljük ha csak 1 és 0 szép csúcs van van
	for(int i=0;i<n;++i)
	{
		for(int j=1;j<m;++j)
		{
			int val = abs(t[i][j]-t[i][j-1]);
			k.pb(val);
		}
	}
	for(int i=1;i<n;++i)
	{
		for(int j=0;j<m;++j)
		{
			int val = abs(t[i-1][j]-t[i][j]);
			k.pb(val);
		}
	}

	sort(all(k)); //rendezzük őket, hogy bináris keresést végrehajthassuk
		
	int l=0, r=k.size()-1; //lo, up
	while(l<r) //bináris keresés
	{
		int mid=(r+l)/2;
		int midval=k[mid];
		if(calc(midval)) { 
			r=mid;
		} else  {
			l=mid+1; 
		}
	}
	printf("%d", k[l]);
	return 0;
}
1 - Accepted
Memory: 1716KiB
Time: 3ms

Program's output:
21
Expected output:
21
Checker output:
ok 1 number(s): "21"

2 - Accepted
Memory: 2064KiB
Time: 3ms

Program's output:
0
Expected output:
0
Checker output:
ok 1 number(s): "0"

3 - Accepted
Memory: 2364KiB
Time: 3ms

Program's output:
1
Expected output:
1
Checker output:
ok 1 number(s): "1"

4 - Accepted
Memory: 2488KiB
Time: 3ms

Program's output:
21
Expected output:
21
Checker output:
ok 1 number(s): "21"

5 - Accepted
Memory: 32812KiB
Time: 386ms

Program's output:
9
Expected output:
9
Checker output:
ok 1 number(s): "9"

6 - Accepted
Memory: 32472KiB
Time: 815ms

Program's output:
887456
Expected output:
887456
Checker output:
ok 1 number(s): "887456"

7 - Accepted
Memory: 17484KiB
Time: 46ms

Program's output:
800838205
Expected output:
800838205
Checker output:
ok 1 number(s): "800838205"

8 - Accepted
Memory: 6340KiB
Time: 65ms

Program's output:
838823483
Expected output:
838823483
Checker output:
ok 1 number(s): "838823483"

9 - Accepted
Memory: 5652KiB
Time: 26ms

Program's output:
779345500
Expected output:
779345500
Checker output:
ok 1 number(s): "779345500"

10 - Accepted
Memory: 9720KiB
Time: 64ms

Program's output:
858627679
Expected output:
858627679
Checker output:
ok 1 number(s): "858627679"

11 - Accepted
Memory: 6744KiB
Time: 14ms

Program's output:
728056287
Expected output:
728056287
Checker output:
ok 1 number(s): "728056287"

12 - Accepted
Memory: 7776KiB
Time: 83ms

Program's output:
861982183
Expected output:
861982183
Checker output:
ok 1 number(s): "861982183"

13 - Accepted
Memory: 4336KiB
Time: 10ms

Program's output:
723371123
Expected output:
723371123
Checker output:
ok 1 number(s): "723371123"

14 - Accepted
Memory: 15372KiB
Time: 207ms

Program's output:
896373992
Expected output:
896373992
Checker output:
ok 1 number(s): "896373992"

15 - Accepted
Memory: 12852KiB
Time: 180ms

Program's output:
910527745
Expected output:
910527745
Checker output:
ok 1 number(s): "910527745"

16 - Accepted
Memory: 17728KiB
Time: 277ms

Program's output:
855478005
Expected output:
855478005
Checker output:
ok 1 number(s): "855478005"

17 - Accepted
Memory: 25440KiB
Time: 513ms

Program's output:
904379815
Expected output:
904379815
Checker output:
ok 1 number(s): "904379815"

18 - Accepted
Memory: 7860KiB
Time: 87ms

Program's output:
867165261
Expected output:
867165261
Checker output:
ok 1 number(s): "867165261"

19 - Accepted
Memory: 18412KiB
Time: 273ms

Program's output:
944776036
Expected output:
944776036
Checker output:
ok 1 number(s): "944776036"

20 - Accepted
Memory: 11780KiB
Time: 172ms

Program's output:
924769782
Expected output:
924769782
Checker output:
ok 1 number(s): "924769782"

21 - Accepted
Memory: 13468KiB
Time: 142ms

Program's output:
850634269
Expected output:
850634269
Checker output:
ok 1 number(s): "850634269"

22 - Accepted
Memory: 26584KiB
Time: 535ms

Program's output:
922216943
Expected output:
922216943
Checker output:
ok 1 number(s): "922216943"

23 - Accepted
Memory: 9352KiB
Time: 97ms

Program's output:
831862676
Expected output:
831862676
Checker output:
ok 1 number(s): "831862676"

24 - Accepted
Memory: 12020KiB
Time: 14ms

Program's output:
817460016
Expected output:
817460016
Checker output:
ok 1 number(s): "817460016"

25 - Accepted
Memory: 26900KiB
Time: 546ms

Program's output:
931672223
Expected output:
931672223
Checker output:
ok 1 number(s): "931672223"

26 - Accepted
Memory: 8960KiB
Time: 74ms

Program's output:
836673665
Expected output:
836673665
Checker output:
ok 1 number(s): "836673665"

27 - Accepted
Memory: 9080KiB
Time: 78ms

Program's output:
962697444
Expected output:
962697444
Checker output:
ok 1 number(s): "962697444"