답안 #1072344

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1072344 2024-08-23T17:15:11 Z dwuy The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
1202 ms 145888 KB
/**         - dwuy -

      />    フ
      |  _  _|
      /`ミ _x ノ
     /      |
    /   ヽ   ?
 / ̄|   | | |
 | ( ̄ヽ__ヽ_)_)
 \二つ

**/
#include <bits/stdc++.h>

#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) (int)((s).size())
#define MASK(k)(1LL<<(k))
#define TASK "test"

using namespace std;

typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;

const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MX = 2005;

int n, m;
int mxvalue, mnvalue;
int a[MX][MX];
bool f[MX][MX];
pii f1[MX][MX];
pii f2[MX][MX];
pii f3[MX][MX];
pii f4[MX][MX];

void nhap(){
    cin >> n >> m;
    cin.ignore();
    for(int i=1; i<=n; i++){
        string s; getline(cin, s); s += ' ';
        for(int j=0, t=1; j<len(s); j++) if(s[j] != ' '){
            a[i][t] = a[i][t]*10 + s[j] - '0';
            if(s[j + 1] == ' ') t++;
        }
    }
}

bool ok(int mid){
    memset(f, 0, sizeof f);
    /// case 1
    /// X O
    /// O O
    for(int i=1; i<=n; i++){
        for(int j=m; j>=0; j--){
            f[i][j] = f[i][j+1] | ((i == 1 || f[i-1][j]) && f1[i][j].se <= mnvalue + mid && f4[i][j+1].fi >= mxvalue - mid);
        }
    }
    if(f[n][0]) return 1;
    
    /// case 2
    /// O X
    /// O O
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m+1; j++){
            f[i][j] = f[i][j-1] | ((i == 1 || f[i-1][j]) && f2[i][j].se <= mnvalue + mid && f3[i][j-1].fi >= mxvalue - mid);
        }
    }
    if(f[n][m+1]) return 1;
    
    memset(f, 0, sizeof f);
    /// case 3
    /// O O
    /// X O
    for(int i=n; i>=1; i--){
        for(int j=m; j>=0; j--){
            f[i][j] = f[i][j+1] | ((i == n || f[i+1][j]) && f3[i][j].se <= mnvalue + mid && f2[i][j+1].fi >= mxvalue - mid);
        }
    }
    if(f[1][0]) return 1;

    /// case 4
    /// O O
    /// O X
    for(int i=n; i>=1; i--){
        for(int j=1; j<=m+1; j++){
            f[i][j] = f[i][j-1] | ((i == n || f[i+1][j]) && f4[i][j].se <= mnvalue + mid && f1[i][j-1].fi >= mxvalue - mid);
        }
    }
    if(f[1][m+1]) return 1;

    return 0;
}

pii combine(pii a, pii b){
    return {min(a.fi, b.fi), max(a.se, b.se)};
}

void solve(){
    for(int i=0; i<=n+1; i++){
        for(int j=0; j<=m+n; j++){
            f1[i][j] = f2[i][j] = f3[i][j] = f4[i][j] = {INF, -INF};
        }
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            f1[i][j] = combine(combine(f1[i-1][j], f1[i][j-1]), {a[i][j], a[i][j]});
        }
        for(int j=m; j>=1; j--){
            f2[i][j] = combine(combine(f2[i-1][j], f2[i][j+1]), {a[i][j], a[i][j]});
        }
    }
    for(int i=n; i>=1; i--){
        for(int j=1; j<=m; j++){
            f3[i][j] = combine(combine(f3[i+1][j], f3[i][j-1]), {a[i][j], a[i][j]});
        }
        for(int j=m; j>=1; j--){
            f4[i][j] = combine(combine(f4[i+1][j], f4[i][j+1]), {a[i][j], a[i][j]});
        }
    }
    
    mnvalue = f1[n][m].first;
    mxvalue = f1[n][m].second;

    int res = -1;
    for(int lo=1, hi=1e9; lo<=hi;){
        int mid = (lo + hi)>>1;
        if(ok(mid)) res = mid, hi = mid - 1;
        else lo = mid + 1;
    }

    cout << res;
}

int32_t main(){
    fastIO;
    //file(TASK);

    nhap();
    solve();

    return 0;
}


# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6488 KB Output is correct
2 Correct 6 ms 6492 KB Output is correct
3 Correct 6 ms 6652 KB Output is correct
4 Correct 7 ms 6492 KB Output is correct
5 Correct 7 ms 6492 KB Output is correct
6 Correct 8 ms 6492 KB Output is correct
7 Correct 8 ms 6488 KB Output is correct
8 Correct 6 ms 6492 KB Output is correct
9 Correct 9 ms 6492 KB Output is correct
10 Correct 8 ms 6492 KB Output is correct
11 Correct 7 ms 6656 KB Output is correct
12 Correct 7 ms 6492 KB Output is correct
13 Correct 6 ms 8648 KB Output is correct
14 Correct 7 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6488 KB Output is correct
2 Correct 6 ms 6492 KB Output is correct
3 Correct 6 ms 6652 KB Output is correct
4 Correct 7 ms 6492 KB Output is correct
5 Correct 7 ms 6492 KB Output is correct
6 Correct 8 ms 6492 KB Output is correct
7 Correct 8 ms 6488 KB Output is correct
8 Correct 6 ms 6492 KB Output is correct
9 Correct 9 ms 6492 KB Output is correct
10 Correct 8 ms 6492 KB Output is correct
11 Correct 7 ms 6656 KB Output is correct
12 Correct 7 ms 6492 KB Output is correct
13 Correct 6 ms 8648 KB Output is correct
14 Correct 7 ms 6492 KB Output is correct
15 Correct 6 ms 6748 KB Output is correct
16 Correct 11 ms 12428 KB Output is correct
17 Correct 13 ms 12376 KB Output is correct
18 Correct 14 ms 12504 KB Output is correct
19 Correct 17 ms 12380 KB Output is correct
20 Correct 16 ms 11612 KB Output is correct
21 Correct 18 ms 14172 KB Output is correct
22 Correct 20 ms 12444 KB Output is correct
23 Correct 16 ms 12344 KB Output is correct
24 Correct 15 ms 11612 KB Output is correct
25 Correct 16 ms 12380 KB Output is correct
26 Correct 21 ms 12524 KB Output is correct
27 Correct 19 ms 12516 KB Output is correct
28 Correct 20 ms 12380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6488 KB Output is correct
2 Correct 6 ms 6492 KB Output is correct
3 Correct 6 ms 6652 KB Output is correct
4 Correct 7 ms 6492 KB Output is correct
5 Correct 7 ms 6492 KB Output is correct
6 Correct 8 ms 6492 KB Output is correct
7 Correct 8 ms 6488 KB Output is correct
8 Correct 6 ms 6492 KB Output is correct
9 Correct 9 ms 6492 KB Output is correct
10 Correct 8 ms 6492 KB Output is correct
11 Correct 7 ms 6656 KB Output is correct
12 Correct 7 ms 6492 KB Output is correct
13 Correct 6 ms 8648 KB Output is correct
14 Correct 7 ms 6492 KB Output is correct
15 Correct 6 ms 6748 KB Output is correct
16 Correct 11 ms 12428 KB Output is correct
17 Correct 13 ms 12376 KB Output is correct
18 Correct 14 ms 12504 KB Output is correct
19 Correct 17 ms 12380 KB Output is correct
20 Correct 16 ms 11612 KB Output is correct
21 Correct 18 ms 14172 KB Output is correct
22 Correct 20 ms 12444 KB Output is correct
23 Correct 16 ms 12344 KB Output is correct
24 Correct 15 ms 11612 KB Output is correct
25 Correct 16 ms 12380 KB Output is correct
26 Correct 21 ms 12524 KB Output is correct
27 Correct 19 ms 12516 KB Output is correct
28 Correct 20 ms 12380 KB Output is correct
29 Correct 668 ms 139180 KB Output is correct
30 Correct 795 ms 145780 KB Output is correct
31 Correct 785 ms 145600 KB Output is correct
32 Correct 811 ms 145888 KB Output is correct
33 Correct 605 ms 129116 KB Output is correct
34 Correct 829 ms 145860 KB Output is correct
35 Correct 1066 ms 145744 KB Output is correct
36 Correct 776 ms 145232 KB Output is correct
37 Correct 1202 ms 145808 KB Output is correct