ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#157457 | #1445. 理想的正方形 | ty | 100 | 798ms | 21656kb | C++11 | 2.5kb | 2022-08-11 23:00:32 | 2022-08-11 23:00:33 |
answer
#ifdef TY
#include "/Users/ty/Desktop/code/ty.h"
#else
#include <bits/stdc++.h>
#endif
using namespace std;
namespace IO
{
static int Len = 0, _t;
char out[1 << 25]; // 32MB
template <typename T>
inline void read(T &x)
{
char ch = getchar();
x = 0, _t = 0;
while (ch < '0' || ch > '9')
_t |= ch == '-', ch = getchar();
while (ch >= '0' && ch <= '9')
x = x * 10 + (ch ^ 48), ch = getchar();
x = _t ? -x : x;
}
inline void putc(char ch) { out[Len++] = ch; }
template <typename T>
inline void write(T x)
{
if (x < 0)
putc('-'), x = -x;
if (x > 9)
write(x / 10);
out[Len++] = x % 10 + 48;
}
inline void flush() { fwrite(out, 1, Len, stdout), Len = 0; }
}
using namespace IO;
#ifdef TY
#define N 10
#else
#define N 1010
#endif
int n, m, k, l, r, q[N], a[N][N], ma[N][N], mi[N][N], Ma[N][N], Mi[N][N], ans = INT_MAX;
int main()
{
#ifdef TY
freopen("/Users/ty/Desktop/code/data.in", "r", stdin);
// freopen("my.out", "w", stdout);
#endif
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
// push k to 1 max
for (int j = 1; j <= m; j++)
{
l = 1, r = 0;
for (int i = 1; i <= n; i++)
{
while (l <= r && a[i][j] >= a[q[r]][j])
r--;
q[++r] = i;
while (l <= r && q[l] <= i - k)
l++;
ma[i][j] = a[q[l]][j];
}
}
// push k to 1 min
for (int j = 1; j <= m; j++)
{
l = 1, r = 0;
for (int i = 1; i <= n; i++)
{
while (l <= r && a[i][j] <= a[q[r]][j])
r--;
q[++r] = i;
while (l <= r && q[l] <= i - k)
l++;
mi[i][j] = a[q[l]][j];
}
}
// cal Smax
for (int i = 1; i <= n; i++)
{
l = 1, r = 0;
for (int j = 1; j <= m; j++)
{
while (l <= r && ma[i][j] >= ma[i][q[r]])
r--;
q[++r] = j;
while (l <= r && q[l] <= j - k)
l++;
Ma[i][j] = ma[i][q[l]];
}
}
// cal Smin
for (int i = 1; i <= n; i++)
{
l = 1, r = 0;
for (int j = 1; j <= m; j++)
{
while (l <= r && mi[i][j] <= mi[i][q[r]])
r--;
q[++r] = j;
while (l <= r && q[l] <= j - k)
l++;
Mi[i][j] = mi[i][q[l]];
}
}
// cal ans
for (int i = k; i <= n; i++)
for (int j = k; j <= m; j++)
ans = min(ans, Ma[i][j] - Mi[i][j]);
printf("%d\n", ans);
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 164ms
memory: 21656kb
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: 157ms
memory: 21652kb
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: 11448kb
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: 2ms
memory: 17964kb
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: 135ms
memory: 21652kb
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: 40ms
memory: 21592kb
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: 77ms
memory: 21592kb
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: 111ms
memory: 21656kb
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: 36ms
memory: 21592kb
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: 76ms
memory: 21592kb
input:
500 1000 100 456896744 779471086 578349238 52507342 192194992 156396237 475084995 775765435 96617474...
output:
998801667
result:
ok single line: '998801667'