ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#205662 | #1445. 理想的正方形 | tanghexuan | 100 | 2327ms | 482620kb | C++11 | 2.8kb | 2024-07-18 22:56:09 | 2024-07-18 22:56:11 |
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][j] 到 [i + (1 << k)][j + (i << k)] 的最小值
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++)
{
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; k < 30; k++)
{
for (long long i = 1; i + (1 << k) - 1 <= a; i++)
{
for (long long j = 1; j + (1 << k) - 1 <= b; j++)
{
dpMin[i][j][k] = min(min(dpMin[i][j][k - 1], dpMin[i + (1 << (k - 1))][j][k - 1]), min(dpMin[i][j + (1 << (k - 1))][k - 1], dpMin[i + (1 << (k - 1))][j + (1 << (k - 1))][k - 1]));
dpMax[i][j][k] = max(max(dpMax[i][j][k - 1], dpMax[i + (1 << (k - 1))][j][k - 1]), max(dpMax[i][j + (1 << (k - 1))][k - 1], dpMax[i + (1 << (k - 1))][j + (1 << (k - 1))][k - 1]));
}
}
}
int x = log2(n);
ans = INF;
for (int i = 1; i + n - 1 <= a; i++)
{
for (int j = 1; j + n - 1 <= b; j++)
{
int a = min(min(dpMin[i][j][x], dpMin[i + n - (1 << x)][j][x]), min(dpMin[i][j + n - (1 << x)][x], dpMin[i + n - (1 << x)][j + n - (1 << x)][x]));
int b = max(max(dpMax[i][j][x], dpMax[i + n - (1 << x)][j][x]), max(dpMax[i][j + n - (1 << x)][x], dpMax[i + n - (1 << x)][j + n - (1 << x)][x]));
TRC_PG("i = %d, j = %d, a = %d, b = %d\n", i, j, a, b);
ans = min(ans, b - a);
}
}
}
void output()
{
cout << ans << "\n";
}
int main()
{
input();
work();
output();
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 484ms
memory: 482616kb
input:
1000 1000 100 804544523 340648618 718292412 235345736 704741136 942776831 741228920 463473302 677289...
output:
998893495
result:
ok single line: '998893495'
Test #2:
score: 10
Accepted
time: 411ms
memory: 482620kb
input:
1000 1000 100 629053044 957239666 456746076 940194386 201529807 592062148 244394389 825620014 223976...
output:
998862873
result:
ok single line: '998862873'
Test #3:
score: 10
Accepted
time: 0ms
memory: 1340kb
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: 10
Accepted
time: 0ms
memory: 7212kb
input:
100 100 10 2 100001 200001 300001 400001 500001 600001 700001 800001 900001 1000001 1100001 1200001 ...
output:
908999
result:
ok single line: '908999'
Test #5:
score: 10
Accepted
time: 419ms
memory: 482616kb
input:
1000 1000 20 1 100001 200001 300001 400001 500001 600001 700001 800001 900001 1000001 1100001 120000...
output:
1901899
result:
ok single line: '1901899'
Test #6:
score: 10
Accepted
time: 92ms
memory: 126204kb
input:
500 500 50 79289095 232165705 955620938 481434262 465576217 112035388 50089892 459799006 181906335 3...
output:
995944328
result:
ok single line: '995944328'
Test #7:
score: 10
Accepted
time: 209ms
memory: 241956kb
input:
500 1000 80 499163842 331022295 940054497 684192083 248823911 842132608 629298697 398526298 98438040...
output:
998299092
result:
ok single line: '998299092'
Test #8:
score: 10
Accepted
time: 372ms
memory: 482616kb
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:
998
result:
ok single line: '998'
Test #9:
score: 10
Accepted
time: 88ms
memory: 126204kb
input:
500 500 100 65532583 920409544 753795976 989349545 818384831 778207112 425141881 853270293 542112588...
output:
999172505
result:
ok single line: '999172505'
Test #10:
score: 10
Accepted
time: 252ms
memory: 241960kb
input:
500 1000 100 456896744 779471086 578349238 52507342 192194992 156396237 475084995 775765435 96617474...
output:
998801667
result:
ok single line: '998801667'