Submission #146127

# Submission time Handle Problem Language Result Execution time Memory
146127 2019-08-22T09:46:47 Z alextodoran The Kingdom of JOIOI (JOI17_joioi) C++14
60 / 100
4000 ms 110276 KB
#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9+1;

const int NM_MAX = 2002;

int n, m;

int ma[NM_MAX][NM_MAX];

int minsub1[NM_MAX][NM_MAX];
int minsub2[NM_MAX][NM_MAX];

int maxc1[NM_MAX][NM_MAX];
int minc1[NM_MAX][NM_MAX];
int maxc2[NM_MAX][NM_MAX];
int minc2[NM_MAX][NM_MAX];

int ans = INF;

void f1 (int mi, int mx, int &a, int &b)
{
    int mia, mxa, mib, mxb;
    mia = mib = INF;
    mxa = mxb = -INF;
    int last = n;
    for(int j = 1; j <= m; j++)
    {
        int l = 0, r = last;
        while(l < r)
        {
            int mid = (l + r + 1) / 2;
            if(minc1[mid][j] < mi || maxc1[mid][j] > mx)
                r = mid - 1;
            else
                l = mid;
        }
        last = l;
        mia = min(mia, minc1[l][j]);
        mib = min(mib, minc2[l + 1][j]);
        mxa = max(mxa, maxc1[l][j]);
        mxb = max(mxb, maxc2[l + 1][j]);
    }
    if(mia == INF)
        mia = 0;
    if(mib == INF)
        mib = 0;
    if(mxa == INF)
        mxa = 0;
    if(mxb == INF)
        mxb = 0;
    a = mxa - mia;
    b = mxb - mib;
}

void f2 (int mi, int mx, int &a, int &b)
{
    int mia, mxa, mib, mxb;
    mia = mib = INF;
    mxa = mxb = -INF;
    int last = n;
    for(int j = m; j >= 1; j--)
    {
        int l = 0, r = last;
        while(l < r)
        {
            int mid = (l + r + 1) / 2;
            if(minc1[mid][j] < mi || maxc1[mid][j] > mx)
                r = mid - 1;
            else
                l = mid;
        }
        last = l;
        mia = min(mia, minc1[l][j]);
        mib = min(mib, minc2[l + 1][j]);
        mxa = max(mxa, maxc1[l][j]);
        mxb = max(mxb, maxc2[l + 1][j]);
    }
    if(mia == INF)
        mia = 0;
    if(mib == INF)
        mib = 0;
    if(mxa == INF)
        mxa = 0;
    if(mxb == INF)
        mxb = 0;
    a = mxa - mia;
    b = mxb - mib;
}

void solve (int val)
{
    int l = 0, r = ans - 1;
    int a, b;
    while(l < r)
    {
        int mid = (l + r + 1) / 2;
        f1(val, val + mid, a, b);
        if(a > b)
            r = mid - 1;
        else
            l = mid;
    }
    f1(val, val + l, a, b);
    ans = min(ans, max(a, b));
    f1(val, val + l + 1, a, b);
    ans = min(ans, max(a, b));
    l = 0; r = ans - 1;
    while(l < r)
    {
        int mid = (l + r + 1) / 2;
        f2(val, val + mid, a, b);
        if(a > b)
            r = mid - 1;
        else
            l = mid;
    }
    f2(val, val + l, a, b);
    ans = min(ans, max(a, b));
    f2(val, val + l + 1, a, b);
    ans = min(ans, max(a, b));
}

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin >> ma[i][j];
    for(int i = 0; i <= n + 1; i++)
        for(int j = 0; j <= m + 1; j++)
            minsub1[i][j] = minsub2[i][j] = INF;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            minsub1[i][j] = min(ma[i][j], min(minsub1[i][j - 1], minsub1[i - 1][j]));
    for(int i = 1; i <= n; i++)
        for(int j = m; j >= 1; j--)
            minsub2[i][j] = min(ma[i][j], min(minsub2[i][j + 1], minsub2[i - 1][j]));
    for(int j = 1; j <= m; j++)
    {
        maxc1[0][j] = -INF;
        for(int i = 1; i <= n; i++)
            maxc1[i][j] = max(ma[i][j], maxc1[i - 1][j]);
        maxc2[n + 1][j] = -INF;
        for(int i = n; i >= 1; i--)
            maxc2[i][j] = max(ma[i][j], maxc2[i + 1][j]);
    }
    for(int j = 1; j <= m; j++)
    {
        minc1[0][j] = INF;
        for(int i = 1; i <= n; i++)
            minc1[i][j] = min(ma[i][j], minc1[i - 1][j]);
        minc2[n + 1][j] = INF;
        for(int i = n; i >= 1; i--)
            minc2[i][j] = min(ma[i][j], minc2[i + 1][j]);
    }
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(ma[i][j] < minc1[i - 1][j] && ((ma[i][j] < minsub1[i - 1][j] && ma[i][j] < minsub1[i][j - 1]) ||
                                              (ma[i][j] < minsub2[i - 1][j] && ma[i][j] < minsub2[i][j + 1])))
                solve(ma[i][j]);
    cout << ans << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 636 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 636 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 3 ms 632 KB Output is correct
11 Correct 2 ms 632 KB Output is correct
12 Correct 2 ms 632 KB Output is correct
13 Correct 2 ms 632 KB Output is correct
14 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 636 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 636 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 3 ms 632 KB Output is correct
11 Correct 2 ms 632 KB Output is correct
12 Correct 2 ms 632 KB Output is correct
13 Correct 2 ms 632 KB Output is correct
14 Correct 2 ms 632 KB Output is correct
15 Correct 3 ms 760 KB Output is correct
16 Correct 19 ms 7032 KB Output is correct
17 Correct 33 ms 7116 KB Output is correct
18 Correct 34 ms 7132 KB Output is correct
19 Correct 41 ms 7160 KB Output is correct
20 Correct 34 ms 6300 KB Output is correct
21 Correct 45 ms 7160 KB Output is correct
22 Correct 53 ms 7072 KB Output is correct
23 Correct 41 ms 7068 KB Output is correct
24 Correct 38 ms 6264 KB Output is correct
25 Correct 41 ms 7132 KB Output is correct
26 Correct 46 ms 7032 KB Output is correct
27 Correct 42 ms 7188 KB Output is correct
28 Correct 42 ms 7160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 636 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 636 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 3 ms 632 KB Output is correct
11 Correct 2 ms 632 KB Output is correct
12 Correct 2 ms 632 KB Output is correct
13 Correct 2 ms 632 KB Output is correct
14 Correct 2 ms 632 KB Output is correct
15 Correct 3 ms 760 KB Output is correct
16 Correct 19 ms 7032 KB Output is correct
17 Correct 33 ms 7116 KB Output is correct
18 Correct 34 ms 7132 KB Output is correct
19 Correct 41 ms 7160 KB Output is correct
20 Correct 34 ms 6300 KB Output is correct
21 Correct 45 ms 7160 KB Output is correct
22 Correct 53 ms 7072 KB Output is correct
23 Correct 41 ms 7068 KB Output is correct
24 Correct 38 ms 6264 KB Output is correct
25 Correct 41 ms 7132 KB Output is correct
26 Correct 46 ms 7032 KB Output is correct
27 Correct 42 ms 7188 KB Output is correct
28 Correct 42 ms 7160 KB Output is correct
29 Correct 2324 ms 104784 KB Output is correct
30 Correct 2736 ms 110276 KB Output is correct
31 Correct 3030 ms 110040 KB Output is correct
32 Correct 2589 ms 110248 KB Output is correct
33 Correct 2147 ms 96868 KB Output is correct
34 Correct 2963 ms 110240 KB Output is correct
35 Execution timed out 4051 ms 110092 KB Time limit exceeded
36 Halted 0 ms 0 KB -