#include<bits/stdc++.h>
using namespace std;
#define int long long
#define in pair<int, int>
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define INF (int)1e17
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL)
const int SMX = 2e3+3;
int a[SMX][SMX];
int col[SMX][SMX];
bool check(int red, int n, int m)
{
int l[n];
int r[n];
for(int i = 0; i < n; i++)
{
for(r[i] = 0; r[i] < m; r[i]++)
{
if(col[i][r[i]] == red)
break;
}
for(int j = r[i]; j < m; j++)
{
if(col[i][j] == (3ll^red))
return false;
}
r[i]--;
l[i] = r[i];
while((l[i] >= 0) && (col[i][l[i]] == 0))
l[i]--;
}
int p = l[0];
bool chad1 = false;
for(int i = 1; i < n && !chad1; i++)
{
if(p > r[i])
{
chad1 = true;
break;
}
p = max(p, l[i]);
}
int q = r[0];
bool chad2 = false;
for(int i = 1; i < n && !chad2; i++)
{
if(q < l[i])
{
chad2 = true;
break;
}
q = min(q, r[i]);
}
if(chad1 && chad2)
return false;
return true;
}
signed main()
{
fast();
int n, m;
cin >> n >> m;
int mx, mn;
mx = -INF; mn = INF;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cin >> a[i][j];
mx = max(mx, a[i][j]); mn = min(mn, a[i][j]);
}
}
int l = 0;
int r = mx-mn;
while(l < r)
{
int mid = (l+r)/2;
bool fl = true;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
col[i][j] = 0;
if((mx-a[i][j]) > mid)
col[i][j]+=1;
if((a[i][j]-mn) > mid)
col[i][j]+=2;
if(col[i][j] == 3)
fl = false;
}
}
if(!fl)
{
l = mid+1;
continue;
}
//can we seperate colors 1 and 2?
bool works = check(1, n, m)||check(2, n, m);
if(works)
r = mid;
else
l = mid+1;
}
cout << l << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2512 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2512 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
4 ms |
8796 KB |
Output is correct |
17 |
Correct |
6 ms |
8880 KB |
Output is correct |
18 |
Correct |
5 ms |
8796 KB |
Output is correct |
19 |
Correct |
7 ms |
8936 KB |
Output is correct |
20 |
Correct |
6 ms |
8796 KB |
Output is correct |
21 |
Correct |
8 ms |
9052 KB |
Output is correct |
22 |
Correct |
6 ms |
9052 KB |
Output is correct |
23 |
Correct |
9 ms |
9304 KB |
Output is correct |
24 |
Correct |
7 ms |
9048 KB |
Output is correct |
25 |
Correct |
11 ms |
9052 KB |
Output is correct |
26 |
Correct |
7 ms |
9052 KB |
Output is correct |
27 |
Correct |
8 ms |
9052 KB |
Output is correct |
28 |
Correct |
7 ms |
9052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2512 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
4 ms |
8796 KB |
Output is correct |
17 |
Correct |
6 ms |
8880 KB |
Output is correct |
18 |
Correct |
5 ms |
8796 KB |
Output is correct |
19 |
Correct |
7 ms |
8936 KB |
Output is correct |
20 |
Correct |
6 ms |
8796 KB |
Output is correct |
21 |
Correct |
8 ms |
9052 KB |
Output is correct |
22 |
Correct |
6 ms |
9052 KB |
Output is correct |
23 |
Correct |
9 ms |
9304 KB |
Output is correct |
24 |
Correct |
7 ms |
9048 KB |
Output is correct |
25 |
Correct |
11 ms |
9052 KB |
Output is correct |
26 |
Correct |
7 ms |
9052 KB |
Output is correct |
27 |
Correct |
8 ms |
9052 KB |
Output is correct |
28 |
Correct |
7 ms |
9052 KB |
Output is correct |
29 |
Correct |
455 ms |
84312 KB |
Output is correct |
30 |
Correct |
466 ms |
84752 KB |
Output is correct |
31 |
Correct |
613 ms |
86540 KB |
Output is correct |
32 |
Correct |
411 ms |
86356 KB |
Output is correct |
33 |
Correct |
319 ms |
78004 KB |
Output is correct |
34 |
Correct |
507 ms |
86440 KB |
Output is correct |
35 |
Correct |
603 ms |
101940 KB |
Output is correct |
36 |
Correct |
530 ms |
96648 KB |
Output is correct |
37 |
Correct |
872 ms |
102104 KB |
Output is correct |