# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
41017 | IvanC | The Kingdom of JOIOI (JOI17_joioi) | C++14 | 2262 ms | 19700 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize ("unroll-loops")
using namespace std;
const int MAXN = 2010;
int matriz[MAXN][MAXN],MinVal,MaxVal,N,M,satisfazem;
void flip1(){
for(int linha = 1;linha<=N;linha++){
for(int i = 1,j = M;i <= j;i++,j--){
swap(matriz[linha][i],matriz[linha][j]);
}
}
}
void flip2(){
for(int coluna = 1;coluna<=M;coluna++){
for(int i = 1,j = N;i<=j;i++,j--){
swap(matriz[i][coluna],matriz[j][coluna]);
}
}
}
int checa1(int delta){
int last = M;
int tot = 0;
for(int linha = 1;linha<=N;linha++){
int old = last;
last = 0;
for(int coluna = 1;coluna<=old;coluna++){
if(matriz[linha][coluna] > MinVal + delta) break;
if(matriz[linha][coluna] < MaxVal - delta) tot++;
last = coluna;
}
}
return tot;
}
int checa2(int delta){
int last = N;
int tot = 0;
for(int coluna = 1;coluna<=M;coluna++){
int old = last;
last = 0;
for(int linha = 1;linha<=old;linha++){
if(matriz[linha][coluna] > MinVal + delta) break;
if(matriz[linha][coluna] < MaxVal - delta) tot++;
last = linha;
}
}
return tot;
}
int checa(int delta){
satisfazem = 0;
for(int i = 1;i<=N;i++){
for(int j = 1;j<=M;j++){
if(matriz[i][j] < MaxVal - delta) satisfazem++;
}
}
if(checa1(delta) == satisfazem) return 1;
flip1();
if(checa1(delta) == satisfazem) return 1;
flip2();
if(checa1(delta) == satisfazem) return 1;
flip1();
if(checa1(delta) == satisfazem) return 1;
if(checa2(delta) == satisfazem) return 1;
flip1();
if(checa2(delta) == satisfazem) return 1;
flip2();
if(checa2(delta) == satisfazem) return 1;
flip1();
if(checa2(delta) == satisfazem) return 1;
return 0;
}
int main(){
scanf("%d %d",&N,&M);
for(int i = 1;i<=N;i++){
for(int j = 1;j<=M;j++){
scanf("%d",&matriz[i][j]);
}
}
MinVal = MaxVal = matriz[1][1];
for(int i = 1;i<=N;i++){
for(int j = 1;j<=M;j++){
MinVal = min(MinVal,matriz[i][j]);
MaxVal = max(MaxVal,matriz[i][j]);
}
}
int ini = 0, fim = MaxVal - MinVal,resp = -1,meio;
while(ini <= fim){
meio = (ini+fim)/2;
if(checa(meio)){
resp = meio;
fim = meio - 1;
}
else{
ini = meio + 1;
}
}
printf("%d\n",resp);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |