ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#148750 | #1445. 理想的正方形 | run_after_dream | 100 | 583ms | 20956kb | C++ | 1.7kb | 2022-07-17 18:59:33 | 2022-07-17 22:02:55 |
answer
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
register int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<3)+(x<<1)+(c^48);
c=getchar();
}
return x*f;
}
const int N = 1005;
int a, b, n;
int w[N][N];
int fa[N][N], fi[N][N];
int ga[N][N], gi[N][N];
int minn[10005], h1 = 0, t1 = 0;
int maxn[10005], h2 = 0, t2 = 0;
int main()
{
a=read(),b=read(),n=read();
for (int i = 1; i <= a; i++)
for (int j = 1; j <= b; j++)
w[i][j]=read();
for (int i = 1; i <= a; i++)
{
memset(minn, 0, sizeof(minn));
h1=h2=1;
t1=t2=0;
memset(maxn, 0, sizeof(maxn));
for (int j = 1; j <= b; j++)
{
while (minn[h1] < j - n + 1 && h1 <= t1) h1++;
while (maxn[h2] < j - n + 1 && h2 <= t2) h2++;
while (w[i][minn[t1]] >= w[i][j] && h1 <= t1) t1--;
while (w[i][maxn[t2]] <= w[i][j] && h2 <= t2) t2--;
minn[++t1] = j;
maxn[++t2] = j;
fi[i][j] = w[i][minn[h1]];
fa[i][j] = w[i][maxn[h2]];
}
}
for (int i = 1; i <= b; i++)
{
memset(minn, 0, sizeof(minn));
h1=h2=1;
t1=t2=1;
memset(maxn, 0, sizeof(maxn));
for (int j = 1; j <= a; j++)
{
while (minn[h1] < j - n + 1 && h1 <= t1) h1++;
while (maxn[h2] < j - n + 1 && h2 <= t2) h2++;
while (fi[minn[t1]][i] >= fi[j][i] && h1 <= t1) t1--;
while (fa[maxn[t2]][i] <= fa[j][i] && h2 <= t2) t2--;
minn[++t1] = j;
maxn[++t2] = j;
gi[j][i] = fi[minn[h1]][i];
ga[j][i] = fa[maxn[h2]][i];
}
}
int ans = 0x3f3f3f3f;
for (int i = n; i <= a; i++)
for (int j = n; j <= b; j++)
ans =min(ans, ga[i][j] - gi[i][j]);
cout<<ans;
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 112ms
memory: 20952kb
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: 115ms
memory: 20952kb
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: 9452kb
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: 15972kb
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: 101ms
memory: 20952kb
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: 18ms
memory: 19588kb
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: 59ms
memory: 19584kb
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: 84ms
memory: 20956kb
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: 29ms
memory: 19584kb
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: 65ms
memory: 19584kb
input:
500 1000 100 456896744 779471086 578349238 52507342 192194992 156396237 475084995 775765435 96617474...
output:
998801667
result:
ok single line: '998801667'