UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#205659#1445. 理想的正方形tanghexuan101766ms482852kbC++112.6kb2024-07-18 22:06:412024-07-18 22:06:43

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

// 有一个 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]));
            }
        }
    }
    long long x = log2(n);
    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]));
            // cout << i << " " << j << " " << a << " " << b << "\n";
            ans = min(ans, b - a);
        }
    }
}

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

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

详细

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

Test #1:

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

input:

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

output:

995996806

result:

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

Test #2:

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

input:

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

output:

996748209

result:

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

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: 0ms
memory: 7256kb

input:

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

output:

707000

result:

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

Test #5:

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

input:

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

output:

1501499

result:

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

Test #6:

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

input:

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

output:

988653705

result:

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

Test #7:

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

input:

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

output:

997544055

result:

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

Test #8:

score: 0
Wrong Answer
time: 289ms
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:

997

result:

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

Test #9:

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

input:

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

output:

997728280

result:

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

Test #10:

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

input:

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

output:

997401824

result:

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