Pagini recente » Cod sursa (job #2405045) | Cod sursa (job #2631986) | Cod sursa (job #2712198) | Cod sursa (job #1740290) | Cod sursa (job #3039962)
#include <fstream>
#include <set>
using namespace std;
const int Nmax = 1000;
int n, m, sol, cate;
int mat[Nmax + 5][Nmax + 5], v[Nmax + 5];
multiset<int> s;
void afisSet(){
int h;
multiset<int>::iterator it;
h = 0;
for(it = s.begin(); it != s.end(); it++){
v[h++] = *it;
}
}
void actualizamSolutie(){
int a, b;
multiset<int>::iterator it;
it = s.end();
it--;
a = *it;
it = s.begin();
b = *it;
if(sol > a - b){
sol = a - b;
cate = 1;
}
else if(sol == a - b){
cate++;
}
}
void Solve(int x, int y){
int pozl, pozc;
for(int i = 1; i <= x; i++){
for(int j = 1; j <= y; j++){
s.insert(mat[i][j]);
}
}
pozc = 1;
for(pozl = 1; pozl <= n - x + 1; pozl++){
if(pozl % 2 == 1){
while(pozc <= m - y){
afisSet();
actualizamSolutie();
for(int i = pozl; i < pozl + x; i++){
s.erase(s.find(mat[i][pozc]));
s.insert(mat[i][pozc + y]);
}
pozc++;
}
afisSet();
actualizamSolutie();
for(int j = m - y + 1; j <= m && pozl <= n - x; j++){
s.erase(s.find(mat[pozl][j]));
s.insert(mat[pozl + x][j]);
}
}
else{
while(pozc > 1){
afisSet();
actualizamSolutie();
for(int i = pozl; i < pozl + x; i++){
s.erase(s.find(mat[i][pozc + y - 1]));
s.insert(mat[i][pozc - 1]);
}
pozc--;
}
afisSet();
actualizamSolutie();
for(int j = 1; j <= y && pozl <= n - x; j++){
s.erase(s.find(mat[pozl][j]));
s.insert(mat[pozl + x][j]);
}
}
}
s.clear();
}
int main(){
ifstream fin("struti.in");
ofstream fout("struti.out");
int t, lin, col;
fin >> n >> m >> t;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
fin >> mat[i][j];
}
}
while(t--){
fin >> lin >> col;
sol = 80000;
cate = 0;
Solve(lin, col);
if(lin != col){
Solve(col, lin);
}
fout << sol << " " << cate << '\n';
}
return 0;
}