Submission

Status:
PPP-PPP-PP

Score: 80

User: Winzzwz

Problemset: สูงต่ำในตาราง

Language: cpp

Time: 0.002 second

Submitted On: 2025-03-09 11:56:33

#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;

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));
            minheap.pop();
        }
        ans = max(abs(gmx-minheap.top().v)+2*b,ans);
    }
    cout << ans;
    return 0;
}