40pts,用的是四轮单调队列,求调!
查看原帖
40pts,用的是四轮单调队列,求调!
109678
Marshall_Left楼主2024/7/28 10:31
#include<bits/stdc++.h>
using namespace std;
int mp[1001][1001];
int mx[1010][1001],mn[1010][1001];
int a,b,n;int que[1010],pos[1010],head=1,tail=0;
int main()
{
    cin>>a>>b>>n;
    for(int i=1;i<=a;i++)
    {
        for(int j=1;j<=b;j++)
        cin>>mp[i][j];
    }
    
    for(int i=1;i<=a;i++)
    {
        for(int j=1;j<=b;j++)
        {
            while(head<=tail && mp[i][j]<=que[tail])tail--;
            while(head<=tail && pos[head]<=j-n)head++;
            tail++;
            pos[tail]=j;
            que[tail]=mp[i][j];
            if(j-n+1>0)
            mn[i][j-n+1]=que[head];
        }
        head=1;tail=0;
        memset(que,0,sizeof(que));
        memset(pos,0,sizeof(pos));
        for(int j=1;j<=b;j++)
        {
            while(head<=tail && mp[i][j]>=que[tail])tail--;
            while(head<=tail && pos[head]<=j-n)head++;
            tail++;
            pos[tail]=j;
            que[tail]=mp[i][j];
            if(j-n+1>0)
            mx[i][j-n+1]=que[head];
        }
    }
    memset(que,0,sizeof(que));
    memset(pos,0,sizeof(pos));
    int sqmx[1001][1001],sqmn[1001][1001];
    head=1;tail=0;//Where the que&pos starts to mean distinctly 
    for(int i=1;i<=b-n+1;i++)
    {
        for(int j=1;j<=a;j++)
        {
            while(head<=tail && mx[j][i]>=que[tail])tail--;
            while(head<=tail && pos[head]<=j-n)head++;
            tail++;
            pos[tail]=j;
            que[tail]=mx[j][i];
            if(j-n+1>0)
            sqmx[j-n+1][i]=que[head];
        }
        head=1;tail=0;
        memset(que,0,sizeof(que));
        memset(pos,0,sizeof(pos));
        for(int j=1;j<=a;j++)
        {
            while(head<=tail && mn[j][i]<=que[tail])tail--;
            while(head<=tail && pos[head]<=j-n)head++;
            tail++;
            pos[tail]=j;
            que[tail]=mn[j][i];
            if(j-n+1>0)
            sqmn[j-n+1][i]=que[head];
        }
    }
    int ans=0x3f3f3f;
    for(int i=1;i<=a-n+1;i++)
    for(int j=1;j<=b-n+1;j++)
        ans=min(ans,sqmx[i][j]-sqmn[i][j]);
    cout<<ans;
    return 0;
}

另外问一句,这种自己照着思路写出来的程序却出现了自己都不清楚的问题的现象算“黑匣子”么?(选做)

2024/7/28 10:31
加载中...