# [杂题] Codechef SnackDown 2017 Onsite Final #MINIMAX Minimax

O(n2logn)$O(n^2\log n)$

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<set>
using namespace std;
typedef pair<int,int> abcd;

const int N=1005;

int n,a[N][N];

abcd b[N*N];

multiset<int> Set;

int rm[N*N],cm[N*N];
int cnt[N];

Set.erase(Set.find(cnt[x])); cnt[x]++; Set.insert(cnt[x]);
}

int main(){
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
for (int i=0;i<n;i++) for (int j=0;j<n;j++) read(a[i][j]),b[i*n+j]=abcd(a[i][j],i*n+j);
sort(b,b+n*n);
rm[0]=0;
for (int i=0;i<n;i++) cnt[i]=0,Set.insert(0);
for (int i=0,j;i<n*n;i=j+1){
while (j+1<=n*n && b[j+1].first==b[i].first)
rm[j+1]=*(Set.begin());
}
Set.clear();
for (int i=0;i<n;i++) cnt[i]=0,Set.insert(0);
cm[n*n-1]=0;
for (int i=n*n-1,j;~i;i=j-1){
while (j-1>=0 && b[j-1].first==b[i].first)
cm[j-1]=*(Set.begin());
}
int ans=1<<30;
for (int i=0,j;i<n*n;i=j+1){
j=i; while (j+1<n*n && b[i].first==b[j+1].first) j++;
ans=min(ans,rm[i]+cm[j]);
}
printf("%d\n",ans);
return 0;
}