답안 #201421

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
201421 2020-02-10T13:43:23 Z quocnguyen1012 The Kingdom of JOIOI (JOI17_joioi) C++14
100 / 100
724 ms 55008 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

const int maxn = 2005;

int a[maxn][maxn], N, M;
int mi = 1e9, ma = 0;

bool check(int val){
    int L = 1 ,  R = M;
    bool ok = 1  , ok1 = 1;
    for(int i = 1 ; i <= N ; ++i){
        int l = M , r = 1;
        for( ; l >= 1 ; --l){
            if(a[i][l] - mi > val)break;
        }
        for( ; r <= M ; ++r){
            if(ma - a[i][r] > val)break;
        }
        --r;++l;
        R = min(R , r);
        if(R < l - 1)ok = 0;
        L = max(L , l);
        if(r + 1 < L)ok1 = 0;
        if(!ok && !ok1)break;
//        cout << l << " " << r << endl;
    }
//    cout << endl;
    if(ok || ok1)return 1;
    ok = 1 , ok1 = 1;
    L = 1;R = M;
    for(int i = 1 ; i <= N ; ++i){
        int l = M , r = 1;
        for( ; l >= 1 ; --l){
            if(ma - a[i][l] > val)break;
        }
        for( ; r <= M ; ++r){
            if(a[i][r] - mi > val)break;
        }
        --r;++l;
        R = min(R , r);
        if(R < l - 1)ok = 0;
        L = max(L , l);
        if(r + 1 < L)ok1 = 0;
        if(!ok && !ok1)break;
//        cout << l << " " << r << endl;
    }
    if(ok || ok1)return 1;
    return 0;
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  if (fopen("A.INP", "r")){
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  cin >> N >> M;
  for (int i = 1; i <= N; ++i){
    for (int j = 1; j <= M; ++j){
      cin >> a[i][j];
      mi = min(mi, a[i][j]);
      ma = max(ma, a[i][j]);
    }
  }
  int l = 0, r = ma - mi, mid;
  while (l <= r){
    mid = (l + r) / 2;
    if (!check(mid)) l = mid + 1;
    else r = mid - 1;
  }
  cout << min(l, ma - mi);
}

Compilation message

joioi.cpp: In function 'int main()':
joioi.cpp:63:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.INP", "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
joioi.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.OUT", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 8 ms 1400 KB Output is correct
17 Correct 11 ms 1528 KB Output is correct
18 Correct 11 ms 1528 KB Output is correct
19 Correct 11 ms 1656 KB Output is correct
20 Correct 10 ms 1400 KB Output is correct
21 Correct 13 ms 1656 KB Output is correct
22 Correct 12 ms 1656 KB Output is correct
23 Correct 13 ms 1660 KB Output is correct
24 Correct 11 ms 1528 KB Output is correct
25 Correct 12 ms 1656 KB Output is correct
26 Correct 13 ms 1656 KB Output is correct
27 Correct 24 ms 1656 KB Output is correct
28 Correct 13 ms 1656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 5 ms 376 KB Output is correct
12 Correct 5 ms 504 KB Output is correct
13 Correct 5 ms 376 KB Output is correct
14 Correct 5 ms 376 KB Output is correct
15 Correct 5 ms 376 KB Output is correct
16 Correct 8 ms 1400 KB Output is correct
17 Correct 11 ms 1528 KB Output is correct
18 Correct 11 ms 1528 KB Output is correct
19 Correct 11 ms 1656 KB Output is correct
20 Correct 10 ms 1400 KB Output is correct
21 Correct 13 ms 1656 KB Output is correct
22 Correct 12 ms 1656 KB Output is correct
23 Correct 13 ms 1660 KB Output is correct
24 Correct 11 ms 1528 KB Output is correct
25 Correct 12 ms 1656 KB Output is correct
26 Correct 13 ms 1656 KB Output is correct
27 Correct 24 ms 1656 KB Output is correct
28 Correct 13 ms 1656 KB Output is correct
29 Correct 482 ms 37324 KB Output is correct
30 Correct 484 ms 37496 KB Output is correct
31 Correct 522 ms 39160 KB Output is correct
32 Correct 510 ms 39292 KB Output is correct
33 Correct 427 ms 34168 KB Output is correct
34 Correct 506 ms 39160 KB Output is correct
35 Correct 687 ms 55008 KB Output is correct
36 Correct 622 ms 25848 KB Output is correct
37 Correct 724 ms 54880 KB Output is correct