Submission #906043

# Submission time Handle Problem Language Result Execution time Memory
906043 2024-01-13T11:42:46 Z andrei_iorgulescu The Kingdom of JOIOI (JOI17_joioi) C++17
100 / 100
2490 ms 86444 KB
#include <bits/stdc++.h>

using namespace std;

int n,m,a[2005][2005],b[2005][2005],vmax = 0,vmin = 1e9;
int c[2005][2005];

bool frumoasa(int k)
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (a[i][j] < vmax - k)
                b[i][j] = 0;
            else if (a[i][j] > vmin + k)
                b[i][j] = 1;
            else
                b[i][j] = 2;
            if (a[i][j] < vmax - k and a[i][j] > vmin + k)
                return false;
        }
    }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            c[i][j] = 0;
    for (int i = 1; i <= n; i++)
    {
        int max0 = 0,min1 = 1e9;
        for (int j = 1; j <= m; j++)
        {
            if (b[i][j] == 0)
                max0 = j;
        }
        for (int j = m; j >= 1; j--)
        {
            if (b[i][j] == 1)
                min1 = j;
        }
        if (max0 > min1)
            return false;
        for (int j = 1; j <= max0; j++)
            c[i][j] = 1;
        for (int j = min1; j <= m; j++)
            c[i][j] = 2;
    }
    for (int j = 1; j <= m; j++)
    {
        int max0 = 0,min1 = 1e9;
        for (int i = 1; i <= n; i++)
        {
            if (b[i][j] == 0)
                max0 = i;
        }
        for (int i = n; i >= 1; i--)
        {
            if (b[i][j] == 1)
                min1 = i;
        }
        if (max0 > min1)
            return false;
        for (int i = 1; i <= max0; i++)
        {
            if (c[i][j] == 2)
                return false;
            c[i][j] = 1;
        }
        for (int i = min1; i <= n; i++)
        {
            if (c[i][j] == 1)
                return false;
            c[i][j] = 2;
        }
    }
    return true;
}

void invert_lins()
{
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m / 2; j++)
            swap(a[i][j],a[i][m - j + 1]);
}

void invert_cols()
{
    for (int j = 1; j <= m; j++)
        for (int i = 1; i <= n / 2; i++)
            swap(a[i][j],a[n - i + 1][j]);
}

bool pot(int k)
{
    ///pot face diferenta <= k?
    ///sa zicem ca cele mai mici o sa fie J-uri
    ///cele mai mari trebuie sa fie I
    ///pot pune J doar la valori <= Vmin + k
    ///pot pune I doar la valori >= Vmax - k
    ///atunci o sa fixez alea care pot fi doar J sau doar I, restul am libertate sa le pun oricum
    ///pot oare sa construiesc matricea a.i sa fie frumoasa?
    ///o matrice frumoasa arata din una din parti astfel: primul rand are prefix maxim de J, apoi ceva mai mic, etc etc etc
    if (frumoasa(k) == true)///normala
        return true;
    invert_lins();
    if (frumoasa(k) == true)///privita de la dr la st
        return true;
    invert_cols();
    if (frumoasa(k) == true)///privita complet pe dos
        return true;
    invert_lins();
    if (frumoasa(k) == true)///privita de jos in sus
        return true;
    return false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> a[i][j],vmax = max(vmax,a[i][j]),vmin = min(vmin,a[i][j]);
    int st = -1,pas = 1 << 29;
    while (pas != 0)
    {
        if (!pot(st + pas))
            st += pas;
        pas /= 2;
    }
    cout << st + 1;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2472 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2520 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2472 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2520 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 9 ms 9560 KB Output is correct
17 Correct 13 ms 9564 KB Output is correct
18 Correct 9 ms 9564 KB Output is correct
19 Correct 12 ms 9456 KB Output is correct
20 Correct 18 ms 7516 KB Output is correct
21 Correct 16 ms 9564 KB Output is correct
22 Correct 10 ms 9564 KB Output is correct
23 Correct 17 ms 9564 KB Output is correct
24 Correct 10 ms 7516 KB Output is correct
25 Correct 20 ms 9564 KB Output is correct
26 Correct 15 ms 9564 KB Output is correct
27 Correct 16 ms 9720 KB Output is correct
28 Correct 15 ms 9564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2472 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2520 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 9 ms 9560 KB Output is correct
17 Correct 13 ms 9564 KB Output is correct
18 Correct 9 ms 9564 KB Output is correct
19 Correct 12 ms 9456 KB Output is correct
20 Correct 18 ms 7516 KB Output is correct
21 Correct 16 ms 9564 KB Output is correct
22 Correct 10 ms 9564 KB Output is correct
23 Correct 17 ms 9564 KB Output is correct
24 Correct 10 ms 7516 KB Output is correct
25 Correct 20 ms 9564 KB Output is correct
26 Correct 15 ms 9564 KB Output is correct
27 Correct 16 ms 9720 KB Output is correct
28 Correct 15 ms 9564 KB Output is correct
29 Correct 1419 ms 68096 KB Output is correct
30 Correct 1469 ms 69096 KB Output is correct
31 Correct 1615 ms 70700 KB Output is correct
32 Correct 1278 ms 70676 KB Output is correct
33 Correct 972 ms 64852 KB Output is correct
34 Correct 1448 ms 70784 KB Output is correct
35 Correct 1912 ms 86332 KB Output is correct
36 Correct 1359 ms 81100 KB Output is correct
37 Correct 2490 ms 86444 KB Output is correct