Submission #224707

# Submission time Handle Problem Language Result Execution time Memory
224707 2020-04-18T16:03:44 Z Ruxandra985 The Kingdom of JOIOI (JOI17_joioi) C++14
60 / 100
4000 ms 81356 KB
#include <bits/stdc++.h>
#define DIMN 2010
using namespace std;

int mini , maxi , n , m , sol;
int a[DIMN][DIMN] , where[DIMN][DIMN] , h[DIMN];
priority_queue <pair <int , pair <int,int> > > up , down;


void solve(){
    int i , j , maxis , minis , maxij , minij , val , ic , jc;

    /// jos includ mini, sus includ maxi

    for (i = 1 ; i <= n ; i++){
        for (j = 1 ; j <= m ; j++)
            where[i][j] = 0;
    }

    for (j = 1 ; j <= m ; j++){

        for (i = 1 ; i <= n ; i++){
            if (a[i][j] == mini)
                break;
        }

        h[j] = max(n - i + 1 , h[j - 1]); /// fie iau exact cat trb fie mai mult


        for (i = 1 ; i <= h[j] ; i++)
            where[n - i + 1][j] = 1; /// apartine tarii de jos

    }

    minij = minis = 2000000000;
    maxij = maxis = -2000000000;

    /// e clar ca toate mini urile sunt jos

    for (i = 1 ; i <= n ; i++){
        for (j = 1 ; j <= m ; j++){
            if (where[i][j] == 0){
                minis = min (minis , a[i][j]);
                maxis = max (maxis , a[i][j]);
            }
            else {
                minij = min (minij , a[i][j]);
                maxij = max (maxij , a[i][j]);
            }
        }
    }

    /// daca exista vreun maxi jos, nu e ok

    if (maxij == maxi)
        return;

    /// altfel, toate mini urile sunt jos, toate maxi urile sunt sus

    while (!down.empty())
        down.pop();
    while (!up.empty())
        up.pop();

    for (j = 1 ; j <= m ; j++){
        if (j == m || h[j] < h[j + 1])
            down.push( make_pair( -a[n - h[j]][j] , make_pair(n - h[j] , j)) );
    }

    /// in down pui candidatii


    for (i = 1 ; i <= n ; i++){
        for (j = 1 ; j <= m ; j++){
            if (where[i][j] == 0){
                up.push( make_pair( -a[i][j] , make_pair(i , j)) );
            }
        }
    }

    while (!down.empty()){

        val = -down.top().first;
        ic = down.top().second.first;
        jc = down.top().second.second;

        down.pop();

        /// val = cea mai mica val cu care pot sa extind

        maxij = max(maxij , val);

        where[ic][jc] = 1;

        h[jc]++;

        if (h[jc - 1] + 1 == h[jc] && jc - 1 != 0 && n - h[jc - 1] > 0)
            down.push( make_pair( -a[n - h[jc - 1]][jc - 1] , make_pair(n - h[jc - 1] , jc - 1)) );

        if ((jc == m || h[jc] != h[jc + 1]) && n - h[jc] > 0)
            down.push( make_pair( -a[n - h[jc]][jc] , make_pair(n - h[jc] , jc)) );

        val = -up.top().first;
        minis = val;
        ic = up.top().second.first;
        jc = up.top().second.second;

        while (where[ic][jc]){
            up.pop();
            if (up.empty())
                break;
            val = -up.top().first;
            ic = up.top().second.first;
            jc = up.top().second.second;
            minis = val;
        }
        if (!up.empty())
            sol = min(sol , max(maxis - minis , maxij - minij));
        else break;
    }


}

int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int i , j , st , dr;
    fscanf (fin,"%d%d",&n,&m);

    mini = 2000000000;
    maxi = -2000000000;

    for (i = 1 ; i <= n ; i++){
        for (j = 1 ; j <= m ; j++){
            fscanf (fin,"%d",&a[i][j]);
            mini = min(mini , a[i][j]);
            maxi = max(maxi , a[i][j]);
        }
    }

    sol = maxi - mini;

    solve();

    for (i = 1 ; i <= n ; i++){
        st = 1;
        dr = m;
        while (st < dr){
            swap(a[i][st] , a[i][dr]);
            st++;
            dr--;
        }
    }

    solve();

    for (j = 1 ; j <= m ; j++){
        st = 1;
        dr = n;
        while (st < dr){
            swap(a[st][j] , a[dr][j]);
            st++;
            dr--;
        }
    }

    solve();

    for (i = 1 ; i <= n ; i++){
        st = 1;
        dr = m;
        while (st < dr){
            swap(a[i][st] , a[i][dr]);
            st++;
            dr--;
        }
    }

    solve();

    fprintf (fout,"%d",sol);

    return 0;
}

Compilation message

joioi.cpp: In function 'int main()':
joioi.cpp:130:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf (fin,"%d%d",&n,&m);
     ~~~~~~~^~~~~~~~~~~~~~~~~~
joioi.cpp:137:20: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             fscanf (fin,"%d",&a[i][j]);
             ~~~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 10 ms 2304 KB Output is correct
17 Correct 40 ms 3452 KB Output is correct
18 Correct 35 ms 3452 KB Output is correct
19 Correct 36 ms 3452 KB Output is correct
20 Correct 33 ms 3196 KB Output is correct
21 Correct 39 ms 3580 KB Output is correct
22 Correct 27 ms 3580 KB Output is correct
23 Correct 38 ms 3580 KB Output is correct
24 Correct 38 ms 3324 KB Output is correct
25 Correct 35 ms 3580 KB Output is correct
26 Correct 32 ms 3580 KB Output is correct
27 Correct 34 ms 3580 KB Output is correct
28 Correct 36 ms 3580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 10 ms 2304 KB Output is correct
17 Correct 40 ms 3452 KB Output is correct
18 Correct 35 ms 3452 KB Output is correct
19 Correct 36 ms 3452 KB Output is correct
20 Correct 33 ms 3196 KB Output is correct
21 Correct 39 ms 3580 KB Output is correct
22 Correct 27 ms 3580 KB Output is correct
23 Correct 38 ms 3580 KB Output is correct
24 Correct 38 ms 3324 KB Output is correct
25 Correct 35 ms 3580 KB Output is correct
26 Correct 32 ms 3580 KB Output is correct
27 Correct 34 ms 3580 KB Output is correct
28 Correct 36 ms 3580 KB Output is correct
29 Correct 2037 ms 79900 KB Output is correct
30 Correct 2175 ms 81348 KB Output is correct
31 Correct 2189 ms 81324 KB Output is correct
32 Correct 2476 ms 81328 KB Output is correct
33 Correct 1910 ms 77384 KB Output is correct
34 Correct 2337 ms 81272 KB Output is correct
35 Execution timed out 4080 ms 81356 KB Time limit exceeded
36 Halted 0 ms 0 KB -