Submission #1199628

#TimeUsernameProblemLanguageResultExecution timeMemory
1199628hyl_backupThe Kingdom of JOIOI (JOI17_joioi)C++20
15 / 100
4098 ms197996 KiB
//The Kingdom of JOIOI
#include <iostream>
#include <set>
#include <climits>
#include <vector>

#define MIN(a, b) ((a<b)? a :b )
#define MAX(a, b) ((a>b)? a :b )

long long h, w, min, max, col, a, b, min_a, min_b, max_a, max_b;
long long limit, ans;

long long mat[2007][2007];
int loga[2007] = {};
int expbin[12] = {};

std::set<std::pair<long long, long long>> set;

void b_log(){
    long long bin=2;
    int cont = 1;
    expbin[0]=0;
    expbin[1]=1;
    for(int i = 1; i<2005; ++i){
        if(i>=bin){
            bin*=2;
            ++cont;
            expbin[cont]=i;
        }
        loga[i]=cont;
        //std::cout << loga[i] <<'\n';
    }
    bin = 1;
    for(int i = 0; i<11; ++i){

    }
}

struct sparse{
    std::vector<int> min;
    std::vector<int> max;
};

sparse table[2007][2007];

void b_sparse(){
    for(int i = h-1; i>=0; --i){
        table[col][i].min.push_back(mat[i][col]);
        table[col][i].max.push_back(mat[i][col]);
        for(int j = 1; j+i<h; j*=2){
            //std::cout << '-'<< i+j-j/2 <<' '<<j/2 <<':'<<loga[j/2]<< '_';
            table[col][i].min.push_back(MIN(table[col][i].min[loga[j/2]], table[col][i+j-j/2].min[loga[j/2]]));
            table[col][i].max.push_back(MAX(table[col][i].max[loga[j/2]], table[col][i+j-j/2].max[loga[j/2]]));
        }
        /*
        for(int x : table[col][i].min){
            std::cout << x <<' ';
        }
        std::cout << '\n';
        for(int x : table[col][i].max){
            std::cout << x <<' ';
        }
        std::cout << '\n';
       // */
    }
}

int min_tree(){
    int range=b-a;
    //std::cout <<b-expbin[loga[range]] <<" :2nd pos\n";
    return MIN(table[col][a].min[loga[range]], table[col][b-expbin[loga[range]]].min[loga[range]]);

}
int max_tree(){
    int range=b-a;
    return MAX(table[col][a].max[loga[range]], table[col][b-expbin[loga[range]]].max[loga[range]]);
}


std::set<std::vector<int>> dp;
std::set<std::vector<int>> dp2;

int main(){
    //std::cin.tie(0);
    //std::ios_base::sync_with_stdio(0);

    b_log();

    std::cin >> h >> w;

    for(int i = 0; i<h; ++i){
        for(int j = 0; j<w; ++j){
            std::cin >> mat[i][j];
            //og.insert(mat[i][j]);
        }
    }

    min = LLONG_MAX;

    for(int j = 0; j<w; ++j){
        col=j;
        b_sparse();
    }

    col = 0;

    a = 3;
    b = 4;
    //std::cout << min_tree() << ' ' << max_tree()<<'\n';

    //return 0;

    ans = LLONG_MAX;

    //ms_b=og;

    dp.insert({h-1, 2000123000, 0, 2000123000, 0});


    for(int j = 0; j<=w; ++j){
        for(std::vector<int> x : dp){
            //limit, min, max, min, max
            /*
            std::cout << j<<"pos ";
            for(int c : x){
                std::cout << c <<' ';
            }
            std::cout <<'\n';
            //*/

            limit=x[0];

            if(j==w){
                ans = MIN(ans, MAX(x[2]-x[1], x[4]-x[3]));
                continue;
            }

            col= j;
            a=0;
            b=h-1;
            dp2.insert({-1, x[1], x[2], MIN(x[3], min_tree()), MAX(x[4], max_tree())});

            min=x[1];
            max=x[2];

            for(int i = 0; i<=limit; ++i){
                min = MIN(min, mat[i][j]);
                max = MAX(max, mat[i][j]);


                a=i+1;
                //std::cout << a << ':'<< b << ' ' << i << '\n';

                if(a<h){
                    dp2.insert({i, min, max, MIN(x[3], min_tree()), MAX(x[4], max_tree())});
                }
                else{
                    dp2.insert({i, min, max, x[3], x[4]});
                }
            }

        }
        dp.clear();
        dp=dp2;
        dp2.clear();
    }

    //return 0 ;
    //std::cout << "prelim: " << ans <<'\n';

    dp.clear();

    dp.insert({-1, 2000123000, 0, 2000123000, 0});

    for(int j = 0; j<=w; ++j){
        for(std::vector<int> x : dp){
            //limit, min, max, min, max
            /*
            std::cout << j<<"pos ";
            for(int c : x){
                std::cout << c <<' ';
            }
            std::cout <<'\n';
            //*/

            limit=x[0];

            if(j==w){
                ans = MIN(ans, MAX(x[2]-x[1], x[4]-x[3]));
                continue;
            }

            col= j;
            a=0;
            b=h-1;
            //dp2.insert({-1, x[1], x[2], MIN(x[3], min_tree()), MAX(x[4], max_tree())});

            min=x[1];
            max=x[2];

            for(int i = 0; i<limit; ++i){
                min = MIN(min, mat[i][j]);
                max = MAX(max, mat[i][j]);
            }

            for(int i = limit; i<h; ++i){
                if(i>=0){
                    min = MIN(min, mat[i][j]);
                    max = MAX(max, mat[i][j]);
                }

                //std::cout <<min <<':' << max <<'\n';

                a=i+1;
                if(a<h){
                    dp2.insert({i, min, max, MIN(x[3], min_tree()), MAX(x[4], max_tree())});
                }
                else{
                    dp2.insert({i, min, max, x[3], x[4]});
                }
            }

        }
        dp.clear();
        dp=dp2;
        dp2.clear();
    }

    std::cout << ans;

    return 0;
}


Compilation message (stderr)

joioi.cpp: In function 'int main()':
joioi.cpp:117:17: warning: narrowing conversion of '(h - 1)' from 'long long int' to 'int' [-Wnarrowing]
  117 |     dp.insert({h-1, 2000123000, 0, 2000123000, 0});
      |                ~^~
joioi.cpp:117:17: warning: narrowing conversion of '(h - 1)' from 'long long int' to 'int' [-Wnarrowing]
joioi.cpp:155:36: warning: narrowing conversion of 'min' from 'long long int' to 'int' [-Wnarrowing]
  155 |                     dp2.insert({i, min, max, MIN(x[3], min_tree()), MAX(x[4], max_tree())});
      |                                    ^~~
joioi.cpp:155:36: warning: narrowing conversion of 'min' from 'long long int' to 'int' [-Wnarrowing]
joioi.cpp:155:41: warning: narrowing conversion of 'max' from 'long long int' to 'int' [-Wnarrowing]
  155 |                     dp2.insert({i, min, max, MIN(x[3], min_tree()), MAX(x[4], max_tree())});
      |                                         ^~~
joioi.cpp:155:41: warning: narrowing conversion of 'max' from 'long long int' to 'int' [-Wnarrowing]
joioi.cpp:158:36: warning: narrowing conversion of 'min' from 'long long int' to 'int' [-Wnarrowing]
  158 |                     dp2.insert({i, min, max, x[3], x[4]});
      |                                    ^~~
joioi.cpp:158:36: warning: narrowing conversion of 'min' from 'long long int' to 'int' [-Wnarrowing]
joioi.cpp:158:41: warning: narrowing conversion of 'max' from 'long long int' to 'int' [-Wnarrowing]
  158 |                     dp2.insert({i, min, max, x[3], x[4]});
      |                                         ^~~
joioi.cpp:158:41: warning: narrowing conversion of 'max' from 'long long int' to 'int' [-Wnarrowing]
joioi.cpp:216:36: warning: narrowing conversion of 'min' from 'long long int' to 'int' [-Wnarrowing]
  216 |                     dp2.insert({i, min, max, MIN(x[3], min_tree()), MAX(x[4], max_tree())});
      |                                    ^~~
joioi.cpp:216:36: warning: narrowing conversion of 'min' from 'long long int' to 'int' [-Wnarrowing]
joioi.cpp:216:41: warning: narrowing conversion of 'max' from 'long long int' to 'int' [-Wnarrowing]
  216 |                     dp2.insert({i, min, max, MIN(x[3], min_tree()), MAX(x[4], max_tree())});
      |                                         ^~~
joioi.cpp:216:41: warning: narrowing conversion of 'max' from 'long long int' to 'int' [-Wnarrowing]
joioi.cpp:219:36: warning: narrowing conversion of 'min' from 'long long int' to 'int' [-Wnarrowing]
  219 |                     dp2.insert({i, min, max, x[3], x[4]});
      |                                    ^~~
joioi.cpp:219:36: warning: narrowing conversion of 'min' from 'long long int' to 'int' [-Wnarrowing]
joioi.cpp:219:41: warning: narrowing conversion of 'max' from 'long long int' to 'int' [-Wnarrowing]
  219 |                     dp2.insert({i, min, max, x[3], x[4]});
      |                                         ^~~
joioi.cpp:219:41: warning: narrowing conversion of 'max' from 'long long int' to 'int' [-Wnarrowing]
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...