답안 #683815

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
683815 2023-01-19T11:58:02 Z Ronin13 The Kingdom of JOIOI (JOI17_joioi) C++14
0 / 100
1 ms 436 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;

const int nmax = 2005;
pii dp[nmax][nmax];
int last[nmax];
int mnx, mny;
int MN;
int n, m;
const int inf = 1e9 + 1;

int mn[nmax][nmax], mx[nmax][nmax];
int a[nmax][nmax];

void fil(){

    for(int i = 1; i <= n; i++){
        mn[i][m + 1] = inf;
        mx[i][m + 1] = -inf;
        for(int j = m; j >= 1; j--)
            mx[i][j] = max(a[i][j], mx[i][j + 1]),
            mn[i][j] = min(a[i][j], mn[i][j + 1]);

    }
}

bool check(int X){
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= m; j++)
        dp[i][j] = {inf, -inf};
        if(i == 0) last[i] = m;
        else last[i] = 0;
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(a[i][j] > MN + X) break;
            last[i]++;
        }
    }

    last[1] = min(last[1], m - 1);
    for(int i= 1; i <= n; i++){
        for(int j = 0; j <= last[i]; j++){
            if(i == mnx && j < mny) {
                dp[i][j] = {-inf, inf};
                continue;
            }
            int u = min(j, last[i - 1]);
            dp[i][j].f = min(dp[i - 1][u].f, mn[i][j + 1]);
            dp[i][j].s = max(dp[i - 1][u].s, mx[i][j + 1]);
        }
        for(int j = last[i] + 1; j <= m; j++)
            dp[i][j] = {-inf, inf};
    }
    for(int j = 1; j <= m; j++){
        if(dp[n][j].s == -inf) continue;
        if(dp[n][j].s - dp[n][j].f <= X) return true;
    }
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= m; j++)
        dp[i][j] = {inf, -inf};
    }
    for(int i = 1; i <= n; i++){
        last[i] = 0;
        for(int j = 1; j <= m; j++){
            if(a[i][j] > MN + X) break;
            last[i]++;
        }
    }
    for(int i= 1; i <= n; i++){
        for(int j = 0; j <= last[i]; j++){
            if(i == mnx && j < mny) {
                dp[i][j] = {-inf, inf};
                continue;
            }
            int u = max(j, last[i - 1]);
            dp[i][j].f = min(dp[i - 1][u].f, mn[i][j + 1]);
            dp[i][j].s = max(dp[i - 1][u].s, mx[i][j + 1]);
        }
        for(int j = last[i] + 1; j <= m; j++)
            dp[i][j] = {-inf, inf};
    }
    for(int j = 0; j < m; j++){
        if(dp[n][j].s == -inf) continue;
        if(dp[n][j].s - dp[n][j].f <= X) return true;
    }
    return false;
}

int main(){

    cin >> n >> m;
    MN = inf;
    for(int i = 1; i <= n; i++){
        for(int j= 1; j <= m; j++){
            cin >> a[i][j];
            if(a[i][j] < MN)
                MN = a[i][j], mnx = i, mny = j;
        }
    }
    fil();

    int l = 0, r = 1e9+ 1;
    while(l + 1 < r){
        int mid = (l + r) / 2;
        if(check(mid)) r  = mid;
        else l = mid;
    }
    //cout << r;
    for(int i = 1 ;i <= n; i++){
        for(int j = 1; j <= m - j + 1; ++j)
            swap(a[i][j], a[i][m - j + 1]);
    }
    mny = m - mny + 1;
    int ans = r;
    l = 0, r = 1e9 +1;
    fil();
    while(l + 1 < r){
        int mid = (l + r) / 2;
        if(check(mid)) r = mid;
        else l = mid;
    }
    cout << min(ans, r);
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 1 ms 436 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 1 ms 436 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 1 ms 436 KB Output isn't correct
3 Halted 0 ms 0 KB -