답안 #224715

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
224715 2020-04-18T16:09:57 Z Ruxandra985 The Kingdom of JOIOI (JOI17_joioi) C++14
60 / 100
4000 ms 81324 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)
        memset (where[i] , 0 , sizeof(where[i]));

    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);

        if (maxij == maxi)
            return;

        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:131: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:138: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]);
             ~~~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 512 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 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 512 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 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 10 ms 2816 KB Output is correct
17 Correct 30 ms 3836 KB Output is correct
18 Correct 31 ms 3836 KB Output is correct
19 Correct 31 ms 3836 KB Output is correct
20 Correct 29 ms 3452 KB Output is correct
21 Correct 34 ms 3836 KB Output is correct
22 Correct 24 ms 3708 KB Output is correct
23 Correct 34 ms 3836 KB Output is correct
24 Correct 31 ms 3452 KB Output is correct
25 Correct 30 ms 3836 KB Output is correct
26 Correct 28 ms 3836 KB Output is correct
27 Correct 28 ms 3836 KB Output is correct
28 Correct 30 ms 3836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 512 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 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 10 ms 2816 KB Output is correct
17 Correct 30 ms 3836 KB Output is correct
18 Correct 31 ms 3836 KB Output is correct
19 Correct 31 ms 3836 KB Output is correct
20 Correct 29 ms 3452 KB Output is correct
21 Correct 34 ms 3836 KB Output is correct
22 Correct 24 ms 3708 KB Output is correct
23 Correct 34 ms 3836 KB Output is correct
24 Correct 31 ms 3452 KB Output is correct
25 Correct 30 ms 3836 KB Output is correct
26 Correct 28 ms 3836 KB Output is correct
27 Correct 28 ms 3836 KB Output is correct
28 Correct 30 ms 3836 KB Output is correct
29 Correct 1154 ms 79660 KB Output is correct
30 Correct 1225 ms 81196 KB Output is correct
31 Correct 1300 ms 81196 KB Output is correct
32 Correct 1207 ms 81236 KB Output is correct
33 Correct 1180 ms 77356 KB Output is correct
34 Correct 1285 ms 81324 KB Output is correct
35 Correct 3167 ms 81324 KB Output is correct
36 Correct 3466 ms 81044 KB Output is correct
37 Execution timed out 4050 ms 81324 KB Time limit exceeded