UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#205661#1445. 理想的正方形tanghexuan102113ms482852kbC++113.6kb2024-07-18 22:42:282024-07-18 22:42:29

answer

#include <bits/stdc++.h>
using namespace std;

static auto __fast_io = []()
{
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    return 0;
}();

// #define DEBUGx
#ifdef DEBUGx
#define TRC_PG(fmt, args...) fprintf(stderr, "\033[1;32m  TRC_PG(%s:%d):\t\033[0m" fmt, __func__, __LINE__, ##args)
#define TRC_PR(fmt, args...) fprintf(stderr, "\033[1;31m  TRC_PR(%s:%d):\t\033[0m" fmt, __func__, __LINE__, ##args)
#define debug fprintf(stderr, "Passing [%s] in LINE %d\t\n", __func__, __LINE__)
#endif
#ifndef DEBUGx
#define TRC_PG(...)
#define TRC_PR(...)
#define debug
#endif

// 有一个 a*b 的整数组成的矩阵,现请你从中找出一个 n*n 的正方形区域,使得该区域所有数中的最大值和最小值的差最小。

const long long MAX = 1000 + 10;
const long long INF = 1e9 + 10;

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

long long dpMin[MAX][MAX][30]; // 表示 [i - (1 << k)][j - (i << k)] 到 [i][j] 的最小值
long long dpMax[MAX][MAX][30];
long long ans;
long long p[MAX][MAX];
long long a, b, n;

void input()
{
    cin >> a >> b >> n;
    for (long long i = 1; i <= a; i++)
    {
        for (long long j = 1; j <= b; j++)
        {
            cin >> p[i][j];
        }
    }
}

void work()
{
    for (long long i = 1; i <= a; i++)
    {
        dpMin[i][0][0] = INF;
    }
    for (long long j = 1; j <= b; j++)
    {
        dpMin[0][j][0] = INF;
    }
    for (long long i = 1; i <= a; i++)
    {
        for (long long j = 1; j <= b; j++)
        {
            dpMin[i][j][0] = p[i][j];
            dpMax[i][j][0] = p[i][j];

            for (long long k = 1; (1 << k) <= i + 1 && (1 << k) <= j + 1; k++)
            {
                dpMin[i][j][k] = min(min(dpMin[i][j][k - 1], dpMin[i - (1 << (k - 1))][j - (1 << (k - 1))][k - 1]),
                                     min(dpMin[i - (1 << (k - 1))][j][k - 1], dpMin[i][j - (1 << (k - 1))][k - 1]));
                dpMax[i][j][k] = max(max(dpMax[i][j][k - 1], dpMax[i - (1 << (k - 1))][j - (1 << (k - 1))][k - 1]),
                                     max(dpMax[i - (1 << (k - 1))][j][k - 1], dpMax[i][j - (1 << (k - 1))][k - 1]));
                TRC_PG("i = %lld, j = %lld, k = %lld, %lld %lld, %lld %lld, %lld %lld, %lld %lld\n", i, j, k, i, j, i - (1 << (k - 1)), j - (1 << (k - 1)), i - (1 << (k - 1)), j, i, j - (1 << (k - 1)));
                TRC_PR("%lld, %lld, %lld, %lld\n", dpMax[i][j][k - 1], dpMax[i - (1 << (k - 1))][j - (1 << (k - 1))][k - 1], dpMax[i - (1 << (k - 1))][j][k - 1], dpMax[i][j - (1 << (k - 1))][k - 1]);
                TRC_PR("%lld %lld\n", dpMin[i][j][k], dpMax[i][j][k]);
            }
        }
    }
    debug;
    long long x = log2(n);
    if ((1 << x) != n)
        x++;
    ans = INF;
    for (long long i = n; i <= a; i++)
    {
        for (long long j = n; j <= b; j++)
        {
            long long a = min(min(dpMin[i][j][x - 1], dpMin[i - (1 << (x - 1))][j - (1 << (x - 1))][x - 1]),
                              min(dpMin[i - (1 << (x - 1))][j][x - 1], dpMin[i][j - (1 << (x - 1))][x - 1]));
            long long b = max(max(dpMax[i][j][x - 1], dpMax[i - (1 << (x - 1))][j - (1 << (x - 1))][x - 1]),
                              max(dpMax[i - (1 << (x - 1))][j][x - 1], dpMax[i][j - (1 << (x - 1))][x - 1]));
            TRC_PG("i = %lld, j = %lld, a = %lld, b = %lld\n", i, j, a, b);
            ans = min(ans, b - a);
        }
    }
}

void output()
{
    cout << ans << "\n";
}

int main()
{
    input();
    work();
    output();
    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 0
Wrong Answer
time: 423ms
memory: 482848kb

input:

1000 1000 100
804544523 340648618 718292412 235345736 704741136 942776831 741228920 463473302 677289...

output:

998110168

result:

wrong answer 1st lines differ - expected: '998893495', found: '998110168'

Test #2:

score: 0
Wrong Answer
time: 373ms
memory: 482848kb

input:

1000 1000 100
629053044 957239666 456746076 940194386 201529807 592062148 244394389 825620014 223976...

output:

999248555

result:

wrong answer 1st lines differ - expected: '998862873', found: '999248555'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1344kb

input:

5 4 2
1 2 5 6
0 17 16 0
16 17 0 1
2 10 2 1
1 2 3 2

output:

2

result:

ok single line: '2'

Test #4:

score: 0
Wrong Answer
time: 7ms
memory: 7256kb

input:

100 100 10
2 100001 200001 300001 400001 500001 600001 700001 800001 900001 1000001 1100001 1200001 ...

output:

909001

result:

wrong answer 1st lines differ - expected: '908999', found: '909001'

Test #5:

score: 0
Wrong Answer
time: 422ms
memory: 482852kb

input:

1000 1000 20
1 100001 200001 300001 400001 500001 600001 700001 800001 900001 1000001 1100001 120000...

output:

1901901

result:

wrong answer 1st lines differ - expected: '1901899', found: '1901901'

Test #6:

score: 0
Wrong Answer
time: 99ms
memory: 126476kb

input:

500 500 50
79289095 232165705 955620938 481434262 465576217 112035388 50089892 459799006 181906335 3...

output:

997508516

result:

wrong answer 1st lines differ - expected: '995944328', found: '997508516'

Test #7:

score: 0
Wrong Answer
time: 222ms
memory: 242188kb

input:

500 1000 80
499163842 331022295 940054497 684192083 248823911 842132608 629298697 398526298 98438040...

output:

999246462

result:

wrong answer 1st lines differ - expected: '998299092', found: '999246462'

Test #8:

score: 0
Wrong Answer
time: 339ms
memory: 482852kb

input:

1000 1000 80
102 134 429 251 299 109 264 669 727 296 112 550 270 270 544 660 131 546 968 219 113 678...

output:

999

result:

wrong answer 1st lines differ - expected: '998', found: '999'

Test #9:

score: 0
Wrong Answer
time: 58ms
memory: 126472kb

input:

500 500 100
65532583 920409544 753795976 989349545 818384831 778207112 425141881 853270293 542112588...

output:

999363463

result:

wrong answer 1st lines differ - expected: '999172505', found: '999363463'

Test #10:

score: 0
Wrong Answer
time: 170ms
memory: 242192kb

input:

500 1000 100
456896744 779471086 578349238 52507342 192194992 156396237 475084995 775765435 96617474...

output:

999319406

result:

wrong answer 1st lines differ - expected: '998801667', found: '999319406'