UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#148853#1445. 理想的正方形xqy2007100835ms20852kbC++112.0kb2022-07-18 08:52:152022-07-18 08:52:16

answer

#include <bits/stdc++.h>

//-----------Above is the header file-----------//

#define FOR(i,a,n) for(int i = (a) ; i <= (n) ; i++)
#define REF(i,n,a) for(int i = (n) ; i >= (a) ; i--)

using namespace std;

typedef long long ll;

const int N = 1e+3 + 7;
const int MOD = 1e+9 + 7;
const int INF = 0x3f3f3f3f;
const int _INF = -0x3f3f3f3f;

int a,b,n,ans = INF;
int mp[N][N];
int q1[N], h1 = 0, t1 = 0;
int q2[N], h2 = 0, t2 = 0;
int colmax[N][N], colmin[N][N], sqmax[N][N], sqmin[N][N];

void init(){
    memset(q1, 0, sizeof q1);
    memset(q2, 0 ,sizeof q2);
    h1 = t1 = h2 = t2 = 0;
    return;
}
    
int main(){

    cin.sync_with_stdio(false);
    cout.sync_with_stdio(false);

    scanf("%d %d %d",&a,&b,&n);
    FOR(i,1,a)
        FOR(j,1,b)
            scanf("%d",&mp[i][j]);
    FOR(i,1,a){
        init();
        FOR(j,1,b){
            while(t1 > h1 && mp[i][q1[t1 - 1]] >= mp[i][j])t1--;
            q1[t1++] = j;
            while(t1 > h1 && q1[h1] < j - n + 1)h1++;
            while(t2 > h2 && mp[i][q2[t2 - 1]] <= mp[i][j])t2--;
            q2[t2++] = j;
            while(t2 > h2 && q2[h2] < j - n + 1)h2++;
            if(j >= n){
                colmax[i][j - n + 1] = mp[i][q2[h2]];
                colmin[i][j - n + 1] = mp[i][q1[h1]];
            } 
        }
    }
    FOR(j,1,b - n + 1){
        init();
        FOR(i,1,a){
            while(t1 > h1 && colmin[q1[t1 - 1]][j] >= colmin[i][j])t1--;
            q1[t1++] = i;
            while(t1 > h1 && q1[h1] < i - n + 1)h1++;
            while(t2 > h2 && colmax[q2[t2 - 1]][j] <= colmax[i][j])t2--;
            q2[t2++] = i;
            while(t2 > h2 && q2[h2] < i - n + 1)h2++;
            if(i >= n){
                sqmax[i - n + 1][j] = colmax[q2[h2]][j];
                sqmin[i - n + 1][j] = colmin[q1[h1]][j];
            }
        }
    }
    FOR(i,1,a - n + 1)
        FOR(j,1,b - n + 1){
            ans = min(ans, sqmax[i][j] - sqmin[i][j]);
        }
    printf("%d",ans);
    return 0;
}

详细

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

Test #1:

score: 10
Accepted
time: 170ms
memory: 20592kb

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: 170ms
memory: 20220kb

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: 7436kb

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: 8252kb

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: 120ms
memory: 20852kb

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: 43ms
memory: 14496kb

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: 79ms
memory: 14268kb

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: 143ms
memory: 20380kb

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: 33ms
memory: 14104kb

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: 77ms
memory: 14116kb

input:

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

output:

998801667

result:

ok single line: '998801667'