# 10.3 T2

 T2

1 ≤ n, m ≤ 2, 000，0 ≤ Ai, j ≤ 1000,000

1、对每一行从小到大排序，f1取排序后每一列最大值
2、对每一列从大到小排序，f2取排序后每一行最小值
3、枚举R=C=x，容易发现x一定在矩阵中出现过否则不是最优
4、二分找有几个f1中最大值比x小，几个f2中的最小值比x大，加起来为x的答案
（这里需要特判一下，因为如果x是最大（小）的，并且x没有出现在矩阵中过，那么答案不是0而是1）

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
bool flag1[3000005],flag2[3000005];
int n,m,type,a[2005][2005],f1[2005][2005],f2[2005][2005],maxx[2005],minn[2005];
{
char last='+',ch=getchar();
while (ch<'0' || ch>'9') last=ch,ch=getchar();
long long ans=0;
while (ch<='9' && ch>='0')
{
ans=ans*10+(ch-48);
ch=getchar();
}
if (last=='-') ans=-ans;
return ans;
}
bool cmp(int x,int y)
{
return x>y;
}
int find1(int x)
{
int l=0,r=m;
while (l<r)
{
//printf("%d %d\n", l, r);
int mid=(l+r+1)>>1;
if (maxx[mid]<x) l=mid;
else r=mid-1;
}
return l;
}
int find2(int x)
{
int l=0,r=n;
while (l<r)
{
//printf("%d %d\n", l, r);
int mid=(l+r+1)>>1;
if (minn[mid]>x) l=mid;
else r=mid-1;
}
return l;
}
int calc(int x)
{
//printf("%d ",x);
int s1=find1(x);
//printf("%d ",s1);
int s2=find2(x);
//printf("%d\n",s2);
if (s1==0 && !flag1[x]) s1=1;
if (s2==0 && !flag2[x]) s2=1;
return s1+s2;
}
int main()
{
freopen("exciting.in","r",stdin);
freopen("exciting.out","w",stdout);
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++) a[i][j]=read();
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++) f1[i][j]=a[i][j];
sort(f1[i]+1,f1[i]+1+m);
flag1[f1[i][1]]=1;
}
for (int i=1; i<=m; i++)
{
for (int j=1; j<=n; j++) f2[i][j]=a[j][i];
sort(f2[i]+1,f2[i]+1+n,cmp);
flag2[f2[i][1]]=1;
}
for (int i=1; i<=m; i++)
{
for (int j=1; j<=n; j++) maxx[i]=max(maxx[i],f1[j][i]);
//printf("%d\n", maxx[i]);
}
for (int i=1; i<=n; i++)
{
minn[i] = 3000005;
for (int j=1; j<=m; j++) minn[i]=min(minn[i],f2[j][i]);
//printf("%d\n", minn[i]);
}
minn[0]=3000005;
int ans=n*m;
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
{
ans=min(ans,calc(a[i][j]));
}
printf("%d\n",ans);
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：10.3 T2 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)