Submission
Status:
PPPPPPPPPP
Score: 100
User: Winzzwz
Problemset: สูงต่ำในตาราง
Language: cpp
Time: 0.002 second
Submitted On: 2025-03-09 12:08:53
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct h1 {
int i,j,v;
bool operator < (const h1&o) const {
return v < o.v;
}
};
struct h2 {
int i,j,v;
bool operator < (const h2&o) const {
return v > o.v;
}
};
int n,b,nn,ans,gmn,gmx;
priority_queue <h1> maxheap;
priority_queue <h2> minheap;
queue <h2> temp;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> b;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> nn;
maxheap.push({i,j,nn});
minheap.push({i,j,nn});
}
}
gmn = minheap.top().v;
gmx = maxheap.top().v;
if (minheap.top().i != maxheap.top().i) {
ans = gmx-gmn+2*b;
} else {
while (minheap.top().i == maxheap.top().i) {
if (abs(minheap.top().v - gmn) >= b) break;
ans = max(ans,abs(maxheap.top().v - minheap.top().v));
temp.push(minheap.top());
minheap.pop();
}
ans = max(abs(gmx-minheap.top().v)+2*b,ans);
while (!temp.empty()) {
minheap.push(temp.front());
temp.pop();
}
while (minheap.top().i == maxheap.top().i) {
if (abs(maxheap.top().v - gmx) >= b) break;
ans = max(ans,abs(maxheap.top().v - minheap.top().v));
maxheap.pop();
}
ans = max(abs(maxheap.top().v-gmn)+2*b,ans);
}
cout << ans;
return 0;
}