# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
42091 | 2018-02-22T11:32:58 Z | chonka | The Kingdom of JOIOI (JOI17_joioi) | C++ | 1435 ms | 262144 KB |
#include<iostream> #include<stdio.h> using namespace std ; #define MAXN 2007 int n , m ; int a[ MAXN ][ MAXN ] ; int mxval ; int mnval ; int ans ; int st[ MAXN ] ; int en[ MAXN ] ; bool f ( int x ) { int i , j ; for ( i = 1 ; i <= n ; i ++ ) { st[ i ] = 0 ; for ( j = 1 ; j <= m ; j ++ ) { if ( a[ i ][ j ] < mxval - x ) { break ; } st[ i ] = j ; } en[ i ] = m + 1 ; for ( j = m ; j >= 1 ; j -- ) { if ( a[ i ][ j ] > mnval + x ) { break ; } en[ i ] = j ; } } for ( i = n - 1 ; i >= 1 ; i -- ) { st[ i ] = min ( st[ i ] , st[ i + 1 ] ) ; } for ( i = 2 ; i <= n ; i ++ ) { en[ i ] = max ( en[ i - 1 ] , en[ i ] ) ; } for ( i = 1 ; i <= n ; i ++ ) { if ( st[ i ] < en[ i ] - 1 ) { return false ; } } return true ; } int getans ( ) { int l , r , mid ; l = 0 ; r = mxval - mnval ; while ( r - l > 3 ) { mid = ( l + r ) / 2 ; if ( f ( mid ) == true ) { r = mid ; } else { l = mid ; } } while ( f ( l ) == false ) { l ++ ; } return l ; } void flip_rows ( ) { int i , j ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= ( m / 2 ) ; j ++ ) { swap ( a[ i ][ j ] , a[ i ][ m - j + 1 ] ) ; } } } void flip_cols ( ) { int i , j ; for ( j = 1 ; j <= m ; j ++ ) { for ( i = 1 ; i <= ( n / 2 ) ; i ++ ) { swap ( a[ i ][ j ] , a[ n - i + 1 ][ j ] ) ; } } } void input ( ) { scanf ( "%d%d" , &n , &m ) ; int i , j ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { scanf ( "%d" , &a[ i ][ j ] ) ; if ( i == 1 && j == 1 ) { mnval = mxval = a[ i ][ j ] ; } if ( mxval < a[ i ][ j ] ) { mxval = a[ i ][ j ] ; } if ( mnval > a[ i ][ j ] ) { mnval = a[ i ][ j ] ; } } } } void solve ( ) { ans = mxval - mnval ; ans = min ( ans , getans ( ) ) ; flip_rows ( ) ; ans = min ( ans , getans ( ) ) ; flip_cols ( ) ; ans = min ( ans , getans ( ) ) ; flip_rows ( ) ; ans = min ( ans , getans ( ) ) ; printf ( "%d\n" , ans ) ; } int main ( ) { input ( ) ; solve ( ) ; return 0 ; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 484 KB | Output is correct |
3 | Correct | 2 ms | 592 KB | Output is correct |
4 | Correct | 2 ms | 592 KB | Output is correct |
5 | Correct | 2 ms | 704 KB | Output is correct |
6 | Correct | 2 ms | 704 KB | Output is correct |
7 | Correct | 2 ms | 704 KB | Output is correct |
8 | Correct | 2 ms | 784 KB | Output is correct |
9 | Correct | 2 ms | 784 KB | Output is correct |
10 | Correct | 2 ms | 784 KB | Output is correct |
11 | Correct | 2 ms | 784 KB | Output is correct |
12 | Correct | 2 ms | 784 KB | Output is correct |
13 | Correct | 2 ms | 784 KB | Output is correct |
14 | Correct | 2 ms | 784 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 484 KB | Output is correct |
3 | Correct | 2 ms | 592 KB | Output is correct |
4 | Correct | 2 ms | 592 KB | Output is correct |
5 | Correct | 2 ms | 704 KB | Output is correct |
6 | Correct | 2 ms | 704 KB | Output is correct |
7 | Correct | 2 ms | 704 KB | Output is correct |
8 | Correct | 2 ms | 784 KB | Output is correct |
9 | Correct | 2 ms | 784 KB | Output is correct |
10 | Correct | 2 ms | 784 KB | Output is correct |
11 | Correct | 2 ms | 784 KB | Output is correct |
12 | Correct | 2 ms | 784 KB | Output is correct |
13 | Correct | 2 ms | 784 KB | Output is correct |
14 | Correct | 2 ms | 784 KB | Output is correct |
15 | Correct | 2 ms | 784 KB | Output is correct |
16 | Correct | 7 ms | 1800 KB | Output is correct |
17 | Correct | 14 ms | 2020 KB | Output is correct |
18 | Correct | 13 ms | 2328 KB | Output is correct |
19 | Correct | 13 ms | 2596 KB | Output is correct |
20 | Correct | 19 ms | 2720 KB | Output is correct |
21 | Correct | 27 ms | 3268 KB | Output is correct |
22 | Correct | 17 ms | 3704 KB | Output is correct |
23 | Correct | 18 ms | 4008 KB | Output is correct |
24 | Correct | 16 ms | 4264 KB | Output is correct |
25 | Correct | 17 ms | 4756 KB | Output is correct |
26 | Correct | 17 ms | 5144 KB | Output is correct |
27 | Correct | 17 ms | 5532 KB | Output is correct |
28 | Correct | 17 ms | 5972 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 484 KB | Output is correct |
3 | Correct | 2 ms | 592 KB | Output is correct |
4 | Correct | 2 ms | 592 KB | Output is correct |
5 | Correct | 2 ms | 704 KB | Output is correct |
6 | Correct | 2 ms | 704 KB | Output is correct |
7 | Correct | 2 ms | 704 KB | Output is correct |
8 | Correct | 2 ms | 784 KB | Output is correct |
9 | Correct | 2 ms | 784 KB | Output is correct |
10 | Correct | 2 ms | 784 KB | Output is correct |
11 | Correct | 2 ms | 784 KB | Output is correct |
12 | Correct | 2 ms | 784 KB | Output is correct |
13 | Correct | 2 ms | 784 KB | Output is correct |
14 | Correct | 2 ms | 784 KB | Output is correct |
15 | Correct | 2 ms | 784 KB | Output is correct |
16 | Correct | 7 ms | 1800 KB | Output is correct |
17 | Correct | 14 ms | 2020 KB | Output is correct |
18 | Correct | 13 ms | 2328 KB | Output is correct |
19 | Correct | 13 ms | 2596 KB | Output is correct |
20 | Correct | 19 ms | 2720 KB | Output is correct |
21 | Correct | 27 ms | 3268 KB | Output is correct |
22 | Correct | 17 ms | 3704 KB | Output is correct |
23 | Correct | 18 ms | 4008 KB | Output is correct |
24 | Correct | 16 ms | 4264 KB | Output is correct |
25 | Correct | 17 ms | 4756 KB | Output is correct |
26 | Correct | 17 ms | 5144 KB | Output is correct |
27 | Correct | 17 ms | 5532 KB | Output is correct |
28 | Correct | 17 ms | 5972 KB | Output is correct |
29 | Correct | 970 ms | 42192 KB | Output is correct |
30 | Correct | 949 ms | 64368 KB | Output is correct |
31 | Correct | 944 ms | 87540 KB | Output is correct |
32 | Correct | 929 ms | 110712 KB | Output is correct |
33 | Correct | 807 ms | 128776 KB | Output is correct |
34 | Correct | 950 ms | 153856 KB | Output is correct |
35 | Correct | 1435 ms | 192628 KB | Output is correct |
36 | Correct | 1286 ms | 226344 KB | Output is correct |
37 | Correct | 1415 ms | 262144 KB | Output is correct |