답안 #831039

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
831039 2023-08-19T15:36:08 Z YassineBenYounes The Kingdom of JOIOI (JOI17_joioi) C++17
60 / 100
502 ms 262144 KB
#include<bits/stdc++.h>
using namespace std;
void init(){
    #ifndef ONLINE_JUDGE
 
freopen("input.txt", "r", stdin);
 
freopen("output.txt", "w", stdout);
 
#endif // ONLINE_JUDGE
}
typedef long long ll;
#define pii pair<int, int>
#define ff first
#define ss second
#define vii vector<pii>
#define pb push_back
#define vi vector<int>
const int mx = 2000;
const int inf = 1e9;
const int LOG = 22;
 
int n, m;
 
int grid[mx][mx];
 
int prefmin1[mx][mx];
int prefmin[mx][mx];
int prefmin2[mx][mx];
int prefmin3[mx][mx];
 
int prefmax1[mx][mx];
int prefmax[mx][mx];
int prefmax2[mx][mx];
int prefmax3[mx][mx];
vector<vi> v;
 
void preprocess(){
    for(int i = 0; i < n;i++){
        for(int j = 0;j < m;j++){
            int x = inf, y = inf;
            if(i > 0)x = prefmin[i-1][j];
            if(j > 0)y = prefmin[i][j-1];
            prefmin[i][j] = min(x, y);
            prefmin[i][j] = min(prefmin[i][j], grid[i][j]);
        }
    }
    for(int i = 0; i < n;i++){
        for(int j = 0;j < m;j++){
            int x = 0, y = 0;
            if(i > 0)x = prefmax[i-1][j];
            if(j > 0)y = prefmax[i][j-1];
            prefmax[i][j] = max(x, y);
            prefmax[i][j] = max(prefmax[i][j], grid[i][j]);
        }
    }
    // 1 : 
    for(int i = n-1; i >= 0;i--){
        for(int j = 0;j < m;j++){
            int x = inf, y = inf;
            if((i+1) < n)x = prefmin1[i+1][j];
            if(j > 0)y = prefmin1[i][j-1];
            prefmin1[i][j] = min(x, y);
            prefmin1[i][j] = min(prefmin1[i][j], grid[i][j]);
        }
    }
    for(int i = n-1; i >= 0;i--){
        for(int j = 0;j < m;j++){
            int x = 0, y = 0;
            if((i+1) < n)x = prefmax1[i+1][j];
            if(j > 0)y = prefmax1[i][j-1];
            prefmax1[i][j] = max(x, y);
            prefmax1[i][j] = max(prefmax1[i][j], grid[i][j]);
        }
    }
    // 2 : 
    for(int i = 0; i < n;i++){
        for(int j = m-1;j >= 0;j--){
            int x = inf, y = inf;
            if(i > 0)x = prefmin2[i-1][j];
            if((j+1) < m)y = prefmin2[i][j+1];
            prefmin2[i][j] = min(x, y);
            prefmin2[i][j] = min(prefmin2[i][j], grid[i][j]);
        }
    }
    for(int i = 0; i < n;i++){
        for(int j = m-1;j >= 0;j--){
            int x = 0, y = 0;
            if(i > 0)x = prefmax2[i-1][j];
            if((j+1) < m)y = prefmax2[i][j+1];
            prefmax2[i][j] = max(x, y);
            prefmax2[i][j] = max(prefmax2[i][j], grid[i][j]);
        }
    }
    // 3:
    for(int i = n-1; i >= 0;i--){
        for(int j = m-1;j >= 0;j--){
            int x = inf, y = inf;
            if((i+1) < n)x = prefmin3[i+1][j];
            if((j+1) < m)y = prefmin3[i][j+1];
            prefmin3[i][j] = min(x, y);
            prefmin3[i][j] = min(prefmin3[i][j], grid[i][j]);
        }
    }
    for(int i = n-1; i >= 0;i--){
        for(int j = m-1;j >= 0;j--){
            int x = 0, y = 0;
            if((i+1) < n)x = prefmax3[i+1][j];
            if((j+1) < m)y = prefmax3[i][j+1];
            prefmax3[i][j] = max(x, y);
            prefmax3[i][j] = max(prefmax3[i][j], grid[i][j]);
        }
    }
}
 
int32_t main(){
    cin.tie();cout.tie();ios::sync_with_stdio(false);
    cin >> n >> m;
    for(int i = 0; i < n;i++){
        for(int j = 0;j < m;j++){
            cin >> grid[i][j];
            v.pb({grid[i][j], i, j});
        }
    }
    preprocess();
    int res = inf;
    sort(v.begin(), v.end());
    int a = inf, b = -inf;
    for(int i = 0; i < v.size();i++){
        if(i > 0){
            int x = v[i-1][1], y = v[i-1][2];
            a = min(a, prefmin[x][y]);
            b = max(b, prefmax[x][y]);
        }
        res = min(res, max(v[v.size()-1][0] - v[i][0], b-a));
    }
    a = inf, b = -inf;
    for(int i = 0; i < v.size();i++){
        if(i > 0){
            int x = v[i-1][1], y = v[i-1][2];
            a = min(a, prefmin1[x][y]);
            b = max(b, prefmax1[x][y]);
        }
        res = min(res, max(v[v.size()-1][0] - v[i][0], b-a));
    }
    a = inf, b = -inf;
    for(int i = 0; i < v.size();i++){
        if(i > 0){
            int x = v[i-1][1], y = v[i-1][2];
            a = min(a, prefmin2[x][y]);
            b = max(b, prefmax2[x][y]);
        }
        res = min(res, max(v[v.size()-1][0] - v[i][0], b-a));
    }
    a = inf, b = -inf;
    for(int i = 0; i < v.size();i++){
        if(i > 0){
            int x = v[i-1][1], y = v[i-1][2];
            a = min(a, prefmin3[x][y]);
            b = max(b, prefmax3[x][y]);
        }
        res = min(res, max(v[v.size()-1][0] - v[i][0], b-a));
    }
    cout << res << endl;
}

Compilation message

joioi.cpp: In function 'int32_t main()':
joioi.cpp:129:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |     for(int i = 0; i < v.size();i++){
      |                    ~~^~~~~~~~~~
joioi.cpp:138:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |     for(int i = 0; i < v.size();i++){
      |                    ~~^~~~~~~~~~
joioi.cpp:147:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |     for(int i = 0; i < v.size();i++){
      |                    ~~^~~~~~~~~~
joioi.cpp:156:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  156 |     for(int i = 0; i < v.size();i++){
      |                    ~~^~~~~~~~~~
joioi.cpp: In function 'void init()':
joioi.cpp:6:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 | freopen("input.txt", "r", stdin);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:8:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 | freopen("output.txt", "w", stdout);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 0 ms 596 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 0 ms 596 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 0 ms 724 KB Output is correct
7 Correct 0 ms 724 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 724 KB Output is correct
10 Correct 0 ms 724 KB Output is correct
11 Correct 0 ms 724 KB Output is correct
12 Correct 0 ms 724 KB Output is correct
13 Correct 0 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 0 ms 596 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 0 ms 596 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 0 ms 724 KB Output is correct
7 Correct 0 ms 724 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 724 KB Output is correct
10 Correct 0 ms 724 KB Output is correct
11 Correct 0 ms 724 KB Output is correct
12 Correct 0 ms 724 KB Output is correct
13 Correct 0 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 980 KB Output is correct
16 Correct 14 ms 10888 KB Output is correct
17 Correct 14 ms 11144 KB Output is correct
18 Correct 14 ms 11124 KB Output is correct
19 Correct 14 ms 11132 KB Output is correct
20 Correct 13 ms 9904 KB Output is correct
21 Correct 15 ms 11148 KB Output is correct
22 Correct 15 ms 11044 KB Output is correct
23 Correct 15 ms 11140 KB Output is correct
24 Correct 15 ms 9904 KB Output is correct
25 Correct 14 ms 11016 KB Output is correct
26 Correct 15 ms 11080 KB Output is correct
27 Correct 15 ms 11016 KB Output is correct
28 Correct 16 ms 11136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 0 ms 596 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 0 ms 596 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 0 ms 724 KB Output is correct
7 Correct 0 ms 724 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 724 KB Output is correct
10 Correct 0 ms 724 KB Output is correct
11 Correct 0 ms 724 KB Output is correct
12 Correct 0 ms 724 KB Output is correct
13 Correct 0 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 980 KB Output is correct
16 Correct 14 ms 10888 KB Output is correct
17 Correct 14 ms 11144 KB Output is correct
18 Correct 14 ms 11124 KB Output is correct
19 Correct 14 ms 11132 KB Output is correct
20 Correct 13 ms 9904 KB Output is correct
21 Correct 15 ms 11148 KB Output is correct
22 Correct 15 ms 11044 KB Output is correct
23 Correct 15 ms 11140 KB Output is correct
24 Correct 15 ms 9904 KB Output is correct
25 Correct 14 ms 11016 KB Output is correct
26 Correct 15 ms 11080 KB Output is correct
27 Correct 15 ms 11016 KB Output is correct
28 Correct 16 ms 11136 KB Output is correct
29 Runtime error 502 ms 262144 KB Execution killed with signal 9
30 Halted 0 ms 0 KB -