题解:CF2121C Those Who Are With Us

· · 题解

CF2121C Those Who Are With Us

思路

不难发现,我们设这个矩形中最大的值为 mx,则当所有最大值可以被一个十字覆盖时,答案为 mx-1,否则答案为 mx。

于是就变成了一个找十字中心的问题。

我们设 s_{j,a} 表示第 j 列 a 出现的次数,h_{i,a} 表示第 i 行 a 出现的次数,则中心点有:

\max(h_{i,a}+s_{j,a}-(g_{i,j}=mx))

然后再分类讨论就可以了。

code

#include<bits/stdc++.h>
#define pii pair<int,int>
namespace AyanamiRei
{
    inline int read()
    {
        int x=0,f=1;
        char c=getchar();
        while(c>'9'||c<'0')
        {
            if(c=='-') f=0;
            c=getchar();
        }
        while(c>='0'&&c<='9')
        {
            x=(x<<3)+(x<<1)+(c-'0');
            c=getchar();
        }
        return f?x:-x;
    }
}
namespace SoryuAsukaRangure
{
    inline void write(int x)
    {
        if(x<0) x=-x,putchar('-');
        if(x>9) write(x/10);
        putchar(x%10+'0');
        return;
    }
}
namespace AyanamiYue
{
    inline void spaceput(int x)
    {
        SoryuAsukaRangure::write(x);
        putchar(' ');
        return;
    }
}
namespace IkariYue
{
    inline void output(int x)
    {
        SoryuAsukaRangure::write(x);
        putchar('\n');
        return;
    }
}
namespace IkariShinji
{
    inline int max(int x,int y)
    {
        return x>y?x:y;
    }
}
namespace NagisaKaworu
{
    inline int min(int x,int y)
    {
        return x<y?x:y;
    }
}
namespace MariMakinamiIllustious
{
    inline double MAX(double x,double y)
    {
        return x>y?x:y;
    }
}
namespace ShikinamiAsukaRangure
{
    inline double MIN(double x,double y)
    {
        return x<y?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=2e5+5; 
constexpr int num=1e2+5;
constexpr int the_birthday_of_Asuka=20011204;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
int t,n,m,mx,tot,x,y,sar;
int heng[N][num],shu[N][num];
vector<int> g[N];
signed main()
{
    t=read();
    while(t--)
    {
        mx=-N,tot=-N,x=-N,y=-N;
        n=read(),m=read();
        for(int i=0;i<=n+3;i++)
        {
            g[i].clear();
        }
        for(int i=0;i<n+3;i++)
        {
            for(int j=0;j<=101;j++)
            {
                heng[i][j]=0;
            }
        }
        for(int i=0;i<m+3;i++)
        {
            for(int j=0;j<=101;j++)
            {
                shu[i][j]=0;
            }
        }
        //以上初始化 
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int a;
                a=read();
                heng[i][a]++,shu[j][a]++;
                g[i].emplace_back(a);
                if(a>mx) mx=a,sar=1;
                else if(a==mx) sar++;
            }
        }
        if(n==1||m==1||sar<=2)//特判(好像没用) 
        {
            output(mx-1);
            continue;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if((heng[i][mx]+shu[j][mx]-(g[i][j]==mx))>=tot)//寻找十字中心坐标 
                {
                    tot=heng[i][mx]+shu[j][mx]-(g[i][j]==mx);
                    x=i;
                    y=j;
                }
            }
        } 
        int flag=1;
        for(int i=0;i<n;i++)
        {
            if(i==x) continue;
            for(int j=0;j<m;j++)
            {
                if(j==y) continue;
                if(g[i][j]==mx)
                {
                    flag=0;
                    break;
                }
            }
            if(!flag) break;
        }
        output(mx-flag);//判断情况 
    } 
    return 0;
}