每次二分后找最大匹配数
Bullet
Problem Description
In GGO, a world dominated by gun and steel, players are fighting for the honor of being the strongest gunmen. Player Shino is a sniper, and her aimed shot kills one monster at a time. Now she is in an n×nn \times nn×n map, and there are monsters in some grids. Each monster has an experience. As a master, however, Shino has a strange self-restrain. She would kill at most one monster in a column, and also at most one in a row. Now she wants to know how to get max experience, under the premise of killing as many monsters as possible.
Input
The first line contains an integer nnn
Then n lines follow. In each line there are n integers, and AijAijAij represents the experience of the monster at grid (i,j)(i,j)(i,j). If Aij=0Aij=0Aij=0, there is no monster at grid (i,j)(i,j)(i,j).
Output
One integer, the value of max experience.
Sample Input
2 2 0 1 8
Sample Output
2
Hint
Source
#define happy
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define pll pair<ll,ll>
#define vi vector<int>
#define P pair<int,int>
#define pb push_back
const int inf=0x3f3f3f3f;
ll rd(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int N=500+10;
int path[N][N],mp[N][N],link[N],vis[N],n;
bool hungarian(int pos){
rep(i,1,n){
if(vis[i]==0&&path[pos][i]==1){
vis[i]=1;
if(link[i]==0||hungarian(link[i])){
link[i]=pos;
return true;
}
}
}
return false;
}
int solve(){
memset(link,0,sizeof(link));
int ans=0;
rep(i,1,n){
memset(vis,0,sizeof(vis));
if(hungarian(i))ans++;
}
return ans;
}
int main(){
#ifdef happy
freopen("in.txt","r",stdin);
#endif
n=rd();
rep(i,1,n)rep(j,1,n){
mp[i][j]=rd();
if(mp[i][j]!=0)
path[i][j]=1;
else path[i][j]=0;
}
int tmp=solve();
if(tmp==0)return puts("0"),0;
int le=-inf,ri=inf;
ll ans=0;
while(le<=ri){
int mid=(le+ri)/2;
rep(i,1,n)rep(j,1,n){
if(mp[i][j]>=mid&&mp[i][j]!=0)
path[i][j]=1;
else path[i][j]=0;
}
int mm=solve();
if(mm==tmp){
ans=mid;
le=mid+1;
}else{
ri=mid-1;
}
}
printf("%lld\n",ans);
}